问题描述
我想查找我需要将这个示例从 battery_service 更改为自行车速度传感器服务的服务。只需执行 navigator.bluetooth.requestDevice(acceptAllDevices=true)
即可与自行车速度传感器设备配对,但由于我没有为其提供正确的服务名称而出错。
navigator.bluetooth.requestDevice({ filters: [{ services: ['battery_service'] }] })
.then(device => { /* … */ })
.catch(error => { console.error(error); });
我只是通过猜测得知心率是 {filters:[{services:[ 'heart_rate' ]}]})
。还没有弄清楚自行车速度和踏频传感器的名称。由于文档已关闭,有没有办法内省 requestDevice
以获得此信息?
https://webbluetoothcg.github.io/web-bluetooth/#biblio-bluetooth-assigned-services
给出链接
https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorsHomePage.aspx
哪个页面不再存在,403 Forbidden error
解决方法
规范已被移动,现在可以在:https://www.bluetooth.com/specifications/specs/gatt-specification-supplement-4/
,Chrome 处理的所有标准化蓝牙 GATT UUID 字符串都在此文件中:https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/modules/bluetooth/bluetooth_uuid.cc。我相信下面的这一行正是您要找的。p>
{"cycling_speed_and_cadence",0x1816},
这意味着您可以使用字符串名称或其 UUID:
// This works.
navigator.bluetooth.requestDevice({ filters: [{ services: ['cycling_speed_and_cadence'] }] });
// And this works as well ;)
navigator.bluetooth.requestDevice({ filters: [{ services: [0x1816] }] });
https://web.dev/bluetooth 也是一个不错的起点。