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