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);
            }
        }
Пример #2
0
        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);
                        });
                    }
                }
            });
        }
        private void EvaluateReading(GlucoseReading reading)
        {
            if (reading.GlucoseLatest < DT1WatchDogService.NonConditionalHypoglycemiaThresholdmgdl)
            {
                service.TriggerAlert(DT1WatchDogService.AlertType.Hypoglycemia, reading.GlucoseLatest.ToString() + "mgdl", false);
                return;
            }

            if (reading.GlucoseLatest < DT1WatchDogService.ConditionalHypoglycemiaThresholdmgdl)
            {
                service.TriggerAlert(DT1WatchDogService.AlertType.Hypoglycemia, reading.GlucoseLatest.ToString() + "mgdl", true);
                return;
            }

            if (reading.GetPredictedGlucoseValue(DT1WatchDogService.PredictionInterval) < DT1WatchDogService.ConditionalHypoglycemiaThresholdmgdl)
            {
                service.TriggerAlert(DT1WatchDogService.AlertType.PredictedHypoglycemia, reading.GlucoseLatest.ToString() + "mgdl", true);
                return;
            }

            if (reading.GlucoseLatest > DT1WatchDogService.HyperglycemiaThresholdmgdl)
            {
                service.TriggerAlert(DT1WatchDogService.AlertType.Hyperglycemia, reading.GlucoseLatest.ToString() + "mgdl", true);
                return;
            }

            if (reading.GetPredictedGlucoseValue(DT1WatchDogService.PredictionInterval) > DT1WatchDogService.HyperglycemiaThresholdmgdl)
            {
                service.TriggerAlert(DT1WatchDogService.AlertType.PredictedHyperglycemia, reading.GlucoseLatest.ToString() + "mgdl", true);
                return;
            }

            if (reading.PerCentCharge < DT1WatchDogService.LowPowerThresholdPerCent)
            {
                service.TriggerAlert(DT1WatchDogService.AlertType.LowBattery, reading.PerCentCharge + "%", false);
                return;
            }

            service.TriggerValidInRangeReading();
        }