[英]Android Ble Glucose - onCharacteristicChanged not called
I'm using Google Translate, so please forgive my poor English.我正在使用谷歌翻译,所以请原谅我糟糕的英语。
Currently, the blood glucose meter is linked using Android Ble.目前,血糖仪采用Android Ble联动。 The device name is "ACCU-CHEK Guide Meter".
设备名称为“ACCU-CHEK Guide Meter”。
I found the right service and Characteristics in onServicesDiscovered.我在 onServicesDiscovered 中找到了正确的服务和特征。
But onCharacteristicChanged is not called, so I don't know the value measured by the glucometer.但是onCharacteristicChanged没有被调用,所以我不知道血糖仪测量的值。
Below is the code I've been working on.下面是我一直在处理的代码。 I've tested several methods, so please forgive the complexity.
我已经测试了几种方法,所以请原谅复杂性。
I have tried change ENABLE_NOTIFICATION_VALUE <-> ENABLE_INDICATION_VALUE.我尝试更改 ENABLE_NOTIFICATION_VALUE <-> ENABLE_INDICATION_VALUE。
Please let me know if I missed anything or made a mistake.如果我遗漏了什么或犯了错误,请告诉我。 Thank you.
谢谢你。
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
mGatt = gatt;
List<BluetoothGattService> services = mGatt.getServices();
for (BluetoothGattService service : gatt.getServices()) {
for (BluetoothGattCharacteristic characteristic2 : service.getCharacteristics()) {
if (CBUUID.fromString(characteristic2.getUuid().toString()).uuidString().equals("2A52")
|| new CBUUID(characteristic2.getUuid().toString()).toString().equals("Record Access Control Point")) {
characteristic2.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
byte[] requestValue = new byte[]{0x01, 0x01};
boolean setValueBoolean = characteristic2.setValue(requestValue);
boolean writeCharacteristicBoolean = gatt.writeCharacteristic(characteristic2);
break;
}
}
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic2, int status) {
super.onCharacteristicWrite(gatt, characteristic2, status);
List<BluetoothGattService> services = mGatt.getServices();
for (BluetoothGattService service : services) {
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
if (CBUUID.fromString(characteristic.getUuid().toString()).uuidString().equals("2A18")) {
boolean result2 = mGatt.setCharacteristicNotification(characteristic, true);
new Handler(Looper.getMainLooper()).postDelayed(() -> {
for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
boolean result = mGatt.writeDescriptor(descriptor);
}
}, 500);
}
}
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
descriptor.getCharacteristic().setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
debugLog("i", currentActivityName, "onCharacteristicChanged " + Arrays.toString(characteristic.getValue()));}
I found it.我找到了。
The problem was that in onCharacteristicWrite the status came out as 129 (Gatt Internal Error).问题在于 onCharacteristicWrite 中的状态为 129(Gatt 内部错误)。
The solution is to set ENABLE_NOTIFICATION in Glucose Measurement and Glucose Measurement Context and ENABLE_INDICATION in Record Access Control Point.解决方案是在 Glucose Measurement 和 Glucose Measurement Context 中设置 ENABLE_NOTIFICATION,在 Record Access Control Point 中设置 ENABLE_INDICATION。
Thanks to both the questioner and the answerer of the question below.感谢以下问题的提问者和回答者。
Reference: BleGattCharacteristicException: GATT exception status 129 when trying to Indicate->write->notify to BLE glucometer参考: BleGattCharacteristicException:尝试指示->写入->通知 BLE 血糖仪时 GATT 异常状态 129
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.