Click here to Skip to main content
15,881,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am using Xamarin in C#, to build a cross platform BLE app. It is based on this example app using the Monkey.Robotics plugin. All the examples use a
C#
ObservableCollection<IDevice> devices;
line, to populate a ListView with all the scanned results which the user can then manually select, setting a variable to equal that value. i.e.
C#
var device = e.SelectedItem as IDevice;
I am connecting to a known type of device, so after that device is selected, I want to then automatically set the Service and Characteristic variables. The structure of `IDevices` seems a bit tricky, so I thought it would be easiest to enumerate the Services and then automatically select the one that matches the ID I am looking for. Like This:

C#
IAdapter adapter;
IDevice device;
IService AppService;

ObservableCollection<IService> services;

    adapter.DeviceConnected += (s, e) => {

        device = e.Device;
        // when services are discovered
        device.ServicesDiscovered += (object se, EventArgs ea) => {
            if (services.Count == 0)
                Device.BeginInvokeOnMainThread (() => {
                    foreach (var service in device.Services) {
                        if (service.ID == 0x2A37.UuidFromPartial ()) {
                            AppService = service as IService;
                        }  else {
                            services.Add (service);
                        }
                    }
                } );
        } ;

        // start looking for services
        device.DiscoverServices ();
    };

Is this how you would do it, or would you collect all the results and then check through them? In which case, how would you structure that?

I could also have a function, to `SearchFor(x)`, and set up a ubiquitous `ObservableCollection`, and just throw x into it? Not sure if it could deal with both `IService` and `ICharacteristic`, unless I just has 2 if statements and defined different behaviour for each. Still, it would be god to see some suggestions for the best way to move forward.

Any thoughts / suggestions would be much appreciated. Thanks.
Posted
Updated 26-Oct-17 9:04am

1 solution

Doing it in this manner allows the list to be updated as the devices are found. So, how you do it depends on what you're trying to achieve. In this case the demo app is scanning for and listing devices, so in this case it's appropriate.
 
Share this answer
 
Comments
Richard Deeming 27-Oct-17 14:05pm    
Hmmmm...

September 2015, and the OP doesn't seem to have been active since posting the question.

So how did this one end up in the "active" list? :)

(Or are you just cleaning up old, unanswered questions?)
Chris Maunder 27-Oct-17 16:39pm    
I was actually working on exactly the same piece of code he worked, had a problem with it, searched, and found this. I figured an extremely late 2c would do wonders to confuse the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900