Bluetooth.requestDevice()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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

Bluetooth接口的 Bluetooth.requestDevice() 方法返回一个带有对应 options 的BluetoothDevice对象的Promise对象。如果没有蓝牙设备选择界面,则此方法返回与条件匹配的第一个设备。

语法

Bluetooth.requestDevice(options).then(function(bluetoothDevice) { ... })

返回值

带有BluetoothDevice 对象的Promise 对象。

参数

options

设置设备请求选项的对象。可用的选项是:

  • filters[]: 一个 BluetoothScanFilters 数组。此过滤器由一个 BluetoothServiceUUID 数组,一个名称参数和一个 namePrefix 参数组成。
  • optionalServices[]: 一个 BluetoothServiceUUID 数组。
  • acceptAllDevices: boolean 表示请求脚本可以接受所有蓝牙设备。默认值为 false。

示例

//扫描选项匹配任何设备广播:

//. 标准心率服务。

//. 两个 16 位服务 ID 0x1802 和 0x1803。

//. 专有的 128 位 UUID 服务 c48e6067-5295-48d3-8d5c-0395f61792b1。

//. 名称为“设备名”的设备。

//. 名称以“前缀”开头的设备。

//

//如果设备包含电池服务,

//即使设备不通告该服务,也可以访问电池服务。

let options = {
  filters: [
    {services: ['heart_rate']},
    {services: [0x1802, 0x1803]},
    {services: ['c48e6067-5295-48d3-8d5c-0395f61792b1']},
    {name: '设备名'},
    {namePrefix: '前缀'}
  ],
  optionalServices: ['battery_service']
}

navigator.bluetooth.requestDevice(options).then(function(device) {
  console.log('名称:' + device.name);
  // 在此处实现设备调用
})
.catch(function(error) {
  console.log("出现错误: " + error);
});

查看详细示例

规范

Specification
Web Bluetooth
# dom-bluetooth-requestdevice

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
requestDevice
Experimental
options.exclusionFilters parameter
Experimental
options.filter.manufacturerData parameter
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.