Web Bluetooth API
Web Bluetooth API 提供了连接和与低功耗蓝牙外围设备交互的能力。
注意: 此 API 在 Web Workers 中不可用(未通过 WorkerNavigator 暴露)。
接口
Bluetooth-
提供了查询蓝牙可用性和请求设备访问权限的方法。
BluetoothCharacteristicProperties-
提供了特定的
BluetoothRemoteGATTCharacteristic的属性。 BluetoothDevice-
在特定的脚本执行环境中表示一个蓝牙设备。
BluetoothRemoteGATTCharacteristic-
表示一个 GATT Characteristic(特性),它是提供有关外围设备服务进一步信息的基本数据元素。
BluetoothRemoteGATTDescriptor-
表示一个 GATT Descriptor(描述符),它提供了关于特性值的进一步信息。
BluetoothRemoteGATTServer-
表示远程设备上的 GATT 服务器。
BluetoothRemoteGATTService-
表示由 GATT 服务器提供的服务,包括设备、引用的服务列表以及该服务的特性列表。
其他接口的扩展
Bluetooth API 扩展了以下 API,并添加了列出的功能。
Navigator
-
返回当前文档的
Bluetooth对象,从而提供对 Web Bluetooth API 功能的访问。
安全注意事项
Web Bluetooth API 只能在安全上下文中启用。
对该 API 的访问受 Permissions Policy 指令 bluetooth 控制。bluetooth 策略的默认允许列表是 self,它允许在同源嵌套框架中使用蓝牙,但默认情况下阻止第三方内容访问。通过在 Permissions-Policy: bluetooth HTTP 标头和所需的 <iframe> 中指定允许的源来启用跨域访问。
为了使用该功能,用户必须首先授予明确的权限(如果由于其他原因不允许访问,例如被 Permissions Policy 阻止,则不会提示用户访问)。当调用 Bluetooth.requestDevice() 来请求访问一个尚未授予权限的新蓝牙设备时(所有者全局对象还必须具有临时激活),将显示权限提示。您可以使用 Bluetooth.getDevices() 来检索先前已为该站点授予权限的设备。
可以使用 Permissions API 的 navigator.permissions.query() 方法和 bluetooth 权限来测试网站是否有权使用蓝牙设备。权限状态将是 granted(已授予)、denied(已拒绝)或 prompt(需要用户确认提示)。
const btPermission = await navigator.permissions.query({ name: "bluetooth" });
if (btPermission.state !== "denied") {
// Do something
}
规范
| 规范 |
|---|
| Web 蓝牙 # bluetooth |
浏览器兼容性
加载中…