我已连接血压设备并在 onServiceDicovered 中设置通知。
我为每个特性设置了通知。 但是 onCharacteristicChanged 仍然没有被调用。
public final static UUID CLIENT_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
gatt.setCharacteristicNotification(characteristic, enabled);
for (BluetoothGattDescriptor dp : characteristic.getDescriptors()) {
dp = characteristic.getDescriptor(CLIENT_UUID);
if (dp != null) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
}
gatt.writeDescriptor(dp);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
我想接收血压值,但从未调用过 onCharacteristicChanged。 但我可以在 ios 或其他示例代码中接收。
谢谢!
最佳答案
首先,血压测量
的属性是Indicate
,不是Notify
iOS的CBPeripheral setNotifyValue
是自动设置Indicate
或Notify
的,而android没有实现。
给你一些代码供引用
if (gatt.setCharacteristicNotification(characteristic, true)) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (descriptor != null) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
} else {
// The characteristic does not have NOTIFY or INDICATE property set;
}
if (gatt.writeDescriptor(descriptor)) {
// Success
} else {
// Failed to set client characteristic notification;
}
} else {
// Failed to set client characteristic notification;
}
} else {
// Failed to register notification;
}
关于android - onCharacteristicChanged 未使用 BLE 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045294/