Service、Characteristic 都有一个 UUID 用于标识服务、特性,Service 的 UUID 格式固定为 0x0000[xxxx]-0000-1000-8000-00805F9B34FB,此中 [xxxx] 是可变部门,其余固定,比如电池服务的 UUID 为 0000180F-0000-1000-8000-00805f9b34fb,可简写为 0x180F,当自定义蓝牙 GATT 服务时定义的 UUID 需要采用雷同的格式。
在使用 Web Bluetooth API 时,我们通过服务名称或服务 UUID 来找到我们需要的蓝牙服务。
Web Bluetooth API
Web Bluetooth 全部接口构建在 Promise 之上,只支持在可信来源中使用(localhost 或 https),重要有以下接口(完整接口查看 MDN 文档):
除了被动读写外,Android 端的 gatt 服务器还支持 notifyCharacteristicChanged 方法,此方法会触发 web 端 characteristic 实例的 characteristicvaluechanged 事件获取最新的特性值,通过这种方式可以做到主动通知 web 端的效果。
通过自定义终端应用实现自定义 GATT 服务器的方式可以完成与 web 端的点对点毗连,但是 web bluetooth 的兼容性还不足以支持完成大型的项目,稳定性也无法考证。
原文地址:https://yuanyxh.com/articles/web_bluetooth_and_point_to_point_connection.html
参考资料