且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

订阅 BLE Gatt 通知 Android

更新时间:2023-01-24 10:56:40

要在 Android 中接收通知,您需要将特性通知设置为 true

To receive notification in Android you need to set characteristic notification to true

gatt.setCharacteristicNotification(characteristic, true);

还需要设置客户端特征配置描述符0x2902

You also need to set the client characteristic configuration descriptor 0x2902

// 0x2902 org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);

一个更好的 API 是让 setCharacteristicNotification 来设置描述符,但不幸的是它似乎并没有那样工作.

A better API would be for setCharacteristicNotification to set the descriptor, but unfortunately it doesn't seem to work that way.