public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (characteristic.Uuid.Equals(DT1WatchDogService.DT1WatchdogDataCharacteristicUUID)) { log.Debug("Watchdog data characteristic changed."); // we got data - fine we disconnect and wait for the next alarm var CLIENT_CHARACTERISTIC_CONFIG = Java.Util.UUID.FromString("00002902-0000-1000-8000-00805f9b34fb"); var clientCharacteristiConfigDescriptor = characteristic.GetDescriptor(CLIENT_CHARACTERISTIC_CONFIG); clientCharacteristiConfigDescriptor.SetValue(BluetoothGattDescriptor.DisableNotificationValue.ToArray()); gatt.WriteDescriptor(clientCharacteristiConfigDescriptor); gatt.Disconnect(); var data = characteristic.GetValue(); var reading = GlucoseReading.ParseRawCharacteristicData(data); // fill in source reading.Source = gatt.Device.Name; dataService.PersistReading(reading); if (reading.ErrorCode == GlucoseReading.ReadingErrorCode.NoError) { dataService.LastValidReading = DateTimeOffset.UtcNow; EvaluateReading(reading); } var dataIntent = new Intent(DT1WatchDogService.IntentIncommingData); service.SendBroadcast(dataIntent); } }
public async Task ScanReadingsAsync() { if (watchdogDevice == null) { throw new InvalidOperationException("ScanReadings can only be executed after a device was detected."); } if ((watchdogDevice.Status == ConnectionStatus.Connected) || (watchdogDevice.Status == ConnectionStatus.Connecting)) { return; } await watchdogDevice.Connect(); var service = await watchdogDevice.GetKnownService(DT1WatchdogSericeUuid); service.WhenCharacteristicDiscovered().Subscribe(async x => { if (x.Uuid == DT1WatchdogDataCharacteristicUUID) { if (await x.EnableNotifications()) { x.WhenNotificationReceived().Subscribe((result) => { result.Characteristic.DisableNotifications(); result.Characteristic.Service.Device.CancelConnection(); var reading = GlucoseReading.ParseRawCharacteristicData(result.Data); }); } } }); }