我连接了一个血压计,并在OnServiceIcovered中设置了通知。
我把每一个特征都设置为notify。
但是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);
我想接受血压,但一经改变就再也没打过电话。
但我可以接收ios或其他示例代码。
谢谢您!
最佳答案
首先,Blood Pressure Measurement
的性质是Indicate
,而不是Notify
https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.blood_pressure.xml
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;
}