Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Using ObservableCollection as a search mechanism for BLE enumeration

$
0
0

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 an 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. 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 Craig from Xamarin suggested it would be easiest to enumerate the Services and then search for the one that matches the ID I am looking for. I thought possibly like This:

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?

Any thoughts / suggestions would be much appreciated. Thanks.

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.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>