[英]onCharacteristicChanged not called with BLE
I've connected a blood pressure device and set notification in onServiceDicovered. 我已连接血压设备并在onServiceDicovered中设置通知。
I set notify with every Characteristic. 我为每个特征设置了通知。 But onCharacteristicChanged is still not called.
但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);
I want to receive blood pressure, but onCharacteristicChanged never called. 我想接受血压,但是特征变化从未打过电话。 But I can receive in ios or other sample code.
但我可以用ios或其他示例代码接收。
Thank you! 谢谢!
First, the property of Blood Pressure Measurement
is Indicate
, not Notify
首先,
Blood Pressure Measurement
的属性是Indicate
,而不是Notify
https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.blood_pressure.xml https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.blood_pressure.xml
iOS the CBPeripheral setNotifyValue
is automatic to set Indicate
or Notify
, but android not implement. iOS的
CBPeripheral setNotifyValue
是自动设置Indicate
或Notify
,但android没有实现。
Give you some code for reference 给你一些代码供参考
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.