客户端配置特征描述符 (CCCD)。
UUID - “00002902-0000-1000-8000-00805f9b34fb”/“gatt.client_characteristic_configuration”。
在 Android 的 Java 代码中设置 CCCD 我们做:
public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00};
BluetoothGattDescriptor descriptor = characteristic.getDescriptor("00002902-0000-1000-8000-00805f9b34fb");
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
使用 Web 蓝牙 API 时如何在 JavaScript 代码中执行类似的配置?
我的 JavaScript 版本:
.then(descriptors => {
let queue = Promise.resolve();
descriptors.forEach(descriptor => {
switch (descriptor.uuid) {
case BluetoothUUID.getDescriptor('gatt.client_characteristic_configuration'):
queue = queue.then(_ => descriptor.readValue()).then(value => {
descriptorCache = descriptor;
});
...
var data = new Uint8Array([0x01, 0x00]);
descriptorCache.writeValue(data);
//descriptorCache.writeValue(new TextEncoder().encode(data));
因安全错误而失败:-(
未捕获( promise 中)DOMException:在标记为排除写入的阻止列表对象上调用 writeValue()。 https://webbluetoothcg.github.io/web-bluetooth/#attacks-on-devices
我了解安全的必要性。 但毕竟很多设备都需要对CCCD进行预设。
最佳答案
调用characteristic.StartNotifications()
为您设置 CCCD。
关于javascript - ENABLE_NOTIFICATION_VALUE - Web 蓝牙 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097579/