gpt4 book ai didi

android - onCharacteristicChanged 未使用 BLE 调用

转载 作者:太空狗 更新时间:2023-10-29 14:47:44 26 4
gpt4 key购买 nike

我已连接血压设备并在 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

https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.blood_pressure.xml

iOS的CBPeripheral setNotifyValue是自动设置IndicateNotify的,而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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com