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 '@ohos.bluetooth.connection'; 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**System capability**: SystemCapability.Communication.Bluetooth.Core 27 28**Parameters** 29 30| Name | Type | Mandatory | Description | 31| -------- | ------ | ---- | ----------------------------------- | 32| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 33| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the pairing is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 34 35**Error codes** 36 37For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 38 39| ID| Error Message| 40| -------- | ---------------------------- | 41|2900001 | Service stopped. | 42|2900003 | Bluetooth switch is off. | 43|2900099 | Operation failed. | 44 45**Example** 46 47```js 48import { BusinessError } from '@ohos.base'; 49try { 50 // The address can be scanned. 51 connection.pairDevice('XX:XX:XX:XX:XX:XX'); 52} catch (err) { 53 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 54} 55``` 56 57 58## connection.pairDevice 59 60pairDevice(deviceId: string): Promise<void> 61 62Pairs a Bluetooth device. This API uses a promise to return the result. 63 64**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 65 66**System capability**: SystemCapability.Communication.Bluetooth.Core 67 68**Parameters** 69 70| Name | Type | Mandatory | Description | 71| -------- | ------ | ---- | ----------------------------------- | 72| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 73 74**Return value** 75 76| Type | Description | 77| ------------------- | ------------- | 78| Promise<void> | Promise used to return the result.| 79 80**Error codes** 81 82For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 83 84| ID| Error Message| 85| -------- | ---------------------------- | 86|2900001 | Service stopped. | 87|2900003 | Bluetooth switch is off. | 88|2900099 | Operation failed. | 89 90**Example** 91 92```js 93import { BusinessError } from '@ohos.base'; 94try { 95 // The address can be scanned. 96 connection.pairDevice('XX:XX:XX:XX:XX:XX'); 97} catch (err) { 98 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 99} 100``` 101 102 103## connection.pairCredibleDevice 104 105pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void 106 107Pairs a trusted device whose address is obtained in a non-Bluetooth scan mode (such as using NFC). This API uses an asynchronous callback to return the result. 108 109**System API**: This is a system API. 110 111**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 112 113**System capability**: SystemCapability.Communication.Bluetooth.Core 114 115**Parameters** 116 117| Name | Type | Mandatory | Description | 118| -------- | ------ | ---- | ----------------------------------- | 119| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 120| transport | [BluetoothTransport](#bluetoothtransport) | Yes | Device type, for example, a classic Bluetooth device or a Bluetooth low energy (BLE) device.| 121| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 122 123**Error codes** 124 125For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 126 127| ID| Error Message| 128| -------- | ---------------------------- | 129|2900001 | Service stopped. | 130|2900003 | Bluetooth switch is off. | 131|2900099 | Operation failed. | 132 133**Example** 134 135```js 136import { BusinessError } from '@ohos.base'; 137try { 138 connection.pairCredibleDevice('68:13:24:79:4C:8C', connection.BluetoothTransport 139 .TRANSPORT_BR_EDR, (err: BusinessError) => { 140 if (err) { 141 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 142 return; 143 } 144 console.info('pairCredibleDevice, err: ' + JSON.stringify(err)); 145 }); 146} catch (err) { 147 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 148} 149``` 150 151 152## connection.pairCredibleDevice 153 154pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void> 155 156Pairs a trusted device whose address is obtained in a non-Bluetooth scan mode (such as using NFC). This API uses a promise to return the result. 157 158**System API**: This is a system API. 159 160**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 161 162**System capability**: SystemCapability.Communication.Bluetooth.Core 163 164**Parameters** 165 166| Name | Type | Mandatory | Description | 167| -------- | ------ | ---- | ----------------------------------- | 168| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 169| transport | [BluetoothTransport](#bluetoothtransport) | Yes | Device type, for example, a classic Bluetooth device or a BLE device.| 170 171**Return value** 172 173| Type | Description | 174| ------------------------------------------------- | ------------------- | 175| Promise<void> | Promise used to return the result.| 176 177**Error codes** 178 179For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 180 181| ID| Error Message| 182| -------- | ---------------------------- | 183|2900001 | Service stopped. | 184|2900003 | Bluetooth switch is off. | 185|2900099 | Operation failed. | 186 187**Example** 188 189```js 190import { BusinessError } from '@ohos.base'; 191try { 192 connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => { 193 console.info('PairCredibleDevice'); 194 }, (err: BusinessError) => { 195 console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message); 196 }); 197} catch (err) { 198 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 199} 200``` 201 202 203## connection.cancelPairedDevice 204 205cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void 206 207Cancels a paired device. 208 209**System API**: This is a system API. 210 211**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 212 213**System capability**: SystemCapability.Communication.Bluetooth.Core 214 215**Parameters** 216 217| Name | Type | Mandatory | Description | 218| -------- | ------ | ---- | ------------------------------------- | 219| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 220| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 221 222**Error codes** 223 224For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 225 226| ID| Error Message| 227| -------- | ---------------------------- | 228|2900001 | Service stopped. | 229|2900003 | Bluetooth switch is off. | 230|2900099 | Operation failed. | 231 232**Example** 233 234```js 235import { BusinessError } from '@ohos.base'; 236try { 237 connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX'); 238} catch (err) { 239 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 240} 241``` 242 243 244## connection.cancelPairedDevice 245 246cancelPairedDevice(deviceId: string): Promise<void> 247 248Cancels a paired device. This API uses a promise to return the result. 249 250**System API**: This is a system API. 251 252**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 253 254**System capability**: SystemCapability.Communication.Bluetooth.Core 255 256**Parameters** 257 258| Name | Type | Mandatory | Description | 259| -------- | ------ | ---- | ------------------------------------- | 260| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 261 262**Return value** 263 264| Type | Description | 265| ------------------- | ------------- | 266| Promise<void> | Promise used to return the result.| 267 268**Error codes** 269 270For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 271 272| ID| Error Message| 273| -------- | ---------------------------- | 274|2900001 | Service stopped. | 275|2900003 | Bluetooth switch is off. | 276|2900099 | Operation failed. | 277 278**Example** 279 280```js 281import { BusinessError } from '@ohos.base'; 282try { 283 connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX'); 284} catch (err) { 285 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 286} 287``` 288 289 290## connection.cancelPairingDevice 291 292cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void 293 294Cancels the pairing of a device. This API uses an asynchronous callback to return the result. 295 296**System API**: This is a system API. 297 298**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 299 300**System capability**: SystemCapability.Communication.Bluetooth.Core 301 302**Parameters** 303 304| Name | Type | Mandatory | Description | 305| -------- | ------ | ---- | ------------------------------------- | 306| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 307| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 308 309**Error codes** 310 311For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 312 313| ID| Error Message| 314| -------- | ---------------------------- | 315|2900001 | Service stopped. | 316|2900003 | Bluetooth switch is off. | 317|2900099 | Operation failed. | 318 319**Example** 320 321```js 322import { BusinessError } from '@ohos.base'; 323try { 324 connection.cancelPairingDevice('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.cancelPairingDevice 332 333cancelPairingDevice(deviceId: string): Promise<void> 334 335Cancels the pairing of a device. This API uses a promise to return the result. 336 337**System API**: This is a system API. 338 339**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 340 341**System capability**: SystemCapability.Communication.Bluetooth.Core 342 343**Parameters** 344 345| Name | Type | Mandatory | Description | 346| -------- | ------ | ---- | ------------------------------------- | 347| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 348 349**Return value** 350 351| Type | Description | 352| ------------------- | ------------- | 353| Promise<void> | Promise used to return the result.| 354 355**Error codes** 356 357For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 358 359| ID| Error Message| 360| -------- | ---------------------------- | 361|2900001 | Service stopped. | 362|2900003 | Bluetooth switch is off. | 363|2900099 | Operation failed. | 364 365**Example** 366 367```js 368import { BusinessError } from '@ohos.base'; 369try { 370 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 371} catch (err) { 372 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 373} 374``` 375 376 377## connection.getRemoteDeviceName 378 379getRemoteDeviceName(deviceId: string): string 380 381Obtains the name of a remote Bluetooth device. 382 383**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 384 385**System capability**: SystemCapability.Communication.Bluetooth.Core 386 387**Parameters** 388 389| Name | Type | Mandatory | Description | 390| -------- | ------ | ---- | --------------------------------- | 391| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 392 393**Return value** 394 395| Type | Description | 396| ------ | ------------- | 397| string | Device name (a string) obtained.| 398 399**Error codes** 400 401For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 402 403| ID| Error Message| 404| -------- | ---------------------------- | 405|2900001 | Service stopped. | 406|2900003 | Bluetooth switch is off. | 407|2900099 | Operation failed. | 408 409**Example** 410 411```js 412import { BusinessError } from '@ohos.base'; 413try { 414 let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX'); 415} catch (err) { 416 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 417} 418``` 419 420 421## connection.getRemoteDeviceClass 422 423getRemoteDeviceClass(deviceId: string): DeviceClass 424 425Obtains the class of a remote Bluetooth device. 426 427**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 428 429**System capability**: SystemCapability.Communication.Bluetooth.Core 430 431**Parameters** 432 433| Name | Type | Mandatory | Description | 434| -------- | ------ | ---- | --------------------------------- | 435| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 436 437**Return value** 438 439| Type | Description | 440| --------------------------- | -------- | 441| [DeviceClass](#deviceclass) | Class of the remote device obtained.| 442 443**Error codes** 444 445For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 446 447| ID| Error Message| 448| -------- | ---------------------------- | 449|2900001 | Service stopped. | 450|2900003 | Bluetooth switch is off. | 451|2900099 | Operation failed. | 452 453**Example** 454 455```js 456import { BusinessError } from '@ohos.base'; 457try { 458 let remoteDeviceClass: connection.DeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX'); 459} catch (err) { 460 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 461} 462``` 463 464 465## connection.getLocalName 466 467getLocalName(): string 468 469Obtains the name of the local Bluetooth device. 470 471**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 472 473**System capability**: SystemCapability.Communication.Bluetooth.Core 474 475**Return value** 476 477| Type | Description | 478| ------ | --------- | 479| string | Name of the local Bluetooth device obtained.| 480 481**Error codes** 482 483For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 484 485| ID| Error Message| 486| -------- | ---------------------------- | 487|2900001 | Service stopped. | 488|2900099 | Operation failed. | 489 490**Example** 491 492```js 493import { BusinessError } from '@ohos.base'; 494try { 495 let localName: string = connection.getLocalName(); 496} catch (err) { 497 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 498} 499``` 500 501 502## connection.getPairedDevices 503 504getPairedDevices(): Array<string> 505 506Obtains the paired devices. 507 508**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 509 510**System capability**: SystemCapability.Communication.Bluetooth.Core 511 512**Return value** 513 514| Type | Description | 515| ------------------- | ------------- | 516| Array<string> | Addresses of the paired Bluetooth devices obtained. | 517 518**Error codes** 519 520For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 521 522| ID| Error Message| 523| -------- | ---------------------------- | 524|2900001 | Service stopped. | 525|2900003 | Bluetooth switch is off. | 526|2900099 | Operation failed. | 527 528**Example** 529 530```js 531import { BusinessError } from '@ohos.base'; 532try { 533 let devices: Array<string> = connection.getPairedDevices(); 534} catch (err) { 535 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 536} 537``` 538 539 540## connection.getPairState<sup>11+</sup> 541 542getPairState(deviceId: string): BondState 543 544Obtains the Bluetooth pairing state. 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 | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 555 556**Return value** 557 558| Type | Description | 559| --------------------------- | -------- | 560| [BondState](#bondstate) | Bluetooth pairing state obtained.| 561 562**Error codes** 563 564For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 565 566| ID| Error Message| 567| -------- | ---------------------------- | 568|2900001 | Service stopped. | 569|2900003 | Bluetooth switch is off. | 570|2900099 | Operation failed. | 571 572**Example** 573 574```js 575import { BusinessError } from '@ohos.base'; 576try { 577 let res: connection.BondState = connection.getPairState("XX:XX:XX:XX:XX:XX"); 578 console.log('getPairState: ' + res); 579} catch (err) { 580 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 581} 582``` 583 584 585## connection.getProfileConnectionState 586 587getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState 588 589Obtains 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. 590 591**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 592 593**System capability**: SystemCapability.Communication.Bluetooth.Core 594 595**Parameters** 596 597| Name | Type | Mandatory | Description | 598| --------- | --------- | ---- | ------------------------------------- | 599| ProfileId | [profileId](js-apis-bluetooth-constant.md#profileid) | No | ID of the target profile, for example, **PROFILE_A2DP_SOURCE**.| 600 601**Return value** 602 603| Type | Description | 604| ------------------------------------------------- | ------------------- | 605| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.| 606 607**Error codes** 608 609For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 610 611| ID| Error Message| 612| -------- | ---------------------------- | 613|2900001 | Service stopped. | 614|2900003 | Bluetooth switch is off. | 615|2900004 | Profile is not supported. | 616|2900099 | Operation failed. | 617 618**Example** 619 620```js 621import { BusinessError } from '@ohos.base'; 622import constant from '@ohos.bluetooth.constant'; 623try { 624 let result: connection.ProfileConnectionState = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE); 625} catch (err) { 626 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 627} 628``` 629 630 631## connection.setDevicePairingConfirmation 632 633setDevicePairingConfirmation(deviceId: string, accept: boolean): void 634 635Sets the device pairing confirmation. 636 637**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 638 639**System capability**: SystemCapability.Communication.Bluetooth.Core 640 641**Parameters** 642 643| Name | Type | Mandatory | Description | 644| ------ | ------- | ---- | -------------------------------- | 645| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 646| 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. | 647 648**Error codes** 649 650For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 651 652| ID| Error Message| 653| -------- | ---------------------------- | 654|2900001 | Service stopped. | 655|2900003 | Bluetooth switch is off. | 656|2900099 | Operation failed. | 657 658**Example** 659 660```js 661import { BusinessError } from '@ohos.base'; 662// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device. 663function onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data is the input parameter for the pairing request. 664 console.info('pin required = '+ JSON.stringify(data)); 665 connection.setDevicePairingConfirmation(data.deviceId, true); 666} 667try { 668 connection.on('pinRequired', onReceivePinRequiredEvent); 669} catch (err) { 670 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 671} 672``` 673 674 675## connection.setDevicePinCode 676 677setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void 678 679Sets the PIN for the device when [PinType](#pintype) is **PIN_TYPE_ENTER_PIN_CODE** or **PIN_TYPE_PIN_16_DIGITS**. This API uses an asynchronous callback to return the result. 680 681**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 682 683**System capability**: SystemCapability.Communication.Bluetooth.Core 684 685**Parameters** 686 687| Name | Type | Mandatory | Description | 688| ------ | ------- | ---- | -------------------------------- | 689| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 690| code | string | Yes | PIN to set. | 691| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 692 693**Error codes** 694 695For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 696 697| ID| Error Message| 698| -------- | ---------------------------- | 699|2900001 | Service stopped. | 700|2900003 | Bluetooth switch is off. | 701|2900099 | Operation failed. | 702 703**Example** 704 705```js 706import { BusinessError } from '@ohos.base'; 707//callback 708try { 709 connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => { 710 console.info('setDevicePinCode,device name err:' + JSON.stringify(err)); 711 }); 712} catch (err) { 713 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 714} 715``` 716 717 718## connection.setDevicePinCode 719 720setDevicePinCode(deviceId: string, code: string): Promise<void> 721 722Sets the PIN for the device when [PinType](#pintype) is **PIN_TYPE_ENTER_PIN_CODE** or **PIN_TYPE_PIN_16_DIGITS**. This API uses a promise to return the result. 723 724**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 725 726**System capability**: SystemCapability.Communication.Bluetooth.Core 727 728**Parameters** 729 730| Name | Type | Mandatory | Description | 731| ------ | ------- | ---- | -------------------------------- | 732| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 733| code | string | Yes | PIN to set. | 734 735**Return value** 736 737| Type | Description | 738| ------------------- | ------------- | 739| Promise<void> | Promise used to return the result.| 740 741**Error codes** 742 743For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 744 745| ID| Error Message| 746| -------- | ---------------------------- | 747|2900001 | Service stopped. | 748|2900003 | Bluetooth switch is off. | 749|2900099 | Operation failed. | 750 751**Example** 752 753```js 754import { BusinessError } from '@ohos.base'; 755//promise 756try { 757 connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => { 758 console.info('setDevicePinCode'); 759 }, (error: BusinessError) => { 760 console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message); 761 }) 762 763} catch (err) { 764 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 765} 766``` 767 768 769## connection.setLocalName 770 771setLocalName(name: string): void 772 773Sets the name of the local Bluetooth device. 774 775**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 776 777**System capability**: SystemCapability.Communication.Bluetooth.Core 778 779**Parameters** 780 781| Name | Type | Mandatory | Description | 782| ---- | ------ | ---- | --------------------- | 783| name | string | Yes | Bluetooth device name to set. It cannot exceed 248 bytes.| 784 785**Error codes** 786 787For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 788 789| ID| Error Message| 790| -------- | ---------------------------- | 791|2900001 | Service stopped. | 792|2900003 | Bluetooth switch is off. | 793|2900099 | Operation failed. | 794 795**Example** 796 797```js 798import { BusinessError } from '@ohos.base'; 799try { 800 connection.setLocalName('device_name'); 801} catch (err) { 802 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 803} 804``` 805 806 807## connection.setBluetoothScanMode 808 809setBluetoothScanMode(mode: ScanMode, duration: number): void 810 811Sets the Bluetooth scan mode so that the device can be discovered by a remote device. 812 813**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 814 815**System capability**: SystemCapability.Communication.Bluetooth.Core 816 817**Parameters** 818 819| Name | Type | Mandatory | Description | 820| -------- | --------------------- | ---- | ---------------------------- | 821| mode | [ScanMode](#scanmode) | Yes | Bluetooth scan mode to set. | 822| duration | number | Yes | Duration (in ms) in which the device can be discovered. The value **0** indicates unlimited time.| 823 824**Error codes** 825 826For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 827 828| ID| Error Message| 829| -------- | ---------------------------- | 830|2900001 | Service stopped. | 831|2900003 | Bluetooth switch is off. | 832|2900099 | Operation failed. | 833 834**Example** 835 836```js 837import { BusinessError } from '@ohos.base'; 838try { 839 // The device can be discovered and connected only when the discoverable and connectable mode is used. 840 connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 841} catch (err) { 842 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 843} 844``` 845 846 847## connection.getBluetoothScanMode 848 849getBluetoothScanMode(): ScanMode 850 851Obtains the Bluetooth scan mode. 852 853**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 854 855**System capability**: SystemCapability.Communication.Bluetooth.Core 856 857**Return value** 858 859| Type | Description | 860| --------------------- | ------- | 861| [ScanMode](#scanmode) | Bluetooth scan mode obtained. | 862 863**Error codes** 864 865For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 866 867| ID| Error Message| 868| -------- | ---------------------------- | 869|2900001 | Service stopped. | 870|2900003 | Bluetooth switch is off. | 871|2900099 | Operation failed. | 872 873**Example** 874 875```js 876import { BusinessError } from '@ohos.base'; 877try { 878 let scanMode: connection.ScanMode = connection.getBluetoothScanMode(); 879} catch (err) { 880 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 881} 882``` 883 884 885## connection.startBluetoothDiscovery 886 887startBluetoothDiscovery(): void 888 889Starts discovery of Bluetooth devices. 890 891**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 892 893**System capability**: SystemCapability.Communication.Bluetooth.Core 894 895**Error codes** 896 897For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 898 899| ID| Error Message| 900| -------- | ---------------------------- | 901|2900001 | Service stopped. | 902|2900003 | Bluetooth switch is off. | 903|2900099 | Operation failed. | 904 905**Example** 906 907```js 908import { BusinessError } from '@ohos.base'; 909function onReceiveEvent(data: Array<string>) { 910 console.log('data length' + data.length); 911} 912try { 913 connection.on('bluetoothDeviceFind', onReceiveEvent); 914 connection.startBluetoothDiscovery(); 915} catch (err) { 916 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 917} 918``` 919 920 921## connection.stopBluetoothDiscovery 922 923stopBluetoothDiscovery(): void 924 925Stops discovery of Bluetooth devices. 926 927**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 928 929**System capability**: SystemCapability.Communication.Bluetooth.Core 930 931**Error codes** 932 933For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 934 935| ID| Error Message| 936| -------- | ---------------------------- | 937|2900001 | Service stopped. | 938|2900003 | Bluetooth switch is off. | 939|2900099 | Operation failed. | 940 941**Example** 942 943```js 944import { BusinessError } from '@ohos.base'; 945try { 946 connection.stopBluetoothDiscovery(); 947} catch (err) { 948 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 949} 950``` 951 952 953## connection.isBluetoothDiscovering<sup>11+</sup> 954 955isBluetoothDiscovering(): boolean 956 957Checks whether Bluetooth discovery is enabled. 958 959**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 960 961**System capability**: SystemCapability.Communication.Bluetooth.Core 962 963**Return value** 964 965| Type | Description | 966| ------------------- | ------------- | 967| boolean | Returns **true** if Bluetooth discovery is enabled; returns **false** otherwise.| 968 969**Error codes** 970 971For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 972 973| ID| Error Message| 974| -------- | ---------------------------- | 975|2900001 | Service stopped. | 976|2900003 | Bluetooth switch is off. | 977|2900099 | Operation failed. | 978 979**Example** 980 981```js 982import { BusinessError } from '@ohos.base'; 983try { 984 let res: boolean = connection.isBluetoothDiscovering(); 985 console.log('isBluetoothDiscovering: ' + res); 986} catch (err) { 987 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 988} 989``` 990 991 992## connection.getLocalProfileUuids 993 994getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void 995 996Obtains the profile UUIDs of the local device. This API uses an asynchronous callback to return the result. 997 998**System API**: This is a system API. 999 1000**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1001 1002**System capability**: SystemCapability.Communication.Bluetooth.Core 1003 1004**Parameters** 1005 1006| Name | Type | Mandatory | Description | 1007| -------- | ------ | ---- | ----------------------------------- | 1008| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Yes | Callback invoked to return the profile UUIDs obtained. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 1009 1010**Error codes** 1011 1012For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1013 1014| ID| Error Message| 1015| -------- | ---------------------------- | 1016|2900001 | Service stopped. | 1017|2900003 | Bluetooth switch is off. | 1018|2900099 | Operation failed. | 1019 1020**Example** 1021 1022```js 1023import { BusinessError } from '@ohos.base'; 1024try { 1025 connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => { 1026 console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 1027 }); 1028} catch (err) { 1029 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1030} 1031``` 1032 1033 1034## connection.getLocalProfileUuids 1035 1036getLocalProfileUuids(): Promise<Array<ProfileUuids>> 1037 1038Obtains the profile UUIDs of the local device. This API uses a promise to return the result. 1039 1040**System API**: This is a system API. 1041 1042**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1043 1044**System capability**: SystemCapability.Communication.Bluetooth.Core 1045 1046**Return value** 1047 1048| Type | Description | 1049| ------------------- | ------------- | 1050| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Promise used to return the profile UUIDs obtained.| 1051 1052**Error codes** 1053 1054For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1055 1056| ID| Error Message| 1057| -------- | ---------------------------- | 1058|2900001 | Service stopped. | 1059|2900003 | Bluetooth switch is off. | 1060|2900099 | Operation failed. | 1061 1062**Example** 1063 1064```js 1065import { BusinessError } from '@ohos.base'; 1066try { 1067 connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { 1068 console.info('getLocalProfileUuids'); 1069 }, (err: BusinessError) => { 1070 console.error('getLocalProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1071 }); 1072} catch (err) { 1073 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1074} 1075``` 1076 1077 1078## connection.getRemoteProfileUuids 1079 1080getRemoteProfileUuids(deviceId: string, callback: AsyncCallback<Array<ProfileUuids>>): void 1081 1082Obtains the profile UUIDs of a remote Bluetooth device. This API uses an asynchronous callback to return the result. 1083 1084**System API**: This is a system API. 1085 1086**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1087 1088**System capability**: SystemCapability.Communication.Bluetooth.Core 1089 1090**Parameters** 1091 1092| Name | Type | Mandatory | Description | 1093| -------- | ------ | ---- | ----------------------------------- | 1094| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 1095| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Yes | Callback invoked to return the profile UUIDs obtained. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 1096 1097**Error codes** 1098 1099For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1100 1101| ID| Error Message| 1102| -------- | ---------------------------- | 1103|2900001 | Service stopped. | 1104|2900003 | Bluetooth switch is off. | 1105|2900099 | Operation failed. | 1106 1107**Example** 1108 1109```js 1110import { BusinessError } from '@ohos.base'; 1111try { 1112 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => { 1113 console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 1114 }); 1115} catch (err) { 1116 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1117} 1118 1119``` 1120 1121 1122## connection.getRemoteProfileUuids 1123 1124getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>> 1125 1126Obtains the profile UUIDs of a remote Bluetooth device. This API uses a promise to return the result. 1127 1128**System API**: This is a system API. 1129 1130**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1131 1132**System capability**: SystemCapability.Communication.Bluetooth.Core 1133 1134**Parameters** 1135 1136| Name | Type | Mandatory | Description | 1137| -------- | ------ | ---- | ----------------------------------- | 1138| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 1139 1140**Return value** 1141 1142| Type | Description | 1143| ------------------- | ------------- | 1144| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Promise used to return the profile UUIDs obtained.| 1145 1146**Error codes** 1147 1148For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1149 1150| ID| Error Message| 1151| -------- | ---------------------------- | 1152|2900001 | Service stopped. | 1153|2900003 | Bluetooth switch is off. | 1154|2900099 | Operation failed. | 1155 1156**Example** 1157 1158```js 1159import { BusinessError } from '@ohos.base'; 1160try { 1161 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { 1162 console.info('getRemoteProfileUuids'); 1163 }, (err: BusinessError) => { 1164 console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1165 }); 1166} catch (err) { 1167 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1168} 1169``` 1170 1171 1172## connection.connectAllowedProfiles<sup>11+</sup> 1173 1174connectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 1175 1176Connects all profiles allowed for a remote device. This API uses an asynchronous callback to return the result. 1177 1178**System API**: This is a system API. 1179 1180**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 1181 1182**System capability**: SystemCapability.Communication.Bluetooth.Core 1183 1184**Parameters** 1185 1186| Name | Type | Mandatory | Description | 1187| -------- | ------ | ---- | ----------------------------------- | 1188| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 1189| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 1190 1191**Error codes** 1192 1193For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1194 1195| ID| Error Message| 1196| -------- | ---------------------------- | 1197|201 | Permission denied. | 1198|202 | Non-system applications are not allowed to use system APIs. | 1199|401 | Invalid parameter. | 1200|801 | Capability not supported. | 1201|2900001 | Service stopped. | 1202|2900003 | Bluetooth switch is off. | 1203|2900099 | Operation failed. | 1204 1205**Example** 1206 1207```js 1208import { BusinessError } from '@ohos.base'; 1209try { 1210 connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 1211 if (err) { 1212 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1213 return; 1214 } 1215 console.info('connectAllowedProfiles, err: ' + JSON.stringify(err)); 1216 }); 1217} catch (err) { 1218 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1219} 1220``` 1221 1222 1223## connection.connectAllowedProfiles<sup>11+</sup> 1224 1225connectAllowedProfiles(deviceId: string): Promise<void> 1226 1227Connects all profiles allowed for a remote device. This API uses a promise to return the result. 1228 1229**System API**: This is a system API. 1230 1231**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 1232 1233**System capability**: SystemCapability.Communication.Bluetooth.Core 1234 1235**Parameters** 1236 1237| Name | Type | Mandatory | Description | 1238| -------- | ------ | ---- | ----------------------------------- | 1239| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 1240 1241**Return value** 1242 1243| Type | Description | 1244| ------------------------------------------------- | ------------------- | 1245| Promise<void> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| 1246 1247**Error codes** 1248 1249For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1250 1251| ID| Error Message| 1252| -------- | ---------------------------- | 1253|201 | Permission denied. | 1254|202 | Non-system applications are not allowed to use system APIs. | 1255|401 | Invalid parameter. | 1256|801 | Capability not supported. | 1257|2900001 | Service stopped. | 1258|2900003 | Bluetooth switch is off. | 1259|2900099 | Operation failed. | 1260 1261**Example** 1262 1263```js 1264import { BusinessError } from '@ohos.base'; 1265try { 1266 connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 1267 console.info('connectAllowedProfiles'); 1268 }, (err: BusinessError) => { 1269 console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 1270 }); 1271} catch (err) { 1272 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1273} 1274``` 1275 1276 1277## connection.disconnectAllowedProfiles<sup>11+</sup> 1278 1279disconnectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 1280 1281Disconnects all connected profiles for a remote device. 1282 1283**System API**: This is a system API. 1284 1285**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 1286 1287**System capability**: SystemCapability.Communication.Bluetooth.Core 1288 1289**Parameters** 1290 1291| Name | Type | Mandatory | Description | 1292| -------- | ------ | ---- | ----------------------------------- | 1293| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 1294| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. This API uses an asynchronous callback to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 1295 1296**Error codes** 1297 1298For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1299 1300| ID| Error Message| 1301| -------- | ---------------------------- | 1302|201 | Permission denied. | 1303|202 | Non-system applications are not allowed to use system APIs. | 1304|401 | Invalid parameter. | 1305|801 | Capability not supported. | 1306|2900001 | Service stopped. | 1307|2900003 | Bluetooth switch is off. | 1308|2900099 | Operation failed. | 1309 1310**Example** 1311 1312```js 1313import { BusinessError } from '@ohos.base'; 1314try { 1315 connection.disconnectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 1316 if (err) { 1317 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1318 return; 1319 } 1320 console.info('disconnectAllowedProfiles, err: ' + JSON.stringify(err)); 1321 }); 1322} catch (err) { 1323 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1324} 1325``` 1326 1327 1328## connection.disconnectAllowedProfiles<sup>11+</sup> 1329 1330disconnectAllowedProfiles(deviceId: string): Promise<void> 1331 1332Disconnects all connected profiles for a remote device. This API uses a promise to return the result. 1333 1334**System API**: This is a system API. 1335 1336**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 1337 1338**System capability**: SystemCapability.Communication.Bluetooth.Core 1339 1340**Parameters** 1341 1342| Name | Type | Mandatory | Description | 1343| -------- | ------ | ---- | ----------------------------------- | 1344| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 1345 1346**Return value** 1347 1348| Type | Description | 1349| ------------------------------------------------- | ------------------- | 1350| Promise<void> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| 1351 1352**Error codes** 1353 1354For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1355 1356| ID| Error Message| 1357| -------- | ---------------------------- | 1358|201 | Permission denied. | 1359|202 | Non-system applications are not allowed to use system APIs. | 1360|401 | Invalid parameter. | 1361|801 | Capability not supported. | 1362|2900001 | Service stopped. | 1363|2900003 | Bluetooth switch is off. | 1364|2900099 | Operation failed. | 1365 1366**Example** 1367 1368```js 1369import { BusinessError } from '@ohos.base'; 1370try { 1371 connection.disconnectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 1372 console.info('disconnectAllowedProfiles'); 1373 }, (err: BusinessError) => { 1374 console.error('disconnectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 1375 }); 1376} catch (err) { 1377 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1378} 1379``` 1380 1381 1382## connection.getRemoteProductId<sup>11+</sup> 1383 1384getRemoteProductId(deviceId: string): string 1385 1386Obtains the product ID of a remote Bluetooth device. 1387 1388**System API**: This is a system API. 1389 1390**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 1391 1392**System capability**: SystemCapability.Communication.Bluetooth.Core 1393 1394**Parameters** 1395 1396| Name | Type | Mandatory | Description | 1397| -------- | ------ | ---- | --------------------------------- | 1398| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 1399 1400**Return value** 1401 1402| Type | Description | 1403| ------ | ------------- | 1404| string | Product ID obtained.| 1405 1406**Error codes** 1407 1408For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1409 1410| ID| Error Message| 1411| -------- | ---------------------------- | 1412|2900001 | Service stopped. | 1413|2900003 | Bluetooth switch is off. | 1414|2900099 | Operation failed. | 1415 1416**Example** 1417 1418```js 1419import connection from '@ohos.bluetooth.connection'; 1420try { 1421 let remoteDeviceProductId = connection.getRemoteProductId('XX:XX:XX:XX:XX:XX'); 1422} catch (err) { 1423 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 1424} 1425``` 1426 1427## connection.on('bluetoothDeviceFind') 1428 1429on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void 1430 1431Subscribes to the discovery of a Bluetooth device. 1432 1433**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1434 1435**System capability**: SystemCapability.Communication.Bluetooth.Core 1436 1437**Parameters** 1438 1439| Name | Type | Mandatory | Description | 1440| -------- | ----------------------------------- | ---- | -------------------------------------- | 1441| type | string | Yes | Event type. The value is **bluetoothDeviceFind**, which indicates an event of discovering a Bluetooth device.| 1442| callback | Callback<Array<string>> | Yes | Callback invoked to return the discovered devices. You need to implement this callback. | 1443 1444**Error codes** 1445 1446For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1447 1448| ID| Error Message| 1449| -------- | ---------------------------- | 1450|2900099 | Operation failed. | 1451 1452**Example** 1453 1454```js 1455import { BusinessError } from '@ohos.base'; 1456function onReceiveEvent(data: Array<string>) { // data is an array of Bluetooth device addresses. 1457 console.info('bluetooth device find = '+ JSON.stringify(data)); 1458} 1459try { 1460 connection.on('bluetoothDeviceFind', onReceiveEvent); 1461} catch (err) { 1462 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1463} 1464``` 1465 1466 1467## connection.off('bluetoothDeviceFind') 1468 1469off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void 1470 1471Unsubscribes from the discovery of a Bluetooth device. 1472 1473**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1474 1475**System capability**: SystemCapability.Communication.Bluetooth.Core 1476 1477**Parameters** 1478 1479| Name | Type | Mandatory | Description | 1480| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1481| type | string | Yes | Event type. The value is **bluetoothDeviceFind**, which indicates an event of discovering a Bluetooth device. | 1482| callback | Callback<Array<string>> | No | Callback for the **bluetoothDeviceFind** event. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.| 1483 1484**Error codes** 1485 1486For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1487 1488| ID| Error Message| 1489| -------- | ---------------------------- | 1490|2900099 | Operation failed. | 1491 1492**Example** 1493 1494```js 1495import { BusinessError } from '@ohos.base'; 1496function onReceiveEvent(data: Array<string>) { 1497 console.info('bluetooth device find = '+ JSON.stringify(data)); 1498} 1499try { 1500 connection.on('bluetoothDeviceFind', onReceiveEvent); 1501 connection.off('bluetoothDeviceFind', onReceiveEvent); 1502} catch (err) { 1503 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1504} 1505``` 1506 1507 1508## connection.on('bondStateChange') 1509 1510on(type: 'bondStateChange', callback: Callback<BondStateParam>): void 1511 1512Subscribes to Bluetooth pairing state changes. 1513 1514**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1515 1516**System capability**: SystemCapability.Communication.Bluetooth.Core 1517 1518**Parameters** 1519 1520| Name | Type | Mandatory | Description | 1521| -------- | ---------------------------------------- | ---- | ------------------------------------ | 1522| type | string | Yes | Event type. The value is **bondStateChange**, which indicates a Bluetooth pairing state change event.| 1523| callback | Callback<[BondStateParam](#bondstateparam)> | Yes | Callback invoked to return the pairing state. You need to implement this callback. | 1524 1525**Error codes** 1526 1527For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1528 1529| ID| Error Message| 1530| -------- | ---------------------------- | 1531|2900099 | Operation failed. | 1532 1533**Example** 1534 1535```js 1536import { BusinessError } from '@ohos.base'; 1537function onReceiveEvent(data: connection.BondStateParam) { // data, as the input parameter of the callback, indicates the pairing state. 1538 console.info('pair state = '+ JSON.stringify(data)); 1539} 1540try { 1541 connection.on('bondStateChange', onReceiveEvent); 1542} catch (err) { 1543 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1544} 1545``` 1546 1547 1548## connection.off('bondStateChange') 1549 1550off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void 1551 1552Unsubscribes from Bluetooth pairing state changes. 1553 1554**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1555 1556**System capability**: SystemCapability.Communication.Bluetooth.Core 1557 1558**Parameters** 1559 1560| Name | Type | Mandatory | Description | 1561| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1562| type | string | Yes | Event type. The value is **bondStateChange**, which indicates a Bluetooth pairing state change event. | 1563| callback | Callback<[BondStateParam](#bondstateparam)> | No | Callback for the change of the Bluetooth pairing state. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.| 1564 1565**Error codes** 1566 1567For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1568 1569| ID| Error Message| 1570| -------- | ---------------------------- | 1571|2900099 | Operation failed. | 1572 1573**Example** 1574 1575```js 1576import { BusinessError } from '@ohos.base'; 1577function onReceiveEvent(data: connection.BondStateParam) { 1578 console.info('bond state = '+ JSON.stringify(data)); 1579} 1580try { 1581 connection.on('bondStateChange', onReceiveEvent); 1582 connection.off('bondStateChange', onReceiveEvent); 1583} catch (err) { 1584 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1585} 1586``` 1587 1588 1589## connection.on('pinRequired') 1590 1591on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void 1592 1593Subscribes to the pairing request events of the remote Bluetooth device. 1594 1595**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1596 1597**System capability**: SystemCapability.Communication.Bluetooth.Core 1598 1599**Parameters** 1600 1601| Name | Type | Mandatory | Description | 1602| -------- | ---------------------------------------- | ---- | -------------------------------- | 1603| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 1604| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | Yes | Callback invoked to return the pairing request. You need to implement this callback.| 1605 1606**Error codes** 1607 1608For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1609 1610| ID| Error Message| 1611| -------- | ---------------------------- | 1612|2900099 | Operation failed. | 1613 1614**Example** 1615 1616```js 1617import { BusinessError } from '@ohos.base'; 1618function onReceiveEvent(data: connection.PinRequiredParam) { // data is the pairing request parameter. 1619 console.info('pin required = '+ JSON.stringify(data)); 1620} 1621try { 1622 connection.on('pinRequired', onReceiveEvent); 1623} catch (err) { 1624 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1625} 1626``` 1627 1628 1629## connection.off('pinRequired') 1630 1631off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void 1632 1633Unsubscribes from the pairing request events of the remote Bluetooth device. 1634 1635**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 1636 1637**System capability**: SystemCapability.Communication.Bluetooth.Core 1638 1639**Parameters** 1640 1641| Name | Type | Mandatory | Description | 1642| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1643| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 1644| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | No | Callback for the Bluetooth pairing request event. The input parameter is the pairing request parameter. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.| 1645 1646**Error codes** 1647 1648For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1649 1650| ID| Error Message| 1651| -------- | ---------------------------- | 1652|2900099 | Operation failed. | 1653 1654**Example** 1655 1656```js 1657import { BusinessError } from '@ohos.base'; 1658function onReceiveEvent(data: connection.PinRequiredParam) { 1659 console.info('pin required = '+ JSON.stringify(data)); 1660} 1661try { 1662 connection.on('pinRequired', onReceiveEvent); 1663 connection.off('pinRequired', onReceiveEvent); 1664} catch (err) { 1665 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1666} 1667``` 1668 1669 1670## BondStateParam 1671 1672Represents the pairing state parameters. 1673 1674**System capability**: SystemCapability.Communication.Bluetooth.Core 1675 1676| Name | Type | Readable | Writable | Description | 1677| -------- | ------ | ---- | ---- | ----------- | 1678| deviceId | string | Yes | No | ID of the device to pair.| 1679| state | BondState | Yes | No | State of the device.| 1680 1681 1682## PinRequiredParam 1683 1684Represents the pairing request parameters. 1685 1686**System capability**: SystemCapability.Communication.Bluetooth.Core 1687 1688| Name | Type | Readable | Writable | Description | 1689| -------- | ------ | ---- | ---- | ----------- | 1690| deviceId | string | Yes | No | ID of the device to pair.| 1691| pinCode | string | Yes | No | Key for the device pairing. | 1692| pinType | [PinType](#pintype) | Yes | No | Type of the device to pair.<br>This is a system API. | 1693 1694 1695## DeviceClass 1696 1697Represents the class of a Bluetooth device. 1698 1699**System capability**: SystemCapability.Communication.Bluetooth.Core 1700 1701| Name | Type | Readable | Writable | Description | 1702| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 1703| majorClass | [MajorClass](js-apis-bluetooth-constant.md#majorclass) | Yes | No | Major class of the Bluetooth device. | 1704| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | Yes | No | Major and minor classes of the Bluetooth device.| 1705| classOfDevice | number | Yes | No | Class of the device. | 1706 1707 1708## BluetoothTransport 1709 1710Enumerates the device types. The default device type is **TRANSPORT_BR_EDR**. 1711 1712**System capability**: SystemCapability.Communication.Bluetooth.Core 1713 1714| Name | Value | Description | 1715| -------------------------------- | ------ | --------------- | 1716| TRANSPORT_BR_EDR | 0 | Classic Bluetooth (BR/EDR) device.| 1717| TRANSPORT_LE | 1 | BLE device. | 1718 1719 1720## ScanMode 1721 1722Enumerates the scan modes. 1723 1724**System capability**: SystemCapability.Communication.Bluetooth.Core 1725 1726| Name | Value | Description | 1727| ---------------------------------------- | ---- | --------------- | 1728| SCAN_MODE_NONE | 0 | No scan mode. | 1729| SCAN_MODE_CONNECTABLE | 1 | Connectable mode. | 1730| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | General discoverable mode. | 1731| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | Limited discoverable mode. | 1732| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | General connectable and discoverable mode.| 1733| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | Limited connectable and discoverable mode.| 1734 1735 1736## BondState 1737 1738Enumerates the pairing states. 1739 1740**System capability**: SystemCapability.Communication.Bluetooth.Core 1741 1742| Name | Value | Description | 1743| ------------------ | ---- | ------ | 1744| BOND_STATE_INVALID | 0 | Invalid pairing.| 1745| BOND_STATE_BONDING | 1 | Pairing. | 1746| BOND_STATE_BONDED | 2 | Paired. | 1747 1748 1749## PinType 1750 1751Enumerates the Bluetooth pairing types. 1752 1753**System API**: This is a system API. 1754 1755**System capability**: SystemCapability.Communication.Bluetooth.Core 1756 1757| Name | Value | Description | 1758| -------------------------------- | ------ | --------------- | 1759| PIN_TYPE_ENTER_PIN_CODE | 0 | The user needs to enter the PIN displayed on the peer device.<br>This is a system API.| 1760| PIN_TYPE_ENTER_PASSKEY | 1 | The user needs to enter the PASSKEY displayed on the peer device.<br>This is a system API. | 1761| PIN_TYPE_CONFIRM_PASSKEY | 2 | The user needs to confirm the PASSKEY displayed on the local device.<br>This is a system API. | 1762| PIN_TYPE_NO_PASSKEY_CONSENT | 3 | There is no PASSKEY, and the user needs to accept or reject the pairing request.<br>This is a system API. | 1763| PIN_TYPE_NOTIFY_PASSKEY | 4 | The user needs to enter the PASSKEY displayed on the local device on the peer device.<br>This is a system API. | 1764| PIN_TYPE_DISPLAY_PIN_CODE | 5 | The user needs to enter the PIN displayed on the peer device for Bluetooth 2.0 devices.<br>This is a system API. | 1765| PIN_TYPE_OOB_CONSENT | 6 | The user needs to accept or reject the out of band (OOB) pairing request.<br>This is a system API. | 1766| PIN_TYPE_PIN_16_DIGITS | 7 | The user needs to enter the 16-digit PIN displayed on the peer device.<br>This is a system API. | 1767