I am using Xamarin.Forms, 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 one of the Xamarin guys suggested it would be easiest to enumerate the Services and then automatically select the one that matches the ID I am looking for. Not quite sure what the best way of doing it, I have tried 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?
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?
Any thoughts / suggestions would be much appreciated. Thanks.