public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { base.OnCharacteristicChanged(gatt, characteristic); if (characteristic.Uuid.Equals(GattMapper.UuidForType <GlucoseMeasurement>())) { try { Consumer.Consume(characteristic); Bluetooth_CMD.poll_queue(); } catch (Exception) { throw; } } }
internal static void process_queue_entry(Bluetooth_CMD btc) { if (GlucoseGattCallback.mBluetoothGatt == null) { Debug.WriteLine("Bluetooth Gatt not initialized"); return; } BluetoothGattService service = null; BluetoothGattCharacteristic characteristic; if (btc.service != null) { service = GlucoseGattCallback.mBluetoothGatt.GetService(btc.service); } if ((service != null) || (btc.service == null)) { if ((service != null) && (btc.characteristic != null)) { characteristic = service.GetCharacteristic(btc.characteristic); } else { characteristic = null; } if (characteristic != null) { switch (btc.cmd) { case "W": characteristic.SetValue(btc.data); if ((characteristic.GetValue().Length > 1)) { GlucoseGattCallback.AwaitingAck = true; GlucoseGattCallback.AwaitingData = true; if (GlucoseGattCallback.Debug) { Debug.WriteLine("Setting await ack blocker 1"); } } Task.Run(() => { try { if (!GlucoseGattCallback.mBluetoothGatt.WriteCharacteristic(characteristic)) { Debug.WriteLine("Failed in write characteristic"); System.Threading.Thread.Sleep(150); if (!GlucoseGattCallback.mBluetoothGatt.WriteCharacteristic(characteristic)) { Debug.WriteLine("Failed second time in write charactersitic"); } } } catch (NullPointerException e) { Debug.WriteLine( "Got null pointer exception writing characteristic - probably temporary failure"); } }); break; case "R": GlucoseGattCallback.mBluetoothGatt.ReadCharacteristic(characteristic); break; case "N": GlucoseGattCallback.mBluetoothGatt.SetCharacteristicNotification(characteristic, true); System.Threading.Thread.Sleep(100); poll_queue(); // we don't get an event from this break; case "U": GlucoseGattCallback.mBluetoothGatt.SetCharacteristicNotification(characteristic, false); break; case "D": BluetoothGattDescriptor descriptor = characteristic.GetDescriptor( GattMapper.UuidForType <ClientCharacteristicConfiguration>()); descriptor.SetValue(btc.data); GlucoseGattCallback.mBluetoothGatt.WriteDescriptor(descriptor); break; default: Debug.WriteLine("Unknown queue cmd: " + btc.cmd); break; } // end switch } else { Debug.WriteLine("Characteristic was null!!!!"); } } else { Debug.WriteLine("Got null service error on: " + btc.service); } }