Forum Xamarin.Forms

Using ObservableCollection as a search mechanism for BLE enumeration

GeorgeEdwardsGeorgeEdwards GBMember ✭✭
edited September 2015 in Xamarin.Forms

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.


  • GeorgeEdwardsGeorgeEdwards GBMember ✭✭

    Would be really good to get some thoughts on this, as I am not having much luck. Thanks.

Sign In or Register to comment.