Web Bluetooth API

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

Web Bluetooth API 提供了与低功耗蓝牙设备进行连接和交互的能力。

备注: 此 API 在 Web Worker不可用(未通过 WorkerNavigator 暴露)。

接口

Bluetooth

提供查询蓝牙可用性和请求访问设备的方法。

BluetoothCharacteristicProperties

提供特定的 BluetoothRemoteGATTCharacteristic 属性。

BluetoothDevice

表示特定脚本执行环境中的蓝牙设备。

BluetoothRemoteGATTCharacteristic

表示 GATT 特性,提供有关外设服务进一步信息。

BluetoothRemoteGATTDescriptor

表示 GATT 描述符,提供有关特性值的进一步信息。

BluetoothRemoteGATTServer

表示远程设备上的 GATT 服务器。

BluetoothRemoteGATTService

表示 GATT 服务器提供的一项服务,包括设备、相关服务列表和该服务的特征列表。

对其他接口的扩展

Bluetooth API 扩展了以下 API,添加了列出的特性。

返回一个当前 document 的 Bluetooth 对象,提供对 Web Bluetooth API 功能的访问。

安全注意事项

Web Bluetooth API 只能在安全的上下文中使用。

对此 API 的访问由权限策略中的 bluetooth 指令控制。bluetooth 策略的默认白名单是 self,它允许在同源嵌套框架中使用蓝牙,但默认禁止第三方内容访问。如要启用跨源访问,需要同时在 Permissions-Policy: bluetooth HTTP 请求标头和所需的 <iframe> 中指定允许的来源。

要使用该特性,用户必须首先显式地授予权限(如果由于其他原因不允许访问,例如被权限策略阻止,则不会弹出提示)。当调用 Bluetooth.requestDevice() 请求访问未授权的新蓝牙设备时,将显示权限提示(拥有的全局对象还必须具有 transient activation)。你可以使用 Bluetooth.getDevices() 来检索先前已被授予站点权限的任何设备。

Permissions API 中的 navigator.permissions.query() 方法可以与 bluetooth 权限一起使用,用于测试站点是否具有使用蓝牙设备的权限。权限状态会是 granteddeniedprompt(需要用户确认提示):

js
const btPermission = await navigator.permissions.query({ name: "bluetooth" });
if (btPermission.state !== "denied") {
  // 执行操作
}

规范

Specification
Web Bluetooth
# bluetooth

浏览器兼容性

BCD tables only load in the browser