• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.connection (蓝牙connection模块)
2
3connection模块提供了对蓝牙操作和管理的方法。
4
5> **说明:**
6>
7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10
11## 导入模块
12
13```js
14import { connection } from '@kit.ConnectivityKit';
15```
16
17
18## ProfileConnectionState
19
20type ProfileConnectionState = constant.ProfileConnectionState
21
22蓝牙设备的profile连接状态。
23
24**系统能力**:SystemCapability.Communication.Bluetooth.Core
25
26| 类型                  | 说明                  |
27| ------------------- | ------------------- |
28| [constant.ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 蓝牙设备的profile连接状态。 |
29
30
31## ProfileId
32
33type ProfileId = constant.ProfileId
34
35蓝牙profile枚举。
36
37**系统能力**:SystemCapability.Communication.Bluetooth.Core
38
39| 类型                  | 说明                  |
40| ------------------- | ------------------- |
41| [constant.ProfileId](js-apis-bluetooth-constant.md#profileid) | 蓝牙profile枚举。 |
42
43
44## ProfileUuids<sup>12+</sup>
45
46type ProfileUuids = constant.ProfileUuids
47
48表示Profile的UUID。
49
50**系统能力**:SystemCapability.Communication.Bluetooth.Core
51
52| 类型                  | 说明                  |
53| ------------------- | ------------------- |
54| [constant.ProfileUuids](js-apis-bluetooth-constant.md#profileuuids) | 表示Profile的UUID。 |
55
56
57## MajorClass
58
59type MajorClass = constant.MajorClass
60
61蓝牙设备主要类别。
62
63**系统能力**:SystemCapability.Communication.Bluetooth.Core
64
65| 类型                  | 说明                  |
66| ------------------- | ------------------- |
67| [constant.MajorClass](js-apis-bluetooth-constant.md#majorclass) | 蓝牙设备主要类别。 |
68
69
70## MajorMinorClass
71
72type MajorMinorClass = constant.MajorMinorClass
73
74主要次要蓝牙设备类别。
75
76**系统能力**:SystemCapability.Communication.Bluetooth.Core
77
78| 类型                  | 说明                  |
79| ------------------- | ------------------- |
80| [constant.MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | 主要次要蓝牙设备类别。 |
81
82
83## connection.pairDevice
84
85pairDevice(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
86
87发起蓝牙配对。使用Callback异步回调。
88
89**需要权限**:ohos.permission.ACCESS_BLUETOOTH
90
91**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
92
93**系统能力**:SystemCapability.Communication.Bluetooth.Core
94
95**参数:**
96
97| 参数名      | 类型     | 必填   | 说明                                  |
98| -------- | ------ | ---- | ----------------------------------- |
99| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
100| callback | AsyncCallback&lt;void&gt;  | 是    | 回调函数。当配对成功,err为undefined,否则为错误对象。 |
101
102**错误码**:
103
104以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
105
106| 错误码ID | 错误信息 |
107| -------- | ---------------------------- |
108|201 | Permission denied.                 |
109|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
110|801 | Capability not supported.          |
111|2900001 | Service stopped.                         |
112|2900003 | Bluetooth disabled.                 |
113|2900099 | Operation failed.                        |
114
115**示例:**
116
117```js
118import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
119// callback
120try {
121    connection.pairDevice('11:22:33:44:55:66', (err: BusinessError) => {
122        console.info('pairDevice, device name err:' + JSON.stringify(err));
123    });
124} catch (err) {
125    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
126}
127
128```
129
130
131## connection.pairDevice
132
133pairDevice(deviceId: string): Promise&lt;void&gt;
134
135发起蓝牙配对。使用Promise异步回调。
136
137**需要权限**:ohos.permission.ACCESS_BLUETOOTH
138
139**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
140
141**系统能力**:SystemCapability.Communication.Bluetooth.Core
142
143**参数:**
144
145| 参数名      | 类型     | 必填   | 说明                                  |
146| -------- | ------ | ---- | ----------------------------------- |
147| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
148
149**返回值:**
150
151| 类型                  | 说明            |
152| ------------------- | ------------- |
153| Promise&lt;void&gt; | 返回promise对象。 |
154
155**错误码**:
156
157以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
158
159| 错误码ID | 错误信息 |
160| -------- | ---------------------------- |
161|201 | Permission denied.                 |
162|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
163|801 | Capability not supported.          |
164|2900001 | Service stopped.                         |
165|2900003 | Bluetooth disabled.                 |
166|2900099 | Operation failed.                        |
167
168**示例:**
169
170```js
171import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
172// promise
173try {
174    connection.pairDevice('11:22:33:44:55:66').then(() => {
175        console.info('pairDevice');
176    }, (error: BusinessError) => {
177        console.info('pairDevice: errCode:' + error.code + ',errMessage' + error.message);
178    })
179
180} catch (err) {
181    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
182}
183```
184
185
186## connection.getRemoteDeviceName
187
188getRemoteDeviceName(deviceId: string): string
189
190获取对端蓝牙设备的名称。
191
192**需要权限**:ohos.permission.ACCESS_BLUETOOTH
193
194**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
195
196**系统能力**:SystemCapability.Communication.Bluetooth.Core
197
198**参数:**
199
200| 参数名      | 类型     | 必填   | 说明                                |
201| -------- | ------ | ---- | --------------------------------- |
202| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
203
204**返回值:**
205
206| 类型     | 说明            |
207| ------ | ------------- |
208| string | 以字符串格式返回设备名称。 |
209
210**错误码**:
211
212以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
213
214| 错误码ID | 错误信息 |
215| -------- | ---------------------------- |
216|201 | Permission denied.                 |
217|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
218|801 | Capability not supported.          |
219|2900001 | Service stopped.                         |
220|2900003 | Bluetooth disabled.                 |
221|2900099 | Operation failed.                        |
222
223**示例:**
224
225```js
226import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
227try {
228    let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX');
229} catch (err) {
230    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
231}
232```
233
234
235## connection.getRemoteDeviceName<sup>16+</sup>
236
237getRemoteDeviceName(deviceId: string, alias?: boolean): string
238
239获取对端蓝牙设备的名称,其中alias为可选参数。
240- 如果携带alias,则根据alias判断是否获取对端蓝牙设备别名。
241- 如果未携带alias,则默认返回对端蓝牙设备别名。
242
243**需要权限**:ohos.permission.ACCESS_BLUETOOTH
244
245**原子化服务API**:从API version 16开始,该接口支持在原子化服务中使用。
246
247**系统能力**:SystemCapability.Communication.Bluetooth.Core
248
249**参数:**
250
251| 参数名      | 类型     | 必填   | 说明                                |
252| -------- | ------ | ---- | --------------------------------- |
253| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
254| alias | boolean | 否    | 是否获取对端蓝牙设备别名。获取成功为true,否则设置为false。 |
255
256**返回值:**
257
258| 类型     | 说明            |
259| ------ | ------------- |
260| string | 以字符串格式返回设备名称。 |
261
262**错误码**:
263
264以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
265
266| 错误码ID | 错误信息 |
267| -------- | ---------------------------- |
268|201 | Permission denied.                 |
269|401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
270|801 | Capability not supported.          |
271|2900001 | Service stopped.                         |
272|2900003 | Bluetooth disabled.                 |
273|2900099 | Failed to obtain the name or alias of the peer Bluetooth device.                        |
274
275**示例:**
276
277```js
278import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
279try {
280    let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX', true);
281} catch (err) {
282    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
283}
284```
285
286
287## connection.getRemoteDeviceClass
288
289getRemoteDeviceClass(deviceId: string): DeviceClass
290
291获取对端蓝牙设备的类别。从API18开始不再校验ohos.permission.ACCESS_BLUETOOTH权限。
292
293**系统能力**:SystemCapability.Communication.Bluetooth.Core
294
295**参数:**
296
297| 参数名      | 类型     | 必填   | 说明                                |
298| -------- | ------ | ---- | --------------------------------- |
299| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
300
301**返回值:**
302
303| 类型                          | 说明       |
304| --------------------------- | -------- |
305| [DeviceClass](#deviceclass) | 远程设备的类别。 |
306
307**错误码**:
308
309以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
310
311| 错误码ID | 错误信息 |
312| -------- | ---------------------------- |
313|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
314|801 | Capability not supported.          |
315|2900001 | Service stopped.                         |
316|2900003 | Bluetooth disabled.                 |
317|2900099 | Operation failed.                        |
318
319**示例:**
320
321```js
322import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
323try {
324    let remoteDeviceClass: connection.DeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX');
325} catch (err) {
326    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
327}
328```
329
330
331## connection.getRemoteProfileUuids<sup>12+</sup>
332
333getRemoteProfileUuids(deviceId: string, callback: AsyncCallback&lt;Array&lt;ProfileUuids&gt;&gt;): void
334
335获取对端蓝牙设备支持的Profile UUID。使用Callback异步回调。
336
337**需要权限**:ohos.permission.ACCESS_BLUETOOTH
338
339**系统能力**:SystemCapability.Communication.Bluetooth.Core
340
341**参数:**
342
343| 参数名      | 类型     | 必填   | 说明                                  |
344| -------- | ------ | ---- | ----------------------------------- |
345| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
346| callback | AsyncCallback&lt;Array&lt;[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)&gt;&gt; | 是    | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 |
347
348**错误码**:
349
350以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
351
352| 错误码ID | 错误信息 |
353| -------- | ---------------------------- |
354|201 | Permission denied.                 |
355|401 | Invalid parameter.    |
356|801 | Capability not supported.          |
357|2900001 | Service stopped.                         |
358|2900003 | Bluetooth disabled.                 |
359|2900099 | Operation failed.                        |
360
361**示例:**
362
363```js
364import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
365try {
366    connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => {
367        console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
368    });
369} catch (err) {
370    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
371}
372
373```
374
375
376## connection.getRemoteProfileUuids<sup>12+</sup>
377
378getRemoteProfileUuids(deviceId: string): Promise&lt;Array&lt;ProfileUuids&gt;&gt;
379
380获取对端蓝牙设备支持的Profile UUID。使用Promise异步回调。
381
382**需要权限**:ohos.permission.ACCESS_BLUETOOTH
383
384**系统能力**:SystemCapability.Communication.Bluetooth.Core
385
386**参数:**
387
388| 参数名      | 类型     | 必填   | 说明                                  |
389| -------- | ------ | ---- | ----------------------------------- |
390| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
391
392**返回值:**
393
394| 类型                  | 说明            |
395| ------------------- | ------------- |
396|   Promise&lt;Array&lt;[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)&gt;&gt; | 返回promise对象。 |
397
398**错误码**:
399
400以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
401
402| 错误码ID | 错误信息 |
403| -------- | ---------------------------- |
404|201 | Permission denied.                 |
405|401 | Invalid parameter.    |
406|801 | Capability not supported.          |
407|2900001 | Service stopped.                         |
408|2900003 | Bluetooth disabled.                 |
409|2900099 | Operation failed.                        |
410
411**示例:**
412
413```js
414import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
415try {
416    connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
417        console.info('getRemoteProfileUuids');
418    }, (err: BusinessError) => {
419        console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
420    });
421} catch (err) {
422    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
423}
424```
425
426
427## connection.getLocalName
428
429getLocalName(): string
430
431获取蓝牙本地设备名称。
432
433**需要权限**:ohos.permission.ACCESS_BLUETOOTH
434
435**系统能力**:SystemCapability.Communication.Bluetooth.Core
436
437**返回值:**
438
439| 类型     | 说明        |
440| ------ | --------- |
441| string | 蓝牙本地设备名称。 |
442
443**错误码**:
444
445以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
446
447| 错误码ID | 错误信息 |
448| -------- | ---------------------------- |
449|201 | Permission denied.                 |
450|801 | Capability not supported.          |
451|2900001 | Service stopped.                         |
452|2900099 | Operation failed.                        |
453
454**示例:**
455
456```js
457import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
458try {
459    let localName: string = connection.getLocalName();
460} catch (err) {
461    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
462}
463```
464
465
466## connection.getPairedDevices
467
468getPairedDevices(): Array&lt;string&gt;
469
470获取蓝牙配对列表。
471
472**需要权限**:ohos.permission.ACCESS_BLUETOOTH
473
474**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
475
476**系统能力**:SystemCapability.Communication.Bluetooth.Core
477
478**返回值:**
479
480| 类型                  | 说明            |
481| ------------------- | ------------- |
482| Array&lt;string&gt; | 已配对蓝牙设备的地址列表。<br>- 基于信息安全考虑,此处获取的设备地址为随机MAC地址。<br>- 配对成功后,该地址不会变更。<br>- 已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。 |
483
484**错误码**:
485
486以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
487
488| 错误码ID | 错误信息 |
489| -------- | ---------------------------- |
490|201 | Permission denied.                 |
491|801 | Capability not supported.          |
492|2900001 | Service stopped.                         |
493|2900003 | Bluetooth disabled.                 |
494|2900099 | Operation failed.                        |
495
496**示例:**
497
498```js
499import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
500try {
501    let devices: Array<string> = connection.getPairedDevices();
502} catch (err) {
503    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
504}
505```
506
507
508## connection.getPairState<sup>11+</sup>
509
510getPairState(deviceId: string): BondState
511
512获取蓝牙配对状态。
513
514**需要权限**:ohos.permission.ACCESS_BLUETOOTH
515
516**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
517
518**系统能力**:SystemCapability.Communication.Bluetooth.Core
519
520**参数:**
521
522| 参数名      | 类型     | 必填   | 说明                                |
523| -------- | ------ | ---- | --------------------------------- |
524| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
525
526**返回值:**
527
528| 类型                          | 说明       |
529| --------------------------- | -------- |
530| [BondState](#bondstate) | 表示设备的蓝牙配对状态。 |
531
532**错误码**:
533
534以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
535
536| 错误码ID | 错误信息 |
537| -------- | ---------------------------- |
538|201 | Permission denied.                 |
539|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
540|801 | Capability not supported.          |
541|2900001 | Service stopped.                         |
542|2900003 | Bluetooth disabled.                 |
543|2900099 | Operation failed.                        |
544
545**示例:**
546
547```js
548import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
549try {
550    let res: connection.BondState = connection.getPairState("XX:XX:XX:XX:XX:XX");
551    console.info('getPairState: ' + res);
552} catch (err) {
553    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
554}
555```
556
557
558## connection.getProfileConnectionState
559
560getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState
561
562获取蓝牙Profile的连接状态,其中ProfileId为可选参数。
563- 如果携带ProfileId,则返回的是当前Profile的连接状态。
564- 如果未携带ProfileId,任一Profile已连接则返回[STATE_CONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate),否则返回[STATE_DISCONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate)。
565
566**需要权限**:ohos.permission.ACCESS_BLUETOOTH
567
568**系统能力**:SystemCapability.Communication.Bluetooth.Core
569
570**参数:**
571
572| 参数名       | 类型        | 必填   | 说明                                    |
573| --------- | --------- | ---- | ------------------------------------- |
574| profileId | [ProfileId](js-apis-bluetooth-constant.md#profileid) | 否    | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 |
575
576**返回值:**
577
578| 类型                                              | 说明                |
579| ------------------------------------------------- | ------------------- |
580| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | profile的连接状态。 |
581
582**错误码**:
583
584以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
585
586| 错误码ID | 错误信息 |
587| -------- | ---------------------------- |
588|201 | Permission denied.                 |
589|401 | Invalid parameter. Possible causes: 1. Incorrect parameter types.        |
590|801 | Capability not supported.          |
591|2900001 | Service stopped.                         |
592|2900003 | Bluetooth disabled.                 |
593|2900004 | Profile not supported.                |
594|2900099 | Operation failed.                        |
595
596**示例:**
597
598```js
599import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
600import { constant } from '@kit.ConnectivityKit';
601try {
602    let result: connection.ProfileConnectionState = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE);
603} catch (err) {
604    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
605}
606```
607
608
609## connection.setDevicePairingConfirmation
610
611setDevicePairingConfirmation(deviceId: string, accept: boolean): void
612
613设置设备配对请求确认。
614
615**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH(该权限仅系统应用可申请)
616
617**系统能力**:SystemCapability.Communication.Bluetooth.Core
618
619**参数:**
620
621| 参数名    | 类型      | 必填   | 说明                               |
622| ------   | ------- | ---- | -------------------------------- |
623| deviceId | string  | 是    | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
624| accept   | boolean | 是    | 接受配对请求设置为true,否则设置为false。        |
625
626**错误码**:
627
628以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
629
630| 错误码ID | 错误信息 |
631| -------- | ---------------------------- |
632|201 | Permission denied.                 |
633|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
634|801 | Capability not supported.          |
635|2900001 | Service stopped.                         |
636|2900003 | Bluetooth disabled.                 |
637|2900099 | Operation failed.                        |
638
639**示例:**
640
641```js
642import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
643// 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认。
644function onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data为配对请求的入参,配对请求参数。
645    console.info('pin required  = '+ JSON.stringify(data));
646    connection.setDevicePairingConfirmation(data.deviceId, true);
647}
648try {
649    connection.on('pinRequired', onReceivePinRequiredEvent);
650} catch (err) {
651    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
652}
653```
654
655
656## connection.setDevicePinCode
657
658setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback&lt;void&gt;): void
659
660当蓝牙配对类型PinType为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。使用Callback异步回调。
661
662**需要权限**:ohos.permission.ACCESS_BLUETOOTH
663
664**系统能力**:SystemCapability.Communication.Bluetooth.Core
665
666**参数:**
667
668| 参数名    | 类型      | 必填   | 说明                               |
669| ------ | ------- | ---- | -------------------------------- |
670| deviceId | string  | 是    | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 |
671| code   | string  | 是    | 用户输入的PIN码。        |
672| callback   | AsyncCallback&lt;void&gt;  | 是    | 回调函数,当设置PinCode成功,err为undefined,否则为错误对象。        |
673
674**错误码**:
675
676以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
677
678| 错误码ID | 错误信息 |
679| -------- | ---------------------------- |
680|201 | Permission denied.                 |
681|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
682|801 | Capability not supported.          |
683|2900001 | Service stopped.                         |
684|2900003 | Bluetooth disabled.                 |
685|2900099 | Operation failed.                        |
686
687**示例:**
688
689```js
690import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
691// callback
692try {
693    connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => {
694        console.info('setDevicePinCode,device name err:' + JSON.stringify(err));
695    });
696} catch (err) {
697    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
698}
699```
700
701
702## connection.setDevicePinCode
703
704setDevicePinCode(deviceId: string, code: string): Promise&lt;void&gt;
705
706当蓝牙配对类型PinType为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。使用Promise异步回调。
707
708**需要权限**:ohos.permission.ACCESS_BLUETOOTH
709
710**系统能力**:SystemCapability.Communication.Bluetooth.Core
711
712**参数:**
713
714| 参数名    | 类型      | 必填   | 说明                               |
715| ------ | ------- | ---- | -------------------------------- |
716| deviceId | string  | 是    | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 |
717| code   | string  | 是    | 用户输入的PIN码。        |
718
719**返回值:**
720
721| 类型                  | 说明            |
722| ------------------- | ------------- |
723| Promise&lt;void&gt; | 返回promise对象。 |
724
725**错误码**:
726
727以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
728
729| 错误码ID | 错误信息 |
730| -------- | ---------------------------- |
731|201 | Permission denied.                 |
732|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
733|801 | Capability not supported.          |
734|2900001 | Service stopped.                         |
735|2900003 | Bluetooth disabled.                 |
736|2900099 | Operation failed.                        |
737
738**示例:**
739
740```js
741import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
742// promise
743try {
744    connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => {
745        console.info('setDevicePinCode');
746    }, (error: BusinessError) => {
747        console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message);
748    })
749
750} catch (err) {
751    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
752}
753```
754
755
756## connection.setLocalName<sup>(deprecated)</sup>
757
758setLocalName(name: string): void
759
760设置蓝牙本地设备名称。
761
762> **说明:**<br/>
763> 从API version 10开始支持,从API version 12开始废弃,不再提供替代接口。
764
765**需要权限**:ohos.permission.ACCESS_BLUETOOTH
766
767**系统能力**:SystemCapability.Communication.Bluetooth.Core
768
769**参数:**
770
771| 参数名  | 类型     | 必填   | 说明                    |
772| ---- | ------ | ---- | --------------------- |
773| name | string | 是    | 要设置的蓝牙名称,最大长度为248字节数。 |
774
775**错误码**:
776
777以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
778
779| 错误码ID | 错误信息 |
780| -------- | ---------------------------- |
781|201 | Permission denied.                 |
782|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
783|801 | Capability not supported.          |
784|2900001 | Service stopped.                         |
785|2900003 | Bluetooth disabled.                 |
786|2900099 | Operation failed.                        |
787
788**示例:**
789
790```js
791import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
792try {
793    connection.setLocalName('device_name');
794} catch (err) {
795    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
796}
797```
798
799
800## connection.setBluetoothScanMode
801
802setBluetoothScanMode(mode: ScanMode, duration: number): void
803
804设置蓝牙扫描模式,可以被远端设备发现。
805
806**需要权限**:ohos.permission.ACCESS_BLUETOOTH
807
808**系统能力**:SystemCapability.Communication.Bluetooth.Core
809
810**参数:**
811
812| 参数名      | 类型                    | 必填   | 说明                           |
813| -------- | --------------------- | ---- | ---------------------------- |
814| mode     | [ScanMode](#scanmode) | 是    | 蓝牙扫描模式。当扫描模式为SCAN_MODE_GENERAL_DISCOVERABLE时,超出duration持续时间(不为0),扫描模式会重新设置为SCAN_MODE_CONNECTABLE。               |
815| duration | number                | 是    | 设备可被发现的持续时间,单位为毫秒。设置为0则持续可发现。 |
816
817**错误码**:
818
819以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
820
821| 错误码ID | 错误信息 |
822| -------- | ---------------------------- |
823|201 | Permission denied.                 |
824|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
825|801 | Capability not supported.          |
826|2900001 | Service stopped.                         |
827|2900003 | Bluetooth disabled.                 |
828|2900099 | Operation failed.                        |
829
830**示例:**
831
832```js
833import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
834try {
835    // 设置为可连接可发现才可被远端设备扫描到,可以连接。
836    connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
837} catch (err) {
838    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
839}
840```
841
842
843## connection.getBluetoothScanMode
844
845getBluetoothScanMode(): ScanMode
846
847获取蓝牙扫描模式。
848
849**需要权限**:ohos.permission.ACCESS_BLUETOOTH
850
851**系统能力**:SystemCapability.Communication.Bluetooth.Core
852
853**返回值:**
854
855| 类型                    | 说明      |
856| --------------------- | ------- |
857| [ScanMode](#scanmode) | 蓝牙扫描模式。 |
858
859**错误码**:
860
861以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
862
863| 错误码ID | 错误信息 |
864| -------- | ---------------------------- |
865|201 | Permission denied.                 |
866|801 | Capability not supported.          |
867|2900001 | Service stopped.                         |
868|2900003 | Bluetooth disabled.                 |
869|2900099 | Operation failed.                        |
870
871**示例:**
872
873```js
874import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
875try {
876    let scanMode: connection.ScanMode = connection.getBluetoothScanMode();
877} catch (err) {
878    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
879}
880```
881
882
883## connection.startBluetoothDiscovery
884
885startBluetoothDiscovery(): void
886
887开启蓝牙扫描,可以发现远端设备。
888
889**需要权限**:ohos.permission.ACCESS_BLUETOOTH
890
891**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
892
893**系统能力**:SystemCapability.Communication.Bluetooth.Core
894
895**错误码**:
896
897以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
898
899| 错误码ID | 错误信息 |
900| -------- | ---------------------------- |
901|201 | Permission denied.                 |
902|801 | Capability not supported.          |
903|2900001 | Service stopped.                         |
904|2900003 | Bluetooth disabled.                 |
905|2900099 | Operation failed.                        |
906
907**示例:**
908
909```js
910import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
911function onReceiveEvent(data: Array<string>) {
912    console.info('data length' + data.length);
913}
914try {
915    connection.on('bluetoothDeviceFind', onReceiveEvent);
916    connection.startBluetoothDiscovery();
917} catch (err) {
918    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
919}
920```
921
922
923## connection.stopBluetoothDiscovery
924
925stopBluetoothDiscovery(): void
926
927关闭蓝牙扫描。
928
929**需要权限**:ohos.permission.ACCESS_BLUETOOTH
930
931**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
932
933**系统能力**:SystemCapability.Communication.Bluetooth.Core
934
935**错误码**:
936
937以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
938
939| 错误码ID | 错误信息 |
940| -------- | ---------------------------- |
941|201 | Permission denied.                 |
942|801 | Capability not supported.          |
943|2900001 | Service stopped.                         |
944|2900003 | Bluetooth disabled.                 |
945|2900099 | Operation failed.                        |
946
947**示例:**
948
949```js
950import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
951try {
952    connection.stopBluetoothDiscovery();
953} catch (err) {
954    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
955}
956```
957
958
959## connection.isBluetoothDiscovering<sup>11+</sup>
960
961isBluetoothDiscovering(): boolean
962
963查询设备的蓝牙发现状态。
964
965**需要权限**:ohos.permission.ACCESS_BLUETOOTH
966
967**系统能力**:SystemCapability.Communication.Bluetooth.Core
968
969**返回值:**
970
971| 类型                  | 说明            |
972| ------------------- | ------------- |
973|   boolean           | 设备已开启蓝牙发现为true,否则为false。 |
974
975**错误码**:
976
977以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
978
979| 错误码ID | 错误信息 |
980| -------- | ---------------------------- |
981|201 | Permission denied.                 |
982|801 | Capability not supported.          |
983|2900001 | Service stopped.                         |
984|2900003 | Bluetooth disabled.                 |
985|2900099 | Operation failed.                        |
986
987**示例:**
988
989```js
990import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
991try {
992    let res: boolean = connection.isBluetoothDiscovering();
993    console.info('isBluetoothDiscovering: ' + res);
994} catch (err) {
995    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
996}
997```
998
999## connection.setRemoteDeviceName<sup>12+</sup>
1000
1001setRemoteDeviceName(deviceId: string, name: string): Promise&lt;void&gt;
1002
1003设置蓝牙远端设备名称。使用Promise异步回调。
1004
1005**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1006
1007**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1008
1009**系统能力**:SystemCapability.Communication.Bluetooth.Core
1010
1011**参数:**
1012
1013| 参数名      | 类型                                  | 必填   | 说明                                     |
1014| -------- | ----------------------------------- | ---- | -------------------------------------- |
1015| deviceId     | string                              | 是    | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 |
1016| name | string | 是    | 修改远端设备名称,最大长度为64字节。    |
1017
1018**返回值:**
1019
1020| 类型                  | 说明            |
1021| ------------------- | ------------- |
1022| Promise&lt;void&gt; | 以Promise形式返回设置蓝牙远端设备名称的结果,设置失败时返回错误码信息。 |
1023
1024**错误码**:
1025
1026以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1027
1028| 错误码ID | 错误信息 |
1029| -------- | ---------------------------- |
1030|201 | Permission denied.                 |
1031|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.            |
1032|2900001 | Service stopped.                         |
1033|2900003 | Bluetooth disabled.                 |
1034
1035**示例:**
1036
1037```js
1038import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1039// promise
1040try {
1041    connection.setRemoteDeviceName('11:22:33:44:55:66', 'RemoteDeviceName').then(() => {
1042        console.info('setRemoteDeviceName success');
1043    }, (error: BusinessError) => {
1044        console.error('setRemoteDeviceName: errCode:' + error.code + ',errMessage' + error.message);
1045    })
1046
1047} catch (err) {
1048    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1049}
1050```
1051
1052
1053## connection.getRemoteDeviceBatteryInfo<sup>12+</sup>
1054
1055getRemoteDeviceBatteryInfo(deviceId: string): Promise&lt;BatteryInfo&gt;
1056
1057获取蓝牙远端设备的电量信息。使用Promise异步回调。
1058
1059**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1060
1061**系统能力**:SystemCapability.Communication.Bluetooth.Core
1062
1063**参数:**
1064
1065| 参数名    | 类型      | 必填   | 说明                               |
1066| ------ | ------- | ---- | -------------------------------- |
1067| deviceId | string  | 是    | 表示远端设备MAC地址,例如:"11:22:33:AA:BB:FF"。 |
1068
1069**返回值:**
1070
1071| 类型                  | 说明         |
1072| ------------------- | ------------- |
1073| Promise&lt;[BatteryInfo](#batteryinfo12)&gt; | 以Promise形式返回蓝牙远端设备的电量信息。 |
1074
1075**错误码**:
1076
1077以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1078
1079| 错误码ID | 错误信息 |
1080| -------- | ---------------------------- |
1081|201 | Permission denied.                 |
1082|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.            |
1083|2900001 | Service stopped.                         |
1084|2900003 | Bluetooth disabled.                 |
1085
1086**示例:**
1087
1088```js
1089import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1090// promise
1091try {
1092    connection.getRemoteDeviceBatteryInfo('11:22:33:AA:BB:FF').then((data: connection.BatteryInfo) => {
1093        console.info('getRemoteDeviceBatteryInfo success, DeviceType:' + JSON.stringify(data));
1094    });
1095} catch (err) {
1096    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1097}
1098```
1099
1100
1101## connection.on('batteryChange')<sup>12+</sup>
1102
1103on(type: 'batteryChange', callback: Callback&lt;BatteryInfo&gt;): void
1104
1105订阅蓝牙远程设备的电量信息变更事件。使用Callback异步回调。
1106
1107**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1108
1109**系统能力**:SystemCapability.Communication.Bluetooth.Core
1110
1111**参数:**
1112
1113| 参数名      | 类型                                  | 必填   | 说明                                     |
1114| -------- | ----------------------------------- | ---- | -------------------------------------- |
1115| type     | string                              | 是    | 填写"batteryChange"字符串,表示蓝牙远端设备的电池信息变更事件。 |
1116| callback | Callback&lt;[BatteryInfo](#batteryinfo12)&gt; | 是    | 表示回调函数的入参,返回电量信息。    |
1117
1118**错误码**:
1119
1120以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1121
1122| 错误码ID | 错误信息 |
1123| -------- | ---------------------------- |
1124|201 | Permission denied.                 |
1125|2900099 | Operation failed.                        |
1126
1127**示例:**
1128
1129```js
1130import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1131let onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => {
1132    console.info('BatteryInfo = '+ JSON.stringify(data));
1133}
1134try {
1135    connection.on('batteryChange', onReceiveEvent);
1136} catch (err) {
1137    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1138}
1139```
1140
1141
1142## connection.off('batteryChange')<sup>12+</sup>
1143
1144off(type: 'batteryChange', callback?: Callback&lt;BatteryInfo&gt;): void
1145
1146取消订阅蓝牙远程设备的电量信息变更事件。
1147
1148**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1149
1150**系统能力**:SystemCapability.Communication.Bluetooth.Core
1151
1152**参数:**
1153
1154| 参数名      | 类型                                  | 必填   | 说明                                       |
1155| -------- | ----------------------------------- | ---- | ---------------------------------------- |
1156| type     | string                              | 是    | 填写"batteryChange"字符串,表示取消蓝牙远端设备的电池信息变更事件。   |
1157| callback | Callback&lt;[BatteryInfo](#batteryinfo12)&gt; | 否    | 表示回调函数的入参,返回电量信息。 |
1158
1159**错误码**:
1160
1161以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1162
1163| 错误码ID | 错误信息 |
1164| -------- | ---------------------------- |
1165|201 | Permission denied.                 |
1166|2900099 | Operation failed.                        |
1167
1168**示例:**
1169
1170```js
1171import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1172let onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => {
1173    console.info('BatteryInfo = '+ JSON.stringify(data));
1174}
1175try {
1176    connection.on('batteryChange', onReceiveEvent);
1177    connection.off('batteryChange', onReceiveEvent);
1178} catch (err) {
1179    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1180}
1181```
1182
1183
1184## connection.on('bluetoothDeviceFind')
1185
1186on(type: 'bluetoothDeviceFind', callback: Callback&lt;Array&lt;string&gt;&gt;): void
1187
1188订阅蓝牙设备发现上报事件。使用Callback异步回调。
1189
1190**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1191
1192**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1193
1194**系统能力**:SystemCapability.Communication.Bluetooth.Core
1195
1196**参数:**
1197
1198| 参数名      | 类型                                  | 必填   | 说明                                     |
1199| -------- | ----------------------------------- | ---- | -------------------------------------- |
1200| type     | string                              | 是    | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 |
1201| callback | Callback&lt;Array&lt;string&gt;&gt; | 是    | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。    |
1202
1203**错误码**:
1204
1205以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1206
1207| 错误码ID | 错误信息 |
1208| -------- | ---------------------------- |
1209|201 | Permission denied.                 |
1210|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1211|801 | Capability not supported.          |
1212|2900099 | Operation failed.                        |
1213
1214**示例:**
1215
1216```js
1217import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1218function onReceiveEvent(data: Array<string>) { // data为蓝牙设备地址集合。
1219    console.info('bluetooth device find = '+ JSON.stringify(data));
1220}
1221try {
1222    connection.on('bluetoothDeviceFind', onReceiveEvent);
1223} catch (err) {
1224    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1225}
1226```
1227
1228
1229## connection.off('bluetoothDeviceFind')
1230
1231off(type: 'bluetoothDeviceFind', callback?: Callback&lt;Array&lt;string&gt;&gt;): void
1232
1233取消订阅蓝牙设备发现上报事件。
1234
1235**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1236
1237**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1238
1239**系统能力**:SystemCapability.Communication.Bluetooth.Core
1240
1241**参数:**
1242
1243| 参数名      | 类型                                  | 必填   | 说明                                       |
1244| -------- | ----------------------------------- | ---- | ---------------------------------------- |
1245| type     | string                              | 是    | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。   |
1246| callback | Callback&lt;Array&lt;string&gt;&gt; | 否    | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 |
1247
1248**错误码**:
1249
1250以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1251
1252| 错误码ID | 错误信息 |
1253| -------- | ---------------------------- |
1254|201 | Permission denied.                 |
1255|801 | Capability not supported.          |
1256|2900099 | Operation failed.                        |
1257
1258**示例:**
1259
1260```js
1261import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1262function onReceiveEvent(data: Array<string>) {
1263    console.info('bluetooth device find = '+ JSON.stringify(data));
1264}
1265try {
1266    connection.on('bluetoothDeviceFind', onReceiveEvent);
1267    connection.off('bluetoothDeviceFind', onReceiveEvent);
1268} catch (err) {
1269    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1270}
1271```
1272
1273
1274## connection.on('bondStateChange')
1275
1276on(type: 'bondStateChange', callback: Callback&lt;BondStateParam&gt;): void
1277
1278订阅蓝牙配对状态改变事件。使用Callback异步回调。
1279
1280**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1281
1282**系统能力**:SystemCapability.Communication.Bluetooth.Core
1283
1284**参数:**
1285
1286| 参数名      | 类型                                       | 必填   | 说明                                   |
1287| -------- | ---------------------------------------- | ---- | ------------------------------------ |
1288| type     | string                                   | 是    | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 |
1289| callback | Callback&lt;[BondStateParam](#bondstateparam)&gt; | 是    | 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。    |
1290
1291**错误码**:
1292
1293以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1294
1295| 错误码ID | 错误信息 |
1296| -------- | ---------------------------- |
1297|201 | Permission denied.                 |
1298|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1299|801 | Capability not supported.          |
1300|2900099 | Operation failed.                        |
1301
1302**示例:**
1303
1304```js
1305import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1306function onReceiveEvent(data: connection.BondStateParam) { // data为回调函数入参,表示配对的状态。
1307    console.info('pair state = '+ JSON.stringify(data));
1308}
1309try {
1310    connection.on('bondStateChange', onReceiveEvent);
1311} catch (err) {
1312    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1313}
1314```
1315
1316
1317## connection.off('bondStateChange')
1318
1319off(type: 'bondStateChange', callback?: Callback&lt;BondStateParam&gt;): void
1320
1321取消订阅蓝牙配对状态改变事件。
1322
1323**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1324
1325**系统能力**:SystemCapability.Communication.Bluetooth.Core
1326
1327**参数:**
1328
1329| 参数名      | 类型                                       | 必填   | 说明                                       |
1330| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
1331| type     | string                                   | 是    | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。     |
1332| callback | Callback&lt;[BondStateParam](#bondstateparam)&gt; | 否    | 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 |
1333
1334**错误码**:
1335
1336以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1337
1338| 错误码ID | 错误信息 |
1339| -------- | ---------------------------- |
1340|201 | Permission denied.                 |
1341|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1342|801 | Capability not supported.          |
1343|2900099 | Operation failed.                        |
1344
1345**示例:**
1346
1347```js
1348import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1349function onReceiveEvent(data: connection.BondStateParam) {
1350    console.info('bond state = '+ JSON.stringify(data));
1351}
1352try {
1353    connection.on('bondStateChange', onReceiveEvent);
1354    connection.off('bondStateChange', onReceiveEvent);
1355} catch (err) {
1356    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1357}
1358```
1359
1360
1361## connection.on('pinRequired')
1362
1363on(type: 'pinRequired', callback: Callback&lt;PinRequiredParam&gt;): void
1364
1365订阅远端蓝牙设备的配对请求事件。使用Callback异步回调。
1366
1367**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1368
1369**系统能力**:SystemCapability.Communication.Bluetooth.Core
1370
1371**参数:**
1372
1373| 参数名      | 类型                                       | 必填   | 说明                               |
1374| -------- | ---------------------------------------- | ---- | -------------------------------- |
1375| type     | string                                   | 是    | 填写"pinRequired"字符串,表示配对请求事件。     |
1376| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | 是    | 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。 |
1377
1378**错误码**:
1379
1380以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1381
1382| 错误码ID | 错误信息 |
1383| -------- | ---------------------------- |
1384|201 | Permission denied.                 |
1385|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1386|801 | Capability not supported.          |
1387|2900099 | Operation failed.                        |
1388
1389**示例:**
1390
1391```js
1392import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1393function onReceiveEvent(data: connection.PinRequiredParam) { // data为配对请求参数。
1394    console.info('pin required = '+ JSON.stringify(data));
1395}
1396try {
1397    connection.on('pinRequired', onReceiveEvent);
1398} catch (err) {
1399    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1400}
1401```
1402
1403
1404## connection.off('pinRequired')
1405
1406off(type: 'pinRequired', callback?: Callback&lt;PinRequiredParam&gt;): void
1407
1408取消订阅远端蓝牙设备的配对请求事件。
1409
1410**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1411
1412**系统能力**:SystemCapability.Communication.Bluetooth.Core
1413
1414**参数:**
1415
1416| 参数名      | 类型                                       | 必填   | 说明                                       |
1417| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
1418| type     | string                                   | 是    | 填写"pinRequired"字符串,表示配对请求事件。             |
1419| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | 否    | 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。 |
1420
1421**错误码**:
1422
1423以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1424
1425| 错误码ID | 错误信息 |
1426| -------- | ---------------------------- |
1427|201 | Permission denied.                 |
1428|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1429|801 | Capability not supported.          |
1430|2900099 | Operation failed.                        |
1431
1432**示例:**
1433
1434```js
1435import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1436function onReceiveEvent(data: connection.PinRequiredParam) {
1437    console.info('pin required = '+ JSON.stringify(data));
1438}
1439try {
1440    connection.on('pinRequired', onReceiveEvent);
1441    connection.off('pinRequired', onReceiveEvent);
1442} catch (err) {
1443    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1444}
1445```
1446
1447
1448## connection.on('discoveryResult')<sup>18+</sup>
1449
1450on(type: 'discoveryResult', callback: Callback&lt;Array&lt;DiscoveryResult&gt;&gt;): void
1451
1452订阅蓝牙设备发现上报事件。使用Callback异步回调。
1453
1454**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1455
1456**系统能力**:SystemCapability.Communication.Bluetooth.Core
1457
1458**参数:**
1459
1460| 参数名      | 类型                                  | 必填   | 说明                                     |
1461| -------- | ----------------------------------- | ---- | -------------------------------------- |
1462| type     | string                              | 是    | 填写"discoveryResult"字符串,表示蓝牙设备发现事件。 |
1463| callback | Callback&lt;Array&lt;[DiscoveryResult](#discoveryresult18)&gt;&gt; | 是    | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。    |
1464
1465**错误码**:
1466
1467以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1468
1469| 错误码ID | 错误信息 |
1470| -------- | ---------------------------- |
1471|201 | Permission denied.                 |
1472|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1473|801 | Capability not supported.          |
1474|2900099 | Operation failed.                        |
1475
1476**示例:**
1477
1478```js
1479import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1480let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data为蓝牙设备扫描结果集合。
1481    console.info('bluetooth device find = '+ JSON.stringify(data));
1482}
1483try {
1484    connection.on('discoveryResult', onReceiveEvent);
1485} catch (err) {
1486    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1487}
1488```
1489
1490
1491## connection.off('discoveryResult')<sup>18+</sup>
1492
1493off(type: 'discoveryResult', callback?: Callback&lt;Array&lt;DiscoveryResult&gt;&gt;): void
1494
1495取消订阅蓝牙设备发现上报事件。
1496
1497**需要权限**:ohos.permission.ACCESS_BLUETOOTH
1498
1499**系统能力**:SystemCapability.Communication.Bluetooth.Core
1500
1501**参数:**
1502
1503| 参数名      | 类型                                  | 必填   | 说明                                       |
1504| -------- | ----------------------------------- | ---- | ---------------------------------------- |
1505| type     | string                              | 是    | 填写"discoveryResult"字符串,表示蓝牙设备发现事件。   |
1506| callback | Callback&lt;Array&lt;[DiscoveryResult](#discoveryresult18)&gt;&gt; | 否    | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。
1507
1508**错误码**:
1509
1510以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1511
1512| 错误码ID | 错误信息 |
1513| -------- | ---------------------------- |
1514|201 | Permission denied.                 |
1515|801 | Capability not supported.          |
1516|2900099 | Operation failed.                        |
1517
1518**示例:**
1519
1520```js
1521import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1522let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data为蓝牙设备扫描结果集合。
1523    console.info('bluetooth device find = '+ JSON.stringify(data));
1524}
1525try {
1526    connection.on('discoveryResult', onReceiveEvent);
1527    connection.off('discoveryResult', onReceiveEvent);
1528} catch (err) {
1529    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1530}
1531```
1532
1533
1534## connection.getLastConnectionTime<sup>15+</sup>
1535
1536getLastConnectionTime(deviceId: string): Promise&lt;number&gt;
1537
1538获取蓝牙远端设备最近一次连接的时间点。使用Promise异步回调。
1539
1540**系统能力**:SystemCapability.Communication.Bluetooth.Core
1541
1542**参数:**
1543
1544| 参数名    | 类型      | 必填   | 说明                               |
1545| ------ | ------- | ---- | -------------------------------- |
1546| deviceId | string  | 是    | 表示远端设备MAC地址。例如:"XX:XX:XX:XX:XX:XX"。 |
1547
1548**返回值:**
1549
1550| 类型                  | 说明         |
1551| ------------------- | ------------- |
1552| Promise&lt;number&gt; | 以Promise形式返回设置蓝牙远端设备最近一次连接的时间点结果,返回promise对象。 |
1553
1554**错误码**:
1555
1556以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1557
1558| 错误码ID | 错误信息 |
1559| -------- | ---------------------------- |
1560|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
1561|801 | Capability not supported.          |
1562|2900001 | Service stopped.                         |
1563|2900003 | Bluetooth disabled.                 |
1564|2900099 | Operation failed.                        |
1565
1566**示例:**
1567
1568```js
1569import { connection } from '@kit.ConnectivityKit';
1570// promise
1571try {
1572    connection.getLastConnectionTime('11:22:33:44:55:66').then((time: number) => {
1573        console.info('connectionTime: ${time}');
1574    });
1575} catch (err) {
1576    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1577}
1578```
1579
1580## connection.connectAllowedProfiles<sup>16+</sup>
1581
1582connectAllowedProfiles(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
1583
1584连接远端设备所有允许连接的profiles。使用Callback异步回调。<br/>需先调用pairDevice发起配对,且仅允许在每次发起配对后30s内调用此接口一次。<br/>建议用法:订阅UUID_VALUE公共事件回调,配对成功后会收到公共事件回调。建议在此回调中调用connectAllowedProfiles接口。
1585
1586**需要权限:**: ohos.permission.ACCESS_BLUETOOTH
1587
1588**系统能力:**: SystemCapability.Communication.Bluetooth.Core
1589
1590**参数:**
1591
1592| 参数名     | 类型    | 必填  | 说明                                 |
1593| -------- | ------ | ---- | ----------------------------------- |
1594| deviceId | string | 是   | 表示连接的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。|
1595| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当发起连接成功,err为undefined,否则为错误对象。  |
1596
1597**错误码:**
1598
1599以下错误码的详细介绍请参见 [通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1600
1601| 错误码ID| 错误信息|
1602| -------- | ---------------------------- |
1603|201     | Permission denied.                       |
1604|401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                       |
1605|801     | Capability not supported.                |
1606|2900001 | Service stopped.                         |
1607|2900003 | Bluetooth disabled.                 |
1608|2900099 | Operation failed.                        |
1609
1610**示例:**
1611
1612```js
1613import { commonEventManager, AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1614import { connection } from '@kit.ConnectivityKit';
1615// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
1616let subscriber: commonEventManager.CommonEventSubscriber;
1617// 订阅者信息
1618let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
1619  events: ["usual.event.bluetooth.remotedevice.UUID_VALUE"]
1620};
1621// 订阅公共事件回调
1622function SubscribeCB(err: BusinessError, data: commonEventManager.CommonEventData) {
1623  if (err) {
1624    console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`);
1625  } else {
1626    console.info(`Succeeded in subscribing, data is ` + JSON.stringify(data));
1627    // 调用connectAllowedProfiles前,需确保已收到UUID_VALUE的系统公共事件
1628    try {
1629        connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => {
1630            if (err) {
1631                console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1632                return;
1633            }
1634            console.info('connectAllowedProfiles, err: ' + JSON.stringify(err));
1635        });
1636    } catch (err) {
1637        console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1638    }
1639  }
1640}
1641// 创建订阅者回调
1642function createCB(err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) {
1643  if(!err) {
1644    console.info(`Succeeded in creating subscriber.`);
1645    subscriber = commonEventSubscriber;
1646    // 订阅公共事件
1647    try {
1648      commonEventManager.subscribe(subscriber, SubscribeCB);
1649    } catch (error) {
1650      let err: BusinessError = error as BusinessError;
1651      console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`);
1652    }
1653  } else {
1654    console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
1655  }
1656}
1657
1658// 创建订阅者,并订阅公共事件回调
1659try {
1660  commonEventManager.createSubscriber(subscribeInfo, createCB);
1661} catch (error) {
1662  let err: BusinessError = error as BusinessError;
1663  console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
1664}
1665```
1666
1667
1668## connection.connectAllowedProfiles<sup>16+</sup>
1669
1670connectAllowedProfiles(deviceId: string): Promise&lt;void&gt;
1671
1672连接远端设备所有允许连接的profiles。使用Promise异步回调。<br/>需先调用pairDevice发起配对,且仅允许在每次发起配对后30s内调用此接口一次。<br/>建议用法:订阅UUID_VALUE公共事件回调,配对成功后会收到公共事件回调.建议在此回调中调用connectAllowedProfiles接口。
1673
1674**需要权限:**: ohos.permission.ACCESS_BLUETOOTH
1675
1676**系统能力:**: SystemCapability.Communication.Bluetooth.Core
1677
1678**参数:**
1679
1680| 参数名     | 类型    | 必填  | 说明                                 |
1681| -------- | ------ | ---- | ----------------------------------- |
1682| deviceId | string | 是   | 表示连接的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。|
1683
1684**返回值:**
1685
1686| 类型                                             | 说明               |
1687| ------------------------------------------------- | ------------------- |
1688| Promise&lt;void&gt; | Promise对象。|
1689
1690**错误码:**
1691
1692以下错误码的详细介绍请参见 [通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
1693
1694| 错误码ID| 错误信息|
1695| -------- | ---------------------------- |
1696|201     | Permission denied.                       |
1697|401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                       |
1698|801     | Capability not supported.                |
1699|2900001 | Service stopped.                         |
1700|2900003 | Bluetooth disabled.                 |
1701|2900099 | Operation failed.                        |
1702
1703**示例:**
1704
1705```js
1706import { commonEventManager, AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
1707import { connection } from '@kit.ConnectivityKit';
1708// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
1709let subscriber: commonEventManager.CommonEventSubscriber;
1710// 订阅者信息
1711let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
1712  events: ["usual.event.bluetooth.remotedevice.UUID_VALUE"]
1713};
1714// 订阅公共事件回调
1715function SubscribeCB(err: BusinessError, data: commonEventManager.CommonEventData) {
1716  if (err) {
1717    console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`);
1718  } else {
1719    console.info(`Succeeded in subscribing, data is ` + JSON.stringify(data));
1720    // 调用connectAllowedProfiles前,需确保已收到UUID_VALUE的系统公共事件
1721    try {
1722        connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => {
1723            console.info('connectAllowedProfiles');
1724        }, (err: BusinessError) => {
1725            console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message);
1726        });
1727    } catch (err) {
1728        console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
1729    }
1730  }
1731}
1732// 创建订阅者回调
1733function createCB(err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) {
1734  if(!err) {
1735    console.info(`Succeeded in creating subscriber.`);
1736    subscriber = commonEventSubscriber;
1737    // 订阅公共事件
1738    try {
1739      commonEventManager.subscribe(subscriber, SubscribeCB);
1740    } catch (error) {
1741      let err: BusinessError = error as BusinessError;
1742      console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`);
1743    }
1744  } else {
1745    console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
1746  }
1747}
1748
1749// 创建订阅者,并订阅公共事件回调
1750try {
1751  commonEventManager.createSubscriber(subscribeInfo, createCB);
1752} catch (error) {
1753  let err: BusinessError = error as BusinessError;
1754  console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
1755}
1756```
1757
1758## BondStateParam
1759
1760描述配对状态参数。
1761
1762**系统能力**:SystemCapability.Communication.Bluetooth.Core
1763
1764| 名称       | 类型   | 可读   | 可写   | 说明          |
1765| -------- | ------ | ---- | ---- | ----------- |
1766| deviceId | string      | 是    | 否    | 表示要配对的设备ID。 |
1767| state    | BondState   | 是    | 否    | 表示配对设备的状态。 |
1768| cause<sup>12+</sup>| [UnbondCause](#unbondcause12) | 是 | 否 | 表示配对失败的原因。|
1769
1770
1771## PinRequiredParam
1772
1773描述配对请求参数。
1774
1775**系统能力**:SystemCapability.Communication.Bluetooth.Core
1776
1777| 名称       | 类型   | 可读   | 可写   | 说明          |
1778| -------- | ------ | ---- | ---- | ----------- |
1779| deviceId | string | 是    | 否    | 表示要配对的设备ID。 |
1780| pinCode  | string | 是    | 否    | 表示要配对的密钥。   |
1781
1782
1783
1784## DeviceClass
1785
1786描述蓝牙设备的类别。
1787
1788**系统能力**:SystemCapability.Communication.Bluetooth.Core
1789
1790| 名称              | 类型                                | 可读   | 可写   | 说明               |
1791| --------------- | ----------------------------------- | ---- | ---- | ---------------- |
1792| majorClass      | [MajorClass](js-apis-bluetooth-constant.md#majorclass)           | 是    | 否    | 表示蓝牙设备主要类别的枚举。   |
1793| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | 是    | 否    | 表示主要次要蓝牙设备类别的枚举。 |
1794| classOfDevice   | number                              | 是    | 否    | 表示设备类别。          |
1795
1796
1797## BatteryInfo<sup>12+</sup>
1798
1799描述电量信息的内容。
1800
1801**系统能力**:SystemCapability.Communication.Bluetooth.Core
1802
1803| 名称       | 类型   | 可读   | 可写   | 说明          |
1804| -------- | ------ | ---- | ---- | ----------- |
1805| batteryLevel  | number | 是    | 否    | 表示远端设备的电量值,如果值为-1,表示没有电量信息。   |
1806| leftEarBatteryLevel  | number | 是    | 否    | 表示左侧耳机的电量值,如果值为-1,表示没有电量信息。   |
1807| leftEarChargeState  | [DeviceChargeState](#devicechargestate12) | 是    | 否    | 表示左侧耳机的充电状态。   |
1808| rightEarBatteryLevel  | number | 是    | 否    | 表示右侧耳机的电量值,如果值为-1,表示没有电量信息。   |
1809| rightEarChargeState  | [DeviceChargeState](#devicechargestate12) | 是    | 否    | 表示右侧耳机的充电状态。   |
1810| boxBatteryLevel  | number | 是    | 否    | 表示耳机仓的电量值,如果值为-1,表示没有电量信息。   |
1811| boxChargeState  | [DeviceChargeState](#devicechargestate12) | 是    | 否    | 表示耳机仓的充电状态。   |
1812
1813
1814## BluetoothTransport
1815
1816枚举,表示设备类型。例如传统蓝牙设备或低功耗蓝牙设备,支持双模默认使用TRANSPORT_BR_EDR。
1817
1818**系统能力**:SystemCapability.Communication.Bluetooth.Core
1819
1820| 名称                               | 值    | 说明              |
1821| -------------------------------- | ------ | --------------- |
1822| TRANSPORT_BR_EDR   | 0 | 表示传统蓝牙(BR/EDR)设备。 |
1823| TRANSPORT_LE  | 1 | 表示低功耗蓝牙(BLE)设备。  |
1824
1825
1826## ScanMode
1827
1828枚举,扫描模式。
1829
1830**系统能力**:SystemCapability.Communication.Bluetooth.Core
1831
1832| 名称                                       | 值  | 说明              |
1833| ---------------------------------------- | ---- | --------------- |
1834| SCAN_MODE_NONE                           | 0    | 没有扫描模式。         |
1835| SCAN_MODE_CONNECTABLE                    | 1    | 可连接扫描模式。        |
1836| SCAN_MODE_GENERAL_DISCOVERABLE           | 2    | general发现模式。    |
1837| SCAN_MODE_LIMITED_DISCOVERABLE           | 3    | limited发现模式。    |
1838| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4    | 可连接general发现模式。 |
1839| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5    | 可连接limited发现模式。 |
1840
1841
1842## BondState
1843
1844枚举,配对状态。
1845
1846**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
1847
1848**系统能力**:SystemCapability.Communication.Bluetooth.Core
1849
1850| 名称                 | 值  | 说明     |
1851| ------------------ | ---- | ------ |
1852| BOND_STATE_INVALID | 0    | 无效的配对。 |
1853| BOND_STATE_BONDING | 1    | 正在配对。  |
1854| BOND_STATE_BONDED  | 2    | 已配对。   |
1855
1856
1857## UnbondCause<sup>12+</sup>
1858
1859枚举,配对失败原因。
1860
1861**系统能力**:SystemCapability.Communication.Bluetooth.Core
1862
1863| 名称                 | 值  | 说明     |
1864| ------------------ | ---- | ------ |
1865| USER_REMOVED        | 0    | 用户主动移除设备。|
1866| REMOTE_DEVICE_DOWN  | 1    | 远端设备关闭。|
1867| AUTH_FAILURE        | 2    | PIN码错误。|
1868| AUTH_REJECTED       | 3    | 远端设备鉴权拒绝。 |
1869| INTERNAL_ERROR      | 4    | 内部错误。 |
1870
1871
1872## DeviceChargeState<sup>12+</sup>
1873
1874枚举,表示充电状态。
1875
1876**系统能力**:SystemCapability.Communication.Bluetooth.Core
1877
1878| 名称                 | 值  | 说明     |
1879| ------------------ | ---- | ------ |
1880| DEVICE_NORMAL_CHARGE_NOT_CHARGED        | 0    | 未充电,不支持超级充电。|
1881| DEVICE_NORMAL_CHARGE_IN_CHARGING       | 1    | 正在充电,不支持超级充电。|
1882| DEVICE_SUPER_CHARGE_NOT_CHARGED        | 2    | 未充电,支持超级充电。|
1883| DEVICE_SUPER_CHARGE_IN_CHARGING       | 3    | 正在充电,支持超级充电。|
1884
1885## DiscoveryResult<sup>18+</sup>
1886
1887描述扫描设备状态参数。
1888
1889**系统能力**:SystemCapability.Communication.Bluetooth.Core
1890
1891| 名称       | 类型   | 可读   | 可写   | 说明          |
1892| -------- | ------ | ---- | ---- | ----------- |
1893| deviceId    | string      | 是    | 否    | 表示扫描到的设备ID。|
1894| rssi     | number      | 是    | 否    | 表示扫描到的设备的信号强度。|
1895| deviceName     | string      | 是    | 否    | 表示扫描到的设备的设备名称。|
1896| deviceClass     | DeviceClass      | 是    | 否    | 表示扫描到的设备的设备类别。|