1# @ohos.bluetooth.connection (Bluetooth Connection Module) 2 3The connection module provides capabilities for pairing with, connecting to, and querying the status of Bluetooth devices. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10 11## Modules to Import 12 13```js 14import { connection } from '@kit.ConnectivityKit'; 15``` 16 17 18## ProfileConnectionState 19 20type ProfileConnectionState = constant.ProfileConnectionState 21 22Defines the connection status of the Bluetooth profile. Supported Bluetooth profiles include Advanced Audio Distribution Profile (A2DP), Hands-Free Profile (HFP), and Human Interface Device (HID). 23 24**System capability**: SystemCapability.Communication.Bluetooth.Core 25 26| Type | Description | 27| ------------------- | ------------------- | 28| [constant.ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Connection status of the Bluetooth profile.| 29 30 31## ProfileId 32 33type ProfileId = constant.ProfileId 34 35Enumerates Bluetooth profiles. 36 37**System capability**: SystemCapability.Communication.Bluetooth.Core 38 39| Type | Description | 40| ------------------- | ------------------- | 41| [constant.ProfileId](js-apis-bluetooth-constant.md#profileid) | Bluetooth profile.| 42 43 44## ProfileUuids<sup>12+</sup> 45 46type ProfileUuids = constant.ProfileUuids 47 48Defines the UUID of the Bluetooth profile. 49 50**System capability**: SystemCapability.Communication.Bluetooth.Core 51 52| Type | Description | 53| ------------------- | ------------------- | 54| [constant.ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12) | Defines the UUID of the Bluetooth profile.| 55 56 57## MajorClass 58 59type MajorClass = constant.MajorClass 60 61Defines the Bluetooth device major class. This is a standard field in the Bluetooth protocol. 62 63**System capability**: SystemCapability.Communication.Bluetooth.Core 64 65| Type | Description | 66| ------------------- | ------------------- | 67| [constant.MajorClass](js-apis-bluetooth-constant.md#majorclass) | Bluetooth device major class.| 68 69 70## MajorMinorClass 71 72type MajorMinorClass = constant.MajorMinorClass 73 74Defines the Bluetooth device subclass, which is further classified based on [MajorClass](js-apis-bluetooth-constant.md#majorclass). This is a standard field in the Bluetooth protocol. 75 76**System capability**: SystemCapability.Communication.Bluetooth.Core 77 78| Type | Description | 79| ------------------- | ------------------- | 80| [constant.MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | Bluetooth device subclass.| 81 82 83## connection.pairDevice 84 85pairDevice(deviceId: string, callback: AsyncCallback<void>): void 86 87Initiates pairing with the peer Bluetooth device. This API uses an asynchronous callback to return the result. 88- You can obtain the Bluetooth pairing status from the callback of [on('bondStateChange')](#connectiononbondstatechange). 89 90**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 91 92**Atomic service API**: This API can be used in atomic services since API version 12. 93 94**System capability**: SystemCapability.Communication.Bluetooth.Core 95 96**Parameters** 97 98| Name | Type | Mandatory | Description | 99| -------- | ------ | ---- | ----------------------------------- | 100| deviceId | string | Yes | Address of the peer Bluetooth device, for example, XX:XX:XX:XX:XX:XX.| 101| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the pairing is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 102 103**Error codes** 104 105For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 106 107| ID| Error Message| 108| -------- | ---------------------------- | 109|201 | Permission denied. | 110|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 111|801 | Capability not supported. | 112|2900001 | Service stopped. | 113|2900003 | Bluetooth disabled. | 114|2900099 | Operation failed. | 115 116**Example** 117 118```js 119import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 120// callback 121try { 122 connection.pairDevice('11:22:33:44:55:66', (err: BusinessError) => { 123 console.info('pairDevice, device name err:' + JSON.stringify(err)); 124 }); 125} catch (err) { 126 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 127} 128 129``` 130 131 132## connection.pairDevice 133 134pairDevice(deviceId: string): Promise<void> 135 136Initiates pairing with the peer Bluetooth device. This API uses a promise to return the result. 137- You can obtain the Bluetooth pairing status from the callback of [on('bondStateChange')](#connectiononbondstatechange). 138 139**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 140 141**Atomic service API**: This API can be used in atomic services since API version 12. 142 143**System capability**: SystemCapability.Communication.Bluetooth.Core 144 145**Parameters** 146 147| Name | Type | Mandatory | Description | 148| -------- | ------ | ---- | ----------------------------------- | 149| deviceId | string | Yes | Address of the peer Bluetooth device, for example, XX:XX:XX:XX:XX:XX.| 150 151**Return value** 152 153| Type | Description | 154| ------------------- | ------------- | 155| Promise<void> | Promise that returns no value.| 156 157**Error codes** 158 159For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 160 161| ID| Error Message| 162| -------- | ---------------------------- | 163|201 | Permission denied. | 164|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 165|801 | Capability not supported. | 166|2900001 | Service stopped. | 167|2900003 | Bluetooth disabled. | 168|2900099 | Operation failed. | 169 170**Example** 171 172```js 173import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 174// promise 175try { 176 connection.pairDevice('11:22:33:44:55:66').then(() => { 177 console.info('pairDevice'); 178 }, (error: BusinessError) => { 179 console.info('pairDevice: errCode:' + error.code + ',errMessage' + error.message); 180 }) 181 182} catch (err) { 183 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 184} 185``` 186 187 188## connection.getRemoteDeviceName 189 190getRemoteDeviceName(deviceId: string): string 191 192Obtains the name of the peer Bluetooth device. 193 194**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 195 196**Atomic service API**: This API can be used in atomic services since API version 12. 197 198**System capability**: SystemCapability.Communication.Bluetooth.Core 199 200**Parameters** 201 202| Name | Type | Mandatory | Description | 203| -------- | ------ | ---- | --------------------------------- | 204| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 205 206**Return value** 207 208| Type | Description | 209| ------ | ------------- | 210| string | Device name (a string) obtained.| 211 212**Error codes** 213 214For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 215 216| ID| Error Message| 217| -------- | ---------------------------- | 218|201 | Permission denied. | 219|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 220|801 | Capability not supported. | 221|2900001 | Service stopped. | 222|2900003 | Bluetooth disabled. | 223|2900099 | Operation failed. | 224 225**Example** 226 227```js 228import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 229try { 230 let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX'); 231} catch (err) { 232 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 233} 234``` 235 236 237## connection.getRemoteDeviceName<sup>16+</sup> 238 239getRemoteDeviceName(deviceId: string, alias?: boolean): string 240 241Obtains the name of the peer device. The **alias** parameter is optional. 242 243**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 244 245**Atomic service API**: This API can be used in atomic services since API version 16. 246 247**System capability**: SystemCapability.Communication.Bluetooth.Core 248 249**Parameters** 250 251| Name | Type | Mandatory | Description | 252| -------- | ------ | ---- | --------------------------------- | 253| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 254| alias | boolean | No | Whether to obtain the alias of the peer device.<br>- If **alias** is present, the application determines whether to obtain the alias of the peer device. The value **true** means to obtain the alias, and the value **false** means to obtain the original name .<br>- If **alias** is not present, the default value is **true**, which means to obtain the alias of the peer device.| 255 256**Return value** 257 258| Type | Description | 259| ------ | ------------- | 260| string | Device name (a string) obtained.| 261 262**Error codes** 263 264For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 265 266| ID| Error Message| 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**Example** 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 291Obtains the class of the peer Bluetooth device. Since API version 18, the **ohos.permission.ACCESS_BLUETOOTH** permission is no longer verified. 292 293**System capability**: SystemCapability.Communication.Bluetooth.Core 294 295**Parameters** 296 297| Name | Type | Mandatory | Description | 298| -------- | ------ | ---- | --------------------------------- | 299| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 300 301**Return value** 302 303| Type | Description | 304| --------------------------- | -------- | 305| [DeviceClass](#deviceclass) | Class of the peer device.| 306 307**Error codes** 308 309For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 310 311| ID| Error Message| 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**Example** 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<Array<ProfileUuids>>): void 334 335Obtains the profile of the peer Bluetooth device based on the specified UUID. This API uses an asynchronous callback to return the result. 336- You are advised to use this API only for paired devices. 337 338**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 339 340**System capability**: SystemCapability.Communication.Bluetooth.Core 341 342**Parameters** 343 344| Name | Type | Mandatory | Description | 345| -------- | ------ | ---- | ----------------------------------- | 346| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 347| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)>> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**, and the set of supported profiles is returned. Otherwise, **err** is an error object.| 348 349**Error codes** 350 351For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 352 353| ID| Error Message| 354| -------- | ---------------------------- | 355|201 | Permission denied. | 356|401 | Invalid parameter. | 357|801 | Capability not supported. | 358|2900001 | Service stopped. | 359|2900003 | Bluetooth disabled. | 360|2900099 | Operation failed. | 361 362**Example** 363 364```js 365import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 366try { 367 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => { 368 console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 369 }); 370} catch (err) { 371 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 372} 373 374``` 375 376 377## connection.getRemoteProfileUuids<sup>12+</sup> 378 379getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>> 380 381Obtains the profile of the peer Bluetooth device based on the specified UUID. This API uses a promise to return the result. 382- You are advised to use this API only for paired devices. 383 384**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 385 386**System capability**: SystemCapability.Communication.Bluetooth.Core 387 388**Parameters** 389 390| Name | Type | Mandatory | Description | 391| -------- | ------ | ---- | ----------------------------------- | 392| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 393 394**Return value** 395 396| Type | Description | 397| ------------------- | ------------- | 398| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)>> | Promise used to return the set of supported profiles .| 399 400**Error codes** 401 402For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 403 404| ID| Error Message| 405| -------- | ---------------------------- | 406|201 | Permission denied. | 407|401 | Invalid parameter. | 408|801 | Capability not supported. | 409|2900001 | Service stopped. | 410|2900003 | Bluetooth disabled. | 411|2900099 | Operation failed. | 412 413**Example** 414 415```js 416import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 417try { 418 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { 419 console.info('getRemoteProfileUuids'); 420 }, (err: BusinessError) => { 421 console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 422 }); 423} catch (err) { 424 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 425} 426``` 427 428 429## connection.getLocalName 430 431getLocalName(): string 432 433Obtains the name of the local Bluetooth device. 434 435**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 436 437**System capability**: SystemCapability.Communication.Bluetooth.Core 438 439**Return value** 440 441| Type | Description | 442| ------ | --------- | 443| string | Name of the local Bluetooth device.| 444 445**Error codes** 446 447For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 448 449| ID| Error Message| 450| -------- | ---------------------------- | 451|201 | Permission denied. | 452|801 | Capability not supported. | 453|2900001 | Service stopped. | 454|2900099 | Operation failed. | 455 456**Example** 457 458```js 459import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 460try { 461 let localName: string = connection.getLocalName(); 462} catch (err) { 463 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 464} 465``` 466 467 468## connection.getPairedDevices 469 470getPairedDevices(): Array<string> 471 472Obtains the addresses of paired Bluetooth devices. 473 474**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 475 476**Atomic service API**: This API can be used in atomic services since API version 12. 477 478**System capability**: SystemCapability.Communication.Bluetooth.Core 479 480**Return value** 481 482| Type | Description | 483| ------------------- | ------------- | 484| Array<string> | Addresses of paired Bluetooth devices.<br>For security purposes, the device addresses obtained are virtual MAC addresses.<br>- The virtual addresses of paired Bluetooth devices will not change.<br>- If a device is unpaired or Bluetooth is disabled, the virtual address will change after the device is paired again.<br>- To persistently save the addresses, call [access.addPersistentDeviceId](js-apis-bluetooth-access.md#accessaddpersistentdeviceid16).| 485 486**Error codes** 487 488For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 489 490| ID| Error Message| 491| -------- | ---------------------------- | 492|201 | Permission denied. | 493|801 | Capability not supported. | 494|2900001 | Service stopped. | 495|2900003 | Bluetooth disabled. | 496|2900099 | Operation failed. | 497 498**Example** 499 500```js 501import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 502try { 503 let devices: Array<string> = connection.getPairedDevices(); 504} catch (err) { 505 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 506} 507``` 508 509 510## connection.getPairState<sup>11+</sup> 511 512getPairState(deviceId: string): BondState 513 514Obtains the pairing status of the peer Bluetooth device. 515 516**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 517 518**Atomic service API**: This API can be used in atomic services since API version 12. 519 520**System capability**: SystemCapability.Communication.Bluetooth.Core 521 522**Parameters** 523 524| Name | Type | Mandatory | Description | 525| -------- | ------ | ---- | --------------------------------- | 526| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 527 528**Return value** 529 530| Type | Description | 531| --------------------------- | -------- | 532| [BondState](#bondstate) | Bluetooth pairing state obtained.| 533 534**Error codes** 535 536For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 537 538| ID| Error Message| 539| -------- | ---------------------------- | 540|201 | Permission denied. | 541|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 542|801 | Capability not supported. | 543|2900001 | Service stopped. | 544|2900003 | Bluetooth disabled. | 545|2900099 | Operation failed. | 546 547**Example** 548 549```js 550import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 551try { 552 let res: connection.BondState = connection.getPairState("XX:XX:XX:XX:XX:XX"); 553 console.info('getPairState: ' + res); 554} catch (err) { 555 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 556} 557``` 558 559 560## connection.getProfileConnectionState 561 562getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState 563 564Obtains the connection status of a Bluetooth profile. The **ProfileId** parameter is optional. 565 566**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 567 568**System capability**: SystemCapability.Communication.Bluetooth.Core 569 570**Parameters** 571 572| Name | Type | Mandatory | Description | 573| --------- | --------- | ---- | ------------------------------------- | 574| profileId | [ProfileId](js-apis-bluetooth-constant.md#profileid) | No | Bluetooth profile. If **ProfileId** is present, the connection status of the specified profile is returned. If **ProfileId** is not present, the connection status of all supported profiles is returned in the following order:<br>- If a profile is connected, [STATE_CONNECTED] (js-apis-bluetooth-constant.md#profileconnectionstate) is returned.<br>- If a profile is being connected, [STATE_CONNECTING] (js-apis-bluetooth-constant.md#profileconnectionstate) is returned.<br>- If a profile is being disconnected, [STATE_DISCONNECTING] (js-apis-bluetooth-constant.md#profileconnectionstate) is returned.<br>- If none of the preceding conditions is met, [STATE_DISCONNECTED] (js-apis-bluetooth-constant.md#profileconnectionstate) is returned.| 575 576**Return value** 577 578| Type | Description | 579| ------------------------------------------------- | ------------------- | 580| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Connection status of the profile.| 581 582**Error codes** 583 584For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 585 586| ID| Error Message| 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**Example** 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 613Confirms the pairing request from the peer Bluetooth device. 614- You can obtain the pairing status of the peer Bluetooth device from the callback of [on('pinRequired')](#connectiononpinrequired). 615 616**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH (available only for system applications) 617 618**System capability**: SystemCapability.Communication.Bluetooth.Core 619 620**Parameters** 621 622| Name | Type | Mandatory | Description | 623| ------ | ------- | ---- | -------------------------------- | 624| deviceId | string | Yes| Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 625| accept | boolean | Yes | Whether to accept the pairing request from the peer device. The value **true** means to accept the pairing request, and the value **false** means the opposite. | 626 627**Error codes** 628 629For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 630 631| ID| Error Message| 632| -------- | ---------------------------- | 633|201 | Permission denied. | 634|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 635|801 | Capability not supported. | 636|2900001 | Service stopped. | 637|2900003 | Bluetooth disabled. | 638|2900099 | Operation failed. | 639 640**Example** 641 642```js 643import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 644// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the peer device. 645function onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data is the input parameter for the pairing request. 646 console.info('pin required = '+ JSON.stringify(data)); 647 connection.setDevicePairingConfirmation(data.deviceId, true); 648} 649try { 650 connection.on('pinRequired', onReceivePinRequiredEvent); 651} catch (err) { 652 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 653} 654``` 655 656 657## connection.setDevicePinCode 658 659setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void 660 661Sets the PIN used to complete Bluetooth pairing. This API uses an asynchronous callback to return the result. 662 663**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 664 665**System capability**: SystemCapability.Communication.Bluetooth.Core 666 667**Parameters** 668 669| Name | Type | Mandatory | Description | 670| ------ | ------- | ---- | -------------------------------- | 671| deviceId | string | Yes | MAC address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 672| code | string | Yes | PIN to set. | 673| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 674 675**Error codes** 676 677For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 678 679| ID| Error Message| 680| -------- | ---------------------------- | 681|201 | Permission denied. | 682|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 683|801 | Capability not supported. | 684|2900001 | Service stopped. | 685|2900003 | Bluetooth disabled. | 686|2900099 | Operation failed. | 687 688**Example** 689 690```js 691import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 692// callback 693try { 694 connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => { 695 console.info('setDevicePinCode,device name err:' + JSON.stringify(err)); 696 }); 697} catch (err) { 698 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 699} 700``` 701 702 703## connection.setDevicePinCode 704 705setDevicePinCode(deviceId: string, code: string): Promise<void> 706 707Sets the PIN used to complete Bluetooth pairing. This API uses a promise to return the result. 708 709**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 710 711**System capability**: SystemCapability.Communication.Bluetooth.Core 712 713**Parameters** 714 715| Name | Type | Mandatory | Description | 716| ------ | ------- | ---- | -------------------------------- | 717| deviceId | string | Yes | MAC address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 718| code | string | Yes | PIN to set. | 719 720**Return value** 721 722| Type | Description | 723| ------------------- | ------------- | 724| Promise<void> | Promise that returns no value.| 725 726**Error codes** 727 728For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 729 730| ID| Error Message| 731| -------- | ---------------------------- | 732|201 | Permission denied. | 733|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 734|801 | Capability not supported. | 735|2900001 | Service stopped. | 736|2900003 | Bluetooth disabled. | 737|2900099 | Operation failed. | 738 739**Example** 740 741```js 742import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 743// promise 744try { 745 connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => { 746 console.info('setDevicePinCode'); 747 }, (error: BusinessError) => { 748 console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message); 749 }) 750 751} catch (err) { 752 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 753} 754``` 755 756 757## connection.setLocalName<sup>(deprecated)</sup> 758 759setLocalName(name: string): void 760 761Sets the name of the local Bluetooth device. The value cannot be an empty string. If the value is an empty string, the operation will fail. 762 763> **NOTE**<br> 764> This API is supported since API version 10 and deprecated since API version 12. No substitute is provided. 765 766**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 767 768**System capability**: SystemCapability.Communication.Bluetooth.Core 769 770**Parameters** 771 772| Name | Type | Mandatory | Description | 773| ---- | ------ | ---- | --------------------- | 774| name | string | Yes | Bluetooth device name. The value range is (0,248], in bytes.| 775 776**Error codes** 777 778For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 779 780| ID| Error Message| 781| -------- | ---------------------------- | 782|201 | Permission denied. | 783|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 784|801 | Capability not supported. | 785|2900001 | Service stopped. | 786|2900003 | Bluetooth disabled. | 787|2900099 | Operation failed. | 788 789**Example** 790 791```js 792import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 793try { 794 connection.setLocalName('device_name'); 795} catch (err) { 796 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 797} 798``` 799 800 801## connection.setBluetoothScanMode 802 803setBluetoothScanMode(mode: ScanMode, duration: number): void 804 805Sets the Bluetooth scan mode, which determines whether the local device can be connected or discovered. 806 807**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 808 809**System capability**: SystemCapability.Communication.Bluetooth.Core 810 811**Parameters** 812 813| Name | Type | Mandatory | Description | 814| -------- | --------------------- | ---- | ---------------------------- | 815| mode | [ScanMode](#scanmode) | Yes | Bluetooth scan mode to set. If the scan times out (**duration** is not **0**) when the scan mode is **SCAN_MODE_GENERAL_DISCOVERABLE**, the scan mode will be reset to **SCAN_MODE_CONNECTABLE**. | 816| duration | number | Yes | Duration (in seconds) in which the device can be discovered. The value **0** indicates unlimited time.| 817 818**Error codes** 819 820For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 821 822| ID| Error Message| 823| -------- | ---------------------------- | 824|201 | Permission denied. | 825|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 826|801 | Capability not supported. | 827|2900001 | Service stopped. | 828|2900003 | Bluetooth disabled. | 829|2900099 | Operation failed. | 830 831**Example** 832 833```js 834import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 835try { 836 // The device can be discovered and connected only when the discoverable and connectable mode is used. 837 connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 838} catch (err) { 839 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 840} 841``` 842 843 844## connection.getBluetoothScanMode 845 846getBluetoothScanMode(): ScanMode 847 848Obtains the Bluetooth scan mode. 849 850**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 851 852**System capability**: SystemCapability.Communication.Bluetooth.Core 853 854**Return value** 855 856| Type | Description | 857| --------------------- | ------- | 858| [ScanMode](#scanmode) | Bluetooth scan mode obtained.| 859 860**Error codes** 861 862For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 863 864| ID| Error Message| 865| -------- | ---------------------------- | 866|201 | Permission denied. | 867|801 | Capability not supported. | 868|2900001 | Service stopped. | 869|2900003 | Bluetooth disabled. | 870|2900099 | Operation failed. | 871 872**Example** 873 874```js 875import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 876try { 877 let scanMode: connection.ScanMode = connection.getBluetoothScanMode(); 878} catch (err) { 879 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 880} 881``` 882 883 884## connection.startBluetoothDiscovery 885 886startBluetoothDiscovery(): void 887 888Starts a Bluetooth scan for device discovery.<br> 889- This API applies to both classic Bluetooth devices and BLE devices.<br> 890- You can obtain the scan result from the callback of [connection.on('bluetoothDeviceFind')](#connectiononbluetoothdevicefind) (supported since API version 10) or [connection.on('discoveryResult')](#connectionondiscoveryresult18) (supported since API version 18). You are advised to use [connection.on('discoveryResult')](#connectionondiscoveryresult18), which can obtain more detailed device information.<br> 891- You can call [stopBluetoothDiscovery](#connectionstopbluetoothdiscovery) to stop the Bluetooth scan. 892 893**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 894 895**Atomic service API**: This API can be used in atomic services since API version 12. 896 897**System capability**: SystemCapability.Communication.Bluetooth.Core 898 899**Error codes** 900 901For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 902 903| ID| Error Message| 904| -------- | ---------------------------- | 905|201 | Permission denied. | 906|801 | Capability not supported. | 907|2900001 | Service stopped. | 908|2900003 | Bluetooth disabled. | 909|2900099 | Operation failed. | 910 911**Example** 912 913```js 914import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 915function onReceiveEvent(data: Array<string>) { 916 console.info('data length' + data.length); 917} 918try { 919 connection.on('bluetoothDeviceFind', onReceiveEvent); 920 connection.startBluetoothDiscovery(); 921} catch (err) { 922 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 923} 924``` 925 926 927## connection.stopBluetoothDiscovery 928 929stopBluetoothDiscovery(): void 930 931Stops the Bluetooth scan.<br> 932- This API applies only to scans initiated by [connection.startBluetoothDiscovery](#connectionstartbluetoothdiscovery).<br> 933- Call this API to stop the Bluetooth scan when device discovery is no longer needed. 934 935**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 936 937**Atomic service API**: This API can be used in atomic services since API version 12. 938 939**System capability**: SystemCapability.Communication.Bluetooth.Core 940 941**Error codes** 942 943For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 944 945| ID| Error Message| 946| -------- | ---------------------------- | 947|201 | Permission denied. | 948|801 | Capability not supported. | 949|2900001 | Service stopped. | 950|2900003 | Bluetooth disabled. | 951|2900099 | Operation failed. | 952 953**Example** 954 955```js 956import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 957try { 958 connection.stopBluetoothDiscovery(); 959} catch (err) { 960 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 961} 962``` 963 964 965## connection.isBluetoothDiscovering<sup>11+</sup> 966 967isBluetoothDiscovering(): boolean 968 969Checks whether the local Bluetooth device is in the device scanning state. 970 971**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 972 973**System capability**: SystemCapability.Communication.Bluetooth.Core 974 975**Return value** 976 977| Type | Description | 978| ------------------- | ------------- | 979| boolean | Whether Bluetooth discovery is in process. The value **true** indicates that Bluetooth discovery is in process, and the value **false** indicates the opposite. | 980 981**Error codes** 982 983For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 984 985| ID| Error Message| 986| -------- | ---------------------------- | 987|201 | Permission denied. | 988|801 | Capability not supported. | 989|2900001 | Service stopped. | 990|2900003 | Bluetooth disabled. | 991|2900099 | Operation failed. | 992 993**Example** 994 995```js 996import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 997try { 998 let res: boolean = connection.isBluetoothDiscovering(); 999 console.info('isBluetoothDiscovering: ' + res); 1000} catch (err) { 1001 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1002} 1003``` 1004 1005## connection.setRemoteDeviceName<sup>12+</sup> 1006 1007setRemoteDeviceName(deviceId: string, name: string): Promise<void> 1008 1009Sets the name of the peer Bluetooth device. The value cannot be an empty string. If the value is an empty string, the operation will fail. This API uses a promise to return the result. 1010- You are advised to use this API only for paired devices. 1011 1012**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1013 1014**Atomic service API**: This API can be used in atomic services since API version 12. 1015 1016**System capability**: SystemCapability.Communication.Bluetooth.Core 1017 1018**Parameters** 1019 1020| Name | Type | Mandatory | Description | 1021| -------- | ----------------------------------- | ---- | -------------------------------------- | 1022| deviceId | string | Yes | MAC address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 1023| name | string | Yes | Bluetooth device name. The value range is (0,64], in bytes. | 1024 1025**Return value** 1026 1027| Type | Description | 1028| ------------------- | ------------- | 1029| Promise<void> | Promise that returns no value.| 1030 1031**Error codes** 1032 1033For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1034 1035| ID| Error Message| 1036| -------- | ---------------------------- | 1037|201 | Permission denied. | 1038|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1039|2900001 | Service stopped. | 1040|2900003 | Bluetooth disabled. | 1041 1042**Example** 1043 1044```js 1045import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1046// promise 1047try { 1048 connection.setRemoteDeviceName('11:22:33:44:55:66', 'RemoteDeviceName').then(() => { 1049 console.info('setRemoteDeviceName success'); 1050 }, (error: BusinessError) => { 1051 console.error('setRemoteDeviceName: errCode:' + error.code + ',errMessage' + error.message); 1052 }) 1053 1054} catch (err) { 1055 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1056} 1057``` 1058 1059 1060## connection.getRemoteDeviceBatteryInfo<sup>12+</sup> 1061 1062getRemoteDeviceBatteryInfo(deviceId: string): Promise<BatteryInfo> 1063 1064Obtains the battery level of the peer Bluetooth device. This API uses a promise to return the result. 1065- You can obtain the battery level of the peer Bluetooth device from the callback of [on('batteryChange')](#connectiononbatterychange12). 1066 1067**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1068 1069**System capability**: SystemCapability.Communication.Bluetooth.Core 1070 1071**Parameters** 1072 1073| Name | Type | Mandatory | Description | 1074| ------ | ------- | ---- | -------------------------------- | 1075| deviceId | string | Yes | MAC address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 1076 1077**Return value** 1078 1079| Type | Description | 1080| ------------------- | ------------- | 1081| Promise<[BatteryInfo](#batteryinfo12)> | Promise used to return the battery level information.| 1082 1083**Error codes** 1084 1085For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1086 1087| ID| Error Message| 1088| -------- | ---------------------------- | 1089|201 | Permission denied. | 1090|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1091|2900001 | Service stopped. | 1092|2900003 | Bluetooth disabled. | 1093 1094**Example** 1095 1096```js 1097import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1098// promise 1099try { 1100 connection.getRemoteDeviceBatteryInfo('11:22:33:AA:BB:FF').then((data: connection.BatteryInfo) => { 1101 console.info('getRemoteDeviceBatteryInfo success, DeviceType:' + JSON.stringify(data)); 1102 }); 1103} catch (err) { 1104 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1105} 1106``` 1107 1108 1109## connection.on('batteryChange')<sup>12+</sup> 1110 1111on(type: 'batteryChange', callback: Callback<BatteryInfo>): void 1112 1113Subscribes to battery change events of the peer device. This API uses an asynchronous callback to return the result. 1114 1115**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1116 1117**System capability**: SystemCapability.Communication.Bluetooth.Core 1118 1119**Parameters** 1120 1121| Name | Type | Mandatory | Description | 1122| -------- | ----------------------------------- | ---- | -------------------------------------- | 1123| type | string | Yes | Event type. The value **batteryChange** indicates the battery change event. This event is triggered when the battery level of the peer device changes.| 1124| callback | Callback<[BatteryInfo](#batteryinfo12)> | Yes | Callback used to return the battery level information. | 1125 1126**Error codes** 1127 1128For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1129 1130| ID| Error Message| 1131| -------- | ---------------------------- | 1132|201 | Permission denied. | 1133|2900099 | Operation failed. | 1134 1135**Example** 1136 1137```js 1138import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1139let onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => { 1140 console.info('BatteryInfo = '+ JSON.stringify(data)); 1141} 1142try { 1143 connection.on('batteryChange', onReceiveEvent); 1144} catch (err) { 1145 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1146} 1147``` 1148 1149 1150## connection.off('batteryChange')<sup>12+</sup> 1151 1152off(type: 'batteryChange', callback?: Callback<BatteryInfo>): void 1153 1154Unsubscribes from battery change events of the peer device. 1155 1156**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1157 1158**System capability**: SystemCapability.Communication.Bluetooth.Core 1159 1160**Parameters** 1161 1162| Name | Type | Mandatory | Description | 1163| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1164| type | string | Yes | Event type. The value **batteryChange** indicates the battery change event. | 1165| callback | Callback<[BatteryInfo](#batteryinfo12)> | No | Callback to unsubscribe.<br>If this parameter is specified, it must be the same as the callback in [connection.on('batteryChange')](#connectiononbatterychange12). If this parameter is not specified, all callbacks corresponding to the event type are unsubscribed.| 1166 1167**Error codes** 1168 1169For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1170 1171| ID| Error Message| 1172| -------- | ---------------------------- | 1173|201 | Permission denied. | 1174|2900099 | Operation failed. | 1175 1176**Example** 1177 1178```js 1179import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1180let onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => { 1181 console.info('BatteryInfo = '+ JSON.stringify(data)); 1182} 1183try { 1184 connection.on('batteryChange', onReceiveEvent); 1185 connection.off('batteryChange', onReceiveEvent); 1186} catch (err) { 1187 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1188} 1189``` 1190 1191 1192## connection.on('bluetoothDeviceFind') 1193 1194on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void 1195 1196Subscribes to scan result reporting events of Bluetooth devices. This API uses an asynchronous callback to return the result.<br> 1197- This API applies to both classic Bluetooth devices and BLE devices.<br> 1198- This API provides only the device address.<br> 1199- You are advised to use [connection.on('discoveryResult')](#connectionondiscoveryresult18) (supported since API version 18), which can obtain more detailed device information, including the device address, signal strength, name, and type. 1200 1201**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1202 1203**Atomic service API**: This API can be used in atomic services since API version 12. 1204 1205**System capability**: SystemCapability.Communication.Bluetooth.Core 1206 1207**Parameters** 1208 1209| Name | Type | Mandatory | Description | 1210| -------- | ----------------------------------- | ---- | -------------------------------------- | 1211| type | string | Yes | Event type. The value **bluetoothDeviceFind** indicates a scan result reporting event. A device scan starts when [connection.startBluetoothDiscovery](#connectionstartbluetoothdiscovery) is called. This event is triggered when a device is discovered.| 1212| callback | Callback<Array<string>> | Yes | Callback used to return the set of device addresses.<br>For security purposes, the device addresses obtained are virtual MAC addresses.<br>- The virtual address remains unchanged after a device is paired successfully.<br>- If a device is unpaired or Bluetooth is disabled, the virtual address will change after the device is paired again.<br>- To persistently save the addresses, call [access.addPersistentDeviceId](js-apis-bluetooth-access.md#accessaddpersistentdeviceid16). | 1213 1214**Error codes** 1215 1216For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1217 1218| ID| Error Message| 1219| -------- | ---------------------------- | 1220|201 | Permission denied. | 1221|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1222|801 | Capability not supported. | 1223|2900099 | Operation failed. | 1224 1225**Example** 1226 1227```js 1228import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1229function onReceiveEvent(data: Array<string>) { // data is an array of Bluetooth device addresses. 1230 console.info('bluetooth device find = '+ JSON.stringify(data)); 1231} 1232try { 1233 connection.on('bluetoothDeviceFind', onReceiveEvent); 1234} catch (err) { 1235 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1236} 1237``` 1238 1239 1240## connection.off('bluetoothDeviceFind') 1241 1242off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void 1243 1244Unsubscribes from Bluetooth scan result reporting events. 1245 1246**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1247 1248**Atomic service API**: This API can be used in atomic services since API version 12. 1249 1250**System capability**: SystemCapability.Communication.Bluetooth.Core 1251 1252**Parameters** 1253 1254| Name | Type | Mandatory | Description | 1255| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1256| type | string | Yes | Event type. The value **bluetoothDeviceFind** indicates a scan result reporting event. | 1257| callback | Callback<Array<string>> | No | Callback used to return the result.<br>If this parameter is specified, it must be the same as the callback in [connection.on('bluetoothDeviceFind')](#connectiononbluetoothdevicefind). If this parameter is not specified, all callbacks corresponding to the event type are unsubscribed.| 1258 1259**Error codes** 1260 1261For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1262 1263| ID| Error Message| 1264| -------- | ---------------------------- | 1265|201 | Permission denied. | 1266|801 | Capability not supported. | 1267|2900099 | Operation failed. | 1268 1269**Example** 1270 1271```js 1272import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1273function onReceiveEvent(data: Array<string>) { 1274 console.info('bluetooth device find = '+ JSON.stringify(data)); 1275} 1276try { 1277 connection.on('bluetoothDeviceFind', onReceiveEvent); 1278 connection.off('bluetoothDeviceFind', onReceiveEvent); 1279} catch (err) { 1280 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1281} 1282``` 1283 1284 1285## connection.on('bondStateChange') 1286 1287on(type: 'bondStateChange', callback: Callback<BondStateParam>): void 1288 1289Subscribes to Bluetooth pairing status change events. This API uses an asynchronous callback to return the result. 1290 1291**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1292 1293**System capability**: SystemCapability.Communication.Bluetooth.Core 1294 1295**Parameters** 1296 1297| Name | Type | Mandatory | Description | 1298| -------- | ---------------------------------------- | ---- | ------------------------------------ | 1299| type | string | Yes | Event type. The value **bondStateChange** indicates a Bluetooth pairing status change event.<br>This event is triggered when [connection.pairDevice](#connectionpairdevice) is called to initiate pairing or the local device receives a pairing request from another device.| 1300| callback | Callback<[BondStateParam](#bondstateparam)> | Yes | Callback used to return the pairing status. | 1301 1302**Error codes** 1303 1304For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1305 1306| ID| Error Message| 1307| -------- | ---------------------------- | 1308|201 | Permission denied. | 1309|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1310|801 | Capability not supported. | 1311|2900099 | Operation failed. | 1312 1313**Example** 1314 1315```js 1316import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1317function onReceiveEvent(data: connection.BondStateParam) { // data, as the input parameter of the callback, indicates the pairing state. 1318 console.info('pair state = '+ JSON.stringify(data)); 1319} 1320try { 1321 connection.on('bondStateChange', onReceiveEvent); 1322} catch (err) { 1323 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1324} 1325``` 1326 1327 1328## connection.off('bondStateChange') 1329 1330off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void 1331 1332Unsubscribes from Bluetooth pairing status change events. 1333 1334**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1335 1336**System capability**: SystemCapability.Communication.Bluetooth.Core 1337 1338**Parameters** 1339 1340| Name | Type | Mandatory | Description | 1341| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1342| type | string | Yes | Event type. The value **bondStateChange** indicates a Bluetooth pairing status change event. | 1343| callback | Callback<[BondStateParam](#bondstateparam)> | No | Callback used to return the result.<br>If this parameter is specified, it must be the same as the callback in [connection.on('bondStateChange')](#connectiononbondstatechange). If this parameter is not specified, all callbacks corresponding to the event type are unsubscribed.| 1344 1345**Error codes** 1346 1347For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1348 1349| ID| Error Message| 1350| -------- | ---------------------------- | 1351|201 | Permission denied. | 1352|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1353|801 | Capability not supported. | 1354|2900099 | Operation failed. | 1355 1356**Example** 1357 1358```js 1359import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1360function onReceiveEvent(data: connection.BondStateParam) { 1361 console.info('bond state = '+ JSON.stringify(data)); 1362} 1363try { 1364 connection.on('bondStateChange', onReceiveEvent); 1365 connection.off('bondStateChange', onReceiveEvent); 1366} catch (err) { 1367 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1368} 1369``` 1370 1371 1372## connection.on('pinRequired') 1373 1374on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void 1375 1376Subscribes to pairing request events. This API uses an asynchronous callback to return the result. 1377 1378**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1379 1380**System capability**: SystemCapability.Communication.Bluetooth.Core 1381 1382**Parameters** 1383 1384| Name | Type | Mandatory | Description | 1385| -------- | ---------------------------------------- | ---- | -------------------------------- | 1386| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event.<br>This event is triggered when [connection.pairDevice](#connectionpairdevice) is called to initiate pairing or the local device receives a pairing request from another device. | 1387| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | Yes | Callback used to return the pairing request.| 1388 1389**Error codes** 1390 1391For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1392 1393| ID| Error Message| 1394| -------- | ---------------------------- | 1395|201 | Permission denied. | 1396|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1397|801 | Capability not supported. | 1398|2900099 | Operation failed. | 1399 1400**Example** 1401 1402```js 1403import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1404function onReceiveEvent(data: connection.PinRequiredParam) { // data is the pairing request parameter. 1405 console.info('pin required = '+ JSON.stringify(data)); 1406} 1407try { 1408 connection.on('pinRequired', onReceiveEvent); 1409} catch (err) { 1410 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1411} 1412``` 1413 1414 1415## connection.off('pinRequired') 1416 1417off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void 1418 1419Unsubscribes from pairing request events. 1420 1421**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1422 1423**System capability**: SystemCapability.Communication.Bluetooth.Core 1424 1425**Parameters** 1426 1427| Name | Type | Mandatory | Description | 1428| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1429| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 1430| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | No | Callback used to return the result.<br>If this parameter is specified, it must be the same as the callback in [connection.on('pinRequired')](#connectiononpinrequired). If this parameter is not specified, all callbacks corresponding to the event type are unsubscribed.| 1431 1432**Error codes** 1433 1434For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1435 1436| ID| Error Message| 1437| -------- | ---------------------------- | 1438|201 | Permission denied. | 1439|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1440|801 | Capability not supported. | 1441|2900099 | Operation failed. | 1442 1443**Example** 1444 1445```js 1446import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1447function onReceiveEvent(data: connection.PinRequiredParam) { 1448 console.info('pin required = '+ JSON.stringify(data)); 1449} 1450try { 1451 connection.on('pinRequired', onReceiveEvent); 1452 connection.off('pinRequired', onReceiveEvent); 1453} catch (err) { 1454 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1455} 1456``` 1457 1458 1459## connection.on('discoveryResult')<sup>18+</sup> 1460 1461on(type: 'discoveryResult', callback: Callback<Array<DiscoveryResult>>): void 1462 1463Subscribes to Bluetooth scan result reporting events. This API uses an asynchronous callback to return the result.<br> 1464- This API applies to both classic Bluetooth devices and BLE devices.<br> 1465- The reported information includes the device address, signal strength, name, and type. 1466 1467**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1468 1469**System capability**: SystemCapability.Communication.Bluetooth.Core 1470 1471**Parameters** 1472 1473| Name | Type | Mandatory | Description | 1474| -------- | ----------------------------------- | ---- | -------------------------------------- | 1475| type | string | Yes | Event type. The value **discoveryResult** indicates a scan result reporting event. A device scan starts when [connection.startBluetoothDiscovery](#connectionstartbluetoothdiscovery) is called. If a device is found, this event is triggered.| 1476| callback | Callback<Array<[DiscoveryResult](#discoveryresult18)>> | Yes | Callback used to return the set of scan results. | 1477 1478**Error codes** 1479 1480For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1481 1482| ID| Error Message| 1483| -------- | ---------------------------- | 1484|201 | Permission denied. | 1485|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1486|801 | Capability not supported. | 1487|2900099 | Operation failed. | 1488 1489**Example** 1490 1491```js 1492import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1493let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data is an array of Bluetooth devices discovered. 1494 console.info('bluetooth device find = '+ JSON.stringify(data)); 1495} 1496try { 1497 connection.on('discoveryResult', onReceiveEvent); 1498} catch (err) { 1499 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1500} 1501``` 1502 1503 1504## connection.off('discoveryResult')<sup>18+</sup> 1505 1506off(type: 'discoveryResult', callback?: Callback<Array<DiscoveryResult>>): void 1507 1508Unsubscribes from the Bluetooth device discovered. 1509 1510**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1511 1512**System capability**: SystemCapability.Communication.Bluetooth.Core 1513 1514**Parameters** 1515 1516| Name | Type | Mandatory | Description | 1517| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1518| type | string | Yes | Event type. The value **discoveryResult** indicates a scan result reporting event. | 1519| callback | Callback<Array<[DiscoveryResult](#discoveryresult18)>> | No | Callback used to return the result.<br>If this parameter is specified, it must be the same as the callback in [connection.on('discoveryResult')](#connectionondiscoveryresult18). If this parameter is not specified, all callbacks corresponding to the event type are unsubscribed.| 1520 1521**Error codes** 1522 1523For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1524 1525| ID| Error Message| 1526| -------- | ---------------------------- | 1527|201 | Permission denied. | 1528|801 | Capability not supported. | 1529|2900099 | Operation failed. | 1530 1531**Example** 1532 1533```js 1534import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1535let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data is an array of Bluetooth devices discovered. 1536 console.info('bluetooth device find = '+ JSON.stringify(data)); 1537} 1538try { 1539 connection.on('discoveryResult', onReceiveEvent); 1540 connection.off('discoveryResult', onReceiveEvent); 1541} catch (err) { 1542 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1543} 1544``` 1545 1546 1547## connection.getLastConnectionTime<sup>15+</sup> 1548 1549getLastConnectionTime(deviceId: string): Promise<number> 1550 1551Obtains the latest connection time of the peer Bluetooth device. This API uses a promise to return the result. 1552 1553**System capability**: SystemCapability.Communication.Bluetooth.Core 1554 1555**Parameters** 1556 1557| Name | Type | Mandatory | Description | 1558| ------ | ------- | ---- | -------------------------------- | 1559| deviceId | string | Yes | MAC address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 1560 1561**Return value** 1562 1563| Type | Description | 1564| ------------------- | ------------- | 1565| Promise<number> | Promise used to return the latest connection time of the peer Bluetooth device.| 1566 1567**Error codes** 1568 1569For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1570 1571| ID| Error Message| 1572| -------- | ---------------------------- | 1573|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1574|801 | Capability not supported. | 1575|2900001 | Service stopped. | 1576|2900003 | Bluetooth disabled. | 1577|2900099 | Operation failed. | 1578 1579**Example** 1580 1581```js 1582import { connection } from '@kit.ConnectivityKit'; 1583// promise 1584try { 1585 connection.getLastConnectionTime('11:22:33:44:55:66').then((time: number) => { 1586 console.info('connectionTime: ${time}'); 1587 }); 1588} catch (err) { 1589 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1590} 1591``` 1592 1593## connection.connectAllowedProfiles<sup>16+</sup> 1594 1595connectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 1596 1597Obtains the profiles supported by the peer device. Supported profiles include A2DP, HFP, and HID. This API uses an asynchronous callback to return the result. 1598- Call [connection.pairDevice](#connectionpairdevice) to initiate pairing first. This API can be called only once within 30 seconds after each pairing is initiated. 1599- Upon successful pairing, you are advised to call [getRemoteProfileUuids](#connectiongetremoteprofileuuids12) to query the profiles supported by the target device. This API is called only if the target device supports the profile required by the application. 1600 1601**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1602 1603**System capability**: SystemCapability.Communication.Bluetooth.Core 1604 1605**Parameters** 1606 1607| Name | Type | Mandatory | Description | 1608| -------- | ------ | ---- | ----------------------------------- | 1609| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 1610| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 1611 1612**Error codes** 1613 1614For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1615 1616| ID| Error Message| 1617| -------- | ---------------------------- | 1618|201 | Permission denied. | 1619|401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1620|801 | Capability not supported. | 1621|2900001 | Service stopped. | 1622|2900003 | Bluetooth disabled. | 1623|2900099 | Operation failed. | 1624 1625**Example** 1626 1627```js 1628import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1629try { 1630 connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 1631 if (err) { 1632 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1633 return; 1634 } 1635 console.info('connectAllowedProfiles'); 1636 }); 1637} catch (err) { 1638 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1639} 1640``` 1641 1642 1643## connection.connectAllowedProfiles<sup>16+</sup> 1644 1645connectAllowedProfiles(deviceId: string): Promise<void> 1646 1647Obtains the profiles supported by the peer device. Supported profiles include A2DP, HFP, and HID. This API uses a promise to return the result. 1648- Call [connection.pairDevice](#connectionpairdevice) to initiate pairing first. This API can be called only once within 30 seconds after each pairing is initiated. 1649- Upon successful pairing, you are advised to call [getRemoteProfileUuids](#connectiongetremoteprofileuuids12) to query the profiles supported by the target device. This API is called only if the target device supports the profile required by the application. 1650 1651**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1652 1653**System capability**: SystemCapability.Communication.Bluetooth.Core 1654 1655**Parameters** 1656 1657| Name | Type | Mandatory | Description | 1658| -------- | ------ | ---- | ----------------------------------- | 1659| deviceId | string | Yes | Address of the peer device, for example, XX:XX:XX:XX:XX:XX.| 1660 1661**Return value** 1662 1663| Type | Description | 1664| ------------------------------------------------- | ------------------- | 1665| Promise<void> | Promise that returns no value.| 1666 1667**Error codes** 1668 1669For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1670 1671| ID| Error Message| 1672| -------- | ---------------------------- | 1673|201 | Permission denied. | 1674|401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1675|801 | Capability not supported. | 1676|2900001 | Service stopped. | 1677|2900003 | Bluetooth disabled. | 1678|2900099 | Operation failed. | 1679 1680**Example** 1681 1682```js 1683import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1684try { 1685 connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 1686 console.info('connectAllowedProfiles'); 1687 }, (err: BusinessError) => { 1688 console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 1689 }); 1690} catch (err) { 1691 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1692} 1693``` 1694 1695## BondStateParam 1696 1697Defines the parameters for the pairing status. 1698 1699**System capability**: SystemCapability.Communication.Bluetooth.Core 1700 1701| Name | Type | Read-Only| Optional | Description | 1702| -------- | ------ | ---- | ---- | ----------- | 1703| deviceId | string | No | No | Address of the peer device.| 1704| state | [BondState](#bondstate) | No | No | Pairing status.| 1705| cause<sup>12+</sup>| [UnbondCause](#unbondcause12) | No| No| Reason why the pairing fails.| 1706 1707 1708## PinRequiredParam 1709 1710Defines the parameters of a pairing request. 1711 1712**System capability**: SystemCapability.Communication.Bluetooth.Core 1713 1714| Name | Type | Read-Only | Optional | Description | 1715| -------- | ------ | ---- | ---- | ----------- | 1716| deviceId | string | No | No | Address of the peer device.| 1717| pinCode | string | No | No | PIN used for pairing. | 1718 1719 1720 1721## DeviceClass 1722 1723Represents the class of a Bluetooth device. 1724 1725**System capability**: SystemCapability.Communication.Bluetooth.Core 1726 1727| Name | Type | Read-Only | Optional | Description | 1728| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 1729| majorClass | [MajorClass](js-apis-bluetooth-constant.md#majorclass) | No | No | Main class. This is a standard field in the Bluetooth protocol. | 1730| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | No | No | Subclass, which is further classified based on the major class. This is a standard field in the Bluetooth protocol.| 1731| classOfDevice | number | No | No | Class of the Bluetooth device. This is a standard field in the Bluetooth protocol. It includes the [MajorClass](js-apis-bluetooth-constant.md#majorclass), [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass), and supported major services. | 1732 1733 1734## BatteryInfo<sup>12+</sup> 1735 1736Describes the battery level of a device.<br>Only devices that support the **Attention** (AT) command (including +XEVENT and IPHONEACCEV) defined by the Bluetooth protocol can report valid battery level information. 1737 1738**System capability**: SystemCapability.Communication.Bluetooth.Core 1739 1740| Name | Type | Read-Only | Optional | Description | 1741| -------- | ------ | ---- | ---- | ----------- | 1742| batteryLevel | number | No | No | Battery level.<br>If the value is **-1**, no battery level information is available. | 1743| leftEarBatteryLevel | number | No | No | Battery level of the left earbud if the device is a Bluetooth earphone.<br>If the value is **-1**, no battery level information is available. | 1744| leftEarChargeState | [DeviceChargeState](#devicechargestate12) | No | No | Charging status of the left earbud if the device is a Bluetooth earbud. | 1745| rightEarBatteryLevel | number | No | No | Battery level of the right earbud if the device is a Bluetooth earphone.<br>If the value is **-1**, no battery level information is available. | 1746| rightEarChargeState | [DeviceChargeState](#devicechargestate12) | No | No | Charging status of the right earbud if the device is a Bluetooth earbud. | 1747| boxBatteryLevel | number | No | No | Battery level of the earbud compartment if the device is a Bluetooth earphone.<br>If the value is **-1**, no battery level information is available. | 1748| boxChargeState | [DeviceChargeState](#devicechargestate12) | No | No | Charging status of the earbud compartment if the device is a Bluetooth earbud. | 1749 1750 1751## BluetoothTransport 1752 1753Enumerates the device transmission modes. 1754 1755**System capability**: SystemCapability.Communication.Bluetooth.Core 1756 1757| Name | Value | Description | 1758| -------------------------------- | ------ | --------------- | 1759| TRANSPORT_BR_EDR | 0 | Legacy Bluetooth basic rate/enhanced data rate (BR/EDR) mode. This mode is used by default if the device supports dual transmission modes.| 1760| TRANSPORT_LE | 1 | BLE mode. | 1761 1762 1763## ScanMode 1764 1765Enumerates the scan modes. The scan mode determines whether the device is discoverable or connectable. 1766 1767**System capability**: SystemCapability.Communication.Bluetooth.Core 1768 1769| Name | Value | Description | 1770| ---------------------------------------- | ---- | --------------- | 1771| SCAN_MODE_NONE | 0 | Undiscoverable and unconnectable mode. | 1772| SCAN_MODE_CONNECTABLE | 1 | Connectable mode. | 1773| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | General discoverable mode, allowing for long-term discovery. | 1774| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | Limited discoverable mode, allowing for discovery within a specific timeframe. | 1775| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | Connectable and general discoverable mode.| 1776| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | Connectable and limited discoverable mode.| 1777 1778 1779## BondState 1780 1781Enumerates the device pairing states. 1782 1783**Atomic service API**: This API can be used in atomic services since API version 12. 1784 1785**System capability**: SystemCapability.Communication.Bluetooth.Core 1786 1787| Name | Value | Description | 1788| ------------------ | ---- | ------ | 1789| BOND_STATE_INVALID | 0 | Unpaired state.| 1790| BOND_STATE_BONDING | 1 | Pairing state. | 1791| BOND_STATE_BONDED | 2 | Paired state. | 1792 1793 1794## UnbondCause<sup>12+</sup> 1795 1796Enumerates the possible causes of a pairing failure. 1797 1798**System capability**: SystemCapability.Communication.Bluetooth.Core 1799 1800| Name | Value | Description | 1801| ------------------ | ---- | ------ | 1802| USER_REMOVED | 0 | The user proactively removes the device. If [BondState](#bondstate) is **BOND_STATE_BONDED**, the pairing is successful.| 1803| REMOTE_DEVICE_DOWN | 1 | The peer device is offline. For example, the Bluetooth of the peer device is disabled.| 1804| AUTH_FAILURE | 2 | Authentication failed. For example, the keys of the devices at both ends do not match.| 1805| AUTH_REJECTED | 3 | Authentication rejected. For example, the peer device rejects the pairing request.| 1806| INTERNAL_ERROR | 4 | Internal error. For example, the device does not support pairing, or the pairing times out.| 1807 1808 1809## DeviceChargeState<sup>12+</sup> 1810 1811Enumerates the device charging states. 1812 1813**System capability**: SystemCapability.Communication.Bluetooth.Core 1814 1815| Name | Value | Description | 1816| ------------------ | ---- | ------ | 1817| DEVICE_NORMAL_CHARGE_NOT_CHARGED | 0 | A device that does not support super-fast charging is currently not charging.| 1818| DEVICE_NORMAL_CHARGE_IN_CHARGING | 1 | A device that does not support super-fast charging is currently charging.| 1819| DEVICE_SUPER_CHARGE_NOT_CHARGED | 2 | A device that supports super-fast charging is currently not charging.| 1820| DEVICE_SUPER_CHARGE_IN_CHARGING | 3 | A device that supports super-fast charging is currently charging.| 1821 1822## DiscoveryResult<sup>18+</sup> 1823 1824Defines the device discovery result. 1825 1826**System capability**: SystemCapability.Communication.Bluetooth.Core 1827 1828| Name | Type | Read-Only | Optional | Description | 1829| -------- | ------ | ---- | ---- | ----------- | 1830| deviceId | string | No | No | Address of the discovered device.<br>For security purposes, the device addresses obtained are virtual MAC addresses.<br>- The virtual address remains unchanged after a device is paired successfully.<br>- If a device is unpaired or Bluetooth is disabled, the virtual address will change after the device is paired again.<br>- To persistently save the addresses, call [access.addPersistentDeviceId](js-apis-bluetooth-access.md#accessaddpersistentdeviceid16).| 1831| rssi | number | No | No | Signal strength, in dBm.| 1832| deviceName | string | No | No | Device name.| 1833| deviceClass | DeviceClass | No | No | Device class.| 1834