1e41f4b71Sopenharmony_ci# @ohos.bluetooth.connection (蓝牙connection模块)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciconnection模块提供了对蓝牙操作和管理的方法。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```js
14e41f4b71Sopenharmony_ciimport { connection } from '@kit.ConnectivityKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## connection.pairDevice
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_cipairDevice(deviceId: string, callback: AsyncCallback<void>): void
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci发起蓝牙配对。使用Callback异步回调。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**参数:**
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                  |
33e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ----------------------------------- |
34e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
35e41f4b71Sopenharmony_ci| callback | AsyncCallback<void>  | 是    | 回调函数。当配对成功,err为undefined,否则为错误对象。 |
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**错误码**:
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
42e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
43e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
44e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
45e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
46e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
47e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
48e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**示例:**
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci```js
53e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
54e41f4b71Sopenharmony_ci//callback
55e41f4b71Sopenharmony_citry {
56e41f4b71Sopenharmony_ci    connection.pairDevice('11:22:33:44:55:66', (err: BusinessError) => {
57e41f4b71Sopenharmony_ci        console.info('pairDevice, device name err:' + JSON.stringify(err));
58e41f4b71Sopenharmony_ci    });
59e41f4b71Sopenharmony_ci} catch (err) {
60e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
61e41f4b71Sopenharmony_ci}
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci## connection.pairDevice
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_cipairDevice(deviceId: string): Promise<void>
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci发起蓝牙配对。使用Promise异步回调。
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**参数:**
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                  |
81e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ----------------------------------- |
82e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci**返回值:**
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci| 类型                  | 说明            |
87e41f4b71Sopenharmony_ci| ------------------- | ------------- |
88e41f4b71Sopenharmony_ci| Promise<void> | 返回promise对象。 |
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci**错误码**:
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
95e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
96e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
97e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
98e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
99e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
100e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
101e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**示例:**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci```js
106e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
107e41f4b71Sopenharmony_ci//promise
108e41f4b71Sopenharmony_citry {
109e41f4b71Sopenharmony_ci    connection.pairDevice('11:22:33:44:55:66').then(() => {
110e41f4b71Sopenharmony_ci        console.info('pairDevice');
111e41f4b71Sopenharmony_ci    }, (error: BusinessError) => {
112e41f4b71Sopenharmony_ci        console.info('pairDevice: errCode:' + error.code + ',errMessage' + error.message);
113e41f4b71Sopenharmony_ci    })
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci} catch (err) {
116e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_ci```
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci## connection.getRemoteDeviceName
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_cigetRemoteDeviceName(deviceId: string): string
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci获取对端蓝牙设备的名称。
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci**参数:**
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                |
136e41f4b71Sopenharmony_ci| -------- | ------ | ---- | --------------------------------- |
137e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci**返回值:**
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci| 类型     | 说明            |
142e41f4b71Sopenharmony_ci| ------ | ------------- |
143e41f4b71Sopenharmony_ci| string | 以字符串格式返回设备名称。 |
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci**错误码**:
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
150e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
151e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
152e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
153e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
154e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
155e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
156e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci**示例:**
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci```js
161e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
162e41f4b71Sopenharmony_citry {
163e41f4b71Sopenharmony_ci    let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX');
164e41f4b71Sopenharmony_ci} catch (err) {
165e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
166e41f4b71Sopenharmony_ci}
167e41f4b71Sopenharmony_ci```
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci## connection.getRemoteDeviceClass
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_cigetRemoteDeviceClass(deviceId: string): DeviceClass
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci获取对端蓝牙设备的类别。
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**参数:**
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                |
183e41f4b71Sopenharmony_ci| -------- | ------ | ---- | --------------------------------- |
184e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci**返回值:**
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci| 类型                          | 说明       |
189e41f4b71Sopenharmony_ci| --------------------------- | -------- |
190e41f4b71Sopenharmony_ci| [DeviceClass](#deviceclass) | 远程设备的类别。 |
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**错误码**:
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
197e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
198e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
199e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
200e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
201e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
202e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
203e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci**示例:**
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci```js
208e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
209e41f4b71Sopenharmony_citry {
210e41f4b71Sopenharmony_ci    let remoteDeviceClass: connection.DeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX');
211e41f4b71Sopenharmony_ci} catch (err) {
212e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
213e41f4b71Sopenharmony_ci}
214e41f4b71Sopenharmony_ci```
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci## connection.getRemoteProfileUuids<sup>12+</sup>
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_cigetRemoteProfileUuids(deviceId: string, callback: AsyncCallback&lt;Array&lt;ProfileUuids&gt;&gt;): void
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci获取对端蓝牙设备支持的Profile UUID。使用Callback异步回调。
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci**参数:**
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                  |
230e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ----------------------------------- |
231e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
232e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;Array&lt;[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)&gt;&gt; | 是    | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 |
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci**错误码**:
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
239e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
240e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
241e41f4b71Sopenharmony_ci|401 | Invalid parameter.    |
242e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
243e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
244e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
245e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci**示例:**
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci```js
250e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
251e41f4b71Sopenharmony_citry {
252e41f4b71Sopenharmony_ci    connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => {
253e41f4b71Sopenharmony_ci        console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
254e41f4b71Sopenharmony_ci    });
255e41f4b71Sopenharmony_ci} catch (err) {
256e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
257e41f4b71Sopenharmony_ci}
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci```
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci## connection.getRemoteProfileUuids<sup>12+</sup>
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_cigetRemoteProfileUuids(deviceId: string): Promise&lt;Array&lt;ProfileUuids&gt;&gt;
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ci获取对端蓝牙设备支持的Profile UUID。使用Promise异步回调。
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci**参数:**
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                  |
275e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ----------------------------------- |
276e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci**返回值:**
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci| 类型                  | 说明            |
281e41f4b71Sopenharmony_ci| ------------------- | ------------- |
282e41f4b71Sopenharmony_ci|   Promise&lt;Array&lt;[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)&gt;&gt; | 返回promise对象。 |
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci**错误码**:
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
289e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
290e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
291e41f4b71Sopenharmony_ci|401 | Invalid parameter.    |
292e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
293e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
294e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
295e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci**示例:**
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci```js
300e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
301e41f4b71Sopenharmony_citry {
302e41f4b71Sopenharmony_ci    connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
303e41f4b71Sopenharmony_ci        console.info('getRemoteProfileUuids');
304e41f4b71Sopenharmony_ci    }, (err: BusinessError) => {
305e41f4b71Sopenharmony_ci        console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
306e41f4b71Sopenharmony_ci    });
307e41f4b71Sopenharmony_ci} catch (err) {
308e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
309e41f4b71Sopenharmony_ci}
310e41f4b71Sopenharmony_ci```
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci## connection.getLocalName
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_cigetLocalName(): string
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci获取蓝牙本地设备名称。
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci**返回值:**
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci| 类型     | 说明        |
326e41f4b71Sopenharmony_ci| ------ | --------- |
327e41f4b71Sopenharmony_ci| string | 蓝牙本地设备名称。 |
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci**错误码**:
330e41f4b71Sopenharmony_ci
331e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
332e41f4b71Sopenharmony_ci
333e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
334e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
335e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
336e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
337e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
338e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_ci**示例:**
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci```js
343e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
344e41f4b71Sopenharmony_citry {
345e41f4b71Sopenharmony_ci    let localName: string = connection.getLocalName();
346e41f4b71Sopenharmony_ci} catch (err) {
347e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
348e41f4b71Sopenharmony_ci}
349e41f4b71Sopenharmony_ci```
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci## connection.getPairedDevices
353e41f4b71Sopenharmony_ci
354e41f4b71Sopenharmony_cigetPairedDevices(): Array&lt;string&gt;
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci获取蓝牙配对列表。
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci**返回值:**
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ci| 类型                  | 说明            |
367e41f4b71Sopenharmony_ci| ------------------- | ------------- |
368e41f4b71Sopenharmony_ci| Array&lt;string&gt; | 已配对蓝牙设备的地址列表。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。 |
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci**错误码**:
371e41f4b71Sopenharmony_ci
372e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
375e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
376e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
377e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
378e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
379e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
380e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci**示例:**
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ci```js
385e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
386e41f4b71Sopenharmony_citry {
387e41f4b71Sopenharmony_ci    let devices: Array<string> = connection.getPairedDevices();
388e41f4b71Sopenharmony_ci} catch (err) {
389e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
390e41f4b71Sopenharmony_ci}
391e41f4b71Sopenharmony_ci```
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ci## connection.getPairState<sup>11+</sup>
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_cigetPairState(deviceId: string): BondState
397e41f4b71Sopenharmony_ci
398e41f4b71Sopenharmony_ci获取蓝牙配对状态。
399e41f4b71Sopenharmony_ci
400e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
403e41f4b71Sopenharmony_ci
404e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core405e41f4b71Sopenharmony_ci
406e41f4b71Sopenharmony_ci**参数:**
407e41f4b71Sopenharmony_ci
408e41f4b71Sopenharmony_ci| 参数名      | 类型     | 必填   | 说明                                |
409e41f4b71Sopenharmony_ci| -------- | ------ | ---- | --------------------------------- |
410e41f4b71Sopenharmony_ci| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
411e41f4b71Sopenharmony_ci
412e41f4b71Sopenharmony_ci**返回值:**
413e41f4b71Sopenharmony_ci
414e41f4b71Sopenharmony_ci| 类型                          | 说明       |
415e41f4b71Sopenharmony_ci| --------------------------- | -------- |
416e41f4b71Sopenharmony_ci| [BondState](#bondstate) | 表示设备的蓝牙配对状态。 |
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci**错误码**:
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
421e41f4b71Sopenharmony_ci
422e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
423e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
424e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
425e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
426e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
427e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
428e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
429e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ci**示例:**
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ci```js
434e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
435e41f4b71Sopenharmony_citry {
436e41f4b71Sopenharmony_ci    let res: connection.BondState = connection.getPairState("XX:XX:XX:XX:XX:XX");
437e41f4b71Sopenharmony_ci    console.info('getPairState: ' + res);
438e41f4b71Sopenharmony_ci} catch (err) {
439e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
440e41f4b71Sopenharmony_ci}
441e41f4b71Sopenharmony_ci```
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_ci
444e41f4b71Sopenharmony_ci## connection.getProfileConnectionState
445e41f4b71Sopenharmony_ci
446e41f4b71Sopenharmony_cigetProfileConnectionState(profileId?: ProfileId): ProfileConnectionState
447e41f4b71Sopenharmony_ci
448e41f4b71Sopenharmony_ci获取蓝牙Profile的连接状态,其中ProfileId为可选参数。如果携带ProfileId,则返回的是当前Profile的连接状态。如果未携带ProfileId,任一Profile已连接则返回[STATE_CONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate),否则返回[STATE_DISCONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate)。
449e41f4b71Sopenharmony_ci
450e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
451e41f4b71Sopenharmony_ci
452e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core453e41f4b71Sopenharmony_ci
454e41f4b71Sopenharmony_ci**参数:**
455e41f4b71Sopenharmony_ci
456e41f4b71Sopenharmony_ci| 参数名       | 类型        | 必填   | 说明                                    |
457e41f4b71Sopenharmony_ci| --------- | --------- | ---- | ------------------------------------- |
458e41f4b71Sopenharmony_ci| profileId | [ProfileId](js-apis-bluetooth-constant.md#profileid) | 否    | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 |
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ci**返回值:**
461e41f4b71Sopenharmony_ci
462e41f4b71Sopenharmony_ci| 类型                                              | 说明                |
463e41f4b71Sopenharmony_ci| ------------------------------------------------- | ------------------- |
464e41f4b71Sopenharmony_ci| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | profile的连接状态。 |
465e41f4b71Sopenharmony_ci
466e41f4b71Sopenharmony_ci**错误码**:
467e41f4b71Sopenharmony_ci
468e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
469e41f4b71Sopenharmony_ci
470e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
471e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
472e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
473e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Incorrect parameter types.        |
474e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
475e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
476e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
477e41f4b71Sopenharmony_ci|2900004 | Profile not supported.                |
478e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
479e41f4b71Sopenharmony_ci
480e41f4b71Sopenharmony_ci**示例:**
481e41f4b71Sopenharmony_ci
482e41f4b71Sopenharmony_ci```js
483e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
484e41f4b71Sopenharmony_ciimport { constant } from '@kit.ConnectivityKit';
485e41f4b71Sopenharmony_citry {
486e41f4b71Sopenharmony_ci    let result: connection.ProfileConnectionState = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE);
487e41f4b71Sopenharmony_ci} catch (err) {
488e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
489e41f4b71Sopenharmony_ci}
490e41f4b71Sopenharmony_ci```
491e41f4b71Sopenharmony_ci
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ci## connection.setDevicePairingConfirmation
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_cisetDevicePairingConfirmation(deviceId: string, accept: boolean): void
496e41f4b71Sopenharmony_ci
497e41f4b71Sopenharmony_ci设置设备配对请求确认。
498e41f4b71Sopenharmony_ci
499e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH(该权限仅系统应用可申请)
500e41f4b71Sopenharmony_ci
501e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ci**参数:**
504e41f4b71Sopenharmony_ci
505e41f4b71Sopenharmony_ci| 参数名    | 类型      | 必填   | 说明                               |
506e41f4b71Sopenharmony_ci| ------   | ------- | ---- | -------------------------------- |
507e41f4b71Sopenharmony_ci| deviceId | string  | 是    | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
508e41f4b71Sopenharmony_ci| accept   | boolean | 是    | 接受配对请求设置为true,否则设置为false。        |
509e41f4b71Sopenharmony_ci
510e41f4b71Sopenharmony_ci**错误码**:
511e41f4b71Sopenharmony_ci
512e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
513e41f4b71Sopenharmony_ci
514e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
515e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
516e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
517e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
518e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
519e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
520e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
521e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
522e41f4b71Sopenharmony_ci
523e41f4b71Sopenharmony_ci**示例:**
524e41f4b71Sopenharmony_ci
525e41f4b71Sopenharmony_ci```js
526e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
527e41f4b71Sopenharmony_ci// 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认
528e41f4b71Sopenharmony_cifunction onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data为配对请求的入参,配对请求参数
529e41f4b71Sopenharmony_ci    console.info('pin required  = '+ JSON.stringify(data));
530e41f4b71Sopenharmony_ci    connection.setDevicePairingConfirmation(data.deviceId, true);
531e41f4b71Sopenharmony_ci}
532e41f4b71Sopenharmony_citry {
533e41f4b71Sopenharmony_ci    connection.on('pinRequired', onReceivePinRequiredEvent);
534e41f4b71Sopenharmony_ci} catch (err) {
535e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
536e41f4b71Sopenharmony_ci}
537e41f4b71Sopenharmony_ci```
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_ci
540e41f4b71Sopenharmony_ci## connection.setDevicePinCode
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_cisetDevicePinCode(deviceId: string, code: string, callback: AsyncCallback&lt;void&gt;): void
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci当蓝牙配对类型PinType为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。使用Callback异步回调。
545e41f4b71Sopenharmony_ci
546e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_ci**参数:**
551e41f4b71Sopenharmony_ci
552e41f4b71Sopenharmony_ci| 参数名    | 类型      | 必填   | 说明                               |
553e41f4b71Sopenharmony_ci| ------ | ------- | ---- | -------------------------------- |
554e41f4b71Sopenharmony_ci| deviceId | string  | 是    | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 |
555e41f4b71Sopenharmony_ci| code   | string  | 是    | 用户输入的PIN码。        |
556e41f4b71Sopenharmony_ci| callback   | AsyncCallback&lt;void&gt;  | 是    | 回调函数,当设置PinCode成功,err为undefined,否则为错误对象。        |
557e41f4b71Sopenharmony_ci
558e41f4b71Sopenharmony_ci**错误码**:
559e41f4b71Sopenharmony_ci
560e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
561e41f4b71Sopenharmony_ci
562e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
563e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
564e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
565e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
566e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
567e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
568e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
569e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
570e41f4b71Sopenharmony_ci
571e41f4b71Sopenharmony_ci**示例:**
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_ci```js
574e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
575e41f4b71Sopenharmony_ci//callback
576e41f4b71Sopenharmony_citry {
577e41f4b71Sopenharmony_ci    connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => {
578e41f4b71Sopenharmony_ci        console.info('setDevicePinCode,device name err:' + JSON.stringify(err));
579e41f4b71Sopenharmony_ci    });
580e41f4b71Sopenharmony_ci} catch (err) {
581e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
582e41f4b71Sopenharmony_ci}
583e41f4b71Sopenharmony_ci```
584e41f4b71Sopenharmony_ci
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci## connection.setDevicePinCode
587e41f4b71Sopenharmony_ci
588e41f4b71Sopenharmony_cisetDevicePinCode(deviceId: string, code: string): Promise&lt;void&gt;
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ci当蓝牙配对类型PinType为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。使用Promise异步回调。
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ci**参数:**
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci| 参数名    | 类型      | 必填   | 说明                               |
599e41f4b71Sopenharmony_ci| ------ | ------- | ---- | -------------------------------- |
600e41f4b71Sopenharmony_ci| deviceId | string  | 是    | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 |
601e41f4b71Sopenharmony_ci| code   | string  | 是    | 用户输入的PIN码。        |
602e41f4b71Sopenharmony_ci
603e41f4b71Sopenharmony_ci**返回值:**
604e41f4b71Sopenharmony_ci
605e41f4b71Sopenharmony_ci| 类型                  | 说明            |
606e41f4b71Sopenharmony_ci| ------------------- | ------------- |
607e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | 返回promise对象。 |
608e41f4b71Sopenharmony_ci
609e41f4b71Sopenharmony_ci**错误码**:
610e41f4b71Sopenharmony_ci
611e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
612e41f4b71Sopenharmony_ci
613e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
614e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
615e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
616e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
617e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
618e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
619e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
620e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
621e41f4b71Sopenharmony_ci
622e41f4b71Sopenharmony_ci**示例:**
623e41f4b71Sopenharmony_ci
624e41f4b71Sopenharmony_ci```js
625e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
626e41f4b71Sopenharmony_ci//promise
627e41f4b71Sopenharmony_citry {
628e41f4b71Sopenharmony_ci    connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => {
629e41f4b71Sopenharmony_ci        console.info('setDevicePinCode');
630e41f4b71Sopenharmony_ci    }, (error: BusinessError) => {
631e41f4b71Sopenharmony_ci        console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message);
632e41f4b71Sopenharmony_ci    })
633e41f4b71Sopenharmony_ci
634e41f4b71Sopenharmony_ci} catch (err) {
635e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
636e41f4b71Sopenharmony_ci}
637e41f4b71Sopenharmony_ci```
638e41f4b71Sopenharmony_ci
639e41f4b71Sopenharmony_ci
640e41f4b71Sopenharmony_ci## connection.setLocalName<sup>(deprecated)</sup>
641e41f4b71Sopenharmony_ci
642e41f4b71Sopenharmony_cisetLocalName(name: string): void
643e41f4b71Sopenharmony_ci
644e41f4b71Sopenharmony_ci设置蓝牙本地设备名称。
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ci> **说明:**<br/>
647e41f4b71Sopenharmony_ci> 从API version 10开始支持,从API version 12开始废弃,不再提供替代接口。
648e41f4b71Sopenharmony_ci
649e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
650e41f4b71Sopenharmony_ci
651e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core652e41f4b71Sopenharmony_ci
653e41f4b71Sopenharmony_ci**参数:**
654e41f4b71Sopenharmony_ci
655e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填   | 说明                    |
656e41f4b71Sopenharmony_ci| ---- | ------ | ---- | --------------------- |
657e41f4b71Sopenharmony_ci| name | string | 是    | 要设置的蓝牙名称,最大长度为248字节数。 |
658e41f4b71Sopenharmony_ci
659e41f4b71Sopenharmony_ci**错误码**:
660e41f4b71Sopenharmony_ci
661e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
662e41f4b71Sopenharmony_ci
663e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
664e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
665e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
666e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
667e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
668e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
669e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
670e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
671e41f4b71Sopenharmony_ci
672e41f4b71Sopenharmony_ci**示例:**
673e41f4b71Sopenharmony_ci
674e41f4b71Sopenharmony_ci```js
675e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
676e41f4b71Sopenharmony_citry {
677e41f4b71Sopenharmony_ci    connection.setLocalName('device_name');
678e41f4b71Sopenharmony_ci} catch (err) {
679e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
680e41f4b71Sopenharmony_ci}
681e41f4b71Sopenharmony_ci```
682e41f4b71Sopenharmony_ci
683e41f4b71Sopenharmony_ci
684e41f4b71Sopenharmony_ci## connection.setBluetoothScanMode
685e41f4b71Sopenharmony_ci
686e41f4b71Sopenharmony_cisetBluetoothScanMode(mode: ScanMode, duration: number): void
687e41f4b71Sopenharmony_ci
688e41f4b71Sopenharmony_ci设置蓝牙扫描模式,可以被远端设备发现。
689e41f4b71Sopenharmony_ci
690e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
691e41f4b71Sopenharmony_ci
692e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core693e41f4b71Sopenharmony_ci
694e41f4b71Sopenharmony_ci**参数:**
695e41f4b71Sopenharmony_ci
696e41f4b71Sopenharmony_ci| 参数名      | 类型                    | 必填   | 说明                           |
697e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ---------------------------- |
698e41f4b71Sopenharmony_ci| mode     | [ScanMode](#scanmode) | 是    | 蓝牙扫描模式。当扫描模式为SCAN_MODE_GENERAL_DISCOVERABLE时,如果超出duration持续时间(不为0),扫描模式会重新设置为SCAN_MODE_CONNECTABLE。               |
699e41f4b71Sopenharmony_ci| duration | number                | 是    | 设备可被发现的持续时间,单位为毫秒;设置为0则持续可发现。 |
700e41f4b71Sopenharmony_ci
701e41f4b71Sopenharmony_ci**错误码**:
702e41f4b71Sopenharmony_ci
703e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
704e41f4b71Sopenharmony_ci
705e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
706e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
707e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
708e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
709e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
710e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
711e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
712e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
713e41f4b71Sopenharmony_ci
714e41f4b71Sopenharmony_ci**示例:**
715e41f4b71Sopenharmony_ci
716e41f4b71Sopenharmony_ci```js
717e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
718e41f4b71Sopenharmony_citry {
719e41f4b71Sopenharmony_ci    // 设置为可连接可发现才可被远端设备扫描到,可以连接。
720e41f4b71Sopenharmony_ci    connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
721e41f4b71Sopenharmony_ci} catch (err) {
722e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
723e41f4b71Sopenharmony_ci}
724e41f4b71Sopenharmony_ci```
725e41f4b71Sopenharmony_ci
726e41f4b71Sopenharmony_ci
727e41f4b71Sopenharmony_ci## connection.getBluetoothScanMode
728e41f4b71Sopenharmony_ci
729e41f4b71Sopenharmony_cigetBluetoothScanMode(): ScanMode
730e41f4b71Sopenharmony_ci
731e41f4b71Sopenharmony_ci获取蓝牙扫描模式。
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
734e41f4b71Sopenharmony_ci
735e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core736e41f4b71Sopenharmony_ci
737e41f4b71Sopenharmony_ci**返回值:**
738e41f4b71Sopenharmony_ci
739e41f4b71Sopenharmony_ci| 类型                    | 说明      |
740e41f4b71Sopenharmony_ci| --------------------- | ------- |
741e41f4b71Sopenharmony_ci| [ScanMode](#scanmode) | 蓝牙扫描模式。 |
742e41f4b71Sopenharmony_ci
743e41f4b71Sopenharmony_ci**错误码**:
744e41f4b71Sopenharmony_ci
745e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
746e41f4b71Sopenharmony_ci
747e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
748e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
749e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
750e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
751e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
752e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
753e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
754e41f4b71Sopenharmony_ci
755e41f4b71Sopenharmony_ci**示例:**
756e41f4b71Sopenharmony_ci
757e41f4b71Sopenharmony_ci```js
758e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
759e41f4b71Sopenharmony_citry {
760e41f4b71Sopenharmony_ci    let scanMode: connection.ScanMode = connection.getBluetoothScanMode();
761e41f4b71Sopenharmony_ci} catch (err) {
762e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
763e41f4b71Sopenharmony_ci}
764e41f4b71Sopenharmony_ci```
765e41f4b71Sopenharmony_ci
766e41f4b71Sopenharmony_ci
767e41f4b71Sopenharmony_ci## connection.startBluetoothDiscovery
768e41f4b71Sopenharmony_ci
769e41f4b71Sopenharmony_cistartBluetoothDiscovery(): void
770e41f4b71Sopenharmony_ci
771e41f4b71Sopenharmony_ci开启蓝牙扫描,可以发现远端设备。
772e41f4b71Sopenharmony_ci
773e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
774e41f4b71Sopenharmony_ci
775e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core778e41f4b71Sopenharmony_ci
779e41f4b71Sopenharmony_ci**错误码**:
780e41f4b71Sopenharmony_ci
781e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
782e41f4b71Sopenharmony_ci
783e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
784e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
785e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
786e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
787e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
788e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
789e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
790e41f4b71Sopenharmony_ci
791e41f4b71Sopenharmony_ci**示例:**
792e41f4b71Sopenharmony_ci
793e41f4b71Sopenharmony_ci```js
794e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
795e41f4b71Sopenharmony_cifunction onReceiveEvent(data: Array<string>) {
796e41f4b71Sopenharmony_ci    console.info('data length' + data.length);
797e41f4b71Sopenharmony_ci}
798e41f4b71Sopenharmony_citry {
799e41f4b71Sopenharmony_ci    connection.on('bluetoothDeviceFind', onReceiveEvent);
800e41f4b71Sopenharmony_ci    connection.startBluetoothDiscovery();
801e41f4b71Sopenharmony_ci} catch (err) {
802e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
803e41f4b71Sopenharmony_ci}
804e41f4b71Sopenharmony_ci```
805e41f4b71Sopenharmony_ci
806e41f4b71Sopenharmony_ci
807e41f4b71Sopenharmony_ci## connection.stopBluetoothDiscovery
808e41f4b71Sopenharmony_ci
809e41f4b71Sopenharmony_cistopBluetoothDiscovery(): void
810e41f4b71Sopenharmony_ci
811e41f4b71Sopenharmony_ci关闭蓝牙扫描。
812e41f4b71Sopenharmony_ci
813e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
814e41f4b71Sopenharmony_ci
815e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
816e41f4b71Sopenharmony_ci
817e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core818e41f4b71Sopenharmony_ci
819e41f4b71Sopenharmony_ci**错误码**:
820e41f4b71Sopenharmony_ci
821e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
822e41f4b71Sopenharmony_ci
823e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
824e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
825e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
826e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
827e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
828e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
829e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
830e41f4b71Sopenharmony_ci
831e41f4b71Sopenharmony_ci**示例:**
832e41f4b71Sopenharmony_ci
833e41f4b71Sopenharmony_ci```js
834e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
835e41f4b71Sopenharmony_citry {
836e41f4b71Sopenharmony_ci    connection.stopBluetoothDiscovery();
837e41f4b71Sopenharmony_ci} catch (err) {
838e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
839e41f4b71Sopenharmony_ci}
840e41f4b71Sopenharmony_ci```
841e41f4b71Sopenharmony_ci
842e41f4b71Sopenharmony_ci
843e41f4b71Sopenharmony_ci## connection.isBluetoothDiscovering<sup>11+</sup>
844e41f4b71Sopenharmony_ci
845e41f4b71Sopenharmony_ciisBluetoothDiscovering(): boolean
846e41f4b71Sopenharmony_ci
847e41f4b71Sopenharmony_ci查询设备的蓝牙发现状态。
848e41f4b71Sopenharmony_ci
849e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
850e41f4b71Sopenharmony_ci
851e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core852e41f4b71Sopenharmony_ci
853e41f4b71Sopenharmony_ci**返回值:**
854e41f4b71Sopenharmony_ci
855e41f4b71Sopenharmony_ci| 类型                  | 说明            |
856e41f4b71Sopenharmony_ci| ------------------- | ------------- |
857e41f4b71Sopenharmony_ci|   boolean           | 设备已开启蓝牙发现为true,否则为false。 |
858e41f4b71Sopenharmony_ci
859e41f4b71Sopenharmony_ci**错误码**:
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
862e41f4b71Sopenharmony_ci
863e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
864e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
865e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
866e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
867e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
868e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
869e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
870e41f4b71Sopenharmony_ci
871e41f4b71Sopenharmony_ci**示例:**
872e41f4b71Sopenharmony_ci
873e41f4b71Sopenharmony_ci```js
874e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
875e41f4b71Sopenharmony_citry {
876e41f4b71Sopenharmony_ci    let res: boolean = connection.isBluetoothDiscovering();
877e41f4b71Sopenharmony_ci    console.info('isBluetoothDiscovering: ' + res);
878e41f4b71Sopenharmony_ci} catch (err) {
879e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
880e41f4b71Sopenharmony_ci}
881e41f4b71Sopenharmony_ci```
882e41f4b71Sopenharmony_ci
883e41f4b71Sopenharmony_ci## connection.setRemoteDeviceName<sup>12+</sup>
884e41f4b71Sopenharmony_ci
885e41f4b71Sopenharmony_cisetRemoteDeviceName(deviceId: string, name: string): Promise&lt;void&gt;
886e41f4b71Sopenharmony_ci
887e41f4b71Sopenharmony_ci设置蓝牙远端设备名称。使用Promise异步回调。
888e41f4b71Sopenharmony_ci
889e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
890e41f4b71Sopenharmony_ci
891e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
892e41f4b71Sopenharmony_ci
893e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core894e41f4b71Sopenharmony_ci
895e41f4b71Sopenharmony_ci**参数:**
896e41f4b71Sopenharmony_ci
897e41f4b71Sopenharmony_ci| 参数名      | 类型                                  | 必填   | 说明                                     |
898e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
899e41f4b71Sopenharmony_ci| deviceId     | string                              | 是    | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 |
900e41f4b71Sopenharmony_ci| name | string | 是    | 修改远端设备名称,最大长度为64字节。    |
901e41f4b71Sopenharmony_ci
902e41f4b71Sopenharmony_ci**返回值:**
903e41f4b71Sopenharmony_ci
904e41f4b71Sopenharmony_ci| 类型                  | 说明            |
905e41f4b71Sopenharmony_ci| ------------------- | ------------- |
906e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | 以Promise形式返回设置蓝牙远端设备名称的结果,设置失败时返回错误码信息。 |
907e41f4b71Sopenharmony_ci
908e41f4b71Sopenharmony_ci**错误码**:
909e41f4b71Sopenharmony_ci
910e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
911e41f4b71Sopenharmony_ci
912e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
913e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
914e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
915e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.            |
916e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
917e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
918e41f4b71Sopenharmony_ci
919e41f4b71Sopenharmony_ci**示例:**
920e41f4b71Sopenharmony_ci
921e41f4b71Sopenharmony_ci```js
922e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
923e41f4b71Sopenharmony_ci//promise
924e41f4b71Sopenharmony_citry {
925e41f4b71Sopenharmony_ci    connection.setRemoteDeviceName('11:22:33:44:55:66', 'RemoteDeviceName').then(() => {
926e41f4b71Sopenharmony_ci        console.info('setRemoteDeviceName success');
927e41f4b71Sopenharmony_ci    }, (error: BusinessError) => {
928e41f4b71Sopenharmony_ci        console.error('setRemoteDeviceName: errCode:' + error.code + ',errMessage' + error.message);
929e41f4b71Sopenharmony_ci    })
930e41f4b71Sopenharmony_ci
931e41f4b71Sopenharmony_ci} catch (err) {
932e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
933e41f4b71Sopenharmony_ci}
934e41f4b71Sopenharmony_ci```
935e41f4b71Sopenharmony_ci
936e41f4b71Sopenharmony_ci
937e41f4b71Sopenharmony_ci## connection.getRemoteDeviceBatteryInfo<sup>12+</sup>
938e41f4b71Sopenharmony_ci
939e41f4b71Sopenharmony_cigetRemoteDeviceBatteryInfo(deviceId: string): Promise&lt;BatteryInfo&gt;
940e41f4b71Sopenharmony_ci
941e41f4b71Sopenharmony_ci获取蓝牙远端设备的电量信息。使用Promise异步回调。
942e41f4b71Sopenharmony_ci
943e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
944e41f4b71Sopenharmony_ci
945e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core946e41f4b71Sopenharmony_ci
947e41f4b71Sopenharmony_ci**参数:**
948e41f4b71Sopenharmony_ci
949e41f4b71Sopenharmony_ci| 参数名    | 类型      | 必填   | 说明                               |
950e41f4b71Sopenharmony_ci| ------ | ------- | ---- | -------------------------------- |
951e41f4b71Sopenharmony_ci| deviceId | string  | 是    | 表示远端设备MAC地址,例如:"11:22:33:AA:BB:FF"。 |
952e41f4b71Sopenharmony_ci
953e41f4b71Sopenharmony_ci**返回值:**
954e41f4b71Sopenharmony_ci
955e41f4b71Sopenharmony_ci| 类型                  | 说明         |
956e41f4b71Sopenharmony_ci| ------------------- | ------------- |
957e41f4b71Sopenharmony_ci| Promise&lt;[BatteryInfo](#batteryinfo12)&gt; | 以Promise形式返回蓝牙远端设备的电量信息。 |
958e41f4b71Sopenharmony_ci
959e41f4b71Sopenharmony_ci**错误码**:
960e41f4b71Sopenharmony_ci
961e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
962e41f4b71Sopenharmony_ci
963e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
964e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
965e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
966e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.            |
967e41f4b71Sopenharmony_ci|2900001 | Service stopped.                         |
968e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled.                 |
969e41f4b71Sopenharmony_ci
970e41f4b71Sopenharmony_ci**示例:**
971e41f4b71Sopenharmony_ci
972e41f4b71Sopenharmony_ci```js
973e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
974e41f4b71Sopenharmony_ci// promise
975e41f4b71Sopenharmony_citry {
976e41f4b71Sopenharmony_ci    connection.getRemoteDeviceBatteryInfo('11:22:33:AA:BB:FF').then((data: connection.BatteryInfo) => {
977e41f4b71Sopenharmony_ci        console.info('getRemoteDeviceBatteryInfo success, DeviceType:' + JSON.stringify(data));
978e41f4b71Sopenharmony_ci    });
979e41f4b71Sopenharmony_ci} catch (err) {
980e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
981e41f4b71Sopenharmony_ci}
982e41f4b71Sopenharmony_ci```
983e41f4b71Sopenharmony_ci
984e41f4b71Sopenharmony_ci
985e41f4b71Sopenharmony_ci## connection.on('batteryChange')<sup>12+</sup>
986e41f4b71Sopenharmony_ci
987e41f4b71Sopenharmony_cion(type: 'batteryChange', callback: Callback&lt;BatteryInfo&gt;): void
988e41f4b71Sopenharmony_ci
989e41f4b71Sopenharmony_ci订阅蓝牙远程设备的电量信息变更事件。使用Callback异步回调。
990e41f4b71Sopenharmony_ci
991e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
992e41f4b71Sopenharmony_ci
993e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core994e41f4b71Sopenharmony_ci
995e41f4b71Sopenharmony_ci**参数:**
996e41f4b71Sopenharmony_ci
997e41f4b71Sopenharmony_ci| 参数名      | 类型                                  | 必填   | 说明                                     |
998e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
999e41f4b71Sopenharmony_ci| type     | string                              | 是    | 填写"batteryChange"字符串,表示蓝牙远端设备的电池信息变更事件。 |
1000e41f4b71Sopenharmony_ci| callback | Callback&lt;[BatteryInfo](#batteryinfo12)&gt; | 是    | 表示回调函数的入参,返回电量信息。    |
1001e41f4b71Sopenharmony_ci
1002e41f4b71Sopenharmony_ci**错误码**:
1003e41f4b71Sopenharmony_ci
1004e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1005e41f4b71Sopenharmony_ci
1006e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1007e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1008e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1009e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1010e41f4b71Sopenharmony_ci
1011e41f4b71Sopenharmony_ci**示例:**
1012e41f4b71Sopenharmony_ci
1013e41f4b71Sopenharmony_ci```js
1014e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1015e41f4b71Sopenharmony_cilet onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => {
1016e41f4b71Sopenharmony_ci    console.info('BatteryInfo = '+ JSON.stringify(data));
1017e41f4b71Sopenharmony_ci}
1018e41f4b71Sopenharmony_citry {
1019e41f4b71Sopenharmony_ci    connection.on('batteryChange', onReceiveEvent);
1020e41f4b71Sopenharmony_ci} catch (err) {
1021e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1022e41f4b71Sopenharmony_ci}
1023e41f4b71Sopenharmony_ci```
1024e41f4b71Sopenharmony_ci
1025e41f4b71Sopenharmony_ci
1026e41f4b71Sopenharmony_ci## connection.off('batteryChange')<sup>12+</sup>
1027e41f4b71Sopenharmony_ci
1028e41f4b71Sopenharmony_cioff(type: 'batteryChange', callback?: Callback&lt;BatteryInfo&gt;): void
1029e41f4b71Sopenharmony_ci
1030e41f4b71Sopenharmony_ci取消订阅蓝牙远程设备的电量信息变更事件。
1031e41f4b71Sopenharmony_ci
1032e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1033e41f4b71Sopenharmony_ci
1034e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1035e41f4b71Sopenharmony_ci
1036e41f4b71Sopenharmony_ci**参数:**
1037e41f4b71Sopenharmony_ci
1038e41f4b71Sopenharmony_ci| 参数名      | 类型                                  | 必填   | 说明                                       |
1039e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | ---------------------------------------- |
1040e41f4b71Sopenharmony_ci| type     | string                              | 是    | 填写"batteryChange"字符串,表示取消蓝牙远端设备的电池信息变更事件。   |
1041e41f4b71Sopenharmony_ci| callback | Callback&lt;[BatteryInfo](#batteryinfo12)&gt; | 否    | 表示回调函数的入参,返回电量信息。 |
1042e41f4b71Sopenharmony_ci
1043e41f4b71Sopenharmony_ci**错误码**:
1044e41f4b71Sopenharmony_ci
1045e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1046e41f4b71Sopenharmony_ci
1047e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1048e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1049e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1050e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1051e41f4b71Sopenharmony_ci
1052e41f4b71Sopenharmony_ci**示例:**
1053e41f4b71Sopenharmony_ci
1054e41f4b71Sopenharmony_ci```js
1055e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1056e41f4b71Sopenharmony_cilet onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => {
1057e41f4b71Sopenharmony_ci    console.info('BatteryInfo = '+ JSON.stringify(data));
1058e41f4b71Sopenharmony_ci}
1059e41f4b71Sopenharmony_citry {
1060e41f4b71Sopenharmony_ci    connection.on('batteryChange', onReceiveEvent);
1061e41f4b71Sopenharmony_ci    connection.off('batteryChange', onReceiveEvent);
1062e41f4b71Sopenharmony_ci} catch (err) {
1063e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1064e41f4b71Sopenharmony_ci}
1065e41f4b71Sopenharmony_ci```
1066e41f4b71Sopenharmony_ci
1067e41f4b71Sopenharmony_ci
1068e41f4b71Sopenharmony_ci## connection.on('bluetoothDeviceFind')
1069e41f4b71Sopenharmony_ci
1070e41f4b71Sopenharmony_cion(type: 'bluetoothDeviceFind', callback: Callback&lt;Array&lt;string&gt;&gt;): void
1071e41f4b71Sopenharmony_ci
1072e41f4b71Sopenharmony_ci订阅蓝牙设备发现上报事件。使用Callback异步回调。
1073e41f4b71Sopenharmony_ci
1074e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1075e41f4b71Sopenharmony_ci
1076e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1077e41f4b71Sopenharmony_ci
1078e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1079e41f4b71Sopenharmony_ci
1080e41f4b71Sopenharmony_ci**参数:**
1081e41f4b71Sopenharmony_ci
1082e41f4b71Sopenharmony_ci| 参数名      | 类型                                  | 必填   | 说明                                     |
1083e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------------------- |
1084e41f4b71Sopenharmony_ci| type     | string                              | 是    | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 |
1085e41f4b71Sopenharmony_ci| callback | Callback&lt;Array&lt;string&gt;&gt; | 是    | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。    |
1086e41f4b71Sopenharmony_ci
1087e41f4b71Sopenharmony_ci**错误码**:
1088e41f4b71Sopenharmony_ci
1089e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1090e41f4b71Sopenharmony_ci
1091e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1092e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1093e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1094e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1095e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
1096e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1097e41f4b71Sopenharmony_ci
1098e41f4b71Sopenharmony_ci**示例:**
1099e41f4b71Sopenharmony_ci
1100e41f4b71Sopenharmony_ci```js
1101e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1102e41f4b71Sopenharmony_cifunction onReceiveEvent(data: Array<string>) { // data为蓝牙设备地址集合
1103e41f4b71Sopenharmony_ci    console.info('bluetooth device find = '+ JSON.stringify(data));
1104e41f4b71Sopenharmony_ci}
1105e41f4b71Sopenharmony_citry {
1106e41f4b71Sopenharmony_ci    connection.on('bluetoothDeviceFind', onReceiveEvent);
1107e41f4b71Sopenharmony_ci} catch (err) {
1108e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1109e41f4b71Sopenharmony_ci}
1110e41f4b71Sopenharmony_ci```
1111e41f4b71Sopenharmony_ci
1112e41f4b71Sopenharmony_ci
1113e41f4b71Sopenharmony_ci## connection.off('bluetoothDeviceFind')
1114e41f4b71Sopenharmony_ci
1115e41f4b71Sopenharmony_cioff(type: 'bluetoothDeviceFind', callback?: Callback&lt;Array&lt;string&gt;&gt;): void
1116e41f4b71Sopenharmony_ci
1117e41f4b71Sopenharmony_ci取消订阅蓝牙设备发现上报事件。
1118e41f4b71Sopenharmony_ci
1119e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1120e41f4b71Sopenharmony_ci
1121e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1122e41f4b71Sopenharmony_ci
1123e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1124e41f4b71Sopenharmony_ci
1125e41f4b71Sopenharmony_ci**参数:**
1126e41f4b71Sopenharmony_ci
1127e41f4b71Sopenharmony_ci| 参数名      | 类型                                  | 必填   | 说明                                       |
1128e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | ---------------------------------------- |
1129e41f4b71Sopenharmony_ci| type     | string                              | 是    | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。   |
1130e41f4b71Sopenharmony_ci| callback | Callback&lt;Array&lt;string&gt;&gt; | 否    | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 |
1131e41f4b71Sopenharmony_ci
1132e41f4b71Sopenharmony_ci**错误码**:
1133e41f4b71Sopenharmony_ci
1134e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1135e41f4b71Sopenharmony_ci
1136e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1137e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1138e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1139e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
1140e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1141e41f4b71Sopenharmony_ci
1142e41f4b71Sopenharmony_ci**示例:**
1143e41f4b71Sopenharmony_ci
1144e41f4b71Sopenharmony_ci```js
1145e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1146e41f4b71Sopenharmony_cifunction onReceiveEvent(data: Array<string>) {
1147e41f4b71Sopenharmony_ci    console.info('bluetooth device find = '+ JSON.stringify(data));
1148e41f4b71Sopenharmony_ci}
1149e41f4b71Sopenharmony_citry {
1150e41f4b71Sopenharmony_ci    connection.on('bluetoothDeviceFind', onReceiveEvent);
1151e41f4b71Sopenharmony_ci    connection.off('bluetoothDeviceFind', onReceiveEvent);
1152e41f4b71Sopenharmony_ci} catch (err) {
1153e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1154e41f4b71Sopenharmony_ci}
1155e41f4b71Sopenharmony_ci```
1156e41f4b71Sopenharmony_ci
1157e41f4b71Sopenharmony_ci
1158e41f4b71Sopenharmony_ci## connection.on('bondStateChange')
1159e41f4b71Sopenharmony_ci
1160e41f4b71Sopenharmony_cion(type: 'bondStateChange', callback: Callback&lt;BondStateParam&gt;): void
1161e41f4b71Sopenharmony_ci
1162e41f4b71Sopenharmony_ci订阅蓝牙配对状态改变事件。使用Callback异步回调。
1163e41f4b71Sopenharmony_ci
1164e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1165e41f4b71Sopenharmony_ci
1166e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1167e41f4b71Sopenharmony_ci
1168e41f4b71Sopenharmony_ci**参数:**
1169e41f4b71Sopenharmony_ci
1170e41f4b71Sopenharmony_ci| 参数名      | 类型                                       | 必填   | 说明                                   |
1171e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------------------ |
1172e41f4b71Sopenharmony_ci| type     | string                                   | 是    | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 |
1173e41f4b71Sopenharmony_ci| callback | Callback&lt;[BondStateParam](#bondstateparam)&gt; | 是    | 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。    |
1174e41f4b71Sopenharmony_ci
1175e41f4b71Sopenharmony_ci**错误码**:
1176e41f4b71Sopenharmony_ci
1177e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1178e41f4b71Sopenharmony_ci
1179e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1180e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1181e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1182e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1183e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
1184e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1185e41f4b71Sopenharmony_ci
1186e41f4b71Sopenharmony_ci**示例:**
1187e41f4b71Sopenharmony_ci
1188e41f4b71Sopenharmony_ci```js
1189e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1190e41f4b71Sopenharmony_cifunction onReceiveEvent(data: connection.BondStateParam) { // data为回调函数入参,表示配对的状态
1191e41f4b71Sopenharmony_ci    console.info('pair state = '+ JSON.stringify(data));
1192e41f4b71Sopenharmony_ci}
1193e41f4b71Sopenharmony_citry {
1194e41f4b71Sopenharmony_ci    connection.on('bondStateChange', onReceiveEvent);
1195e41f4b71Sopenharmony_ci} catch (err) {
1196e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1197e41f4b71Sopenharmony_ci}
1198e41f4b71Sopenharmony_ci```
1199e41f4b71Sopenharmony_ci
1200e41f4b71Sopenharmony_ci
1201e41f4b71Sopenharmony_ci## connection.off('bondStateChange')
1202e41f4b71Sopenharmony_ci
1203e41f4b71Sopenharmony_cioff(type: 'bondStateChange', callback?: Callback&lt;BondStateParam&gt;): void
1204e41f4b71Sopenharmony_ci
1205e41f4b71Sopenharmony_ci取消订阅蓝牙配对状态改变事件。
1206e41f4b71Sopenharmony_ci
1207e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1208e41f4b71Sopenharmony_ci
1209e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1210e41f4b71Sopenharmony_ci
1211e41f4b71Sopenharmony_ci**参数:**
1212e41f4b71Sopenharmony_ci
1213e41f4b71Sopenharmony_ci| 参数名      | 类型                                       | 必填   | 说明                                       |
1214e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
1215e41f4b71Sopenharmony_ci| type     | string                                   | 是    | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。     |
1216e41f4b71Sopenharmony_ci| callback | Callback&lt;[BondStateParam](#bondstateparam)&gt; | 否    | 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 |
1217e41f4b71Sopenharmony_ci
1218e41f4b71Sopenharmony_ci**错误码**:
1219e41f4b71Sopenharmony_ci
1220e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1221e41f4b71Sopenharmony_ci
1222e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1223e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1224e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1225e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1226e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
1227e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1228e41f4b71Sopenharmony_ci
1229e41f4b71Sopenharmony_ci**示例:**
1230e41f4b71Sopenharmony_ci
1231e41f4b71Sopenharmony_ci```js
1232e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1233e41f4b71Sopenharmony_cifunction onReceiveEvent(data: connection.BondStateParam) {
1234e41f4b71Sopenharmony_ci    console.info('bond state = '+ JSON.stringify(data));
1235e41f4b71Sopenharmony_ci}
1236e41f4b71Sopenharmony_citry {
1237e41f4b71Sopenharmony_ci    connection.on('bondStateChange', onReceiveEvent);
1238e41f4b71Sopenharmony_ci    connection.off('bondStateChange', onReceiveEvent);
1239e41f4b71Sopenharmony_ci} catch (err) {
1240e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1241e41f4b71Sopenharmony_ci}
1242e41f4b71Sopenharmony_ci```
1243e41f4b71Sopenharmony_ci
1244e41f4b71Sopenharmony_ci
1245e41f4b71Sopenharmony_ci## connection.on('pinRequired')
1246e41f4b71Sopenharmony_ci
1247e41f4b71Sopenharmony_cion(type: 'pinRequired', callback: Callback&lt;PinRequiredParam&gt;): void
1248e41f4b71Sopenharmony_ci
1249e41f4b71Sopenharmony_ci订阅远端蓝牙设备的配对请求事件。使用Callback异步回调。
1250e41f4b71Sopenharmony_ci
1251e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1252e41f4b71Sopenharmony_ci
1253e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1254e41f4b71Sopenharmony_ci
1255e41f4b71Sopenharmony_ci**参数:**
1256e41f4b71Sopenharmony_ci
1257e41f4b71Sopenharmony_ci| 参数名      | 类型                                       | 必填   | 说明                               |
1258e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | -------------------------------- |
1259e41f4b71Sopenharmony_ci| type     | string                                   | 是    | 填写"pinRequired"字符串,表示配对请求事件。     |
1260e41f4b71Sopenharmony_ci| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | 是    | 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。 |
1261e41f4b71Sopenharmony_ci
1262e41f4b71Sopenharmony_ci**错误码**:
1263e41f4b71Sopenharmony_ci
1264e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1265e41f4b71Sopenharmony_ci
1266e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1267e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1268e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1269e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1270e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
1271e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1272e41f4b71Sopenharmony_ci
1273e41f4b71Sopenharmony_ci**示例:**
1274e41f4b71Sopenharmony_ci
1275e41f4b71Sopenharmony_ci```js
1276e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1277e41f4b71Sopenharmony_cifunction onReceiveEvent(data: connection.PinRequiredParam) { // data为配对请求参数
1278e41f4b71Sopenharmony_ci    console.info('pin required = '+ JSON.stringify(data));
1279e41f4b71Sopenharmony_ci}
1280e41f4b71Sopenharmony_citry {
1281e41f4b71Sopenharmony_ci    connection.on('pinRequired', onReceiveEvent);
1282e41f4b71Sopenharmony_ci} catch (err) {
1283e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1284e41f4b71Sopenharmony_ci}
1285e41f4b71Sopenharmony_ci```
1286e41f4b71Sopenharmony_ci
1287e41f4b71Sopenharmony_ci
1288e41f4b71Sopenharmony_ci## connection.off('pinRequired')
1289e41f4b71Sopenharmony_ci
1290e41f4b71Sopenharmony_cioff(type: 'pinRequired', callback?: Callback&lt;PinRequiredParam&gt;): void
1291e41f4b71Sopenharmony_ci
1292e41f4b71Sopenharmony_ci取消订阅远端蓝牙设备的配对请求事件。
1293e41f4b71Sopenharmony_ci
1294e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1295e41f4b71Sopenharmony_ci
1296e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1297e41f4b71Sopenharmony_ci
1298e41f4b71Sopenharmony_ci**参数:**
1299e41f4b71Sopenharmony_ci
1300e41f4b71Sopenharmony_ci| 参数名      | 类型                                       | 必填   | 说明                                       |
1301e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
1302e41f4b71Sopenharmony_ci| type     | string                                   | 是    | 填写"pinRequired"字符串,表示配对请求事件。             |
1303e41f4b71Sopenharmony_ci| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | 否    | 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。 |
1304e41f4b71Sopenharmony_ci
1305e41f4b71Sopenharmony_ci**错误码**:
1306e41f4b71Sopenharmony_ci
1307e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1308e41f4b71Sopenharmony_ci
1309e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
1310e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
1311e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
1312e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1313e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
1314e41f4b71Sopenharmony_ci|2900099 | Operation failed.                        |
1315e41f4b71Sopenharmony_ci
1316e41f4b71Sopenharmony_ci**示例:**
1317e41f4b71Sopenharmony_ci
1318e41f4b71Sopenharmony_ci```js
1319e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1320e41f4b71Sopenharmony_cifunction onReceiveEvent(data: connection.PinRequiredParam) {
1321e41f4b71Sopenharmony_ci    console.info('pin required = '+ JSON.stringify(data));
1322e41f4b71Sopenharmony_ci}
1323e41f4b71Sopenharmony_citry {
1324e41f4b71Sopenharmony_ci    connection.on('pinRequired', onReceiveEvent);
1325e41f4b71Sopenharmony_ci    connection.off('pinRequired', onReceiveEvent);
1326e41f4b71Sopenharmony_ci} catch (err) {
1327e41f4b71Sopenharmony_ci    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1328e41f4b71Sopenharmony_ci}
1329e41f4b71Sopenharmony_ci```
1330e41f4b71Sopenharmony_ci
1331e41f4b71Sopenharmony_ci
1332e41f4b71Sopenharmony_ci## BondStateParam
1333e41f4b71Sopenharmony_ci
1334e41f4b71Sopenharmony_ci描述配对状态参数。
1335e41f4b71Sopenharmony_ci
1336e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1337e41f4b71Sopenharmony_ci
1338e41f4b71Sopenharmony_ci| 名称       | 类型   | 可读   | 可写   | 说明          |
1339e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ---- | ----------- |
1340e41f4b71Sopenharmony_ci| deviceId | string      | 是    | 否    | 表示要配对的设备ID。 |
1341e41f4b71Sopenharmony_ci| state    | BondState   | 是    | 否    | 表示配对设备的状态。 |
1342e41f4b71Sopenharmony_ci| cause<sup>12+</sup>| [UnbondCause](#unbondcause12) | 是 | 否 | 表示配对失败的原因。|
1343e41f4b71Sopenharmony_ci
1344e41f4b71Sopenharmony_ci
1345e41f4b71Sopenharmony_ci## PinRequiredParam
1346e41f4b71Sopenharmony_ci
1347e41f4b71Sopenharmony_ci描述配对请求参数。
1348e41f4b71Sopenharmony_ci
1349e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1350e41f4b71Sopenharmony_ci
1351e41f4b71Sopenharmony_ci| 名称       | 类型   | 可读   | 可写   | 说明          |
1352e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ---- | ----------- |
1353e41f4b71Sopenharmony_ci| deviceId | string | 是    | 否    | 表示要配对的设备ID。 |
1354e41f4b71Sopenharmony_ci| pinCode  | string | 是    | 否    | 表示要配对的密钥。   |
1355e41f4b71Sopenharmony_ci
1356e41f4b71Sopenharmony_ci
1357e41f4b71Sopenharmony_ci
1358e41f4b71Sopenharmony_ci## DeviceClass
1359e41f4b71Sopenharmony_ci
1360e41f4b71Sopenharmony_ci描述蓝牙设备的类别。
1361e41f4b71Sopenharmony_ci
1362e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1363e41f4b71Sopenharmony_ci
1364e41f4b71Sopenharmony_ci| 名称              | 类型                                | 可读   | 可写   | 说明               |
1365e41f4b71Sopenharmony_ci| --------------- | ----------------------------------- | ---- | ---- | ---------------- |
1366e41f4b71Sopenharmony_ci| majorClass      | [MajorClass](js-apis-bluetooth-constant.md#majorclass)           | 是    | 否    | 表示蓝牙设备主要类别的枚举。   |
1367e41f4b71Sopenharmony_ci| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | 是    | 否    | 表示主要次要蓝牙设备类别的枚举。 |
1368e41f4b71Sopenharmony_ci| classOfDevice   | number                              | 是    | 否    | 表示设备类别。          |
1369e41f4b71Sopenharmony_ci
1370e41f4b71Sopenharmony_ci
1371e41f4b71Sopenharmony_ci## BatteryInfo<sup>12+</sup>
1372e41f4b71Sopenharmony_ci
1373e41f4b71Sopenharmony_ci描述电量信息的内容。
1374e41f4b71Sopenharmony_ci
1375e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1376e41f4b71Sopenharmony_ci
1377e41f4b71Sopenharmony_ci| 名称       | 类型   | 可读   | 可写   | 说明          |
1378e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ---- | ----------- |
1379e41f4b71Sopenharmony_ci| batteryLevel  | number | 是    | 否    | 表示远端设备的电量值,如果值为-1,表示没有电量信息。   |
1380e41f4b71Sopenharmony_ci| leftEarBatteryLevel  | number | 是    | 否    | 表示左侧耳机的电量值,如果值为-1,表示没有电量信息。   |
1381e41f4b71Sopenharmony_ci| leftEarChargeState  | [DeviceChargeState](#devicechargestate12) | 是    | 否    | 表示左侧耳机的充电状态。   |
1382e41f4b71Sopenharmony_ci| rightEarBatteryLevel  | number | 是    | 否    | 表示右侧耳机的电量值,如果值为-1,表示没有电量信息。   |
1383e41f4b71Sopenharmony_ci| rightEarChargeState  | [DeviceChargeState](#devicechargestate12) | 是    | 否    | 表示右侧耳机的充电状态。   |
1384e41f4b71Sopenharmony_ci| boxBatteryLevel  | number | 是    | 否    | 表示耳机仓的电量值,如果值为-1,表示没有电量信息。   |
1385e41f4b71Sopenharmony_ci| boxChargeState  | [DeviceChargeState](#devicechargestate12) | 是    | 否    | 表示耳机仓的充电状态。   |
1386e41f4b71Sopenharmony_ci
1387e41f4b71Sopenharmony_ci
1388e41f4b71Sopenharmony_ci## BluetoothTransport
1389e41f4b71Sopenharmony_ci
1390e41f4b71Sopenharmony_ci枚举,表示设备类型。例如传统蓝牙设备或低功耗蓝牙设备,支持双模默认使用TRANSPORT_BR_EDR。
1391e41f4b71Sopenharmony_ci
1392e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1393e41f4b71Sopenharmony_ci
1394e41f4b71Sopenharmony_ci| 名称                               | 值    | 说明              |
1395e41f4b71Sopenharmony_ci| -------------------------------- | ------ | --------------- |
1396e41f4b71Sopenharmony_ci| TRANSPORT_BR_EDR   | 0 | 表示传统蓝牙(BR/EDR)设备。 |
1397e41f4b71Sopenharmony_ci| TRANSPORT_LE  | 1 | 表示低功耗蓝牙(BLE)设备。  |
1398e41f4b71Sopenharmony_ci
1399e41f4b71Sopenharmony_ci
1400e41f4b71Sopenharmony_ci## ScanMode
1401e41f4b71Sopenharmony_ci
1402e41f4b71Sopenharmony_ci枚举,扫描模式。
1403e41f4b71Sopenharmony_ci
1404e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1405e41f4b71Sopenharmony_ci
1406e41f4b71Sopenharmony_ci| 名称                                       | 值  | 说明              |
1407e41f4b71Sopenharmony_ci| ---------------------------------------- | ---- | --------------- |
1408e41f4b71Sopenharmony_ci| SCAN_MODE_NONE                           | 0    | 没有扫描模式。         |
1409e41f4b71Sopenharmony_ci| SCAN_MODE_CONNECTABLE                    | 1    | 可连接扫描模式。        |
1410e41f4b71Sopenharmony_ci| SCAN_MODE_GENERAL_DISCOVERABLE           | 2    | general发现模式。    |
1411e41f4b71Sopenharmony_ci| SCAN_MODE_LIMITED_DISCOVERABLE           | 3    | limited发现模式。    |
1412e41f4b71Sopenharmony_ci| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4    | 可连接general发现模式。 |
1413e41f4b71Sopenharmony_ci| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5    | 可连接limited发现模式。 |
1414e41f4b71Sopenharmony_ci
1415e41f4b71Sopenharmony_ci
1416e41f4b71Sopenharmony_ci## BondState
1417e41f4b71Sopenharmony_ci
1418e41f4b71Sopenharmony_ci枚举,配对状态。
1419e41f4b71Sopenharmony_ci
1420e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1421e41f4b71Sopenharmony_ci
1422e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1423e41f4b71Sopenharmony_ci
1424e41f4b71Sopenharmony_ci| 名称                 | 值  | 说明     |
1425e41f4b71Sopenharmony_ci| ------------------ | ---- | ------ |
1426e41f4b71Sopenharmony_ci| BOND_STATE_INVALID | 0    | 无效的配对。 |
1427e41f4b71Sopenharmony_ci| BOND_STATE_BONDING | 1    | 正在配对。  |
1428e41f4b71Sopenharmony_ci| BOND_STATE_BONDED  | 2    | 已配对。   |
1429e41f4b71Sopenharmony_ci
1430e41f4b71Sopenharmony_ci
1431e41f4b71Sopenharmony_ci## UnbondCause<sup>12+</sup>
1432e41f4b71Sopenharmony_ci
1433e41f4b71Sopenharmony_ci枚举,配对失败原因。
1434e41f4b71Sopenharmony_ci
1435e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1436e41f4b71Sopenharmony_ci
1437e41f4b71Sopenharmony_ci| 名称                 | 值  | 说明     |
1438e41f4b71Sopenharmony_ci| ------------------ | ---- | ------ |
1439e41f4b71Sopenharmony_ci| USER_REMOVED        | 0    | 用户主动移除设备。|
1440e41f4b71Sopenharmony_ci| REMOTE_DEVICE_DOWN  | 1    | 远端设备关闭。|
1441e41f4b71Sopenharmony_ci| AUTH_FAILURE        | 2    | PIN码错误。|
1442e41f4b71Sopenharmony_ci| AUTH_REJECTED       | 3    | 远端设备鉴权拒绝。|
1443e41f4b71Sopenharmony_ci| INTERNAL_ERROR      | 4    | 内部错误。|
1444e41f4b71Sopenharmony_ci
1445e41f4b71Sopenharmony_ci
1446e41f4b71Sopenharmony_ci## DeviceChargeState<sup>12+</sup>
1447e41f4b71Sopenharmony_ci
1448e41f4b71Sopenharmony_ci枚举,表示充电状态。
1449e41f4b71Sopenharmony_ci
1450e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core1451e41f4b71Sopenharmony_ci
1452e41f4b71Sopenharmony_ci| 名称                 | 值  | 说明     |
1453e41f4b71Sopenharmony_ci| ------------------ | ---- | ------ |
1454e41f4b71Sopenharmony_ci| DEVICE_NORMAL_CHARGE_NOT_CHARGED        | 0    | 未充电,不支持超级充电。|
1455e41f4b71Sopenharmony_ci| DEVICE_NORMAL_CHARGE_IN_CHARGING       | 1    | 正在充电,不支持超级充电。|
1456e41f4b71Sopenharmony_ci| DEVICE_SUPER_CHARGE_NOT_CHARGED        | 2    | 未充电,支持超级充电。|
1457e41f4b71Sopenharmony_ci| DEVICE_SUPER_CHARGE_IN_CHARGING       | 3    | 正在充电,支持超级充电。|