是否可以在浏览器刷新时保持蓝牙 LE 连接?或者至少减少配对时间?
2 回答
最终 navigator.permissions.query 将支持这一点。来自Web 蓝牙规范的示例代码
navigator.permissions.query({
name: "bluetooth",
deviceId: sessionStorage.lastDevice,
}).then(result => {
if (result.devices.length == 1) {
return result.devices[0];
} else {
throw new DOMException("Lost permission", "NotFoundError");
}
}).then(...);
但是,目前没有浏览器实现这个。
截至2017 年第三季度,Chromium 实现正在积极开发 Web 蓝牙,但不是此功能。
我最近实现了一个新的权限后端以及两个 API,它们将使以前允许的蓝牙设备能够被使用。
新的权限后端在 chrome://flags/#enable-web-bluetooth-new-permissions-backend 后面实现。新后端将保留授予的设备权限,requestDevice()
直到在“站点设置”或“页面信息”对话框中重置该权限。
getDevices()
和在watchAdvertisements()
Chrome 85.0.4165.0 或更高版本的 chrome://flags/#enable-experimental-web-platform-features 标志后面实现。这些 API 的推荐用途是用于getDevices()
检索允许的 BluetoothDevices 数组,然后调用watchAdvertisements()
这些设备以开始扫描。当从设备检测到广告数据包时,advertisementreceived
将在其对应的设备上触发事件。此时,蓝牙设备在范围内并可以连接。
请尝试使用此新功能,并使用 Blink>Bluetooth 组件在https://crbug.com上提交任何错误。