From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/bluetooth/requestdevice/index.html | 145 +++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 files/zh-cn/web/api/bluetooth/requestdevice/index.html (limited to 'files/zh-cn/web/api/bluetooth/requestdevice/index.html') diff --git a/files/zh-cn/web/api/bluetooth/requestdevice/index.html b/files/zh-cn/web/api/bluetooth/requestdevice/index.html new file mode 100644 index 0000000000..abfad37108 --- /dev/null +++ b/files/zh-cn/web/api/bluetooth/requestdevice/index.html @@ -0,0 +1,145 @@ +--- +title: Bluetooth.requestDevice() +slug: Web/API/Bluetooth/requestDevice +tags: + - API + - Web蓝牙 + - 蓝牙 +translation_of: Web/API/Bluetooth/requestDevice +--- +

{{APIRef()}}{{SeeCompatTable}}

+ +

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

+ +

语法

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

返回值

+ +

带有{{domxref("BluetoothDevice")}} 对象的{{jsxref("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);
+});
+ +

查看详细示例 

+ +

规范

+ + + + + + + + + + + + + + +
规范状态说明
{{SpecName('Web Bluetooth', '#dom-bluetooth-requestdevice', 'requestDevice()')}}{{Spec2('Web Bluetooth')}}Initial definition.
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome (56)}}  {{CompatOpera(43)}} 
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatChrome (56)}}   {{CompatOperaMobile(43)}} 
+
-- cgit v1.2.3-54-g00ecf