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 |
瀏覽器相容性
載入中…