1# @ohos.bluetooth.connection (Bluetooth Connection Module) 2 3The **connection** module provides APIs for operating and managing Bluetooth. 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## connection.pairDevice 19 20pairDevice(deviceId: string, callback: AsyncCallback<void>): void 21 22Pairs a Bluetooth device. This API uses an asynchronous callback to return the result. 23 24**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 25 26**Atomic service API**: This API can be used in atomic services since API version 12. 27 28**System capability**: SystemCapability.Communication.Bluetooth.Core 29 30**Parameters** 31 32| Name | Type | Mandatory | Description | 33| -------- | ------ | ---- | ----------------------------------- | 34| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 35| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the pairing is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 36 37**Error codes** 38 39For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 40 41| ID| Error Message| 42| -------- | ---------------------------- | 43|201 | Permission denied. | 44|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 45|801 | Capability not supported. | 46|2900001 | Service stopped. | 47|2900003 | Bluetooth disabled. | 48|2900099 | Operation failed. | 49 50**Example** 51 52```js 53import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 54//callback 55try { 56 connection.pairDevice('11:22:33:44:55:66', (err: BusinessError) => { 57 console.info('pairDevice, device name err:' + JSON.stringify(err)); 58 }); 59} catch (err) { 60 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 61} 62 63``` 64 65 66## connection.pairDevice 67 68pairDevice(deviceId: string): Promise<void> 69 70Pairs a Bluetooth device. This API uses a promise to return the result. 71 72**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 73 74**Atomic service API**: This API can be used in atomic services since API version 12. 75 76**System capability**: SystemCapability.Communication.Bluetooth.Core 77 78**Parameters** 79 80| Name | Type | Mandatory | Description | 81| -------- | ------ | ---- | ----------------------------------- | 82| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 83 84**Return value** 85 86| Type | Description | 87| ------------------- | ------------- | 88| Promise<void> | Promise used to return the result.| 89 90**Error codes** 91 92For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 93 94| ID| Error Message| 95| -------- | ---------------------------- | 96|201 | Permission denied. | 97|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 98|801 | Capability not supported. | 99|2900001 | Service stopped. | 100|2900003 | Bluetooth disabled. | 101|2900099 | Operation failed. | 102 103**Example** 104 105```js 106import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 107//promise 108try { 109 connection.pairDevice('11:22:33:44:55:66').then(() => { 110 console.info('pairDevice'); 111 }, (error: BusinessError) => { 112 console.info('pairDevice: errCode:' + error.code + ',errMessage' + error.message); 113 }) 114 115} catch (err) { 116 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 117} 118``` 119 120 121## connection.getRemoteDeviceName 122 123getRemoteDeviceName(deviceId: string): string 124 125Obtains the name of a remote Bluetooth device. 126 127**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 128 129**Atomic service API**: This API can be used in atomic services since API version 12. 130 131**System capability**: SystemCapability.Communication.Bluetooth.Core 132 133**Parameters** 134 135| Name | Type | Mandatory | Description | 136| -------- | ------ | ---- | --------------------------------- | 137| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 138 139**Return value** 140 141| Type | Description | 142| ------ | ------------- | 143| string | Device name (a string) obtained.| 144 145**Error codes** 146 147For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 148 149| ID| Error Message| 150| -------- | ---------------------------- | 151|201 | Permission denied. | 152|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 153|801 | Capability not supported. | 154|2900001 | Service stopped. | 155|2900003 | Bluetooth disabled. | 156|2900099 | Operation failed. | 157 158**Example** 159 160```js 161import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 162try { 163 let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX'); 164} catch (err) { 165 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 166} 167``` 168 169 170## connection.getRemoteDeviceClass 171 172getRemoteDeviceClass(deviceId: string): DeviceClass 173 174Obtains the class of a remote Bluetooth device. 175 176**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 177 178**System capability**: SystemCapability.Communication.Bluetooth.Core 179 180**Parameters** 181 182| Name | Type | Mandatory | Description | 183| -------- | ------ | ---- | --------------------------------- | 184| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 185 186**Return value** 187 188| Type | Description | 189| --------------------------- | -------- | 190| [DeviceClass](#deviceclass) | Class of the remote device obtained.| 191 192**Error codes** 193 194For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 195 196| ID| Error Message| 197| -------- | ---------------------------- | 198|201 | Permission denied. | 199|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 200|801 | Capability not supported. | 201|2900001 | Service stopped. | 202|2900003 | Bluetooth disabled. | 203|2900099 | Operation failed. | 204 205**Example** 206 207```js 208import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 209try { 210 let remoteDeviceClass: connection.DeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX'); 211} catch (err) { 212 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 213} 214``` 215 216 217## connection.getRemoteProfileUuids<sup>12+</sup> 218 219getRemoteProfileUuids(deviceId: string, callback: AsyncCallback<Array<ProfileUuids>>): void 220 221Obtains the profile UUIDs of a remote Bluetooth device. This API uses an asynchronous callback to return the result. 222 223**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 224 225**System capability**: SystemCapability.Communication.Bluetooth.Core 226 227**Parameters** 228 229| Name | Type | Mandatory | Description | 230| -------- | ------ | ---- | ----------------------------------- | 231| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 232| 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**. Otherwise, **err** is an error object.| 233 234**Error codes** 235 236For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 237 238| ID| Error Message| 239| -------- | ---------------------------- | 240|201 | Permission denied. | 241|401 | Invalid parameter. | 242|801 | Capability not supported. | 243|2900001 | Service stopped. | 244|2900003 | Bluetooth disabled. | 245|2900099 | Operation failed. | 246 247**Example** 248 249```js 250import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 251try { 252 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => { 253 console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 254 }); 255} catch (err) { 256 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 257} 258 259``` 260 261 262## connection.getRemoteProfileUuids<sup>12+</sup> 263 264getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>> 265 266Obtains the profile UUIDs of a remote Bluetooth device. This API uses a promise to return the result. 267 268**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 269 270**System capability**: SystemCapability.Communication.Bluetooth.Core 271 272**Parameters** 273 274| Name | Type | Mandatory | Description | 275| -------- | ------ | ---- | ----------------------------------- | 276| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 277 278**Return value** 279 280| Type | Description | 281| ------------------- | ------------- | 282| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids12)>> | Promise used to return the result.| 283 284**Error codes** 285 286For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 287 288| ID| Error Message| 289| -------- | ---------------------------- | 290|201 | Permission denied. | 291|401 | Invalid parameter. | 292|801 | Capability not supported. | 293|2900001 | Service stopped. | 294|2900003 | Bluetooth disabled. | 295|2900099 | Operation failed. | 296 297**Example** 298 299```js 300import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 301try { 302 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { 303 console.info('getRemoteProfileUuids'); 304 }, (err: BusinessError) => { 305 console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 306 }); 307} catch (err) { 308 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 309} 310``` 311 312 313## connection.getLocalName 314 315getLocalName(): string 316 317Obtains the name of the local Bluetooth device. 318 319**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 320 321**System capability**: SystemCapability.Communication.Bluetooth.Core 322 323**Return value** 324 325| Type | Description | 326| ------ | --------- | 327| string | Name of the local Bluetooth device obtained.| 328 329**Error codes** 330 331For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 332 333| ID| Error Message| 334| -------- | ---------------------------- | 335|201 | Permission denied. | 336|801 | Capability not supported. | 337|2900001 | Service stopped. | 338|2900099 | Operation failed. | 339 340**Example** 341 342```js 343import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 344try { 345 let localName: string = connection.getLocalName(); 346} catch (err) { 347 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 348} 349``` 350 351 352## connection.getPairedDevices 353 354getPairedDevices(): Array<string> 355 356Obtains the paired devices. 357 358**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 359 360**Atomic service API**: This API can be used in atomic services since API version 12. 361 362**System capability**: SystemCapability.Communication.Bluetooth.Core 363 364**Return value** 365 366| Type | Description | 367| ------------------- | ------------- | 368| Array<string> | Addresses of the paired Bluetooth devices. For security purposes, the device addresses obtained are random MAC addresses. The random MAC address remains unchanged after a device is paired successfully. It changes when the paired device is unpaired and scanned again or the Bluetooth service is turned off.| 369 370**Error codes** 371 372For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 373 374| ID| Error Message| 375| -------- | ---------------------------- | 376|201 | Permission denied. | 377|801 | Capability not supported. | 378|2900001 | Service stopped. | 379|2900003 | Bluetooth disabled. | 380|2900099 | Operation failed. | 381 382**Example** 383 384```js 385import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 386try { 387 let devices: Array<string> = connection.getPairedDevices(); 388} catch (err) { 389 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 390} 391``` 392 393 394## connection.getPairState<sup>11+</sup> 395 396getPairState(deviceId: string): BondState 397 398Obtains the Bluetooth pairing state. 399 400**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 401 402**Atomic service API**: This API can be used in atomic services since API version 12. 403 404**System capability**: SystemCapability.Communication.Bluetooth.Core 405 406**Parameters** 407 408| Name | Type | Mandatory | Description | 409| -------- | ------ | ---- | --------------------------------- | 410| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 411 412**Return value** 413 414| Type | Description | 415| --------------------------- | -------- | 416| [BondState](#bondstate) | Bluetooth pairing state obtained.| 417 418**Error codes** 419 420For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 421 422| ID| Error Message| 423| -------- | ---------------------------- | 424|201 | Permission denied. | 425|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 426|801 | Capability not supported. | 427|2900001 | Service stopped. | 428|2900003 | Bluetooth disabled. | 429|2900099 | Operation failed. | 430 431**Example** 432 433```js 434import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 435try { 436 let res: connection.BondState = connection.getPairState("XX:XX:XX:XX:XX:XX"); 437 console.info('getPairState: ' + res); 438} catch (err) { 439 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 440} 441``` 442 443 444## connection.getProfileConnectionState 445 446getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState 447 448Obtains the connection state of a Bluetooth profile. The **ProfileId** parameter is optional. If **ProfileId** is specified, the connection state of the specified profile is returned. If no **ProfileId** is specified, [STATE_CONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate) is returned by any connected profile. If no profile is connected, [STATE_DISCONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate) is returned. 449 450**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 451 452**System capability**: SystemCapability.Communication.Bluetooth.Core 453 454**Parameters** 455 456| Name | Type | Mandatory | Description | 457| --------- | --------- | ---- | ------------------------------------- | 458| profileId | [ProfileId](js-apis-bluetooth-constant.md#profileid) | No | ID of the target profile, for example, **PROFILE_A2DP_SOURCE**.| 459 460**Return value** 461 462| Type | Description | 463| ------------------------------------------------- | ------------------- | 464| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.| 465 466**Error codes** 467 468For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 469 470| ID| Error Message| 471| -------- | ---------------------------- | 472|201 | Permission denied. | 473|401 | Invalid parameter. Possible causes: 1. Incorrect parameter types. | 474|801 | Capability not supported. | 475|2900001 | Service stopped. | 476|2900003 | Bluetooth disabled. | 477|2900004 | Profile not supported. | 478|2900099 | Operation failed. | 479 480**Example** 481 482```js 483import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 484import { constant } from '@kit.ConnectivityKit'; 485try { 486 let result: connection.ProfileConnectionState = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE); 487} catch (err) { 488 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 489} 490``` 491 492 493## connection.setDevicePairingConfirmation 494 495setDevicePairingConfirmation(deviceId: string, accept: boolean): void 496 497Sets the device pairing confirmation. 498 499**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH (available only for system applications) 500 501**System capability**: SystemCapability.Communication.Bluetooth.Core 502 503**Parameters** 504 505| Name | Type | Mandatory | Description | 506| ------ | ------- | ---- | -------------------------------- | 507| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 508| accept | boolean | Yes | Whether to accept the pairing request. The value **true** means to accept the pairing request, and the value **false** means the opposite. | 509 510**Error codes** 511 512For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 513 514| ID| Error Message| 515| -------- | ---------------------------- | 516|201 | Permission denied. | 517|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 518|801 | Capability not supported. | 519|2900001 | Service stopped. | 520|2900003 | Bluetooth disabled. | 521|2900099 | Operation failed. | 522 523**Example** 524 525```js 526import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 527// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device. 528function onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data is the input parameter for the pairing request. 529 console.info('pin required = '+ JSON.stringify(data)); 530 connection.setDevicePairingConfirmation(data.deviceId, true); 531} 532try { 533 connection.on('pinRequired', onReceivePinRequiredEvent); 534} catch (err) { 535 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 536} 537``` 538 539 540## connection.setDevicePinCode 541 542setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void 543 544Sets the PIN for the device when **PinType** is **PIN_TYPE_ENTER_PIN_CODE** or **PIN_TYPE_PIN_16_DIGITS**. This API uses an asynchronous callback to return the result. 545 546**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 547 548**System capability**: SystemCapability.Communication.Bluetooth.Core 549 550**Parameters** 551 552| Name | Type | Mandatory | Description | 553| ------ | ------- | ---- | -------------------------------- | 554| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 555| code | string | Yes | PIN to set. | 556| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 557 558**Error codes** 559 560For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 561 562| ID| Error Message| 563| -------- | ---------------------------- | 564|201 | Permission denied. | 565|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 566|801 | Capability not supported. | 567|2900001 | Service stopped. | 568|2900003 | Bluetooth disabled. | 569|2900099 | Operation failed. | 570 571**Example** 572 573```js 574import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 575//callback 576try { 577 connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => { 578 console.info('setDevicePinCode,device name err:' + JSON.stringify(err)); 579 }); 580} catch (err) { 581 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 582} 583``` 584 585 586## connection.setDevicePinCode 587 588setDevicePinCode(deviceId: string, code: string): Promise<void> 589 590Sets the PIN for the device when **PinType** is **PIN_TYPE_ENTER_PIN_CODE** or **PIN_TYPE_PIN_16_DIGITS**. This API uses a promise to return the result. 591 592**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 593 594**System capability**: SystemCapability.Communication.Bluetooth.Core 595 596**Parameters** 597 598| Name | Type | Mandatory | Description | 599| ------ | ------- | ---- | -------------------------------- | 600| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 601| code | string | Yes | PIN to set. | 602 603**Return value** 604 605| Type | Description | 606| ------------------- | ------------- | 607| Promise<void> | Promise used to return the result.| 608 609**Error codes** 610 611For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 612 613| ID| Error Message| 614| -------- | ---------------------------- | 615|201 | Permission denied. | 616|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 617|801 | Capability not supported. | 618|2900001 | Service stopped. | 619|2900003 | Bluetooth disabled. | 620|2900099 | Operation failed. | 621 622**Example** 623 624```js 625import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 626//promise 627try { 628 connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => { 629 console.info('setDevicePinCode'); 630 }, (error: BusinessError) => { 631 console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message); 632 }) 633 634} catch (err) { 635 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 636} 637``` 638 639 640## connection.setLocalName 641 642setLocalName(name: string): void 643 644Sets the name of the local Bluetooth device. 645 646**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 647 648**System capability**: SystemCapability.Communication.Bluetooth.Core 649 650**Parameters** 651 652| Name | Type | Mandatory | Description | 653| ---- | ------ | ---- | --------------------- | 654| name | string | Yes | Bluetooth device name to set. It cannot exceed 248 bytes.| 655 656**Error codes** 657 658For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 659 660| ID| Error Message| 661| -------- | ---------------------------- | 662|201 | Permission denied. | 663|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 664|801 | Capability not supported. | 665|2900001 | Service stopped. | 666|2900003 | Bluetooth disabled. | 667|2900099 | Operation failed. | 668 669**Example** 670 671```js 672import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 673try { 674 connection.setLocalName('device_name'); 675} catch (err) { 676 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 677} 678``` 679 680 681## connection.setBluetoothScanMode 682 683setBluetoothScanMode(mode: ScanMode, duration: number): void 684 685Sets the Bluetooth scan mode so that the device can be discovered by a remote device. 686 687**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 688 689**System capability**: SystemCapability.Communication.Bluetooth.Core 690 691**Parameters** 692 693| Name | Type | Mandatory | Description | 694| -------- | --------------------- | ---- | ---------------------------- | 695| 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**. | 696| duration | number | Yes | Duration (in ms) in which the device can be discovered. The value **0** indicates unlimited time.| 697 698**Error codes** 699 700For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 701 702| ID| Error Message| 703| -------- | ---------------------------- | 704|201 | Permission denied. | 705|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 706|801 | Capability not supported. | 707|2900001 | Service stopped. | 708|2900003 | Bluetooth disabled. | 709|2900099 | Operation failed. | 710 711**Example** 712 713```js 714import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 715try { 716 // The device can be discovered and connected only when the discoverable and connectable mode is used. 717 connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 718} catch (err) { 719 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 720} 721``` 722 723 724## connection.getBluetoothScanMode 725 726getBluetoothScanMode(): ScanMode 727 728Obtains the Bluetooth scan mode. 729 730**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 731 732**System capability**: SystemCapability.Communication.Bluetooth.Core 733 734**Return value** 735 736| Type | Description | 737| --------------------- | ------- | 738| [ScanMode](#scanmode) | Bluetooth scan mode obtained.| 739 740**Error codes** 741 742For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 743 744| ID| Error Message| 745| -------- | ---------------------------- | 746|201 | Permission denied. | 747|801 | Capability not supported. | 748|2900001 | Service stopped. | 749|2900003 | Bluetooth disabled. | 750|2900099 | Operation failed. | 751 752**Example** 753 754```js 755import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 756try { 757 let scanMode: connection.ScanMode = connection.getBluetoothScanMode(); 758} catch (err) { 759 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 760} 761``` 762 763 764## connection.startBluetoothDiscovery 765 766startBluetoothDiscovery(): void 767 768Starts to discover Bluetooth devices. 769 770**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 771 772**Atomic service API**: This API can be used in atomic services since API version 12. 773 774**System capability**: SystemCapability.Communication.Bluetooth.Core 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|801 | Capability not supported. | 784|2900001 | Service stopped. | 785|2900003 | Bluetooth disabled. | 786|2900099 | Operation failed. | 787 788**Example** 789 790```js 791import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 792function onReceiveEvent(data: Array<string>) { 793 console.info('data length' + data.length); 794} 795try { 796 connection.on('bluetoothDeviceFind', onReceiveEvent); 797 connection.startBluetoothDiscovery(); 798} catch (err) { 799 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 800} 801``` 802 803 804## connection.stopBluetoothDiscovery 805 806stopBluetoothDiscovery(): void 807 808Stops discovering Bluetooth devices. 809 810**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 811 812**Atomic service API**: This API can be used in atomic services since API version 12. 813 814**System capability**: SystemCapability.Communication.Bluetooth.Core 815 816**Error codes** 817 818For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 819 820| ID| Error Message| 821| -------- | ---------------------------- | 822|201 | Permission denied. | 823|801 | Capability not supported. | 824|2900001 | Service stopped. | 825|2900003 | Bluetooth disabled. | 826|2900099 | Operation failed. | 827 828**Example** 829 830```js 831import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 832try { 833 connection.stopBluetoothDiscovery(); 834} catch (err) { 835 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 836} 837``` 838 839 840## connection.isBluetoothDiscovering<sup>11+</sup> 841 842isBluetoothDiscovering(): boolean 843 844Checks whether Bluetooth discovery is enabled. 845 846**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 847 848**System capability**: SystemCapability.Communication.Bluetooth.Core 849 850**Return value** 851 852| Type | Description | 853| ------------------- | ------------- | 854| boolean | Returns **true** if Bluetooth discovery is enabled; returns **false** otherwise.| 855 856**Error codes** 857 858For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 859 860| ID| Error Message| 861| -------- | ---------------------------- | 862|201 | Permission denied. | 863|801 | Capability not supported. | 864|2900001 | Service stopped. | 865|2900003 | Bluetooth disabled. | 866|2900099 | Operation failed. | 867 868**Example** 869 870```js 871import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 872try { 873 let res: boolean = connection.isBluetoothDiscovering(); 874 console.info('isBluetoothDiscovering: ' + res); 875} catch (err) { 876 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 877} 878``` 879 880## connection.setRemoteDeviceName<sup>12+</sup> 881 882setRemoteDeviceName(deviceId: string, name: string): Promise<void> 883 884Sets the name of a remote Bluetooth device. This API uses a promise to return the result. 885 886**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 887 888**Atomic service API**: This API can be used in atomic services since API version 12. 889 890**System capability**: SystemCapability.Communication.Bluetooth.Core 891 892**Parameters** 893 894| Name | Type | Mandatory | Description | 895| -------- | ----------------------------------- | ---- | -------------------------------------- | 896| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 897| name | string | Yes | Name of the remote device to set. The name cannot exceed 64 bytes. | 898 899**Return value** 900 901| Type | Description | 902| ------------------- | ------------- | 903| Promise<void> | Promise used to return the device name set. If the operation fails, an error code is returned.| 904 905**Error codes** 906 907For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 908 909| ID| Error Message| 910| -------- | ---------------------------- | 911|201 | Permission denied. | 912|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 913|2900001 | Service stopped. | 914|2900003 | Bluetooth disabled. | 915 916**Example** 917 918```js 919import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 920//promise 921try { 922 connection.setRemoteDeviceName('11:22:33:44:55:66', 'RemoteDeviceName').then(() => { 923 console.info('setRemoteDeviceName success'); 924 }, (error: BusinessError) => { 925 console.error('setRemoteDeviceName: errCode:' + error.code + ',errMessage' + error.message); 926 }) 927 928} catch (err) { 929 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 930} 931``` 932 933 934## connection.getRemoteDeviceBatteryInfo<sup>12+</sup> 935 936getRemoteDeviceBatteryInfo(deviceId: string): Promise<BatteryInfo> 937 938obtains the battery information of a remote Bluetooth device. This API uses a promise to return the result. 939 940**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 941 942**System capability**: SystemCapability.Communication.Bluetooth.Core 943 944**Parameters** 945 946| Name | Type | Mandatory | Description | 947| ------ | ------- | ---- | -------------------------------- | 948| deviceId | string | Yes | MAC address of the remote device, for example, **11:22:33:AA:BB:FF**.| 949 950**Return value** 951 952| Type | Description | 953| ------------------- | ------------- | 954| Promise<[BatteryInfo](#batteryinfo12)> | Promise used to return the battery information obtained.| 955 956**Error codes** 957 958For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 959 960| ID| Error Message| 961| -------- | ---------------------------- | 962|201 | Permission denied. | 963|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 964|2900001 | Service stopped. | 965|2900003 | Bluetooth disabled. | 966 967**Example** 968 969```js 970import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 971// promise 972try { 973 connection.getRemoteDeviceBatteryInfo('11:22:33:AA:BB:FF').then((data: connection.BatteryInfo) => { 974 console.info('getRemoteDeviceBatteryInfo success, DeviceType:' + JSON.stringify(data)); 975 }); 976} catch (err) { 977 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 978} 979``` 980 981 982## connection.on('batteryChange')<sup>12+</sup> 983 984on(type: 'batteryChange', callback: Callback<BatteryInfo>): void 985 986Subscribes to the changes in the battery information of a remote Bluetooth device. This API uses an asynchronous callback to return the result. 987 988**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 989 990**System capability**: SystemCapability.Communication.Bluetooth.Core 991 992**Parameters** 993 994| Name | Type | Mandatory | Description | 995| -------- | ----------------------------------- | ---- | -------------------------------------- | 996| type | string | Yes | Event type. The value is **'batteryChange'**, which indicates the change in battery information of a remote Bluetooth device.| 997| callback | Callback<[BatteryInfo](#batteryinfo12)> | Yes | Callback used to return the battery information. | 998 999**Error codes** 1000 1001For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1002 1003| ID| Error Message| 1004| -------- | ---------------------------- | 1005|201 | Permission denied. | 1006|2900099 | Operation failed. | 1007 1008**Example** 1009 1010```js 1011import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1012let onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => { 1013 console.info('BatteryInfo = '+ JSON.stringify(data)); 1014} 1015try { 1016 connection.on('batteryChange', onReceiveEvent); 1017} catch (err) { 1018 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1019} 1020``` 1021 1022 1023## connection.off('batteryChange')<sup>12+</sup> 1024 1025off(type: 'batteryChange', callback?: Callback<BatteryInfo>): void 1026 1027Unsubscribes from the changes in the battery information of a remote Bluetooth device. 1028 1029**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1030 1031**System capability**: SystemCapability.Communication.Bluetooth.Core 1032 1033**Parameters** 1034 1035| Name | Type | Mandatory | Description | 1036| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1037| type | string | Yes | Event type. The value is **'batteryChange'**, which indicates the change in battery information of a remote Bluetooth device. | 1038| callback | Callback<[BatteryInfo](#batteryinfo12)> | No | Callback to unregister.| 1039 1040**Error codes** 1041 1042For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1043 1044| ID| Error Message| 1045| -------- | ---------------------------- | 1046|201 | Permission denied. | 1047|2900099 | Operation failed. | 1048 1049**Example** 1050 1051```js 1052import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1053let onReceiveEvent: (data: connection.BatteryInfo) => void = (data: connection.BatteryInfo) => { 1054 console.info('BatteryInfo = '+ JSON.stringify(data)); 1055} 1056try { 1057 connection.on('batteryChange', onReceiveEvent); 1058 connection.off('batteryChange', onReceiveEvent); 1059} catch (err) { 1060 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1061} 1062``` 1063 1064 1065## connection.on('bluetoothDeviceFind') 1066 1067on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void 1068 1069Subscribes to the discovery of a Bluetooth device. This API uses an asynchronous callback to return the result. 1070 1071**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1072 1073**Atomic service API**: This API can be used in atomic services since API version 12. 1074 1075**System capability**: SystemCapability.Communication.Bluetooth.Core 1076 1077**Parameters** 1078 1079| Name | Type | Mandatory | Description | 1080| -------- | ----------------------------------- | ---- | -------------------------------------- | 1081| type | string | Yes | Event type. The value is **bluetoothDeviceFind**, which indicates an event of discovering a Bluetooth device.| 1082| callback | Callback<Array<string>> | Yes | Callback used to return the discovered devices. You need to implement this callback. For security purposes, the device addresses are random MAC addresses. The random MAC address remains unchanged after a device is paired successfully. It changes when the paired device is unpaired and scanned again or the Bluetooth service is turned off. | 1083 1084**Error codes** 1085 1086For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1087 1088| ID| Error Message| 1089| -------- | ---------------------------- | 1090|201 | Permission denied. | 1091|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1092|801 | Capability not supported. | 1093|2900099 | Operation failed. | 1094 1095**Example** 1096 1097```js 1098import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1099function onReceiveEvent(data: Array<string>) { // data is an array of Bluetooth device addresses. 1100 console.info('bluetooth device find = '+ JSON.stringify(data)); 1101} 1102try { 1103 connection.on('bluetoothDeviceFind', onReceiveEvent); 1104} catch (err) { 1105 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1106} 1107``` 1108 1109 1110## connection.off('bluetoothDeviceFind') 1111 1112off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void 1113 1114Unsubscribes from the discovery of a Bluetooth device. 1115 1116**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1117 1118**Atomic service API**: This API can be used in atomic services since API version 12. 1119 1120**System capability**: SystemCapability.Communication.Bluetooth.Core 1121 1122**Parameters** 1123 1124| Name | Type | Mandatory | Description | 1125| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1126| type | string | Yes | Event type. The value is **bluetoothDeviceFind**, which indicates an event of discovering a Bluetooth device. | 1127| callback | Callback<Array<string>> | No | Callback to unregister. If this parameter is not set, this API unregisters all callbacks for the specified **type**.| 1128 1129**Error codes** 1130 1131For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1132 1133| ID| Error Message| 1134| -------- | ---------------------------- | 1135|201 | Permission denied. | 1136|801 | Capability not supported. | 1137|2900099 | Operation failed. | 1138 1139**Example** 1140 1141```js 1142import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1143function onReceiveEvent(data: Array<string>) { 1144 console.info('bluetooth device find = '+ JSON.stringify(data)); 1145} 1146try { 1147 connection.on('bluetoothDeviceFind', onReceiveEvent); 1148 connection.off('bluetoothDeviceFind', onReceiveEvent); 1149} catch (err) { 1150 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1151} 1152``` 1153 1154 1155## connection.on('bondStateChange') 1156 1157on(type: 'bondStateChange', callback: Callback<BondStateParam>): void 1158 1159Subscribes to Bluetooth pairing state changes. This API uses an asynchronous callback to return the result. 1160 1161**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1162 1163**System capability**: SystemCapability.Communication.Bluetooth.Core 1164 1165**Parameters** 1166 1167| Name | Type | Mandatory | Description | 1168| -------- | ---------------------------------------- | ---- | ------------------------------------ | 1169| type | string | Yes | Event type. The value is **bondStateChange**, which indicates a Bluetooth pairing state change event.| 1170| callback | Callback<[BondStateParam](#bondstateparam)> | Yes | Callback used to return the pairing state. You need to implement this callback. | 1171 1172**Error codes** 1173 1174For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1175 1176| ID| Error Message| 1177| -------- | ---------------------------- | 1178|201 | Permission denied. | 1179|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1180|801 | Capability not supported. | 1181|2900099 | Operation failed. | 1182 1183**Example** 1184 1185```js 1186import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1187function onReceiveEvent(data: connection.BondStateParam) { // data, as the input parameter of the callback, indicates the pairing state. 1188 console.info('pair state = '+ JSON.stringify(data)); 1189} 1190try { 1191 connection.on('bondStateChange', onReceiveEvent); 1192} catch (err) { 1193 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1194} 1195``` 1196 1197 1198## connection.off('bondStateChange') 1199 1200off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void 1201 1202Unsubscribes from Bluetooth pairing state changes. 1203 1204**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1205 1206**System capability**: SystemCapability.Communication.Bluetooth.Core 1207 1208**Parameters** 1209 1210| Name | Type | Mandatory | Description | 1211| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1212| type | string | Yes | Event type. The value is **bondStateChange**, which indicates a Bluetooth pairing state change event. | 1213| callback | Callback<[BondStateParam](#bondstateparam)> | No | Callback to unregister. If this parameter is not set, this API unregisters all callbacks for the specified **type**.| 1214 1215**Error codes** 1216 1217For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 1218 1219| ID| Error Message| 1220| -------- | ---------------------------- | 1221|201 | Permission denied. | 1222|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1223|801 | Capability not supported. | 1224|2900099 | Operation failed. | 1225 1226**Example** 1227 1228```js 1229import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1230function onReceiveEvent(data: connection.BondStateParam) { 1231 console.info('bond state = '+ JSON.stringify(data)); 1232} 1233try { 1234 connection.on('bondStateChange', onReceiveEvent); 1235 connection.off('bondStateChange', onReceiveEvent); 1236} catch (err) { 1237 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1238} 1239``` 1240 1241 1242## connection.on('pinRequired') 1243 1244on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void 1245 1246Subscribes to the pairing request events of the remote Bluetooth device. This API uses an asynchronous callback to return the result. 1247 1248**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 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 **pinRequired** indicates a pairing request event. | 1257| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | Yes | Callback used to return the pairing request. You need to implement this callback.| 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|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1267|801 | Capability not supported. | 1268|2900099 | Operation failed. | 1269 1270**Example** 1271 1272```js 1273import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1274function onReceiveEvent(data: connection.PinRequiredParam) { // data is the pairing request parameter. 1275 console.info('pin required = '+ JSON.stringify(data)); 1276} 1277try { 1278 connection.on('pinRequired', onReceiveEvent); 1279} catch (err) { 1280 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1281} 1282``` 1283 1284 1285## connection.off('pinRequired') 1286 1287off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void 1288 1289Unsubscribes from the pairing request events of the remote Bluetooth device. 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 **pinRequired** indicates a pairing request event. | 1300| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | No | Callback to unregister. The input parameter is the pairing request parameter. If this parameter is not set, this API unregisters all callbacks for the specified **type**.| 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.PinRequiredParam) { 1318 console.info('pin required = '+ JSON.stringify(data)); 1319} 1320try { 1321 connection.on('pinRequired', onReceiveEvent); 1322 connection.off('pinRequired', onReceiveEvent); 1323} catch (err) { 1324 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1325} 1326``` 1327 1328 1329## BondStateParam 1330 1331Represents the pairing state parameters. 1332 1333**System capability**: SystemCapability.Communication.Bluetooth.Core 1334 1335| Name | Type | Readable | Writable | Description | 1336| -------- | ------ | ---- | ---- | ----------- | 1337| deviceId | string | Yes | No | ID of the device to pair.| 1338| state | BondState | Yes | No | State of the device.| 1339| cause<sup>12+</sup>| [UnbondCause](#unbondcause12) | Yes| No| Cause of the pairing failure.| 1340 1341 1342## PinRequiredParam 1343 1344Represents the pairing request parameters. 1345 1346**System capability**: SystemCapability.Communication.Bluetooth.Core 1347 1348| Name | Type | Readable | Writable | Description | 1349| -------- | ------ | ---- | ---- | ----------- | 1350| deviceId | string | Yes | No | ID of the device to pair.| 1351| pinCode | string | Yes | No | Key for the device pairing. | 1352 1353 1354 1355## DeviceClass 1356 1357Represents the class of a Bluetooth device. 1358 1359**System capability**: SystemCapability.Communication.Bluetooth.Core 1360 1361| Name | Type | Readable | Writable | Description | 1362| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 1363| majorClass | [MajorClass](js-apis-bluetooth-constant.md#majorclass) | Yes | No | Major class of the Bluetooth device. | 1364| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | Yes | No | Major and minor classes of the Bluetooth device.| 1365| classOfDevice | number | Yes | No | Class of the device. | 1366 1367 1368## BatteryInfo<sup>12+</sup> 1369 1370Represents the battery information. 1371 1372**System capability**: SystemCapability.Communication.Bluetooth.Core 1373 1374| Name | Type | Readable | Writable | Description | 1375| -------- | ------ | ---- | ---- | ----------- | 1376| batteryLevel | number | Yes | No | Battery level of the remote device. If the value is **-1**, there is no battery information. | 1377| leftEarBatteryLevel | number | Yes | No | Battery level of the left earphone. If the value is **-1**, there is no battery information. | 1378| leftEarChargeState | [DeviceChargeState](#devicechargestate12) | Yes | No | Charging state of the left earphone. | 1379| rightEarBatteryLevel | number | Yes | No | Battery level of the right earphone. If the value is **-1**, there is no battery information. | 1380| rightEarChargeState | [DeviceChargeState](#devicechargestate12) | Yes | No | Charging state of the right earphone. | 1381| boxBatteryLevel | number | Yes | No | Battery level of the earbud compartment. If the value is **-1**, there is no battery information. | 1382| boxChargeState | [DeviceChargeState](#devicechargestate12) | Yes | No | Charging state of the earbud compartment. | 1383 1384 1385## BluetoothTransport 1386 1387Enumerates the device types. The default device type is **TRANSPORT_BR_EDR**. 1388 1389**System capability**: SystemCapability.Communication.Bluetooth.Core 1390 1391| Name | Value | Description | 1392| -------------------------------- | ------ | --------------- | 1393| TRANSPORT_BR_EDR | 0 | Classic Bluetooth (BR/EDR) device.| 1394| TRANSPORT_LE | 1 | BLE device. | 1395 1396 1397## ScanMode 1398 1399Enumerates the scan modes. 1400 1401**System capability**: SystemCapability.Communication.Bluetooth.Core 1402 1403| Name | Value | Description | 1404| ---------------------------------------- | ---- | --------------- | 1405| SCAN_MODE_NONE | 0 | No scan mode. | 1406| SCAN_MODE_CONNECTABLE | 1 | Connectable mode. | 1407| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | General discoverable mode. | 1408| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | Limited discoverable mode. | 1409| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | General connectable and discoverable mode.| 1410| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | Limited connectable and discoverable mode.| 1411 1412 1413## BondState 1414 1415Enumerates the pairing states. 1416 1417**Atomic service API**: This API can be used in atomic services since API version 12. 1418 1419**System capability**: SystemCapability.Communication.Bluetooth.Core 1420 1421| Name | Value | Description | 1422| ------------------ | ---- | ------ | 1423| BOND_STATE_INVALID | 0 | Invalid pairing.| 1424| BOND_STATE_BONDING | 1 | Pairing. | 1425| BOND_STATE_BONDED | 2 | Paired. | 1426 1427 1428## UnbondCause<sup>12+</sup> 1429 1430Enumerates the possible causes of a pairing failure. 1431 1432**System capability**: SystemCapability.Communication.Bluetooth.Core 1433 1434| Name | Value | Description | 1435| ------------------ | ---- | ------ | 1436| USER_REMOVED | 0 | The user proactively removes the device.| 1437| REMOTE_DEVICE_DOWN | 1 | The remote device is shut down.| 1438| AUTH_FAILURE | 2 | The PIN is incorrect.| 1439| AUTH_REJECTED | 3 | The remote device authentication is rejected.| 1440| INTERNAL_ERROR | 4 | Internal error.| 1441 1442 1443## DeviceChargeState<sup>12+</sup> 1444 1445Enumerates the charging states. 1446 1447**System capability**: SystemCapability.Communication.Bluetooth.Core 1448 1449| Name | Value | Description | 1450| ------------------ | ---- | ------ | 1451| DEVICE_NORMAL_CHARGE_NOT_CHARGED | 0 | The device is not charged and does not support supercharging.| 1452| DEVICE_NORMAL_CHARGE_IN_CHARGING | 1 | The device is being charged and does not support supercharging.| 1453| DEVICE_SUPER_CHARGE_NOT_CHARGED | 2 | The device is not charged and supports supercharging.| 1454| DEVICE_SUPER_CHARGE_IN_CHARGING | 3 | The device is being charged and supports supercharging.| 1455