private BLEManager() { serviceID = new Guid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); rxID = new Guid("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"); txID = new Guid("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"); ble = Plugin.BLE.CrossBluetoothLE.Current; ble.Adapter.ScanTimeout = 10000; ble.Adapter.DeviceDiscovered += async(s, e) => { BLEConnectionEvent(BLEConnectionState.DeviceDiscovered); await ble.Adapter.StopScanningForDevicesAsync(); device = e.Device; try { await ble.Adapter.ConnectToDeviceAsync(device); if (device.State != DeviceState.Connected) { BLEConnectionEvent(BLEConnectionState.CannotConnect); return; } BLEConnectionEvent(BLEConnectionState.DeviceConnected); var service = await device.GetServiceAsync(serviceID); BLEConnectionEvent(BLEConnectionState.ServiceFound); RxCharacteristic = await service.GetCharacteristicAsync(rxID); TxCharacteristic = await service.GetCharacteristicAsync(txID); await RxCharacteristic.StartUpdatesAsync(); IsConnected = true; BLEConnectionEvent(BLEConnectionState.CharacteristicFound); } catch (DeviceConnectionException) { BLEConnectionEvent(BLEConnectionState.CannotConnect); } }; ble.Adapter.ScanTimeoutElapsed += (s, e) => BLEConnectionEvent(BLEConnectionState.ScanTimeoutElapsed); }
private async void DeviceDiscovered(object s, DeviceEventArgs e) { BLEConnectionEvent(BLEConnectionState.DeviceDiscovered); await ble.Adapter.StopScanningForDevicesAsync(); device = e.Device; try { await ble.Adapter.ConnectToDeviceAsync(device); if (device.State != DeviceState.Connected) { BLEConnectionEvent(BLEConnectionState.CannotConnect); return; } BLEConnectionEvent(BLEConnectionState.DeviceConnected); var service = await device.GetServiceAsync(serviceID); BLEConnectionEvent(BLEConnectionState.ServiceFound); RxCharacteristic = await service.GetCharacteristicAsync(rxID); TxCharacteristic = await service.GetCharacteristicAsync(txID); await RxCharacteristic.StartUpdatesAsync(); IsConnected = true; BLEConnectionEvent(BLEConnectionState.CharacteristicFound); //RxCharacteristic.ValueUpdated += StateDataStore.Instance.BleRxUpdated; } catch (DeviceConnectionException) { BLEConnectionEvent(BLEConnectionState.CannotConnect); } }