javascript - web-bluetooth 从设备 GATT 获取存储的数据
问题描述
我的目标是获取存储在设备中的数据。
就像正在测量温度或其他任何东西并将其存储到内存中的设备。我需要通过记录访问控制点 (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 并接收数据。
解决方案
调用writeValue()
失败,因为对 CCCD 的访问在阻止列表中。调用startNotifications()
将根据需要写入描述符以启用通知。
我们需要调查这个startNotifications()
失败的“未知原因”。您使用什么操作系统?请按照说明报告 Web 蓝牙错误并在 Chromium 项目的问题跟踪器中提交问题。
推荐阅读
- arrays - 使用 QUERY 和 PIVOT 并且需要总空白量为零
- java - junit 测试中的 Thread.sleep 也调用 InterruptedException
- javascript - 节点js路径文件
- kubernetes - Kubernetes 是否将 JSON 格式作为输入文件来创建 configmap 和 secret?
- python-3.x - 如何使用 Google Cloud Vision API 检测笔迹
- azure-data-explorer - How to access data in a string via KQL
- xcode - 如何为collectionViewCell进行水平自动滚动?
- javascript - 我的脚本 js 不会改变我的照片 src
- json - SonarQube - 分析 JSON 文件
- c# - c# hangman - 如何在每个新行中显示在正确位置尝试过的字母