我的目标是获取存储在设备中的数据。
就像正在测量温度或其他任何东西并将其存储到内存中的设备。我需要通过记录访问控制点 (RACP) 查询所有这些数据设备。
实现它的第一个想法是
获得特征
开始通知
将代码写入描述符
通过 eventListener 获取所有数据
结果:在启动通知时抛出错误使用的示例:
https://googlechrome.github.io/samples/web-bluetooth/notifications-async-await.html https://bugs.chromium.org/p/chromium/issues/detail?id=664863
- 下一个想法是不启动通知,因为特征是 INDICATE,WRITE 类型。因此正在考虑添加侦听器并写入设备文档中的描述符代码,其中指出:
OP Code: 1 – 报告存储的记录
即使删除了 startNotifications 行也会引发错误,所以我的代码示例是:
const mainService = 'my correct service uuid';
const characteristicUUID1 = 'my correct char uuid';
const characteristicUUID2 = 'my correct char uuid';
const descriptorUUID = '00002902-0000-1000-8000-00805f9b34fb';
let deviceCache = null;
let serverCache = null;
let serviceCache = null;
let characteristicCacheA = null;
let characteristicCacheB = null;
let descriptorCache = null;
try {
deviceCache = await navigator.bluetooth.requestDevice({ filters: [{ name: 'my device' }] });
console.log('Connecting to GATT Server...');
serverCache = await deviceCache.gatt.connect();
console.log('Getting Services...');
serviceCache = await serverCache.getPrimaryService(mainService);
console.log('Getting Characteristics A...');
characteristicCacheA = await serviceCache.getCharacteristic(characteristicUUID1);
console.log('Start Notifications A...');
await characteristicCacheA.startNotifications();
console.log('Getting Characteristics B...');
characteristicCacheB = await serviceCache.getCharacteristic(characteristicUUID2);
console.log('Start Notifications B...');
await characteristicCacheB.startNotifications();
console.log('Add event listener...');
characteristicCacheA.addEventListener('characteristicvaluechanged', this.handleNotifications);
console.log('Getting Descriptor...');
descriptorCache = await characteristicCacheA.getDescriptor(descriptorUUID);
console.log('Write value to descr...');
await descriptorCache.writeValue(new Uint8Array([1]));
} catch (error) {
console.log(error.message, 'error');
}
通知错误是(使用实验性 chrome 功能不会引发错误):
错误:GATT 操作因未知原因而失败。
描述符的错误是:
writeValue() 在标记为排除写入的黑名单对象上调用。
我的设备也要求输入密码,但网络正在连接而没有任何提示。所以也许它说写入描述符被阻止了。
如何处理 pin 输入 - 没有线索(一旦我在启用 chrome 实验功能后提示输入 pin 并不确定它是否相关)。
我的逻辑正确吗?- 不这么认为。
有什么建议么?
到目前为止我调查了什么?
- https://googlechrome.github.io/samples/web-bluetooth/
- https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html
- https://webbluetoothcg.github.io/web-bluetooth/
编辑:阅读本文后 - https://medium.com/@devdevcharlie/experimenting-with-web-bluetooth-1f1176047ddd
我认为正确的逻辑应该是,写入您需要的命令特征命令(例如获取所有数据)。之后从设备文档中找到负责此数据的正确特征,并启动通知并添加 eventListener 并接收数据。