• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 蓝牙
2
3
4> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
5>
6> - 从API Version 7 开始,该接口不再维护,推荐使用新接口[`@ohos.bluetooth`](js-apis-bluetooth.md)。
7>
8> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9
10
11## 导入模块
12
13
14```
15import bluetooth from '@system.bluetooth';
16```
17
18
19## 权限列表
20
21ohos.permission.USE_BLUETOOTH
22
23ohos.permission.DISCOVER_BLUETOOTH
24
25ohos.permission.MANAGE_BLUETOOTH
26
27
28## bluetooth.startBLEScan(OBJECT)
29
30开始搜寻附近的低功耗蓝牙外围设备。此操作比较耗费系统资源,请在搜索并连接到设备后调用[bluetooth.stopBLEScan](#bluetoothstopblescanobject)方法停止搜索。
31
32**需要权限:** ohos.permission.DISCOVER_BLUETOOTHohos.permission.LOCATION
33
34**系统能力:** SystemCapability.Communication.Bluetooth.Lite
35
36**参数:**
37**表1** StartBLEScanOptions
38
39| 参数名 | 类型 | 必填 | 说明 |
40| -------- | -------- | -------- | -------- |
41| interval | number | 否 | 上报设备的间隔,单位毫秒,默认值为0。0表示找到新设备立即上报,其他数值根据传入的间隔上报。 |
42| success | Function | 否 | 接口调用成功的回调函数。 |
43| fail | Function | 否 | 接口调用失败的回调函数。 |
44| complete | Function | 否 | 接口调用结束的回调函数。 |
45
46**示例:**
47
48  ```
49  bluetooth.startBLEScan({
50    interval:0,
51    success() {
52      console.log('call bluetooth.startBLEScan success.');
53    },
54    fail(code, data) {
55      console.log('call bluetooth.startBLEScan failed, code: ${code}, data: ${data}.');
56    },
57    complete() {
58      console.log('call bluetooth.startBLEScan complete.');
59    }
60  });
61  ```
62
63
64## bluetooth.stopBLEScan(OBJECT)
65
66停止搜寻附近的低功耗蓝牙外围设备。与[bluetooth.startBLEScan(OBJECT)](#bluetoothstartblescanobject)接口配套使用。
67
68**需要权限:** ohos.permission.DISCOVER_BLUETOOTHohos.permission.LOCATION
69
70**系统能力:** SystemCapability.Communication.Bluetooth.Lite
71
72**参数:**
73**表2** StopBLEScanOptions
74
75| 参数名 | 类型 | 必填 | 说明 |
76| -------- | -------- | -------- | -------- |
77| success | Function | 否 | 接口调用成功的回调函数。 |
78| fail | Function | 否 | 接口调用失败的回调函数。 |
79| complete | Function | 否 | 接口调用结束的回调函数。 |
80
81**示例:**
82
83  ```
84  bluetooth.stopBLEScan({
85    success() {
86      console.log('call bluetooth.stopBLEScan success.');
87    },
88    fail(data, code) {
89      console.log('call bluethooth.stopBLEScan fail, code: ${code}, data: ${data}.');
90    },
91    complete() {
92      console.log('call bluethooth.stopBLEScan complete.');
93    }
94  });
95  ```
96
97
98## bluetooth.subscribeBLEFound(OBJECT)
99
100订阅寻找到新设备。再次调用时,会覆盖前一次调用效果,即仅最后一次调用生效。
101
102**需要权限:**  ohos.permission.DISCOVER_BLUETOOTHohos.permission.LOCATION
103
104**系统能力:** SystemCapability.Communication.Bluetooth.Lite
105
106**参数:**
107**表3** SubscribeBLEFoundOptions
108
109| 参数 | 类型 | 必填 | 说明 |
110| -------- | -------- | -------- | -------- |
111| success | Function | 是 | 寻找到新设备上报时调用的回调函数。 |
112| fail | Function | 否 | 接口调用失败的回调函数。 |
113
114**表4** success返回值:
115
116| 参数名 | 类型 | 说明 |
117| -------- | -------- | -------- |
118| devices | Array<BluetoothDevice> | 新搜索到的设备列表。 |
119
120**表5** BluethoothDevice
121
122| 参数名 | 类型 | 说明 |
123| -------- | -------- | -------- |
124| addrType | string | 设备地址类型,可能值有:<br/>-&nbsp;public:&nbsp;公共地址<br/>-&nbsp;random:&nbsp;随机地址 |
125| addr | string | 设备MAC地址。 |
126| rssi | number | 设备蓝牙的信号强弱指标。 |
127| txpower | string | 广播数据中的txpower字段。 |
128| data | hex&nbsp;string | 广播数据(包含广播数据和扫描响应数据),十六进制字符串。 |
129
130**示例:**
131
132  ```
133  bluetooth.subscribeBLEFound({
134    success(data) {
135      console.log('call bluetooth.subscribeBLEFound success, data: ${data}.');
136    },
137    fail(data, code) {
138      console.log('call bluetooth.startBLEScan failed, data: ${data}, code: ${code}.');
139    }
140  });
141  ```
142
143
144## bluetooth.unsubscribeBLEFound()
145
146解除订阅寻找到新设备。
147
148**需要权限:**  ohos.permission.DISCOVER_BLUETOOTHohos.permission.LOCATION
149
150**系统能力:** SystemCapability.Communication.Bluetooth.Lite
151
152**示例:**
153
154  ```
155  bluetooth.unsubscribeBLEFound();
156  ```
157
158
159## 常见错误码
160
161| 错误码 | 说明 |
162| -------- | -------- |
163| 1100 | 是否处于已连接状态。 |
164| 1101 | 当前蓝牙适配器不可用。 |
165| 1102 | 没有找到指定设备。 |
166| 1103 | 连接失败。 |
167| 1104 | 没有找到指定服务。 |
168| 1105 | 没有找到指定特征值。 |
169| 1106 | 当前连接已断开。 |
170| 1107 | 当前特征值不支持此操作。 |
171| 1108 | 其余所有系统上报的异常。 |
172| 1109 | 系统版本不支持&nbsp;BLE。 |
173