1# @ohos.bluetooth.connection (Bluetooth Connection Module) (System API) 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> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.bluetooth.connection (Bluetooth Connection Module)](js-apis-bluetooth-connection.md). 9 10 11## Modules to Import 12 13```js 14import { connection } from '@kit.ConnectivityKit'; 15``` 16 17 18## connection.pairCredibleDevice 19 20pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void 21 22Pairs 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. 23 24**System API**: This is a system API. 25 26**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 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| transport | [BluetoothTransport](js-apis-bluetooth-connection.md#bluetoothtransport) | Yes | Device type, for example, a classic Bluetooth device or a Bluetooth low energy (BLE) device.| 36| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 37 38**Error codes** 39 40For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 41 42| ID| Error Message| 43| -------- | ---------------------------- | 44|201 | Permission denied. | 45|202 | Non-system applications are not allowed to use system APIs. | 46|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 47|801 | Capability not supported. | 48|2900001 | Service stopped. | 49|2900003 | Bluetooth disabled. | 50|2900099 | Operation failed. | 51 52**Example** 53 54```js 55import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 56try { 57 connection.pairCredibleDevice('68:13:24:79:4C:8C', connection.BluetoothTransport 58 .TRANSPORT_BR_EDR, (err: BusinessError) => { 59 if (err) { 60 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 61 return; 62 } 63 console.info('pairCredibleDevice, err: ' + JSON.stringify(err)); 64 }); 65} catch (err) { 66 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 67} 68``` 69 70 71## connection.pairCredibleDevice 72 73pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void> 74 75Pairs 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. 76 77**System API**: This is a system API. 78 79**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 80 81**System capability**: SystemCapability.Communication.Bluetooth.Core 82 83**Parameters** 84 85| Name | Type | Mandatory | Description | 86| -------- | ------ | ---- | ----------------------------------- | 87| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 88| transport | [BluetoothTransport](js-apis-bluetooth-connection.md#bluetoothtransport) | Yes | Device type, for example, a classic Bluetooth device or a BLE device.| 89 90**Return value** 91 92| Type | Description | 93| ------------------------------------------------- | ------------------- | 94| Promise<void> | Promise used to return the result.| 95 96**Error codes** 97 98For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 99 100| ID| Error Message| 101| -------- | ---------------------------- | 102|201 | Permission denied. | 103|202 | Non-system applications are not allowed to use system APIs. | 104|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 105|801 | Capability not supported. | 106|2900001 | Service stopped. | 107|2900003 | Bluetooth disabled. | 108|2900099 | Operation failed. | 109 110**Example** 111 112```js 113import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 114try { 115 connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => { 116 console.info('PairCredibleDevice'); 117 }, (err: BusinessError) => { 118 console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message); 119 }); 120} catch (err) { 121 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 122} 123``` 124 125 126## connection.cancelPairedDevice 127 128cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void 129 130Cancels a paired device. This API uses an asynchronous callback to return the result. 131 132**System API**: This is a system API. 133 134**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 135 136**System capability**: SystemCapability.Communication.Bluetooth.Core 137 138**Parameters** 139 140| Name | Type | Mandatory | Description | 141| -------- | ------ | ---- | ------------------------------------- | 142| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 143| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 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|202 | Non-system applications are not allowed to use system APIs. | 153|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 154|801 | Capability not supported. | 155|2900001 | Service stopped. | 156|2900003 | Bluetooth disabled. | 157|2900099 | Operation failed. | 158 159**Example** 160 161```js 162import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 163//callback 164try { 165 connection.cancelPairedDevice('11:22:33:44:55:66', (err: BusinessError) => { 166 console.info('cancelPairedDevice, device name err:' + JSON.stringify(err)); 167 }); 168} catch (err) { 169 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 170} 171``` 172 173 174## connection.cancelPairedDevice 175 176cancelPairedDevice(deviceId: string): Promise<void> 177 178Cancels a paired device. This API uses a promise to return the result. 179 180**System API**: This is a system API. 181 182**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 183 184**System capability**: SystemCapability.Communication.Bluetooth.Core 185 186**Parameters** 187 188| Name | Type | Mandatory | Description | 189| -------- | ------ | ---- | ------------------------------------- | 190| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 191 192**Return value** 193 194| Type | Description | 195| ------------------- | ------------- | 196| Promise<void> | Promise used to return the result.| 197 198**Error codes** 199 200For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 201 202| ID| Error Message| 203| -------- | ---------------------------- | 204|201 | Permission denied. | 205|202 | Non-system applications are not allowed to use system APIs. | 206|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 207|801 | Capability not supported. | 208|2900001 | Service stopped. | 209|2900003 | Bluetooth disabled. | 210|2900099 | Operation failed. | 211 212**Example** 213 214```js 215import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 216//promise 217try { 218 connection.cancelPairedDevice('11:22:33:44:55:66').then(() => { 219 console.info('cancelPairedDevice'); 220 }, (error: BusinessError) => { 221 console.info('cancelPairedDevice: errCode:' + error.code + ',errMessage' + error.message); 222 }) 223 224} catch (err) { 225 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 226} 227``` 228 229 230## connection.cancelPairingDevice 231 232cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void 233 234Cancels the pairing of a device. This API uses an asynchronous callback to return the result. 235 236**System API**: This is a system API. 237 238**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 239 240**System capability**: SystemCapability.Communication.Bluetooth.Core 241 242**Parameters** 243 244| Name | Type | Mandatory | Description | 245| -------- | ------ | ---- | ------------------------------------- | 246| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 247| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 248 249**Error codes** 250 251For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 252 253| ID| Error Message| 254| -------- | ---------------------------- | 255|201 | Permission denied. | 256|202 | Non-system applications are not allowed to use system APIs. | 257|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 258|801 | Capability not supported. | 259|2900001 | Service stopped. | 260|2900003 | Bluetooth disabled. | 261|2900099 | Operation failed. | 262 263**Example** 264 265```js 266import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 267try { 268 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 269} catch (err) { 270 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 271} 272``` 273 274 275## connection.cancelPairingDevice 276 277cancelPairingDevice(deviceId: string): Promise<void> 278 279Cancels the pairing of a device. This API uses a promise to return the result. 280 281**System API**: This is a system API. 282 283**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 284 285**System capability**: SystemCapability.Communication.Bluetooth.Core 286 287**Parameters** 288 289| Name | Type | Mandatory | Description | 290| -------- | ------ | ---- | ------------------------------------- | 291| deviceId | string | Yes | Address of the device to cancel, for example, XX:XX:XX:XX:XX:XX.| 292 293**Return value** 294 295| Type | Description | 296| ------------------- | ------------- | 297| Promise<void> | Promise used to return the result.| 298 299**Error codes** 300 301For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 302 303| ID| Error Message| 304| -------- | ---------------------------- | 305|201 | Permission denied. | 306|202 | Non-system applications are not allowed to use system APIs. | 307|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 308|801 | Capability not supported. | 309|2900001 | Service stopped. | 310|2900003 | Bluetooth disabled. | 311|2900099 | Operation failed. | 312 313**Example** 314 315```js 316import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 317try { 318 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 319} catch (err) { 320 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 321} 322``` 323 324 325## connection.getLocalProfileUuids 326 327getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void 328 329Obtains the profile UUIDs of the local device. This API uses an asynchronous callback to return the result. 330 331**System API**: This is a system API. 332 333**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 334 335**System capability**: SystemCapability.Communication.Bluetooth.Core 336 337**Parameters** 338 339| Name | Type | Mandatory | Description | 340| -------- | ------ | ---- | ----------------------------------- | 341| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Yes | Callback used to return the profile UUIDs obtained. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 342 343**Error codes** 344 345For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 346 347| ID| Error Message| 348| -------- | ---------------------------- | 349|201 | Permission denied. | 350|202 | Non-system applications are not allowed to use system APIs. | 351|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 352|801 | Capability not supported. | 353|2900001 | Service stopped. | 354|2900003 | Bluetooth disabled. | 355|2900099 | Operation failed. | 356 357**Example** 358 359```js 360import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 361try { 362 connection.getLocalProfileUuids((err: BusinessError, data: Array<connection.ProfileUuids>) => { 363 console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 364 }); 365} catch (err) { 366 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 367} 368``` 369 370 371## connection.getLocalProfileUuids 372 373getLocalProfileUuids(): Promise<Array<ProfileUuids>> 374 375Obtains the profile UUIDs of the local device. This API uses a promise to return the result. 376 377**System API**: This is a system API. 378 379**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 380 381**System capability**: SystemCapability.Communication.Bluetooth.Core 382 383**Return value** 384 385| Type | Description | 386| ------------------- | ------------- | 387| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | Promise used to return the result.| 388 389**Error codes** 390 391For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 392 393| ID| Error Message| 394| -------- | ---------------------------- | 395|201 | Permission denied. | 396|202 | Non-system applications are not allowed to use system APIs. | 397|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 398|801 | Capability not supported. | 399|2900001 | Service stopped. | 400|2900003 | Bluetooth disabled. | 401|2900099 | Operation failed. | 402 403**Example** 404 405```js 406import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 407try { 408 connection.getLocalProfileUuids().then(() => { 409 console.info('getLocalProfileUuids'); 410 }, (err: BusinessError) => { 411 console.error('getLocalProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 412 }); 413} catch (err) { 414 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 415} 416``` 417 418 419## connection.connectAllowedProfiles<sup>11+</sup> 420 421connectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 422 423Connects all profiles allowed for a remote device. This API uses an asynchronous callback to return the result. 424 425**System API**: This is a system API. 426 427**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_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 target remote device, for example, XX:XX:XX:XX:XX.| 436| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 437 438**Error codes** 439 440For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 441 442| ID| Error Message| 443| -------- | ---------------------------- | 444|201 | Permission denied. | 445|202 | Non-system applications are not allowed to use system APIs. | 446|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 447|801 | Capability not supported. | 448|2900001 | Service stopped. | 449|2900003 | Bluetooth disabled. | 450|2900099 | Operation failed. | 451 452**Example** 453 454```js 455import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 456try { 457 connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 458 if (err) { 459 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 460 return; 461 } 462 console.info('connectAllowedProfiles, err: ' + JSON.stringify(err)); 463 }); 464} catch (err) { 465 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 466} 467``` 468 469 470## connection.connectAllowedProfiles<sup>11+</sup> 471 472connectAllowedProfiles(deviceId: string): Promise<void> 473 474Connects all profiles allowed for a remote device. This API uses a promise to return the result. 475 476**System API**: This is a system API. 477 478**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 479 480**System capability**: SystemCapability.Communication.Bluetooth.Core 481 482**Parameters** 483 484| Name | Type | Mandatory | Description | 485| -------- | ------ | ---- | ----------------------------------- | 486| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 487 488**Return value** 489 490| Type | Description | 491| ------------------------------------------------- | ------------------- | 492| Promise<void> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| 493 494**Error codes** 495 496For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 497 498| ID| Error Message| 499| -------- | ---------------------------- | 500|201 | Permission denied. | 501|202 | Non-system applications are not allowed to use system APIs. | 502|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 503|801 | Capability not supported. | 504|2900001 | Service stopped. | 505|2900003 | Bluetooth disabled. | 506|2900099 | Operation failed. | 507 508**Example** 509 510```js 511import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 512try { 513 connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 514 console.info('connectAllowedProfiles'); 515 }, (err: BusinessError) => { 516 console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 517 }); 518} catch (err) { 519 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 520} 521``` 522 523 524## connection.disconnectAllowedProfiles<sup>11+</sup> 525 526disconnectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 527 528Disconnects all connected profiles for a remote device. This API uses an asynchronous callback to return the result. 529 530**System API**: This is a system API. 531 532**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 533 534**System capability**: SystemCapability.Communication.Bluetooth.Core 535 536**Parameters** 537 538| Name | Type | Mandatory | Description | 539| -------- | ------ | ---- | ----------------------------------- | 540| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 541| callback | AsyncCallback<void> | Yes | Callback used 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. | 542 543**Error codes** 544 545For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 546 547| ID| Error Message| 548| -------- | ---------------------------- | 549|201 | Permission denied. | 550|202 | Non-system applications are not allowed to use system APIs. | 551|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 552|801 | Capability not supported. | 553|2900001 | Service stopped. | 554|2900003 | Bluetooth disabled. | 555|2900099 | Operation failed. | 556 557**Example** 558 559```js 560import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 561try { 562 connection.disconnectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 563 if (err) { 564 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 565 return; 566 } 567 console.info('disconnectAllowedProfiles, err: ' + JSON.stringify(err)); 568 }); 569} catch (err) { 570 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 571} 572``` 573 574 575## connection.disconnectAllowedProfiles<sup>11+</sup> 576 577disconnectAllowedProfiles(deviceId: string): Promise<void> 578 579Disconnects all connected profiles for a remote device. This API uses a promise to return the result. 580 581**System API**: This is a system API. 582 583**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 584 585**System capability**: SystemCapability.Communication.Bluetooth.Core 586 587**Parameters** 588 589| Name | Type | Mandatory | Description | 590| -------- | ------ | ---- | ----------------------------------- | 591| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX.| 592 593**Return value** 594 595| Type | Description | 596| ------------------------------------------------- | ------------------- | 597| Promise<void> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| 598 599**Error codes** 600 601For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 602 603| ID| Error Message| 604| -------- | ---------------------------- | 605|201 | Permission denied. | 606|202 | Non-system applications are not allowed to use system APIs. | 607|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 608|801 | Capability not supported. | 609|2900001 | Service stopped. | 610|2900003 | Bluetooth disabled. | 611|2900099 | Operation failed. | 612 613**Example** 614 615```js 616import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 617try { 618 connection.disconnectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 619 console.info('disconnectAllowedProfiles'); 620 }, (err: BusinessError) => { 621 console.error('disconnectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 622 }); 623} catch (err) { 624 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 625} 626``` 627 628 629## connection.getRemoteProductId<sup>11+</sup> 630 631getRemoteProductId(deviceId: string): string 632 633Obtains the product ID of a remote Bluetooth device. 634 635**System API**: This is a system API. 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 647**Return value** 648 649| Type | Description | 650| ------ | ------------- | 651| string | Product ID obtained.| 652 653**Error codes** 654 655For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 656 657| ID| Error Message| 658| -------- | ---------------------------- | 659|201 | Permission denied. | 660|202 | Non-system applications are not allowed to use system APIs. | 661|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 662|801 | Capability not supported. | 663|2900001 | Service stopped. | 664|2900003 | Bluetooth disabled. | 665|2900099 | Operation failed. | 666 667**Example** 668 669```js 670try { 671 let remoteDeviceProductId = connection.getRemoteProductId('XX:XX:XX:XX:XX:XX'); 672} catch (err) { 673 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 674} 675``` 676 677## connection.on('discoveryResult')<sup>12+</sup> 678 679on(type: 'discoveryResult', callback: Callback<Array<DiscoveryResult>>): void 680 681Subscribes to the Bluetooth device discovery result. This API uses an asynchronous callback to return the result. 682 683**System API**: This is a system API. 684 685**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.GET_BLUETOOTH_PEERS_MAC 686 687**System capability**: SystemCapability.Communication.Bluetooth.Core 688 689**Parameters** 690 691| Name | Type | Mandatory | Description | 692| -------- | ----------------------------------- | ---- | -------------------------------------- | 693| type | string | Yes | Event type. The value is **discoveryResult**, which indicates information about the Bluetooth devices discovered.| 694| callback | Callback<Array<[DiscoveryResult](#discoveryresult12)>> | Yes | Callback used to return information about the discovered devices. You need to create the callback and register it here. | 695 696**Error codes** 697 698For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 699 700| ID| Error Message| 701| -------- | ---------------------------- | 702|201 | Permission denied. | 703|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 704|801 | Capability not supported. | 705|2900099 | Operation failed. | 706 707**Example** 708 709```js 710import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 711let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data is an array of Bluetooth devices discovered. 712 console.info('bluetooth device find = '+ JSON.stringify(data)); 713} 714try { 715 connection.on('discoveryResult', onReceiveEvent); 716} catch (err) { 717 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 718} 719``` 720 721 722## connection.off('discoveryResult')<sup>12+</sup> 723 724off(type: 'discoveryResult', callback?: Callback<Array<DiscoveryResult>>): void 725 726Unsubscribes from the Bluetooth device discovery result. 727 728**System API**: This is a system API. 729 730**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.GET_BLUETOOTH_PEERS_MAC 731 732**System capability**: SystemCapability.Communication.Bluetooth.Core 733 734**Parameters** 735 736| Name | Type | Mandatory | Description | 737| -------- | ----------------------------------- | ---- | ---------------------------------------- | 738| type | string | Yes | Event type. The value is **discoveryResult**, which indicates information about the Bluetooth devices discovered. | 739| callback | Callback<Array<[DiscoveryResult](#discoveryresult12)>> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for the specified event type.| 740 741**Error codes** 742 743For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 744 745| ID| Error Message| 746| -------- | ---------------------------- | 747|201 | Permission denied. | 748|801 | Capability not supported. | 749|2900099 | Operation failed. | 750 751**Example** 752 753```js 754import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 755let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data is an array of Bluetooth devices discovered. 756 console.info('bluetooth device find = '+ JSON.stringify(data)); 757} 758try { 759 connection.on('discoveryResult', onReceiveEvent); 760 connection.off('discoveryResult', onReceiveEvent); 761} catch (err) { 762 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 763} 764``` 765 766 767## connection.setRemoteDeviceType<sup>12+</sup> 768 769setRemoteDeviceType(deviceId: string, type: DeviceType): Promise<void> 770 771Sets the type of a remote Bluetooth device. This API uses a promise to return the result. 772 773**System API**: This is a system API. 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| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 784| type | [DeviceType](#devicetype12) | Yes | Device type to set. | 785 786**Return value** 787 788| Type | Description | 789| ------------------- | ------------- | 790| Promise<void> | Promise used to return the device type set. If the operation fails, an error code is returned.| 791 792**Error codes** 793 794For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 795 796| ID| Error Message| 797| -------- | ---------------------------- | 798|201 | Permission denied. | 799|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 800|2900001 | Service stopped. | 801|2900003 | Bluetooth disabled. | 802 803**Example** 804 805```js 806import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 807//promise 808try { 809 connection.setRemoteDeviceType('11:22:33:44:55:66', connection.DeviceType.DEVICE_TYPE_HEADSET).then(() => { 810 console.info('setRemoteDeviceType success'); 811 }); 812} catch (err) { 813 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 814} 815``` 816 817 818## connection.getRemoteDeviceType<sup>12+</sup> 819 820getRemoteDeviceType(deviceId: string): Promise<DeviceType> 821 822Obtains the type of a remote Bluetooth device. This API uses a promise to return the result. 823 824**System API**: This is a system API. 825 826**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 827 828**System capability**: SystemCapability.Communication.Bluetooth.Core 829 830**Parameters** 831 832| Name | Type | Mandatory | Description | 833| ------ | ------- | ---- | -------------------------------- | 834| deviceId | string | Yes | MAC address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 835 836**Return value** 837 838| Type | Description | 839| ------------------- | ------------- | 840| Promise<[DeviceType](#devicetype12)> | Promise used to returnthe device type obtained.| 841 842**Error codes** 843 844For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 845 846| ID| Error Message| 847| -------- | ---------------------------- | 848|201 | Permission denied. | 849|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 850|2900001 | Service stopped. | 851|2900003 | Bluetooth disabled. | 852 853**Example** 854 855```js 856import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 857//promise 858try { 859 connection.getRemoteDeviceType('11:22:33:44:55:66').then((data: connection.DeviceType) => { 860 console.info('getRemoteDeviceType success, DeviceType:' + JSON.stringify(data)); 861 }); 862} catch (err) { 863 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 864} 865``` 866 867 868## PinRequiredParam 869 870Represents the pairing request parameters. 871 872**System capability**: SystemCapability.Communication.Bluetooth.Core 873 874| Name | Type | Readable | Writable | Description | 875| -------- | ------ | ---- | ---- | ----------- | 876| pinType | [PinType](#pintype) | Yes | No | Type of the device to pair.<br>This is a system API. | 877 878## PinType 879 880Enumerates the Bluetooth pairing types. 881 882**System API**: This is a system API. 883 884**System capability**: SystemCapability.Communication.Bluetooth.Core 885 886| Name | Value | Description | 887| -------------------------------- | ------ | --------------- | 888| PIN_TYPE_ENTER_PIN_CODE | 0 | The user needs to enter the PIN displayed on the peer device.<br>This is a system API.| 889| PIN_TYPE_ENTER_PASSKEY | 1 | The user needs to enter the PASSKEY displayed on the peer device.<br>This is a system API. | 890| PIN_TYPE_CONFIRM_PASSKEY | 2 | The user needs to confirm the PASSKEY displayed on the local device.<br>This is a system API. | 891| 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. | 892| 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. | 893| 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. | 894| 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. | 895| 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. | 896 897 898## DiscoveryResult<sup>12+</sup> 899 900Represents information about the discovered device. 901 902**System API**: This is a system API. 903 904**System capability**: SystemCapability.Communication.Bluetooth.Core 905 906| Name | Type | Readable | Writable | Description | 907| -------- | ------ | ---- | ---- | ----------- | 908| deviceId<sup>12+</sup> | string | Yes | No | ID of the discovered device.<br>This is a system API. | 909| rssi<sup>12+</sup> | number | Yes | No | RSSI of the discovered device.<br>This is a system API. | 910| deviceName<sup>12+</sup> | string | Yes | No | Name of the discovered device.<br>This is a system API. | 911| deviceClass<sup>12+</sup> | DeviceClass | Yes | No | Bluetooth class of the discovered device.<br>This is a system API. | 912 913 914## DeviceType<sup>12+</sup> 915 916Enumerates the custom types of a remote Bluetooth device. 917 918**System API**: This is a system API. 919 920**System capability**: SystemCapability.Communication.Bluetooth.Core 921 922| Name | Value | Description | 923| -------------------------------- | ------ | --------------- | 924| DEVICE_TYPE_DEFAULT<sup>12+</sup> | 0 | Default device type, which is the same as the original type.<br>This is a system API.| 925| DEVICE_TYPE_CAR<sup>12+</sup> | 1 | Car.<br>This is a system API. | 926| DEVICE_TYPE_HEADSET<sup>12+</sup> | 2 | Headset.<br>This is a system API. | 927| DEVICE_TYPE_HEARING<sup>12+</sup> | 3 | Hearing aid.<br>This is a system API. | 928| DEVICE_TYPE_GLASSES<sup>12+</sup> | 4 | Glasses. <br>This is a system API. | 929| DEVICE_TYPE_WATCH<sup>12+</sup> | 5 | Watch.<br>This is a system API. | 930| DEVICE_TYPE_SPEAKER<sup>12+</sup> | 6 | Speaker.<br>This is a system API. | 931| DEVICE_TYPE_OTHERS<sup>12+</sup> | 7 | Other device.<br>This is a system API. | 932 933 934## BatteryInfo<sup>12+</sup> 935 936Represents the battery information. 937 938**System capability**: SystemCapability.Communication.Bluetooth.Core 939 940| Name | Type | Readable | Writable | Description | 941| -------- | ------ | ---- | ---- | ----------- | 942| deviceId | string | Yes | No | MAC address of the remote device.<br>This is a system API.| 943