android - onCharacteristicChanged 未使用 BLE 调用

标签 android bluetooth-lowenergy

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

相关文章:

java - Android数据绑定(bind)双向不起作用

android - 为什么没有调用 mConnection.onServiceConnected 方法

android - 将 BLE 设备连接到 Android 并发送数据 (nUART)

android - Nexus 4 上的 Android 4.3 蓝牙低功耗服务发现

android - 什么是 textViewResourceId?

Android - 将 XML 解析为 ArrayList

java - 我做的不正确或理解 Android 上的双缓冲吗?

安卓 BLE API : GATT Notification not received

ios - 如何降低广告 RSSI 值?

python - 如何将 tensorflow 2.0 估计器模型转换为 tensorflow lite?