1# @ohos.bluetoothManager (Bluetooth) 2 3The **Bluetooth** module provides classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 9. 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 bluetoothManager from '@ohos.bluetoothManager'; 15``` 16 17 18## bluetoothManager.enableBluetooth<a name="enableBluetooth"></a> 19 20enableBluetooth(): void 21 22Enables Bluetooth. 23 24**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 25 26**System capability**: SystemCapability.Communication.Bluetooth.Core 27 28**Error codes** 29 30For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 31 32| ID| Error Message| 33| -------- | ---------------------------- | 34|2900001 | Service stopped. | 35|2900099 | Operation failed. | 36 37**Example** 38 39```js 40try { 41 bluetoothManager.enableBluetooth(); 42} catch (err) { 43 console.error("errCode:" + err.code + ",errMessage:" + err.message); 44} 45``` 46 47 48## bluetoothManager.disableBluetooth<a name="disableBluetooth"></a> 49 50disableBluetooth(): void 51 52Disables Bluetooth. 53 54**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 55 56**System capability**: SystemCapability.Communication.Bluetooth.Core 57 58**Error codes** 59 60For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 61 62| ID| Error Message| 63| -------- | ---------------------------- | 64|2900001 | Service stopped. | 65|2900099 | Operation failed. | 66 67**Example** 68 69```js 70try { 71 bluetoothManager.disableBluetooth(); 72} catch (err) { 73 console.error("errCode:" + err.code + ",errMessage:" + err.message); 74} 75``` 76 77 78## bluetoothManager.getLocalName<a name="getLocalName"></a> 79 80getLocalName(): string 81 82Obtains the name of the local Bluetooth device. 83 84**Required permissions**: ohos.permission.USE_BLUETOOTH 85 86**System capability**: SystemCapability.Communication.Bluetooth.Core 87 88**Return value** 89 90| Type | Description | 91| ------ | --------- | 92| string | Name of the local Bluetooth device obtained.| 93 94**Error codes** 95 96For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 97 98| ID| Error Message| 99| -------- | ---------------------------- | 100|2900001 | Service stopped. | 101|2900099 | Operation failed. | 102 103**Example** 104 105```js 106try { 107 let localName = bluetoothManager.getLocalName(); 108} catch (err) { 109 console.error("errCode:" + err.code + ",errMessage:" + err.message); 110} 111``` 112 113 114## bluetoothManager.getState 115 116getState(): BluetoothState 117 118Obtains the Bluetooth state. 119 120**Required permissions**: ohos.permission.USE_BLUETOOTH 121 122**System capability**: SystemCapability.Communication.Bluetooth.Core 123 124**Return value** 125 126| Type | Description | 127| --------------------------------- | --------- | 128| [BluetoothState](#bluetoothstate) | Bluetooth state obtained.| 129 130**Error codes** 131 132For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 133 134| ID| Error Message| 135| -------- | ---------------------------- | 136|2900001 | Service stopped. | 137|2900099 | Operation failed. | 138 139**Example** 140 141```js 142try { 143 let state = bluetoothManager.getState(); 144} catch (err) { 145 console.error("errCode:" + err.code + ",errMessage:" + err.message); 146} 147``` 148 149 150## bluetoothManager.getBtConnectionState 151 152getBtConnectionState(): ProfileConnectionState 153 154Obtains the local profile connection state. 155 156**Required permissions**: ohos.permission.USE_BLUETOOTH 157 158**System capability**: SystemCapability.Communication.Bluetooth.Core 159 160**Return value** 161 162| Type | Description | 163| ---------------------------------------- | ------------------- | 164| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.| 165 166**Error codes** 167 168For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 169 170| ID| Error Message| 171| -------- | ---------------------------- | 172|2900001 | Service stopped. | 173|2900003 | Bluetooth switch is off. | 174|2900099 | Operation failed. | 175 176**Example** 177 178```js 179try { 180 let connectionState = bluetoothManager.getBtConnectionState(); 181} catch (err) { 182 console.error("errCode:" + err.code + ",errMessage:" + err.message); 183} 184``` 185 186 187## bluetoothManager.setLocalName<a name="setLocalName"></a> 188 189setLocalName(name: string): void 190 191Sets the name of the local Bluetooth device. 192 193**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 194 195**System capability**: SystemCapability.Communication.Bluetooth.Core 196 197**Parameters** 198 199| Name | Type | Mandatory | Description | 200| ---- | ------ | ---- | --------------------- | 201| name | string | Yes | Bluetooth device name to set. It cannot exceed 248 bytes.| 202 203**Error codes** 204 205For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 206 207| ID| Error Message| 208| -------- | ---------------------------- | 209|2900001 | Service stopped. | 210|2900003 | Bluetooth switch is off. | 211|2900099 | Operation failed. | 212 213**Example** 214 215```js 216try { 217 bluetoothManager.setLocalName('device_name'); 218} catch (err) { 219 console.error("errCode:" + err.code + ",errMessage:" + err.message); 220} 221``` 222 223 224## bluetoothManager.pairDevice 225 226pairDevice(deviceId: string): void 227 228Initiates Bluetooth pairing. 229 230**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 231 232**System capability**: SystemCapability.Communication.Bluetooth.Core 233 234**Parameters** 235 236| Name | Type | Mandatory | Description | 237| -------- | ------ | ---- | ----------------------------------- | 238| deviceId | string | Yes | Address of the remote device to pair, for example, XX:XX:XX:XX:XX:XX.| 239 240**Error codes** 241 242For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 243 244| ID| Error Message| 245| -------- | ---------------------------- | 246|2900001 | Service stopped. | 247|2900003 | Bluetooth switch is off. | 248|2900099 | Operation failed. | 249 250**Example** 251 252```js 253try { 254 // The address can be scanned. 255 bluetoothManager.pairDevice("XX:XX:XX:XX:XX:XX"); 256} catch (err) { 257 console.error("errCode:" + err.code + ",errMessage:" + err.message); 258} 259``` 260 261 262## bluetoothManager.getProfileConnectionState<a name="getProfileConnectionState"></a> 263 264getProfileConnectionState(profileId: ProfileId): ProfileConnectionState 265 266Obtains the connection state of a profile. 267 268**Required permissions**: ohos.permission.USE_BLUETOOTH 269 270**System capability**: SystemCapability.Communication.Bluetooth.Core 271 272**Parameters** 273 274| Name | Type | Mandatory | Description | 275| --------- | --------- | ---- | ------------------------------------- | 276| ProfileId | profileId | Yes | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.| 277 278**Return value** 279 280| Type | Description | 281| ------------------------------------------------- | ------------------- | 282| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.| 283 284**Error codes** 285 286For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 287 288| ID| Error Message| 289| -------- | ---------------------------- | 290|2900001 | Service stopped. | 291|2900003 | Bluetooth switch is off. | 292|2900004 | Profile is not supported. | 293|2900099 | Operation failed. | 294 295**Example** 296 297```js 298try { 299 let result = bluetoothManager.getProfileConnectionState(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE); 300} catch (err) { 301 console.error("errCode:" + err.code + ",errMessage:" + err.message); 302} 303``` 304 305 306## bluetoothManager.cancelPairedDevice<a name="cancelPairedDevice"></a> 307 308cancelPairedDevice(deviceId: string): void 309 310Cancels a paired remote device. 311 312**System API**: This is a system API. 313 314**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 315 316**System capability**: SystemCapability.Communication.Bluetooth.Core 317 318**Parameters** 319 320| Name | Type | Mandatory | Description | 321| -------- | ------ | ---- | ------------------------------------- | 322| deviceId | string | Yes | Address of the remote device to cancel, for example, XX:XX:XX:XX:XX:XX.| 323 324**Error codes** 325 326For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 327 328| ID| Error Message| 329| -------- | ---------------------------- | 330|2900001 | Service stopped. | 331|2900003 | Bluetooth switch is off. | 332|2900099 | Operation failed. | 333 334**Example** 335 336```js 337try { 338 bluetoothManager.cancelPairedDevice("XX:XX:XX:XX:XX:XX"); 339} catch (err) { 340 console.error("errCode:" + err.code + ",errMessage:" + err.message); 341} 342``` 343 344 345## bluetoothManager.getRemoteDeviceName<a name="getRemoteDeviceName"></a> 346 347getRemoteDeviceName(deviceId: string): string 348 349Obtains the name of the remote Bluetooth device. 350 351**Required permissions**: ohos.permission.USE_BLUETOOTH 352 353**System capability**: SystemCapability.Communication.Bluetooth.Core 354 355**Parameters** 356 357| Name | Type | Mandatory | Description | 358| -------- | ------ | ---- | --------------------------------- | 359| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.| 360 361**Return value** 362 363| Type | Description | 364| ------ | ------------- | 365| string | Device name (a string) obtained.| 366 367**Error codes** 368 369For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 370 371| ID| Error Message| 372| -------- | ---------------------------- | 373|2900001 | Service stopped. | 374|2900003 | Bluetooth switch is off. | 375|2900099 | Operation failed. | 376 377**Example** 378 379```js 380try { 381 let remoteDeviceName = bluetoothManager.getRemoteDeviceName("XX:XX:XX:XX:XX:XX"); 382} catch (err) { 383 console.error("errCode:" + err.code + ",errMessage:" + err.message); 384} 385``` 386 387 388## bluetoothManager.getRemoteDeviceClass<a name="getRemoteDeviceClass"></a> 389 390getRemoteDeviceClass(deviceId: string): DeviceClass 391 392Obtains the class of the remote Bluetooth device. 393 394**Required permissions**: ohos.permission.USE_BLUETOOTH 395 396**System capability**: SystemCapability.Communication.Bluetooth.Core 397 398**Parameters** 399 400| Name | Type | Mandatory | Description | 401| -------- | ------ | ---- | --------------------------------- | 402| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.| 403 404**Return value** 405 406| Type | Description | 407| --------------------------- | -------- | 408| [DeviceClass](#deviceclass) | Class of the remote device obtained.| 409 410**Error codes** 411 412For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 413 414| ID| Error Message| 415| -------- | ---------------------------- | 416|2900001 | Service stopped. | 417|2900003 | Bluetooth switch is off. | 418|2900099 | Operation failed. | 419 420**Example** 421 422```js 423try { 424 let remoteDeviceClass = bluetoothManager.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX"); 425} catch (err) { 426 console.error("errCode:" + err.code + ",errMessage:" + err.message); 427} 428``` 429 430 431## bluetoothManager.getPairedDevices<a name="getPairedDevices"></a> 432 433getPairedDevices(): Array<string> 434 435Obtains the paired devices. 436 437**Required permissions**: ohos.permission.USE_BLUETOOTH 438 439**System capability**: SystemCapability.Communication.Bluetooth.Core 440 441**Return value** 442 443| Type | Description | 444| ------------------- | ------------- | 445| Array<string> | Addresses of the paired Bluetooth devices.| 446 447**Error codes** 448 449For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 450 451| ID| Error Message| 452| -------- | ---------------------------- | 453|2900001 | Service stopped. | 454|2900003 | Bluetooth switch is off. | 455|2900099 | Operation failed. | 456 457**Example** 458 459```js 460try { 461 let devices = bluetoothManager.getPairedDevices(); 462} catch (err) { 463 console.error("errCode:" + err.code + ",errMessage:" + err.message); 464} 465``` 466 467 468## bluetoothManager.setBluetoothScanMode<a name="setBluetoothScanMode"></a> 469 470setBluetoothScanMode(mode: ScanMode, duration: number): void 471 472Sets the Bluetooth scan mode so that the device can be discovered by a remote device. 473 474**Required permissions**: ohos.permission.USE_BLUETOOTH 475 476**System capability**: SystemCapability.Communication.Bluetooth.Core 477 478**Parameters** 479 480| Name | Type | Mandatory | Description | 481| -------- | --------------------- | ---- | ---------------------------- | 482| mode | [ScanMode](#scanmode) | Yes | Bluetooth scan mode to set. | 483| duration | number | Yes | Duration (in ms) in which the device can be discovered. The value **0** indicates unlimited time.| 484 485**Error codes** 486 487For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 488 489| ID| Error Message| 490| -------- | ---------------------------- | 491|2900001 | Service stopped. | 492|2900003 | Bluetooth switch is off. | 493|2900099 | Operation failed. | 494 495**Example** 496 497```js 498try { 499 // The device can be discovered and connected only when the discoverable and connectable mode is used. 500 bluetoothManager.setBluetoothScanMode(bluetoothManager.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 501} catch (err) { 502 console.error("errCode:" + err.code + ",errMessage:" + err.message); 503} 504``` 505 506 507## bluetoothManager.getBluetoothScanMode<a name="getBluetoothScanMode"></a> 508 509getBluetoothScanMode(): ScanMode 510 511Obtains the Bluetooth scan mode. 512 513**Required permissions**: ohos.permission.USE_BLUETOOTH 514 515**System capability**: SystemCapability.Communication.Bluetooth.Core 516 517**Return value** 518 519| Type | Description | 520| --------------------- | ------- | 521| [ScanMode](#scanmode) | Bluetooth scan mode to set.| 522 523**Error codes** 524 525For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 526 527| ID| Error Message| 528| -------- | ---------------------------- | 529|2900001 | Service stopped. | 530|2900003 | Bluetooth switch is off. | 531|2900099 | Operation failed. | 532 533**Example** 534 535```js 536try { 537 let scanMode = bluetoothManager.getBluetoothScanMode(); 538} catch (err) { 539 console.error("errCode:" + err.code + ",errMessage:" + err.message); 540} 541``` 542 543 544## bluetoothManager.startBluetoothDiscovery<a name="startBluetoothDiscovery"></a> 545 546startBluetoothDiscovery(): void 547 548Starts Bluetooth scan to discover remote devices. 549 550**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION 551 552**System capability**: SystemCapability.Communication.Bluetooth.Core 553 554**Error codes** 555 556For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 557 558| ID| Error Message| 559| -------- | ---------------------------- | 560|2900001 | Service stopped. | 561|2900003 | Bluetooth switch is off. | 562|2900099 | Operation failed. | 563 564**Example** 565 566```js 567let deviceId; 568function onReceiveEvent(data) { 569 deviceId = data; 570} 571try { 572 bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent); 573 bluetoothManager.startBluetoothDiscovery(); 574} catch (err) { 575 console.error("errCode:" + err.code + ",errMessage:" + err.message); 576} 577``` 578 579 580## bluetoothManager.stopBluetoothDiscovery<a name="stopBluetoothDiscovery"></a> 581 582stopBluetoothDiscovery(): void 583 584Stops Bluetooth scan. 585 586**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 587 588**System capability**: SystemCapability.Communication.Bluetooth.Core 589 590**Error codes** 591 592For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 593 594| ID| Error Message| 595| -------- | ---------------------------- | 596|2900001 | Service stopped. | 597|2900003 | Bluetooth switch is off. | 598|2900099 | Operation failed. | 599 600**Example** 601 602```js 603try { 604 bluetoothManager.stopBluetoothDiscovery(); 605} catch (err) { 606 console.error("errCode:" + err.code + ",errMessage:" + err.message); 607} 608``` 609 610 611## bluetoothManager.setDevicePairingConfirmation<a name="setDevicePairingConfirmation"></a> 612 613setDevicePairingConfirmation(device: string, accept: boolean): void 614 615Sets the device pairing confirmation. 616 617**Required permissions**: ohos.permission.MANAGE_BLUETOOTH 618 619**System capability**: SystemCapability.Communication.Bluetooth.Core 620 621**Parameters** 622 623| Name | Type | Mandatory | Description | 624| ------ | ------- | ---- | -------------------------------- | 625| device | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 626| 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. | 627 628**Error codes** 629 630For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 631 632| ID| Error Message| 633| -------- | ---------------------------- | 634|2900001 | Service stopped. | 635|2900003 | Bluetooth switch is off. | 636|2900099 | Operation failed. | 637 638**Example** 639 640```js 641 642try { 643 // Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device. 644 function onReceivePinRequiredEvent(data) { // data is the input parameter for the pairing request. 645 console.info('pin required = '+ JSON.stringify(data)); 646 bluetoothManager.setDevicePairingConfirmation(data.deviceId, true); 647 } 648 bluetoothManager.on("pinRequired", onReceivePinRequiredEvent); 649} catch (err) { 650 console.error("errCode:" + err.code + ",errMessage:" + err.message); 651} 652``` 653 654 655## bluetoothManager.on('bluetoothDeviceFind') 656 657on(type: "bluetoothDeviceFind", callback: Callback<Array<string>>): void 658 659Subscribes to the Bluetooth device discovery events. 660 661**Required permissions**: ohos.permission.USE_BLUETOOTH 662 663**System capability**: SystemCapability.Communication.Bluetooth.Core 664 665**Parameters** 666 667| Name | Type | Mandatory | Description | 668| -------- | ----------------------------------- | ---- | -------------------------------------- | 669| type | string | Yes | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered.| 670| callback | Callback<Array<string>> | Yes | Callback invoked to return the discovered devices. You need to implement this callback. | 671 672**Error codes** 673 674For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 675 676| ID| Error Message| 677| -------- | ---------------------------- | 678|2900099 | Operation failed. | 679 680**Example** 681 682```js 683function onReceiveEvent(data) { // data is a set of Bluetooth device addresses. 684 console.info('bluetooth device find = '+ JSON.stringify(data)); 685} 686try { 687 bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent); 688} catch (err) { 689 console.error("errCode:" + err.code + ",errMessage:" + err.message); 690} 691``` 692 693 694## bluetoothManager.off('bluetoothDeviceFind') 695 696off(type: "bluetoothDeviceFind", callback?: Callback<Array<string>>): void 697 698Unsubscribes from the Bluetooth device discovery events. 699 700**Required permissions**: ohos.permission.USE_BLUETOOTH 701 702**System capability**: SystemCapability.Communication.Bluetooth.Core 703 704**Parameters** 705 706| Name | Type | Mandatory | Description | 707| -------- | ----------------------------------- | ---- | ---------------------------------------- | 708| type | string | Yes | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered. | 709| callback | Callback<Array<string>> | No | Callback for the **bluetoothDeviceFind** event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 710 711**Error codes** 712 713For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 714 715| ID| Error Message| 716| -------- | ---------------------------- | 717|2900099 | Operation failed. | 718 719**Example** 720 721```js 722function onReceiveEvent(data) { 723 console.info('bluetooth device find = '+ JSON.stringify(data)); 724} 725try { 726 bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent); 727 bluetoothManager.off('bluetoothDeviceFind', onReceiveEvent); 728} catch (err) { 729 console.error("errCode:" + err.code + ",errMessage:" + err.message); 730} 731``` 732 733 734## bluetoothManager.on('pinRequired') 735 736on(type: "pinRequired", callback: Callback<PinRequiredParam>): void 737 738Subscribes to the pairing request events of the remote Bluetooth device. 739 740**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 741 742**System capability**: SystemCapability.Communication.Bluetooth.Core 743 744**Parameters** 745 746| Name | Type | Mandatory | Description | 747| -------- | ---------------------------------------- | ---- | -------------------------------- | 748| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 749| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | Yes | Callback invoked to return the pairing request. You need to implement this callback.| 750 751**Error codes** 752 753For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 754 755| ID| Error Message| 756| -------- | ---------------------------- | 757|2900099 | Operation failed. | 758 759**Example** 760 761```js 762function onReceiveEvent(data) { // data is the pairing request parameter. 763 console.info('pin required = '+ JSON.stringify(data)); 764} 765try { 766 bluetoothManager.on('pinRequired', onReceiveEvent); 767} catch (err) { 768 console.error("errCode:" + err.code + ",errMessage:" + err.message); 769} 770``` 771 772 773## bluetoothManager.off('pinRequired') 774 775off(type: "pinRequired", callback?: Callback<PinRequiredParam>): void 776 777Unsubscribes from the pairing request events of the remote Bluetooth device. 778 779**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 780 781**System capability**: SystemCapability.Communication.Bluetooth.Core 782 783**Parameters** 784 785| Name | Type | Mandatory | Description | 786| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 787| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 788| 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 method unsubscribes from all callbacks corresponding to **type**.| 789 790**Error codes** 791 792For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 793 794| ID| Error Message| 795| -------- | ---------------------------- | 796|2900099 | Operation failed. | 797 798**Example** 799 800```js 801function onReceiveEvent(data) { 802 console.info('pin required = '+ JSON.stringify(data)); 803} 804try { 805 bluetoothManager.on('pinRequired', onReceiveEvent); 806 bluetoothManager.off('pinRequired', onReceiveEvent); 807} catch (err) { 808 console.error("errCode:" + err.code + ",errMessage:" + err.message); 809} 810``` 811 812 813## bluetoothManager.on('bondStateChange') 814 815on(type: "bondStateChange", callback: Callback<BondStateParam>): void 816 817Subscribes to the Bluetooth pairing state change events. 818 819**Required permissions**: ohos.permission.USE_BLUETOOTH 820 821**System capability**: SystemCapability.Communication.Bluetooth.Core 822 823**Parameters** 824 825| Name | Type | Mandatory | Description | 826| -------- | ---------------------------------------- | ---- | ------------------------------------ | 827| type | string | Yes | Event type. The value **bondStateChange** indicates a Bluetooth pairing state change event.| 828| callback | Callback<[BondStateParam](#BondStateParam)> | Yes | Callback invoked to return the pairing state. You need to implement this callback. | 829 830**Error codes** 831 832For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 833 834| ID| Error Message| 835| -------- | ---------------------------- | 836|2900099 | Operation failed. | 837 838**Example** 839 840```js 841function onReceiveEvent(data) { // data, as the input parameter of the callback, indicates the pairing state. 842 console.info('pair state = '+ JSON.stringify(data)); 843} 844try { 845 bluetoothManager.on('bondStateChange', onReceiveEvent); 846} catch (err) { 847 console.error("errCode:" + err.code + ",errMessage:" + err.message); 848} 849``` 850 851 852## bluetoothManager.off('bondStateChange') 853 854off(type: "bondStateChange", callback?: Callback<BondStateParam>): void 855 856Unsubscribes from the Bluetooth pairing state change events. 857 858**Required permissions**: ohos.permission.USE_BLUETOOTH 859 860**System capability**: SystemCapability.Communication.Bluetooth.Core 861 862**Parameters** 863 864| Name | Type | Mandatory | Description | 865| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 866| type | string | Yes | Event type. The value **bondStateChange** indicates a Bluetooth pairing state change event. | 867| callback | Callback<[BondStateParam](#BondStateParam)> | No | Callback for the change of the Bluetooth pairing state. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 868 869**Error codes** 870 871For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 872 873| ID| Error Message| 874| -------- | ---------------------------- | 875|2900099 | Operation failed. | 876 877**Example** 878 879```js 880function onReceiveEvent(data) { 881 console.info('bond state = '+ JSON.stringify(data)); 882} 883try { 884 bluetoothManager.on('bondStateChange', onReceiveEvent); 885 bluetoothManager.off('bondStateChange', onReceiveEvent); 886} catch (err) { 887 console.error("errCode:" + err.code + ",errMessage:" + err.message); 888} 889``` 890 891 892## bluetoothManager.on('stateChange') 893 894on(type: "stateChange", callback: Callback<BluetoothState>): void 895 896Subscribes to the Bluetooth connection state change events. 897 898**Required permissions**: ohos.permission.USE_BLUETOOTH 899 900**System capability**: SystemCapability.Communication.Bluetooth.Core 901 902**Parameters** 903 904| Name | Type | Mandatory | Description | 905| -------- | ---------------------------------------- | ---- | -------------------------------- | 906| type | string | Yes | Event type. The value **stateChange** indicates a Bluetooth connection state change event. | 907| callback | Callback<[BluetoothState](#bluetoothstate)> | Yes | Callback invoked to return the Bluetooth connection state. You need to implement this callback.| 908 909**Error codes** 910 911For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 912 913| ID| Error Message| 914| -------- | ---------------------------- | 915|2900099 | Operation failed. | 916 917**Example** 918 919```js 920function onReceiveEvent(data) { 921 console.info('bluetooth state = '+ JSON.stringify(data)); 922} 923try { 924 bluetoothManager.on('stateChange', onReceiveEvent); 925} catch (err) { 926 console.error("errCode:" + err.code + ",errMessage:" + err.message); 927} 928``` 929 930 931## bluetoothManager.off('stateChange') 932 933off(type: "stateChange", callback?: Callback<BluetoothState>): void 934 935Unsubscribes from the Bluetooth connection state change events. 936 937**Required permissions**: ohos.permission.USE_BLUETOOTH 938 939**System capability**: SystemCapability.Communication.Bluetooth.Core 940 941**Parameters** 942 943| Name | Type | Mandatory | Description | 944| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 945| type | string | Yes | Event type. The value **stateChange** indicates a Bluetooth connection state change event. | 946| callback | Callback<[BluetoothState](#bluetoothstate)> | No | Callback for the Bluetooth connection state change event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 947 948**Error codes** 949 950For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 951 952| ID| Error Message| 953| -------- | ---------------------------- | 954|2900099 | Operation failed. | 955 956**Example** 957 958```js 959function onReceiveEvent(data) { 960 console.info('bluetooth state = '+ JSON.stringify(data)); 961} 962try { 963 bluetoothManager.on('stateChange', onReceiveEvent); 964 bluetoothManager.off('stateChange', onReceiveEvent); 965} catch (err) { 966 console.error("errCode:" + err.code + ",errMessage:" + err.message); 967} 968``` 969 970 971## bluetoothManager.sppListen<a name="sppListen"></a> 972 973sppListen(name: string, option: SppOption, callback: AsyncCallback<number>): void 974 975Creates a server listening socket. 976 977**Required permissions**: ohos.permission.USE_BLUETOOTH 978 979**System capability**: SystemCapability.Communication.Bluetooth.Core 980 981**Parameters** 982 983| Name | Type | Mandatory | Description | 984| -------- | --------------------------- | ---- | ----------------------- | 985| name | string | Yes | Name of the service. | 986| option | [SppOption](#sppoption) | Yes | Serial port profile (SPP) listening configuration. | 987| callback | AsyncCallback<number> | Yes | Callback invoked to return the server socket ID.| 988 989**Error codes** 990 991For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 992 993| ID| Error Message| 994| -------- | ---------------------------- | 995|2900001 | Service stopped. | 996|2900003 | Bluetooth switch is off. | 997|2900004 | Profile is not supported. | 998|2900099 | Operation failed. | 999 1000**Example** 1001 1002```js 1003let serverNumber = -1; 1004function serverSocket(code, number) { 1005 console.log('bluetooth error code: ' + code.code); 1006 if (code.code == 0) { 1007 console.log('bluetooth serverSocket Number: ' + number); 1008 serverNumber = number; 1009 } 1010} 1011 1012let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0}; 1013try { 1014 bluetoothManager.sppListen('server1', sppOption, serverSocket); 1015} catch (err) { 1016 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1017} 1018``` 1019 1020 1021## bluetoothManager.sppAccept<a name="sppAccept"></a> 1022 1023sppAccept(serverSocket: number, callback: AsyncCallback<number>): void 1024 1025Listens for a connection to be made to this socket from the client and accepts it. 1026 1027**System capability**: SystemCapability.Communication.Bluetooth.Core 1028 1029**Parameters** 1030 1031| Name | Type | Mandatory | Description | 1032| ------------ | --------------------------- | ---- | ----------------------- | 1033| serverSocket | number | Yes | Server socket ID. | 1034| callback | AsyncCallback<number> | Yes | Callback invoked to return the client socket ID.| 1035 1036**Error codes** 1037 1038For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1039 1040| ID| Error Message| 1041| -------- | ---------------------------- | 1042|2900001 | Service stopped. | 1043|2900003 | Bluetooth switch is off. | 1044|2900004 | Profile is not supported. | 1045|2900099 | Operation failed. | 1046 1047**Example** 1048 1049```js 1050let serverNumber = -1; 1051function serverSocket(code, number) { 1052 console.log('bluetooth error code: ' + code.code); 1053 if (code.code == 0) { 1054 console.log('bluetooth serverSocket Number: ' + number); 1055 serverNumber = number; 1056 } 1057} 1058let clientNumber = -1; 1059function acceptClientSocket(code, number) { 1060 console.log('bluetooth error code: ' + code.code); 1061 if (code.code == 0) { 1062 console.log('bluetooth clientSocket Number: ' + number); 1063 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the server. 1064 clientNumber = number; 1065 } 1066} 1067try { 1068 bluetoothManager.sppAccept(serverNumber, acceptClientSocket); 1069} catch (err) { 1070 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1071} 1072``` 1073 1074 1075## bluetoothManager.sppConnect<a name="sppConnect"></a> 1076 1077sppConnect(device: string, option: SppOption, callback: AsyncCallback<number>): void 1078 1079Initiates an SPP connection to a remote device from the client. 1080 1081**Required permissions**: ohos.permission.USE_BLUETOOTH 1082 1083**System capability**: SystemCapability.Communication.Bluetooth.Core 1084 1085**Parameters** 1086 1087| Name | Type | Mandatory | Description | 1088| -------- | --------------------------- | ---- | ------------------------------ | 1089| device | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 1090| option | [SppOption](#sppoption) | Yes | Configuration for connecting to the SPP client. | 1091| callback | AsyncCallback<number> | Yes | Callback invoked to return the client socket ID. | 1092 1093**Error codes** 1094 1095For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1096 1097| ID| Error Message| 1098| -------- | ---------------------------- | 1099|2900001 | Service stopped. | 1100|2900003 | Bluetooth switch is off. | 1101|2900004 | Profile is not supported. | 1102|2900099 | Operation failed. | 1103 1104**Example** 1105 1106```js 1107 1108let clientNumber = -1; 1109function clientSocket(code, number) { 1110 if (code.code != 0) { 1111 return; 1112 } 1113 console.log('bluetooth serverSocket Number: ' + number); 1114 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 1115 clientNumber = number; 1116} 1117let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0}; 1118try { 1119 bluetoothManager.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket); 1120} catch (err) { 1121 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1122} 1123``` 1124 1125 1126## bluetoothManager.sppCloseServerSocket<a name="sppCloseServerSocket"></a> 1127 1128sppCloseServerSocket(socket: number): void 1129 1130Closes the listening socket of the server. 1131 1132**System capability**: SystemCapability.Communication.Bluetooth.Core 1133 1134**Parameters** 1135 1136| Name | Type | Mandatory | Description | 1137| ------ | ------ | ---- | --------------- | 1138| socket | number | Yes | ID of the listening socket on the server. The ID is obtained by **sppListen**.| 1139 1140**Error codes** 1141 1142For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1143 1144| ID| Error Message| 1145| -------- | ---------------------------- | 1146|2900001 | Service stopped. | 1147|2900099 | Operation failed. | 1148 1149**Example** 1150 1151```js 1152let serverNumber = -1; 1153function serverSocket(code, number) { 1154 console.log('bluetooth error code: ' + code.code); 1155 if (code.code == 0) { 1156 console.log('bluetooth serverSocket Number: ' + number); 1157 serverNumber = number; 1158 } 1159} 1160try { 1161 bluetoothManager.sppCloseServerSocket(serverNumber); 1162} catch (err) { 1163 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1164} 1165``` 1166 1167 1168## bluetoothManager.sppCloseClientSocket<a name="sppCloseClientSocket"></a> 1169 1170sppCloseClientSocket(socket: number): void 1171 1172Closes the client socket. 1173 1174**System capability**: SystemCapability.Communication.Bluetooth.Core 1175 1176**Parameters** 1177 1178| Name | Type | Mandatory | Description | 1179| ------ | ------ | ---- | ------------- | 1180| Name | Type | Mandatory | Description | 1181| socket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.| 1182 1183**Error codes** 1184 1185For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1186 1187| ID| Error Message| 1188| -------- | ---------------------------- | 1189|2900001 | Service stopped. | 1190|2900099 | Operation failed. | 1191 1192**Example** 1193 1194```js 1195let clientNumber = -1; 1196function clientSocket(code, number) { 1197 if (code.code != 0) { 1198 return; 1199 } 1200 console.log('bluetooth serverSocket Number: ' + number); 1201 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 1202 clientNumber = number; 1203} 1204try { 1205 bluetoothManager.sppCloseClientSocket(clientNumber); 1206} catch (err) { 1207 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1208} 1209``` 1210 1211 1212## bluetoothManager.sppWrite<a name="sppWrite"></a> 1213 1214sppWrite(clientSocket: number, data: ArrayBuffer): void 1215 1216Writes data to the remote device through the socket. 1217 1218**System capability**: SystemCapability.Communication.Bluetooth.Core 1219 1220**Parameters** 1221 1222| Name | Type | Mandatory | Description | 1223| ------------ | ----------- | ---- | ------------- | 1224| clientSocket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.| 1225| data | ArrayBuffer | Yes | Data to write. | 1226 1227**Error codes** 1228 1229For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1230 1231| ID| Error Message| 1232| -------- | ---------------------------- | 1233|2901054 | IO error. | 1234|2900099 | Operation failed. | 1235 1236**Example** 1237 1238```js 1239let clientNumber = -1; 1240function clientSocket(code, number) { 1241 if (code.code != 0) { 1242 return; 1243 } 1244 console.log('bluetooth serverSocket Number: ' + number); 1245 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 1246 clientNumber = number; 1247} 1248let arrayBuffer = new ArrayBuffer(8); 1249let data = new Uint8Array(arrayBuffer); 1250data[0] = 123; 1251try { 1252 bluetoothManager.sppWrite(clientNumber, arrayBuffer); 1253} catch (err) { 1254 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1255} 1256``` 1257 1258 1259## bluetoothManager.on('sppRead') 1260 1261on(type: "sppRead", clientSocket: number, callback: Callback<ArrayBuffer>): void 1262 1263Subscribes to the SPP read request events. 1264 1265**System capability**: SystemCapability.Communication.Bluetooth.Core 1266 1267**Parameters** 1268 1269| Name | Type | Mandatory | Description | 1270| ------------ | --------------------------- | ---- | -------------------------- | 1271| type | string | Yes | Event type. The value **sppRead** indicates an SPP read request event.| 1272| clientSocket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**. | 1273| callback | Callback<ArrayBuffer> | Yes | Callback invoked to return the data read. | 1274 1275**Error codes** 1276 1277For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1278 1279| ID| Error Message| 1280| -------- | ---------------------------- | 1281|2901054 | IO error. | 1282|2900099 | Operation failed. | 1283 1284**Example** 1285 1286```js 1287let clientNumber = -1; 1288function clientSocket(code, number) { 1289 if (code.code != 0) { 1290 return; 1291 } 1292 console.log('bluetooth serverSocket Number: ' + number); 1293 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 1294 clientNumber = number; 1295} 1296function dataRead(dataBuffer) { 1297 let data = new Uint8Array(dataBuffer); 1298 console.log('bluetooth data is: ' + data[0]); 1299} 1300try { 1301 bluetoothManager.on('sppRead', clientNumber, dataRead); 1302} catch (err) { 1303 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1304} 1305``` 1306 1307 1308## bluetoothManager.off('sppRead') 1309 1310off(type: "sppRead", clientSocket: number, callback?: Callback<ArrayBuffer>): void 1311 1312Unsubscribes from the SPP read request events. 1313 1314**System capability**: SystemCapability.Communication.Bluetooth.Core 1315 1316**Parameters** 1317 1318| Name | Type | Mandatory | Description | 1319| ------------ | --------------------------- | ---- | ---------------------------------------- | 1320| type | string | Yes | Event type. The value **sppRead** indicates an SPP read request event. | 1321| clientSocket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**. | 1322| callback | Callback<ArrayBuffer> | No | Callback for the SPP read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 1323 1324**Example** 1325 1326```js 1327let clientNumber = -1; 1328function clientSocket(code, number) { 1329 if (code.code != 0) { 1330 return; 1331 } 1332 console.log('bluetooth serverSocket Number: ' + number); 1333 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 1334 clientNumber = number; 1335} 1336try { 1337 bluetoothManager.off('sppRead', clientNumber); 1338} catch (err) { 1339 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1340} 1341``` 1342 1343## bluetoothManager.getProfileInstance<a name="getProfileInstance"></a> 1344 1345getProfileInstance(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile | HidHostProfile | PanProfile 1346 1347Obtains a profile instance. API version 9 is added with **HidHostProfile** and **PanProfile**. 1348 1349**System capability**: SystemCapability.Communication.Bluetooth.Core 1350 1351**Parameters** 1352 1353| Name | Type | Mandatory | Description | 1354| --------- | --------- | ---- | ------------------------------------- | 1355| profileId | [ProfileId](#ProfileId) | Yes | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.| 1356 1357**Return value** 1358 1359| Type | Description | 1360| ------------------------------------------------------------ | ------------------------------------------------------------ | 1361| [A2dpSourceProfile](#a2dpsourceprofile), [HandsFreeAudioGatewayProfile](#handsfreeaudiogatewayprofile), [HidHostProfile](#hidhostprofile), or [PanProfile](#panprofile)| Profile instance obtained, which can be **A2dpSourceProfile**, **HandsFreeAudioGatewayProfile**, **HidHostProfile**, or **PanProfile**.| 1362 1363**Example** 1364 1365```js 1366try { 1367 let hidHost = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST); 1368} catch (err) { 1369 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1370} 1371``` 1372 1373 1374## bluetoothManager.BLE 1375 1376### bluetoothManager.BLE.createGattServer 1377 1378createGattServer(): GattServer 1379 1380Creates a **GattServer** instance. 1381 1382**System capability**: SystemCapability.Communication.Bluetooth.Core 1383 1384**Return value** 1385 1386| Type | Description | 1387| ------------------------- | ------------------------------------ | 1388| [GattServer](#gattserver) | **GattServer** instance created. Before using an API of the server, you must create a **GattSever** instance.| 1389 1390**Example** 1391 1392```js 1393let gattServer = bluetoothManager.BLE.createGattServer(); 1394``` 1395 1396 1397### bluetoothManager.BLE.createGattClientDevice 1398 1399createGattClientDevice(deviceId: string): GattClientDevice 1400 1401Creates a **GattClientDevice** instance. 1402 1403**System capability**: SystemCapability.Communication.Bluetooth.Core 1404 1405**Parameters** 1406 1407| Name | Type | Mandatory | Description | 1408| -------- | ------ | ---- | ------------------------------------ | 1409| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 1410 1411**Return value** 1412 1413| Type | Description | 1414| ------------------------------------- | ------------------------------------ | 1415| [GattClientDevice](#gattclientdevice) | **GattClientDevice** instance created. Before using an API of the client, you must create a **GattClientDevice** instance.| 1416 1417**Example** 1418 1419```js 1420try { 1421 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 1422} catch (err) { 1423 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1424} 1425``` 1426 1427 1428### bluetoothManager.BLE.getConnectedBLEDevices 1429 1430getConnectedBLEDevices(): Array<string> 1431 1432Obtains the BLE devices connected to this device. 1433 1434**Required permissions**: ohos.permission.USE_BLUETOOTH 1435 1436**System capability**: SystemCapability.Communication.Bluetooth.Core 1437 1438**Return value** 1439 1440| Type | Description | 1441| ------------------- | ------------------- | 1442| Array<string> | Addresses of the BLE devices connected to this device.| 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|2900001 | Service stopped. | 1451|2900003 | Bluetooth switch is off. | 1452|2900099 | Operation failed. | 1453 1454**Example** 1455 1456```js 1457try { 1458 let result = bluetoothManager.BLE.getConnectedBLEDevices(); 1459} catch (err) { 1460 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1461} 1462``` 1463 1464 1465### bluetoothManager.BLE.startBLEScan 1466 1467startBLEScan(filters: Array<ScanFilter>, options?: ScanOptions): void 1468 1469Starts a BLE scan. 1470 1471**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH, ohos.permission.MANAGE_BLUETOOTH, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION 1472 1473**System capability**: SystemCapability.Communication.Bluetooth.Core 1474 1475**Parameters** 1476 1477| Name | Type | Mandatory | Description | 1478| ------- | -------------------------------------- | ---- | ----------------------------------- | 1479| filters | Array<[ScanFilter](#scanfilter)> | Yes | Criteria for filtering the scan result. Set this parameter to **null** if you do not want to filter the scan result.| 1480| options | [ScanOptions](#scanoptions) | No | Scan options. | 1481 1482**Error codes** 1483 1484For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1485 1486| ID| Error Message| 1487| -------- | ---------------------------- | 1488|2900001 | Service stopped. | 1489|2900003 | Bluetooth switch is off. | 1490|2900099 | Operation failed. | 1491 1492**Example** 1493 1494```js 1495function onReceiveEvent(data) { 1496 console.info('BLE scan device find result = '+ JSON.stringify(data)); 1497} 1498try { 1499 bluetoothManager.BLE.on("BLEDeviceFind", onReceiveEvent); 1500 bluetoothManager.BLE.startBLEScan( 1501 [{ 1502 deviceId:"XX:XX:XX:XX:XX:XX", 1503 name:"test", 1504 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb" 1505 }], 1506 { 1507 interval: 500, 1508 dutyMode: bluetoothManager.ScanDuty.SCAN_MODE_LOW_POWER, 1509 matchMode: bluetoothManager.MatchMode.MATCH_MODE_AGGRESSIVE, 1510 } 1511 ); 1512} catch (err) { 1513 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1514} 1515``` 1516 1517 1518### bluetoothManager.BLE.stopBLEScan 1519 1520stopBLEScan(): void 1521 1522Stops the BLE scan. 1523 1524**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1525 1526**System capability**: SystemCapability.Communication.Bluetooth.Core 1527 1528**Error codes** 1529 1530For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1531 1532| ID| Error Message| 1533| -------- | ---------------------------- | 1534|2900001 | Service stopped. | 1535|2900003 | Bluetooth switch is off. | 1536|2900099 | Operation failed. | 1537 1538**Example** 1539 1540```js 1541try { 1542 bluetoothManager.BLE.stopBLEScan(); 1543} catch (err) { 1544 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1545} 1546``` 1547 1548 1549### bluetoothManager.BLE.on('BLEDeviceFind') 1550 1551on(type: "BLEDeviceFind", callback: Callback<Array<ScanResult>>): void 1552 1553Subscribe to the BLE device discovery events. 1554 1555**Required permissions**: ohos.permission.USE_BLUETOOTH 1556 1557**System capability**: SystemCapability.Communication.Bluetooth.Core 1558 1559**Parameters** 1560 1561| Name | Type | Mandatory | Description | 1562| -------- | ---------------------------------------- | ---- | ----------------------------------- | 1563| type | string | Yes | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered. | 1564| callback | Callback<Array<[ScanResult](#scanresult)>> | Yes | Callback invoked to return the discovered devices. You need to implement this callback.| 1565 1566**Error codes** 1567 1568For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1569 1570| ID| Error Message| 1571| -------- | ---------------------------- | 1572|2900099 | Operation failed. | 1573 1574**Example** 1575 1576```js 1577function onReceiveEvent(data) { 1578 console.info('bluetooth device find = '+ JSON.stringify(data)); 1579} 1580try { 1581 bluetoothManager.BLE.on('BLEDeviceFind', onReceiveEvent); 1582} catch (err) { 1583 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1584} 1585``` 1586 1587 1588### bluetoothManager.BLE.off('BLEDeviceFind') 1589 1590off(type: "BLEDeviceFind", callback?: Callback<Array<ScanResult>>): void 1591 1592Unsubscribes from the BLE device discovery events. 1593 1594**Required permissions**: ohos.permission.USE_BLUETOOTH 1595 1596**System capability**: SystemCapability.Communication.Bluetooth.Core 1597 1598**Parameters** 1599 1600| Name | Type | Mandatory | Description | 1601| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1602| type | string | Yes | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered. | 1603| callback | Callback<Array<[ScanResult](#scanresult)>> | No | Callback for the **BLEDeviceFind** event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 1604 1605**Error codes** 1606 1607For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1608 1609| ID| Error Message| 1610| -------- | ---------------------------- | 1611|2900099 | Operation failed. | 1612 1613**Example** 1614 1615```js 1616function onReceiveEvent(data) { 1617 console.info('bluetooth device find = '+ JSON.stringify(data)); 1618} 1619try { 1620 bluetoothManager.BLE.on('BLEDeviceFind', onReceiveEvent); 1621 bluetoothManager.BLE.off('BLEDeviceFind', onReceiveEvent); 1622} catch (err) { 1623 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1624} 1625``` 1626 1627 1628## BaseProfile 1629 1630Provides the profile base class. 1631 1632 1633### getConnectionDevices<a name="getConnectionDevices"></a> 1634 1635getConnectionDevices(): Array<string> 1636 1637Obtains the connected devices. 1638 1639**Required permissions**: ohos.permission.USE_BLUETOOTH 1640 1641**System capability**: SystemCapability.Communication.Bluetooth.Core 1642 1643**Return value** 1644 1645| Type | Description | 1646| ------------------- | ------------- | 1647| Array<string> | Addresses of the connected devices.| 1648 1649**Error codes** 1650 1651For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1652 1653| ID| Error Message| 1654| -------- | ---------------------------- | 1655|2900001 | Service stopped. | 1656|2900003 | Bluetooth switch is off. | 1657|2900004 | Profile is not supported. | 1658|2900099 | Operation failed. | 1659 1660**Example** 1661 1662```js 1663try { 1664 let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1665 let retArray = a2dpSrc.getConnectionDevices(); 1666} catch (err) { 1667 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1668} 1669``` 1670 1671### getDeviceState<a name="getDeviceState"></a> 1672 1673getDeviceState(device: string): ProfileConnectionState 1674 1675Obtains the connection state of the profile. 1676 1677**Required permissions**: ohos.permission.USE_BLUETOOTH 1678 1679**System capability**: SystemCapability.Communication.Bluetooth.Core 1680 1681**Parameters** 1682 1683| Name | Type | Mandatory | Description | 1684| ------ | ------ | ---- | ------- | 1685| device | string | Yes | Address of the target device.| 1686 1687**Return value** 1688 1689| Type | Description | 1690| ------------------------------------------------- | ----------------------- | 1691| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.| 1692 1693**Error codes** 1694 1695For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1696 1697| ID| Error Message| 1698| -------- | ---------------------------- | 1699|2900001 | Service stopped. | 1700|2900003 | Bluetooth switch is off. | 1701|2900004 | Profile is not supported. | 1702|2900099 | Operation failed. | 1703 1704**Example** 1705 1706```js 1707try { 1708 let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1709 let ret = a2dpSrc.getDeviceState('XX:XX:XX:XX:XX:XX'); 1710} catch (err) { 1711 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1712} 1713``` 1714 1715## A2dpSourceProfile 1716 1717Before using an API of **A2dpSourceProfile**, you need to create an instance of this class by using **getProfile()**. 1718 1719 1720### connect<a name="a2dp-connect"></a> 1721 1722connect(device: string): void 1723 1724Sets up an Advanced Audio Distribution Profile (A2DP) connection. 1725 1726**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1727 1728**System capability**: SystemCapability.Communication.Bluetooth.Core 1729 1730**Parameters** 1731 1732| Name | Type | Mandatory | Description | 1733| ------ | ------ | ---- | ------- | 1734| device | string | Yes | Address of the target device.| 1735 1736**Error codes** 1737 1738For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1739 1740| ID| Error Message| 1741| -------- | ---------------------------- | 1742|2900001 | Service stopped. | 1743|2900003 | Bluetooth switch is off. | 1744|2900004 | Profile is not supported. | 1745|2900099 | Operation failed. | 1746 1747**Example** 1748 1749```js 1750try { 1751 let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1752 a2dpSrc.connect('XX:XX:XX:XX:XX:XX'); 1753} catch (err) { 1754 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1755} 1756``` 1757 1758 1759### disconnect<a name="a2dp-disconnect"></a> 1760 1761disconnect(device: string): void 1762 1763Disconnects an A2DP connection. 1764 1765**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1766 1767**System capability**: SystemCapability.Communication.Bluetooth.Core 1768 1769**Parameters** 1770 1771| Name | Type | Mandatory | Description | 1772| ------ | ------ | ---- | ------- | 1773| device | string | Yes | Address of the target device.| 1774 1775**Error codes** 1776 1777For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1778 1779| ID| Error Message| 1780| -------- | ---------------------------- | 1781|2900001 | Service stopped. | 1782|2900003 | Bluetooth switch is off. | 1783|2900004 | Profile is not supported. | 1784|2900099 | Operation failed. | 1785 1786**Example** 1787 1788```js 1789try { 1790 let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1791 a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX'); 1792} catch (err) { 1793 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1794} 1795``` 1796 1797 1798### on('connectionStateChange') 1799 1800on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#StateChangeParam)>): void 1801 1802Subscribes to the A2DP connection state change events. 1803 1804**System capability**: SystemCapability.Communication.Bluetooth.Core 1805 1806**Parameters** 1807 1808| Name | Type | Mandatory | Description | 1809| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1810| type | string | Yes | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.| 1811| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback invoked to return the A2DP connection state change event. | 1812 1813**Return value** 1814 1815No value is returned. 1816 1817**Example** 1818 1819```js 1820function onReceiveEvent(data) { 1821 console.info('a2dp state = '+ JSON.stringify(data)); 1822} 1823let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1824a2dpSrc.on('connectionStateChange', onReceiveEvent); 1825``` 1826 1827 1828### off('connectionStateChange') 1829 1830off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 1831 1832Unsubscribes from the A2DP connection state change events. 1833 1834**System capability**: SystemCapability.Communication.Bluetooth.Core 1835 1836**Parameters** 1837 1838| Name | Type | Mandatory | Description | 1839| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1840| type | string | Yes | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.| 1841| callback | Callback<[StateChangeParam](#StateChangeParam)> | No | Callback for the A2DP connection state change event. | 1842 1843**Return value** 1844 1845No value is returned. 1846 1847**Example** 1848 1849```js 1850function onReceiveEvent(data) { 1851 console.info('a2dp state = '+ JSON.stringify(data)); 1852} 1853let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1854a2dpSrc.on('connectionStateChange', onReceiveEvent); 1855a2dpSrc.off('connectionStateChange', onReceiveEvent); 1856``` 1857 1858 1859### getPlayingState 1860 1861getPlayingState(device: string): PlayingState 1862 1863Obtains the playing state of a device. 1864 1865**System capability**: SystemCapability.Communication.Bluetooth.Core 1866 1867**Parameters** 1868 1869| Name | Type | Mandatory | Description | 1870| ------ | ------ | ---- | ------- | 1871| device | string | Yes | Address of the target device.| 1872 1873**Return value** 1874 1875| Type | Description | 1876| ----------------------------- | ---------- | 1877| [PlayingState](#PlayingState) | Playing state of the remote device obtained.| 1878 1879**Error codes** 1880 1881For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1882 1883| ID| Error Message| 1884| -------- | ---------------------------- | 1885|2900001 | Service stopped. | 1886|2900003 | Bluetooth switch is off. | 1887|2900004 | Profile is not supported. | 1888|2900099 | Operation failed. | 1889 1890**Example** 1891 1892```js 1893try { 1894 let a2dpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1895 let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX'); 1896} catch (err) { 1897 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1898} 1899``` 1900 1901 1902## HandsFreeAudioGatewayProfile 1903 1904Before using an API of **HandsFreeAudioGatewayProfile**, you need to create an instance of this class by using **getProfile()**. 1905 1906 1907### connect<a name="hfp-connect"></a> 1908 1909connect(device: string): void 1910 1911Sets up a Hands-free Profile (HFP) connection of a device. 1912 1913**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1914 1915**System capability**: SystemCapability.Communication.Bluetooth.Core 1916 1917**Parameters** 1918 1919| Name | Type | Mandatory | Description | 1920| ------ | ------ | ---- | ------- | 1921| device | string | Yes | Address of the target device.| 1922 1923**Error codes** 1924 1925For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1926 1927| ID| Error Message| 1928| -------- | ---------------------------- | 1929|2900001 | Service stopped. | 1930|2900003 | Bluetooth switch is off. | 1931|2900004 | Profile is not supported. | 1932|2900099 | Operation failed. | 1933 1934**Example** 1935 1936```js 1937try { 1938 let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as bluetoothManager.HandsFreeAudioGatewayProfile; 1939 hfpAg.connect('XX:XX:XX:XX:XX:XX'); 1940} catch (err) { 1941 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1942} 1943``` 1944 1945 1946### disconnect<a name="hfp-disconnect"></a> 1947 1948disconnect(device: string): void 1949 1950Disconnects the HFP connection of a device. 1951 1952**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1953 1954**System capability**: SystemCapability.Communication.Bluetooth.Core 1955 1956**Parameters** 1957 1958| Name | Type | Mandatory | Description | 1959| ------ | ------ | ---- | ------- | 1960| device | string | Yes | Address of the target device.| 1961 1962**Error codes** 1963 1964For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 1965 1966| ID| Error Message| 1967| -------- | ---------------------------- | 1968|2900001 | Service stopped. | 1969|2900003 | Bluetooth switch is off. | 1970|2900004 | Profile is not supported. | 1971|2900099 | Operation failed. | 1972 1973**Example** 1974 1975```js 1976try { 1977 let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as bluetoothManager.HandsFreeAudioGatewayProfile; 1978 hfpAg.disconnect('XX:XX:XX:XX:XX:XX'); 1979} catch (err) { 1980 console.error("errCode:" + err.code + ",errMessage:" + err.message); 1981} 1982``` 1983 1984 1985### on('connectionStateChange') 1986 1987on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#StateChangeParam)>): void 1988 1989Subscribes to the HFP connection state change events. 1990 1991**System capability**: SystemCapability.Communication.Bluetooth.Core 1992 1993**Parameters** 1994 1995| Name | Type | Mandatory | Description | 1996| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1997| type | string | Yes | Event type. The value **connectionStateChange** indicates an HFP connection state change event. | 1998| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback invoked to return the HFP connection state change event. | 1999 2000**Example** 2001 2002```js 2003function onReceiveEvent(data) { 2004 console.info('hfp state = '+ JSON.stringify(data)); 2005} 2006let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as 2007 bluetoothManager.HandsFreeAudioGatewayProfile; 2008hfpAg.on('connectionStateChange', onReceiveEvent); 2009``` 2010 2011 2012### off('connectionStateChange') 2013 2014off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 2015 2016Unsubscribes from the HFP connection state change events. 2017 2018**System capability**: SystemCapability.Communication.Bluetooth.Core 2019 2020**Parameters** 2021 2022| Name | Type | Mandatory | Description | 2023| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2024| type | string | Yes | Event type. The value **connectionStateChange** indicates an HFP connection state change event. | 2025| callback | Callback<[StateChangeParam](#StateChangeParam)> | No | Callback for the HFP connection state change event. | 2026 2027**Example** 2028 2029```js 2030function onReceiveEvent(data) { 2031 console.info('hfp state = '+ JSON.stringify(data)); 2032} 2033let hfpAg = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as 2034 bluetoothManager.HandsFreeAudioGatewayProfile; 2035hfpAg.on('connectionStateChange', onReceiveEvent); 2036hfpAg.off('connectionStateChange', onReceiveEvent); 2037``` 2038 2039 2040## HidHostProfile 2041 2042Before using an API of **HidHostProfile**, you need to create an instance of this class by using **getProfile()**. 2043 2044 2045### connect<a name="HidHost-connect"></a> 2046 2047connect(device: string): void 2048 2049Connects to the HidHost service of a device. 2050 2051**System API**: This is a system API. 2052 2053**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 2054 2055**System capability**: SystemCapability.Communication.Bluetooth.Core 2056 2057**Parameters** 2058 2059| Name | Type | Mandatory | Description | 2060| ------ | ------ | ---- | ------- | 2061| device | string | Yes | Address of the target device.| 2062 2063**Error codes** 2064 2065For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2066 2067| ID| Error Message| 2068| -------- | ---------------------------- | 2069|2900001 | Service stopped. | 2070|2900003 | Bluetooth switch is off. | 2071|2900004 | Profile is not supported. | 2072|2900099 | Operation failed. | 2073 2074**Example** 2075 2076```js 2077try { 2078 let hidHostProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 2079 hidHostProfile.connect('XX:XX:XX:XX:XX:XX'); 2080} catch (err) { 2081 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2082} 2083``` 2084 2085 2086### disconnect<a name="HidHost-disconnect"></a> 2087 2088disconnect(device: string): void 2089 2090Disconnects from the HidHost service of a device. 2091 2092**System API**: This is a system API. 2093 2094**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 2095 2096**System capability**: SystemCapability.Communication.Bluetooth.Core 2097 2098**Parameters** 2099 2100| Name | Type | Mandatory | Description | 2101| ------ | ------ | ---- | ------- | 2102| device | string | Yes | Address of the target device.| 2103 2104**Error codes** 2105 2106For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2107 2108| ID| Error Message| 2109| -------- | ---------------------------- | 2110|2900001 | Service stopped. | 2111|2900003 | Bluetooth switch is off. | 2112|2900004 | Profile is not supported. | 2113|2900099 | Operation failed. | 2114 2115**Example** 2116 2117```js 2118try { 2119 let hidHostProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 2120 hidHostProfile.disconnect('XX:XX:XX:XX:XX:XX'); 2121} catch (err) { 2122 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2123} 2124``` 2125 2126 2127### on('connectionStateChange') 2128 2129on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#StateChangeParam)>): void 2130 2131Subscribes to the HidHost connection state change events. 2132 2133**System capability**: SystemCapability.Communication.Bluetooth.Core 2134 2135**Parameters** 2136 2137| Name | Type | Mandatory | Description | 2138| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2139| type | string | Yes | Event type. The value **connectionStateChange** indicates a HidHost connection state change event. | 2140| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback invoked to return the HidHost connection state change event. | 2141 2142**Example** 2143 2144```js 2145function onReceiveEvent(data) { 2146 console.info('hidHost state = '+ JSON.stringify(data)); 2147} 2148let hidHost = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 2149hidHost.on('connectionStateChange', onReceiveEvent); 2150``` 2151 2152 2153### off('connectionStateChange') 2154 2155off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 2156 2157Unsubscribes from the HidHost connection state change events. 2158 2159**System capability**: SystemCapability.Communication.Bluetooth.Core 2160 2161**Parameters** 2162 2163| Name | Type | Mandatory| Description | 2164| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- | 2165| type | string | Yes | Event type. The value **connectionStateChange** indicates a HidHost connection state change event. | 2166| callback | Callback<[StateChangeParam](#StateChangeParam)> | No | Callback for the HidHost connection state change event. | 2167 2168**Example** 2169 2170```js 2171function onReceiveEvent(data) { 2172 console.info('hidHost state = '+ JSON.stringify(data)); 2173} 2174let hidHost = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 2175hidHost.on('connectionStateChange', onReceiveEvent); 2176hidHost.off('connectionStateChange', onReceiveEvent); 2177``` 2178 2179 2180## PanProfile 2181 2182Before using an API of **PanProfile**, you need to create an instance of this class by using **getProfile()**. 2183 2184 2185### disconnect<a name="PanP-disconnect"></a> 2186 2187disconnect(device: string): void 2188 2189Disconnects from the Personal Area Network (PAN) service of a device. 2190 2191**System API**: This is a system API. 2192 2193**Required permissions**: ohos.permission.USE_BLUETOOTH 2194 2195**System capability**: SystemCapability.Communication.Bluetooth.Core 2196 2197**Parameters** 2198 2199| Name | Type | Mandatory | Description | 2200| ------ | ------ | ---- | ------- | 2201| device | string | Yes | Address of the target device.| 2202 2203**Error codes** 2204 2205For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2206 2207| ID| Error Message| 2208| -------- | ---------------------------- | 2209|2900001 | Service stopped. | 2210|2900003 | Bluetooth switch is off. | 2211|2900004 | Profile is not supported. | 2212|2900099 | Operation failed. | 2213 2214**Example** 2215 2216```js 2217try { 2218 let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2219 panProfile.disconnect('XX:XX:XX:XX:XX:XX'); 2220} catch (err) { 2221 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2222} 2223``` 2224 2225 2226### on('connectionStateChange') 2227 2228on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#StateChangeParam)>): void 2229 2230Subscribes to the PAN connection state change events. 2231 2232**System capability**: SystemCapability.Communication.Bluetooth.Core 2233 2234**Parameters** 2235 2236| Name | Type | Mandatory | Description | 2237| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2238| type | string | Yes | Event type. The value **connectionStateChange** indicates a PAN connection state change event. | 2239| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback invoked to return the PAN connection state change event. | 2240 2241**Example** 2242 2243```js 2244function onReceiveEvent(data) { 2245 console.info('pan state = '+ JSON.stringify(data)); 2246} 2247let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2248panProfile.on('connectionStateChange', onReceiveEvent); 2249``` 2250 2251 2252### off('connectionStateChange') 2253 2254off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 2255 2256Unsubscribes from the PAN connection state change events. 2257 2258**System capability**: SystemCapability.Communication.Bluetooth.Core 2259 2260**Parameters** 2261 2262| Name | Type | Mandatory| Description | 2263| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- | 2264| type | string | Yes | Event type. The value **connectionStateChange** indicates a PAN connection state change event. | 2265| callback | Callback<[StateChangeParam](#StateChangeParam)> | No | Callback for the PAN connection state change event. | 2266 2267**Example** 2268 2269```js 2270function onReceiveEvent(data) { 2271 console.info('pan state = '+ JSON.stringify(data)); 2272} 2273let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2274panProfile.on('connectionStateChange', onReceiveEvent); 2275panProfile.off('connectionStateChange', onReceiveEvent); 2276``` 2277 2278 2279### setTethering<a name="setTethering"></a> 2280 2281setTethering(enable: boolean): void 2282 2283Sets tethering. 2284 2285**System API**: This is a system API. 2286 2287**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 2288 2289**System capability**: SystemCapability.Communication.Bluetooth.Core 2290 2291**Parameters** 2292 2293| Name | Type | Mandatory | Description | 2294| ------ | ------ | ---- | ------- | 2295| value | boolean | Yes | Whether to set tethering over a Bluetooth PAN.| 2296 2297**Error codes** 2298 2299For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2300 2301| ID| Error Message| 2302| -------- | ---------------------------- | 2303|2900001 | Service stopped. | 2304|2900003 | Bluetooth switch is off. | 2305|2900004 | Profile is not supported. | 2306|2900099 | Operation failed. | 2307 2308**Example** 2309 2310```js 2311try { 2312 let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2313 panProfile.setTethering(true); 2314} catch (err) { 2315 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2316} 2317``` 2318 2319 2320### isTetheringOn<a name="isTetheringOn"></a> 2321 2322isTetheringOn(): boolean 2323 2324Obtains the network sharing status. 2325 2326**System API**: This is a system API. 2327 2328**System capability**: SystemCapability.Communication.Bluetooth.Core 2329 2330**Return value** 2331 2332| Type | Description | 2333| --------------------- | --------------------------------- | 2334| boolean | Returns **true** if tethering is available over a Bluetooth PAN; return **false** otherwise.| 2335 2336**Example** 2337 2338```js 2339try { 2340 let panProfile = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2341 let ret = panProfile.isTetheringOn(); 2342} catch (err) { 2343 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2344} 2345``` 2346 2347 2348## GattServer 2349 2350Implements the Generic Attribute Profile (GATT) server. Before using an API of this class, you need to create a **GattServer** instance using **createGattServer()**. 2351 2352 2353### startAdvertising 2354 2355startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void 2356 2357Starts BLE advertising. 2358 2359**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 2360 2361**System capability**: SystemCapability.Communication.Bluetooth.Core 2362 2363**Parameters** 2364 2365| Name | Type | Mandatory | Description | 2366| ----------- | ------------------------------------- | ---- | -------------- | 2367| setting | [AdvertiseSetting](#advertisesetting) | Yes | Settings related to BLE advertising. | 2368| advData | [AdvertiseData](#advertisedata) | Yes | Content of the BLE advertisement packet. | 2369| advResponse | [AdvertiseData](#advertisedata) | No | Response to the BLE scan request.| 2370 2371**Error codes** 2372 2373For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2374 2375| ID| Error Message| 2376| -------- | ---------------------------- | 2377|2900001 | Service stopped. | 2378|2900003 | Bluetooth switch is off. | 2379|2900099 | Operation failed. | 2380 2381**Example** 2382 2383```js 2384let manufactureValueBuffer = new Uint8Array(4); 2385manufactureValueBuffer[0] = 1; 2386manufactureValueBuffer[1] = 2; 2387manufactureValueBuffer[2] = 3; 2388manufactureValueBuffer[3] = 4; 2389 2390let serviceValueBuffer = new Uint8Array(4); 2391serviceValueBuffer[0] = 4; 2392serviceValueBuffer[1] = 6; 2393serviceValueBuffer[2] = 7; 2394serviceValueBuffer[3] = 8; 2395console.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 2396console.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 2397let gattServer = bluetoothManager.BLE.createGattServer(); 2398try { 2399 gattServer.startAdvertising({ 2400 interval:150, 2401 txPower:60, 2402 connectable:true, 2403 },{ 2404 serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 2405 manufactureData:[{ 2406 manufactureId:4567, 2407 manufactureValue:manufactureValueBuffer.buffer 2408 }], 2409 serviceData:[{ 2410 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 2411 serviceValue:serviceValueBuffer.buffer 2412 }], 2413 },{ 2414 serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"], 2415 manufactureData:[{ 2416 manufactureId:1789, 2417 manufactureValue:manufactureValueBuffer.buffer 2418 }], 2419 serviceData:[{ 2420 serviceUuid:"00001889-0000-1000-8000-00805f9b34fb", 2421 serviceValue:serviceValueBuffer.buffer 2422 }], 2423 }); 2424} catch (err) { 2425 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2426} 2427``` 2428 2429 2430### stopAdvertising 2431 2432stopAdvertising(): void 2433 2434Stops BLE advertising. 2435 2436**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 2437 2438**System capability**: SystemCapability.Communication.Bluetooth.Core 2439 2440**Error codes** 2441 2442For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2443 2444| ID| Error Message| 2445| -------- | ---------------------------- | 2446|2900001 | Service stopped. | 2447|2900003 | Bluetooth switch is off. | 2448|2900099 | Operation failed. | 2449 2450**Example** 2451 2452```js 2453let server = bluetoothManager.BLE.createGattServer(); 2454try { 2455 server.stopAdvertising(); 2456} catch (err) { 2457 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2458} 2459``` 2460 2461 2462### addService 2463 2464addService(service: GattService): void 2465 2466Adds a service to this GATT server. 2467 2468**Required permissions**: ohos.permission.USE_BLUETOOTH 2469 2470**System capability**: SystemCapability.Communication.Bluetooth.Core 2471 2472**Parameters** 2473 2474| Name | Type | Mandatory | Description | 2475| ------- | --------------------------- | ---- | ------------------------ | 2476| service | [GattService](#gattservice) | Yes | Service to add. Settings related to BLE advertising.| 2477 2478**Error codes** 2479 2480For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2481 2482| ID| Error Message| 2483| -------- | ---------------------------- | 2484|2900001 | Service stopped. | 2485|2900003 | Bluetooth switch is off. | 2486|2900099 | Operation failed. | 2487 2488**Example** 2489 2490```js 2491// Create descriptors. 2492let descriptors = []; 2493let arrayBuffer = new ArrayBuffer(8); 2494let descV = new Uint8Array(arrayBuffer); 2495descV[0] = 11; 2496let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2497 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2498 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 2499descriptors[0] = descriptor; 2500 2501// Create characteristics. 2502let characteristics = []; 2503let arrayBufferC = new ArrayBuffer(8); 2504let cccV = new Uint8Array(arrayBufferC); 2505cccV[0] = 1; 2506let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2507 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2508let characteristicN = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2509 characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2510characteristics[0] = characteristic; 2511 2512// Create a gattService instance. 2513let gattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]}; 2514 2515let gattServer = bluetoothManager.BLE.createGattServer(); 2516try { 2517 gattServer.addService(gattService); 2518} catch (err) { 2519 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2520} 2521``` 2522 2523 2524### removeService 2525 2526removeService(serviceUuid: string): void 2527 2528Removes a service from this GATT server. 2529 2530**Required permissions**: ohos.permission.USE_BLUETOOTH 2531 2532**System capability**: SystemCapability.Communication.Bluetooth.Core 2533 2534**Parameters** 2535 2536| Name | Type | Mandatory | Description | 2537| ----------- | ------ | ---- | ---------------------------------------- | 2538| serviceUuid | string | Yes | Universally unique identifier (UUID) of the service to remove, for example, **00001810-0000-1000-8000-00805F9B34FB**.| 2539 2540**Error codes** 2541 2542For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2543 2544| ID| Error Message| 2545| -------- | ---------------------------- | 2546|2900001 | Service stopped. | 2547|2900003 | Bluetooth switch is off. | 2548|2900004 | Profile is not supported. | 2549|2900099 | Operation failed. | 2550 2551**Example** 2552 2553```js 2554let server = bluetoothManager.BLE.createGattServer(); 2555try { 2556 server.removeService('00001810-0000-1000-8000-00805F9B34FB'); 2557} catch (err) { 2558 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2559} 2560``` 2561 2562 2563### close 2564 2565close(): void 2566 2567Closes this GATT server to unregister it from the protocol stack. After this method is called, this [GattServer](#gattserver) cannot be used. 2568 2569**Required permissions**: ohos.permission.USE_BLUETOOTH 2570 2571**System capability**: SystemCapability.Communication.Bluetooth.Core 2572 2573**Error codes** 2574 2575For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2576 2577| ID| Error Message| 2578| -------- | ---------------------------- | 2579|2900001 | Service stopped. | 2580|2900003 | Bluetooth switch is off. | 2581|2900099 | Operation failed. | 2582 2583**Example** 2584 2585```js 2586let server = bluetoothManager.BLE.createGattServer(); 2587try { 2588 server.close(); 2589} catch (err) { 2590 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2591} 2592``` 2593 2594 2595### notifyCharacteristicChanged 2596 2597notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): void 2598 2599Notifies the connected client device when a characteristic value changes. 2600 2601**Required permissions**: ohos.permission.USE_BLUETOOTH 2602 2603**System capability**: SystemCapability.Communication.Bluetooth.Core 2604 2605**Parameters** 2606 2607| Name | Type | Mandatory | Description | 2608| -------------------- | ---------------------------------------- | ---- | --------------------------------------- | 2609| deviceId | string | Yes | Address of the client that receives notifications, for example, XX:XX:XX:XX:XX:XX.| 2610| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristic) | Yes | New characteristic value. | 2611 2612**Error codes** 2613 2614For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2615 2616| ID| Error Message| 2617| -------- | ---------------------------- | 2618|2900001 | Service stopped. | 2619|2900003 | Bluetooth switch is off. | 2620|2900099 | Operation failed. | 2621 2622**Example** 2623 2624```js 2625// Create descriptors. 2626let descriptors = []; 2627let arrayBuffer = new ArrayBuffer(8); 2628let descV = new Uint8Array(arrayBuffer); 2629descV[0] = 11; 2630let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2631 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2632 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 2633descriptors[0] = descriptor; 2634let arrayBufferC = new ArrayBuffer(8); 2635let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2636 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2637let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2638 characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false}; 2639let server = bluetoothManager.BLE.createGattServer(); 2640try { 2641 server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic); 2642} catch (err) { 2643 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2644} 2645``` 2646 2647 2648### sendResponse 2649 2650sendResponse(serverResponse: ServerResponse): void 2651 2652Sends a response to a read or write request from the GATT client. 2653 2654**Required permissions**: ohos.permission.USE_BLUETOOTH 2655 2656**System capability**: SystemCapability.Communication.Bluetooth.Core 2657 2658**Parameters** 2659 2660| Name | Type | Mandatory | Description | 2661| -------------- | --------------------------------- | ---- | --------------- | 2662| serverResponse | [ServerResponse](#serverresponse) | Yes | Response returned by the GATT server.| 2663 2664**Error codes** 2665 2666For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 2667 2668| ID| Error Message| 2669| -------- | ---------------------------- | 2670|2900001 | Service stopped. | 2671|2900003 | Bluetooth switch is off. | 2672|2900099 | Operation failed. | 2673 2674**Example** 2675 2676```js 2677/* send response */ 2678let arrayBufferCCC = new ArrayBuffer(8); 2679let cccValue = new Uint8Array(arrayBufferCCC); 2680cccValue[0] = 1123; 2681let serverResponse = { 2682 "deviceId": "XX:XX:XX:XX:XX:XX", 2683 "transId": 0, 2684 "status": 0, 2685 "offset": 0, 2686 "value": arrayBufferCCC, 2687}; 2688 2689let gattServer = bluetoothManager.BLE.createGattServer(); 2690try { 2691 gattServer.sendResponse(serverResponse); 2692} catch (err) { 2693 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2694} 2695``` 2696 2697 2698### on('characteristicRead') 2699 2700on(type: "characteristicRead", callback: Callback<CharacteristicReadRequest>): void 2701 2702Subscribes to the characteristic read request events. 2703 2704**Required permissions**: ohos.permission.USE_BLUETOOTH 2705 2706**System capability**: SystemCapability.Communication.Bluetooth.Core 2707 2708**Parameters** 2709 2710| Name | Type | Mandatory | Description | 2711| -------- | ---------------------------------------- | ---- | ------------------------------------- | 2712| type | string | Yes | Event type. The value **characteristicRead** indicates a characteristic read request event.| 2713| callback | Callback<[CharacteristicReadRequest](#characteristicreadrequest)> | Yes | Callback invoked to return a characteristic read request event from the GATT client. | 2714 2715**Example** 2716 2717```js 2718let arrayBufferCCC = new ArrayBuffer(8); 2719let cccValue = new Uint8Array(arrayBufferCCC); 2720cccValue[0] = 1123; 2721function ReadCharacteristicReq(CharacteristicReadRequest) { 2722 let deviceId = CharacteristicReadRequest.deviceId; 2723 let transId = CharacteristicReadRequest.transId; 2724 let offset = CharacteristicReadRequest.offset; 2725 let characteristicUuid = CharacteristicReadRequest.characteristicUuid; 2726 2727 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 2728 2729 try { 2730 gattServer.sendResponse(serverResponse); 2731 } catch (err) { 2732 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2733 } 2734} 2735 2736let gattServer = bluetoothManager.BLE.createGattServer(); 2737gattServer.on("characteristicRead", ReadCharacteristicReq); 2738``` 2739 2740 2741### off('characteristicRead') 2742 2743off(type: "characteristicRead", callback?: Callback<CharacteristicReadRequest>): void 2744 2745Unsubscribes from the characteristic read request events. 2746 2747**Required permissions**: ohos.permission.USE_BLUETOOTH 2748 2749**System capability**: SystemCapability.Communication.Bluetooth.Core 2750 2751**Parameters** 2752 2753| Name | Type | Mandatory | Description | 2754| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2755| type | string | Yes | Event type. The value **characteristicRead** indicates a characteristic read request event. | 2756| callback | Callback<[CharacteristicReadRequest](#characteristicreadrequest)> | No | Callback for the characteristic read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 2757 2758**Example** 2759 2760```js 2761let gattServer = bluetoothManager.BLE.createGattServer(); 2762gattServer.off("characteristicRead"); 2763``` 2764 2765 2766### on('characteristicWrite') 2767 2768on(type: "characteristicWrite", callback: Callback<CharacteristicWriteRequest>): void 2769 2770Subscribes to the characteristic write request events. 2771 2772**Required permissions**: ohos.permission.USE_BLUETOOTH 2773 2774**System capability**: SystemCapability.Communication.Bluetooth.Core 2775 2776**Parameters** 2777 2778| Name | Type | Mandatory | Description | 2779| -------- | ---------------------------------------- | ---- | -------------------------------------- | 2780| type | string | Yes | Event type. The value **characteristicWrite** indicates a characteristic write request event.| 2781| callback | Callback<[CharacteristicWriteRequest](#characteristicwriterequest)> | Yes | Callback invoked to return a characteristic write request from the GATT client. | 2782 2783**Example** 2784 2785```js 2786let arrayBufferCCC = new ArrayBuffer(8); 2787let cccValue = new Uint8Array(arrayBufferCCC); 2788function WriteCharacteristicReq(CharacteristicWriteRequest) { 2789 let deviceId = CharacteristicWriteRequest.deviceId; 2790 let transId = CharacteristicWriteRequest.transId; 2791 let offset = CharacteristicWriteRequest.offset; 2792 let isPrep = CharacteristicWriteRequest.isPrep; 2793 let needRsp = CharacteristicWriteRequest.needRsp; 2794 let value = new Uint8Array(CharacteristicWriteRequest.value); 2795 let characteristicUuid = CharacteristicWriteRequest.characteristicUuid; 2796 2797 cccValue[0] = value[0]; 2798 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 2799 2800 try { 2801 gattServer.sendResponse(serverResponse); 2802 } catch (err) { 2803 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2804 } 2805} 2806 2807let gattServer = bluetoothManager.BLE.createGattServer(); 2808gattServer.on("characteristicWrite", WriteCharacteristicReq); 2809``` 2810 2811 2812### off('characteristicWrite') 2813 2814off(type: "characteristicWrite", callback?: Callback<CharacteristicWriteRequest>): void 2815 2816Unsubscribes from the characteristic write request events. 2817 2818**Required permissions**: ohos.permission.USE_BLUETOOTH 2819 2820**System capability**: SystemCapability.Communication.Bluetooth.Core 2821 2822**Parameters** 2823 2824| Name | Type | Mandatory | Description | 2825| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2826| type | string | Yes | Event type. The value **characteristicWrite** indicates a characteristic write request event. | 2827| callback | Callback<[CharacteristicWriteRequest](#characteristicwriterequest)> | No | Callback for the characteristic write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 2828 2829**Example** 2830 2831```js 2832let gattServer = bluetoothManager.BLE.createGattServer(); 2833gattServer.off("characteristicWrite"); 2834``` 2835 2836 2837### on('descriptorRead') 2838 2839on(type: "descriptorRead", callback: Callback<DescriptorReadRequest>): void 2840 2841Subscribes to the descriptor read request events. 2842 2843**Required permissions**: ohos.permission.USE_BLUETOOTH 2844 2845**System capability**: SystemCapability.Communication.Bluetooth.Core 2846 2847**Parameters** 2848 2849| Name | Type | Mandatory | Description | 2850| -------- | ---------------------------------------- | ---- | --------------------------------- | 2851| type | string | Yes | Event type. The value **descriptorRead** indicates a descriptor read request event.| 2852| callback | Callback<[DescriptorReadRequest](#descriptorreadrequest)> | Yes | Callback invoked to return a descriptor read request event from the GATT client. | 2853 2854**Example** 2855 2856```js 2857let arrayBufferDesc = new ArrayBuffer(8); 2858let descValue = new Uint8Array(arrayBufferDesc); 2859descValue[0] = 1101; 2860function ReadDescriptorReq(DescriptorReadRequest) { 2861 let deviceId = DescriptorReadRequest.deviceId; 2862 let transId = DescriptorReadRequest.transId; 2863 let offset = DescriptorReadRequest.offset; 2864 let descriptorUuid = DescriptorReadRequest.descriptorUuid; 2865 2866 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 2867 2868 try { 2869 gattServer.sendResponse(serverResponse); 2870 } catch (err) { 2871 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2872 } 2873} 2874 2875let gattServer = bluetoothManager.BLE.createGattServer(); 2876gattServer.on("descriptorRead", ReadDescriptorReq); 2877``` 2878 2879 2880### off('descriptorRead') 2881 2882off(type: "descriptorRead", callback?: Callback<DescriptorReadRequest>): void 2883 2884Unsubscribes from the descriptor read request events. 2885 2886**Required permissions**: ohos.permission.USE_BLUETOOTH 2887 2888**System capability**: SystemCapability.Communication.Bluetooth.Core 2889 2890**Parameters** 2891 2892| Name | Type | Mandatory | Description | 2893| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2894| type | string | Yes | Event type. The value **descriptorRead** indicates a descriptor read request event. | 2895| callback | Callback<[DescriptorReadRequest](#descriptorreadrequest)> | No | Callback for the descriptor read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 2896 2897**Example** 2898 2899```js 2900let gattServer = bluetoothManager.BLE.createGattServer(); 2901gattServer.off("descriptorRead"); 2902``` 2903 2904 2905### on('descriptorWrite') 2906 2907on(type: "descriptorWrite", callback: Callback<DescriptorWriteRequest>): void 2908 2909Subscribes to the descriptor write request events. 2910 2911**Required permissions**: ohos.permission.USE_BLUETOOTH 2912 2913**System capability**: SystemCapability.Communication.Bluetooth.Core 2914 2915**Parameters** 2916 2917| Name | Type | Mandatory | Description | 2918| -------- | ---------------------------------------- | ---- | ---------------------------------- | 2919| type | string | Yes | Event type. The value **descriptorWrite** indicates a descriptor write request event.| 2920| callback | Callback<[DescriptorWriteRequest](#descriptorwriterequest)> | Yes | Callback invoked to return a descriptor write request from the GATT client. | 2921 2922**Example** 2923 2924```js 2925let arrayBufferDesc = new ArrayBuffer(8); 2926let descValue = new Uint8Array(arrayBufferDesc); 2927function WriteDescriptorReq(DescriptorWriteRequest) { 2928 let deviceId = DescriptorWriteRequest.deviceId; 2929 let transId = DescriptorWriteRequest.transId; 2930 let offset = DescriptorWriteRequest.offset; 2931 let isPrep = DescriptorWriteRequest.isPrep; 2932 let needRsp = DescriptorWriteRequest.needRsp; 2933 let value = new Uint8Array(DescriptorWriteRequest.value); 2934 let descriptorUuid = DescriptorWriteRequest.descriptorUuid; 2935 2936 descValue[0] = value[0]; 2937 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 2938 2939 try { 2940 gattServer.sendResponse(serverResponse); 2941 } catch (err) { 2942 console.error("errCode:" + err.code + ",errMessage:" + err.message); 2943 } 2944} 2945 2946let gattServer = bluetoothManager.BLE.createGattServer(); 2947gattServer.on("descriptorRead", WriteDescriptorReq); 2948``` 2949 2950 2951### off('descriptorWrite') 2952 2953off(type: "descriptorWrite", callback?: Callback<DescriptorWriteRequest>): void 2954 2955Unsubscribes from the descriptor write request events. 2956 2957**Required permissions**: ohos.permission.USE_BLUETOOTH 2958 2959**System capability**: SystemCapability.Communication.Bluetooth.Core 2960 2961**Parameters** 2962 2963| Name | Type | Mandatory | Description | 2964| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2965| type | string | Yes | Event type. The value **descriptorWrite** indicates a descriptor write request event. | 2966| callback | Callback<[DescriptorWriteRequest](#descriptorwriterequest)> | No | Callback for the descriptor write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 2967 2968**Example** 2969 2970```js 2971let gattServer = bluetoothManager.BLE.createGattServer(); 2972gattServer.off("descriptorWrite"); 2973``` 2974 2975 2976### on('connectStateChange') 2977 2978on(type: "connectStateChange", callback: Callback<BLEConnectChangedState>): void 2979 2980Subscribes to the BLE connection state change events. 2981 2982**Required permissions**: ohos.permission.USE_BLUETOOTH 2983 2984**System capability**: SystemCapability.Communication.Bluetooth.Core 2985 2986**Parameters** 2987 2988| Name | Type | Mandatory | Description | 2989| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2990| type | string | Yes | Event type. The value **connectStateChange** indicates a BLE connection state change event.| 2991| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | Yes | Callback invoked to return the BLE connection state. | 2992 2993**Example** 2994 2995```js 2996function Connected(BLEConnectChangedState) { 2997 let deviceId = BLEConnectChangedState.deviceId; 2998 let status = BLEConnectChangedState.state; 2999} 3000 3001let gattServer = bluetoothManager.BLE.createGattServer(); 3002gattServer.on("connectStateChange", Connected); 3003``` 3004 3005 3006### off('connectStateChange') 3007 3008off(type: "connectStateChange", callback?: Callback<BLEConnectChangedState>): void 3009 3010Unsubscribes from the BLE connection state change events. 3011 3012**Required permissions**: ohos.permission.USE_BLUETOOTH 3013 3014**System capability**: SystemCapability.Communication.Bluetooth.Core 3015 3016**Parameters** 3017 3018| Name | Type | Mandatory | Description | 3019| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3020| type | string | Yes | Event type. The value **connectStateChange** indicates a BLE connection state change event.| 3021| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | No | Callback for the BLE connection state change event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 3022 3023**Example** 3024 3025```js 3026let gattServer = bluetoothManager.BLE.createGattServer(); 3027gattServer.off("connectStateChange"); 3028``` 3029 3030 3031## GattClientDevice 3032 3033Implements the GATT client. Before using an API of this class, you must create a **GattClientDevice** instance using **createGattClientDevice(deviceId: string)**. 3034 3035 3036### connect 3037 3038connect(): void 3039 3040Initiates a connection to the remote BLE device. 3041 3042**Required permissions**: ohos.permission.USE_BLUETOOTH 3043 3044**System capability**: SystemCapability.Communication.Bluetooth.Core 3045 3046**Error codes** 3047 3048For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3049 3050| ID| Error Message| 3051| -------- | ---------------------------- | 3052|2900001 | Service stopped. | 3053|2900003 | Bluetooth switch is off. | 3054|2900099 | Operation failed. | 3055 3056**Example** 3057 3058```js 3059try { 3060 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3061 device.connect(); 3062} catch (err) { 3063 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3064} 3065``` 3066 3067 3068### disconnect 3069 3070disconnect(): void 3071 3072Disconnects from the remote BLE device. 3073 3074**Required permissions**: ohos.permission.USE_BLUETOOTH 3075 3076**System capability**: SystemCapability.Communication.Bluetooth.Core 3077 3078**Error codes** 3079 3080For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3081 3082| ID| Error Message| 3083| -------- | ---------------------------- | 3084|2900001 | Service stopped. | 3085|2900003 | Bluetooth switch is off. | 3086|2900099 | Operation failed. | 3087 3088**Example** 3089 3090```js 3091try { 3092 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3093 device.disconnect(); 3094} catch (err) { 3095 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3096} 3097``` 3098 3099 3100### close 3101 3102close(): void 3103 3104Closes this GATT client to unregister it from the protocol stack. After this API is called, this [GattClientDevice](#gattclientdevice) instance cannot be used. 3105 3106**Required permissions**: ohos.permission.USE_BLUETOOTH 3107 3108**System capability**: SystemCapability.Communication.Bluetooth.Core 3109 3110**Error codes** 3111 3112For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3113 3114| ID| Error Message| 3115| -------- | ---------------------------- | 3116|2900001 | Service stopped. | 3117|2900003 | Bluetooth switch is off. | 3118|2900099 | Operation failed. | 3119 3120**Example** 3121 3122```js 3123try { 3124 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3125 device.close(); 3126} catch (err) { 3127 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3128} 3129``` 3130 3131 3132 3133 3134### getServices 3135 3136getServices(callback: AsyncCallback<Array<GattService>>): void 3137 3138Obtains all services of the remote BLE device. This API uses an asynchronous callback to return the result. 3139 3140**Required permissions**: ohos.permission.USE_BLUETOOTH 3141 3142**System capability**: SystemCapability.Communication.Bluetooth.Core 3143 3144**Parameters** 3145 3146| Name | Type | Mandatory | Description | 3147| -------- | ---------------------------------------- | ---- | ------------------------ | 3148| callback | AsyncCallback<Array<[GattService](#gattservice)>> | Yes | Callback invoked to return the services obtained.| 3149 3150**Error codes** 3151 3152For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3153 3154| ID| Error Message| 3155| -------- | ---------------------------- | 3156|2900001 | Service stopped. | 3157|2900099 | Operation failed. | 3158 3159**Example** 3160 3161```js 3162// Callback 3163function getServices(code, gattServices) { 3164 if (code.code == 0) { 3165 let services = gattServices; 3166 console.log('bluetooth code is ' + code.code); 3167 console.log("bluetooth services size is ", services.length); 3168 3169 for (let i = 0; i < services.length; i++) { 3170 console.log('bluetooth serviceUuid is ' + services[i].serviceUuid); 3171 } 3172 } 3173} 3174 3175try { 3176 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3177 device.connect(); 3178 device.getServices(getServices); 3179} catch (err) { 3180 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3181} 3182``` 3183 3184 3185### getServices 3186 3187getServices(): Promise<Array<GattService>> 3188 3189Obtains all services of the remote BLE device. This API uses a promise to return the result. 3190 3191**Required permissions**: ohos.permission.USE_BLUETOOTH 3192 3193**System capability**: SystemCapability.Communication.Bluetooth.Core 3194 3195**Return value** 3196 3197| Type | Description | 3198| ---------------------------------------- | --------------------------- | 3199| Promise<Array<[GattService](#gattservice)>> | Promise used to return the services obtained.| 3200 3201**Error codes** 3202 3203For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3204 3205| ID| Error Message| 3206| -------- | ---------------------------- | 3207|2900001 | Service stopped. | 3208|2900099 | Operation failed. | 3209 3210**Example** 3211 3212```js 3213// Promise 3214try { 3215 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3216 device.connect(); 3217 device.getServices().then(result => { 3218 console.info("getServices successfully:" + JSON.stringify(result)); 3219 }); 3220} catch (err) { 3221 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3222} 3223``` 3224 3225 3226### readCharacteristicValue 3227 3228readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback<BLECharacteristic>): void 3229 3230Reads the characteristic value of the specific service of the remote BLE device. This API uses an asynchronous callback to return the result. 3231 3232**Required permissions**: ohos.permission.USE_BLUETOOTH 3233 3234**System capability**: SystemCapability.Communication.Bluetooth.Core 3235 3236**Parameters** 3237 3238| Name | Type | Mandatory | Description | 3239| -------------- | ---------------------------------------- | ---- | ----------------------- | 3240| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | Characteristic value to read. | 3241| callback | AsyncCallback<[BLECharacteristic](#blecharacteristic)> | Yes | Callback invoked to return the characteristic value read.| 3242 3243**Error codes** 3244 3245For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3246 3247| ID| Error Message| 3248| -------- | ---------------------------- | 3249|2900001 | Service stopped. | 3250|2901000 | Read forbidden. | 3251|2900099 | Operation failed. | 3252 3253**Example** 3254 3255```js 3256function readCcc(code, BLECharacteristic) { 3257 if (code.code != 0) { 3258 return; 3259 } 3260 console.log('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid); 3261 let value = new Uint8Array(BLECharacteristic.characteristicValue); 3262 console.log('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 3263} 3264 3265let descriptors = []; 3266let bufferDesc = new ArrayBuffer(8); 3267let descV = new Uint8Array(bufferDesc); 3268descV[0] = 11; 3269let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3270characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3271descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3272descriptors[0] = descriptor; 3273 3274let bufferCCC = new ArrayBuffer(8); 3275let cccV = new Uint8Array(bufferCCC); 3276cccV[0] = 1; 3277let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3278characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3279characteristicValue: bufferCCC, descriptors:descriptors}; 3280 3281try { 3282 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3283 device.readCharacteristicValue(characteristic, readCcc); 3284} catch (err) { 3285 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3286} 3287``` 3288 3289 3290### readCharacteristicValue 3291 3292readCharacteristicValue(characteristic: BLECharacteristic): Promise<BLECharacteristic> 3293 3294Reads the characteristic value of the specific service of the remote BLE device. This API uses an asynchronous callback to return the result. 3295 3296**Required permissions**: ohos.permission.USE_BLUETOOTH 3297 3298**System capability**: SystemCapability.Communication.Bluetooth.Core 3299 3300**Parameters** 3301 3302| Name | Type | Mandatory | Description | 3303| -------------- | --------------------------------------- | ---- | -------- | 3304| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | Characteristic value to read.| 3305 3306**Return value** 3307 3308| Type | Description | 3309| ---------------------------------------- | -------------------------- | 3310| Promise<[BLECharacteristic](#blecharacteristic)> | Promise used to return the characteristic value read.| 3311 3312**Error codes** 3313 3314For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3315 3316| ID| Error Message| 3317| -------- | ---------------------------- | 3318|2900001 | Service stopped. | 3319|2901000 | Read forbidden. | 3320|2900099 | Operation failed. | 3321 3322**Example** 3323 3324```js 3325let descriptors = []; 3326let bufferDesc = new ArrayBuffer(8); 3327let descV = new Uint8Array(bufferDesc); 3328descV[0] = 11; 3329let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3330characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3331descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3332descriptors[0] = descriptor; 3333 3334let bufferCCC = new ArrayBuffer(8); 3335let cccV = new Uint8Array(bufferCCC); 3336cccV[0] = 1; 3337let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3338characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3339characteristicValue: bufferCCC, descriptors:descriptors}; 3340 3341try { 3342 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3343 device.readCharacteristicValue(characteristic); 3344} catch (err) { 3345 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3346} 3347``` 3348 3349 3350### readDescriptorValue 3351 3352readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<BLEDescriptor>): void 3353 3354Reads the descriptor contained in the specific characteristic of the remote BLE device. This API uses an asynchronous callback to return the result. 3355 3356**Required permissions**: ohos.permission.USE_BLUETOOTH 3357 3358**System capability**: SystemCapability.Communication.Bluetooth.Core 3359 3360**Parameters** 3361 3362| Name | Type | Mandatory | Description | 3363| ---------- | ---------------------------------------- | ---- | ----------------------- | 3364| descriptor | [BLEDescriptor](#bledescriptor) | Yes | Descriptor to read. | 3365| callback | AsyncCallback<[BLEDescriptor](#bledescriptor)> | Yes | Callback invoked to return the descriptor read.| 3366 3367**Error codes** 3368 3369For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3370 3371| ID| Error Message| 3372| -------- | ---------------------------- | 3373|2900001 | Service stopped. | 3374|2901000 | Read forbidden. | 3375|2900099 | Operation failed. | 3376 3377**Example** 3378 3379```js 3380function readDesc(code, BLEDescriptor) { 3381 if (code.code != 0) { 3382 return; 3383 } 3384 console.log('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid); 3385 let value = new Uint8Array(BLEDescriptor.descriptorValue); 3386 console.log('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 3387} 3388 3389let bufferDesc = new ArrayBuffer(8); 3390let descV = new Uint8Array(bufferDesc); 3391descV[0] = 11; 3392let descriptor = { 3393 serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3394 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3395 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3396 descriptorValue: bufferDesc 3397}; 3398try { 3399 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3400 device.readDescriptorValue(descriptor, readDesc); 3401} catch (err) { 3402 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3403} 3404``` 3405 3406 3407### readDescriptorValue 3408 3409readDescriptorValue(descriptor: BLEDescriptor): Promise<BLEDescriptor> 3410 3411Reads the descriptor contained in the specific characteristic of the remote BLE device. This API uses an asynchronous callback to return the result. 3412 3413**Required permissions**: ohos.permission.USE_BLUETOOTH 3414 3415**System capability**: SystemCapability.Communication.Bluetooth.Core 3416 3417**Parameters** 3418 3419| Name | Type | Mandatory | Description | 3420| ---------- | ------------------------------- | ---- | -------- | 3421| descriptor | [BLEDescriptor](#bledescriptor) | Yes | Descriptor to read.| 3422 3423**Return value** 3424 3425| Type | Description | 3426| ---------------------------------------- | -------------------------- | 3427| Promise<[BLEDescriptor](#bledescriptor)> | Promise used to return the descriptor read.| 3428 3429**Error codes** 3430 3431For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3432 3433| ID| Error Message| 3434| -------- | ---------------------------- | 3435|2900001 | Service stopped. | 3436|2901000 | Read forbidden. | 3437|2900099 | Operation failed. | 3438 3439**Example** 3440 3441```js 3442let bufferDesc = new ArrayBuffer(8); 3443let descV = new Uint8Array(bufferDesc); 3444descV[0] = 11; 3445let descriptor = { 3446 serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3447 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3448 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3449 descriptorValue: bufferDesc 3450}; 3451try { 3452 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3453 device.readDescriptorValue(descriptor); 3454} catch (err) { 3455 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3456} 3457``` 3458 3459 3460### writeCharacteristicValue 3461 3462writeCharacteristicValue(characteristic: BLECharacteristic): void 3463 3464Writes a characteristic value to the remote BLE device. 3465 3466**Required permissions**: ohos.permission.USE_BLUETOOTH 3467 3468**System capability**: SystemCapability.Communication.Bluetooth.Core 3469 3470**Parameters** 3471 3472| Name | Type | Mandatory | Description | 3473| -------------- | --------------------------------------- | ---- | ------------------- | 3474| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | Binary value and other parameters of the BLE device characteristic.| 3475 3476**Error codes** 3477 3478For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3479 3480| ID| Error Message| 3481| -------- | ---------------------------- | 3482|2900001 | Service stopped. | 3483|2901001 | Write forbidden. | 3484|2900099 | Operation failed. | 3485 3486**Example** 3487 3488```js 3489let descriptors = []; 3490let bufferDesc = new ArrayBuffer(8); 3491let descV = new Uint8Array(bufferDesc); 3492descV[0] = 11; 3493let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3494 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3495 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3496descriptors[0] = descriptor; 3497 3498let bufferCCC = new ArrayBuffer(8); 3499let cccV = new Uint8Array(bufferCCC); 3500cccV[0] = 1; 3501let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3502 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3503 characteristicValue: bufferCCC, descriptors:descriptors}; 3504try { 3505 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3506 device.writeCharacteristicValue(characteristic); 3507} catch (err) { 3508 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3509} 3510``` 3511 3512 3513### writeDescriptorValue 3514 3515writeDescriptorValue(descriptor: BLEDescriptor): void 3516 3517Writes binary data to the specific descriptor of the remote BLE device. 3518 3519**Required permissions**: ohos.permission.USE_BLUETOOTH 3520 3521**System capability**: SystemCapability.Communication.Bluetooth.Core 3522 3523**Parameters** 3524 3525| Name | Type | Mandatory | Description | 3526| ---------- | ------------------------------- | ---- | ------------------ | 3527| descriptor | [BLEDescriptor](#bledescriptor) | Yes | Binary value and other parameters of the BLE device descriptor.| 3528| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 3529 3530**Error codes** 3531 3532For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3533 3534| ID| Error Message| 3535| -------- | ---------------------------- | 3536|2900001 | Service stopped. | 3537|2901001 | Write forbidden. | 3538|2900099 | Operation failed. | 3539 3540**Example** 3541 3542```js 3543let bufferDesc = new ArrayBuffer(8); 3544let descV = new Uint8Array(bufferDesc); 3545descV[0] = 22; 3546let descriptor = { 3547 serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3548 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3549 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3550 descriptorValue: bufferDesc 3551}; 3552try { 3553 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3554 device.writeDescriptorValue(descriptor); 3555} catch (err) { 3556 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3557} 3558``` 3559 3560 3561### setBLEMtuSize 3562 3563setBLEMtuSize(mtu: number): void 3564 3565Sets the maximum transmission unit (MTU) that can be transmitted between the GATT client and its remote BLE device. This API can be used only after a connection is set up by calling [connect](#connect). 3566 3567**Required permissions**: ohos.permission.USE_BLUETOOTH 3568 3569**System capability**: SystemCapability.Communication.Bluetooth.Core 3570 3571**Parameters** 3572 3573| Name | Type | Mandatory | Description | 3574| ---- | ------ | ---- | -------------- | 3575| mtu | number | Yes | MTU to set, which ranges from 22 to 512 bytes.| 3576 3577**Error codes** 3578 3579For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3580 3581| ID| Error Message| 3582| -------- | ---------------------------- | 3583|2900001 | Service stopped. | 3584|2900099 | Operation failed. | 3585 3586**Example** 3587 3588```js 3589try { 3590 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3591 device.setBLEMtuSize(128); 3592} catch (err) { 3593 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3594} 3595``` 3596 3597 3598### setNotifyCharacteristicChanged 3599 3600setNotifyCharacteristicChanged(characteristic: BLECharacteristic, enable: boolean): void 3601 3602Sets the function of notifying the GATT client when the characteristic value of the remote BLE device changes. 3603 3604**Required permissions**: ohos.permission.USE_BLUETOOTH 3605 3606**System capability**: SystemCapability.Communication.Bluetooth.Core 3607 3608**Parameters** 3609 3610| Name | Type | Mandatory | Description | 3611| -------------- | --------------------------------------- | ---- | ----------------------------- | 3612| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | BLE characteristic to listen for. | 3613| enable | boolean | Yes | Whether to enable the notify function. The value **true** means to enable the notify function, and the value **false** means the opposite.| 3614 3615**Error codes** 3616 3617For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3618 3619| ID| Error Message| 3620| -------- | ---------------------------- | 3621|2900001 | Service stopped. | 3622|2900099 | Operation failed. | 3623 3624**Example** 3625 3626```js 3627// Create descriptors. 3628let descriptors = []; 3629let arrayBuffer = new ArrayBuffer(8); 3630let descV = new Uint8Array(arrayBuffer); 3631descV[0] = 11; 3632let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3633 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3634 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 3635descriptors[0] = descriptor; 3636let arrayBufferC = new ArrayBuffer(8); 3637let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3638 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 3639try { 3640 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3641 device.setNotifyCharacteristicChanged(characteristic, false); 3642} catch (err) { 3643 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3644} 3645 3646``` 3647 3648 3649### on('BLECharacteristicChange') 3650 3651on(type: "BLECharacteristicChange", callback: Callback<BLECharacteristic>): void 3652 3653Subscribes to the BLE characteristic change events. The client can receive a notification from the server only after **setNotifyCharacteristicChanged** is called. 3654 3655**Required permissions**: ohos.permission.USE_BLUETOOTH 3656 3657**System capability**: SystemCapability.Communication.Bluetooth.Core 3658 3659**Parameters** 3660 3661| Name | Type | Mandatory | Description | 3662| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3663| type | string | Yes | Event type. The value **BLECharacteristicChange** indicates a characteristic value change event.| 3664| callback | Callback<[BLECharacteristic](#blecharacteristic)> | Yes | Callback invoked to return the characteristic value changes. | 3665 3666**Example** 3667 3668```js 3669function CharacteristicChange(CharacteristicChangeReq) { 3670 let serviceUuid = CharacteristicChangeReq.serviceUuid; 3671 let characteristicUuid = CharacteristicChangeReq.characteristicUuid; 3672 let value = new Uint8Array(CharacteristicChangeReq.characteristicValue); 3673} 3674try { 3675 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3676 device.on('BLECharacteristicChange', CharacteristicChange); 3677} catch (err) { 3678 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3679} 3680``` 3681 3682 3683### off('BLECharacteristicChange') 3684 3685off(type: "BLECharacteristicChange", callback?: Callback<BLECharacteristic>): void 3686 3687Unsubscribes from the BLE characteristic change events. 3688 3689**Required permissions**: ohos.permission.USE_BLUETOOTH 3690 3691**System capability**: SystemCapability.Communication.Bluetooth.Core 3692 3693**Parameters** 3694 3695| Name | Type | Mandatory | Description | 3696| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3697| type | string | Yes | Event type. The value **BLECharacteristicChange** indicates a characteristic value change event.| 3698| callback | Callback<[BLECharacteristic](#blecharacteristic)> | No | Callback for the characteristic value change event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 3699 3700**Example** 3701 3702```js 3703try { 3704 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3705 device.off('BLECharacteristicChange'); 3706} catch (err) { 3707 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3708} 3709``` 3710 3711 3712### on('BLEConnectionStateChange') 3713 3714on(type: "BLEConnectionStateChange", callback: Callback<BLEConnectChangedState>): void 3715 3716Subscribes to the BLE connection state change events. 3717 3718**Required permissions**: ohos.permission.USE_BLUETOOTH 3719 3720**System capability**: SystemCapability.Communication.Bluetooth.Core 3721 3722**Parameters** 3723 3724| Name | Type | Mandatory | Description | 3725| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3726| type | string | Yes | Event type. The value **BLEConnectionStateChange** indicates a BLE connection state change event.| 3727| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | Yes | Callback invoked to return the BLE connection state. | 3728 3729**Example** 3730 3731```js 3732function ConnectStateChanged(state) { 3733 console.log('bluetooth connect state changed'); 3734 let connectState = state.state; 3735} 3736try { 3737 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3738 device.on('BLEConnectionStateChange', ConnectStateChanged); 3739} catch (err) { 3740 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3741} 3742``` 3743 3744 3745### off('BLEConnectionStateChange') 3746 3747off(type: "BLEConnectionStateChange", callback?: Callback<BLEConnectChangedState>): void 3748 3749Unsubscribes from the BLE connection state change events. 3750 3751**Required permissions**: ohos.permission.USE_BLUETOOTH 3752 3753**System capability**: SystemCapability.Communication.Bluetooth.Core 3754 3755**Parameters** 3756 3757| Name | Type | Mandatory | Description | 3758| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3759| type | string | Yes | Event type. The value **BLEConnectionStateChange** indicates a BLE connection state change event.| 3760| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | No | Callback for the BLE connection state change event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 3761 3762**Example** 3763 3764```js 3765try { 3766 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3767 device.off('BLEConnectionStateChange'); 3768} catch (err) { 3769 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3770} 3771``` 3772 3773 3774### getDeviceName 3775 3776getDeviceName(callback: AsyncCallback<string>): void 3777 3778Obtains the name of the remote BLE device. This API uses an asynchronous callback to return the result. 3779 3780**Required permissions**: ohos.permission.USE_BLUETOOTH 3781 3782**System capability**: SystemCapability.Communication.Bluetooth.Core 3783 3784**Parameters** 3785 3786| Name | Type | Mandatory | Description | 3787| -------- | --------------------------- | ---- | ------------------------------- | 3788| callback | AsyncCallback<string> | Yes | Callback invoked to return the remote BLE device name obtained.| 3789 3790**Error codes** 3791 3792For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3793 3794| ID| Error Message| 3795| -------- | ---------------------------- | 3796|2900001 | Service stopped. | 3797|2900099 | Operation failed. | 3798 3799**Example** 3800 3801```js 3802// callback 3803try { 3804 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 3805 gattClient.connect(); 3806 let deviceName = gattClient.getDeviceName((err, data)=> { 3807 console.info('device name err ' + JSON.stringify(err)); 3808 console.info('device name' + JSON.stringify(data)); 3809 }) 3810} catch (err) { 3811 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3812} 3813``` 3814 3815 3816### getDeviceName 3817 3818getDeviceName(): Promise<string> 3819 3820Obtains the name of the remote BLE device. This API uses a promise to return the result. 3821 3822**Required permissions**: ohos.permission.USE_BLUETOOTH 3823 3824**System capability**: SystemCapability.Communication.Bluetooth.Core 3825 3826**Return value** 3827 3828| Type | Description | 3829| --------------------- | ---------------------------------- | 3830| Promise<string> | Promise used to return the remote BLE device name.| 3831 3832**Error codes** 3833 3834For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3835 3836| ID| Error Message| 3837| -------- | ---------------------------- | 3838|2900001 | Service stopped. | 3839|2900099 | Operation failed. | 3840 3841**Example** 3842 3843```js 3844// promise 3845try { 3846 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 3847 gattClient.connect(); 3848 let deviceName = gattClient.getDeviceName().then((data) => { 3849 console.info('device name' + JSON.stringify(data)); 3850 }) 3851} catch (err) { 3852 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3853} 3854``` 3855 3856 3857### getRssiValue 3858 3859getRssiValue(callback: AsyncCallback<number>): void 3860 3861Obtains the received signal strength indication (RSSI) of the remote BLE device. This API uses an asynchronous callback to return the result. It can be used only after a connection is set up by calling [connect](#connect). 3862 3863**Required permissions**: ohos.permission.USE_BLUETOOTH 3864 3865**System capability**: SystemCapability.Communication.Bluetooth.Core 3866 3867**Parameters** 3868 3869| Name | Type | Mandatory | Description | 3870| -------- | --------------------------- | ---- | ------------------------------ | 3871| callback | AsyncCallback<number> | Yes | Callback invoked to return the RSSI, in dBm.| 3872 3873**Error codes** 3874 3875For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3876 3877| ID| Error Message| 3878| -------- | ---------------------------- | 3879|2900099 | Operation failed. | 3880 3881**Example** 3882 3883```js 3884// callback 3885try { 3886 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 3887 gattClient.connect(); 3888 let rssi = gattClient.getRssiValue((err, data)=> { 3889 console.info('rssi err ' + JSON.stringify(err)); 3890 console.info('rssi value' + JSON.stringify(data)); 3891 }) 3892} catch (err) { 3893 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3894} 3895``` 3896 3897 3898### getRssiValue 3899 3900getRssiValue(): Promise<number> 3901 3902Obtains the RSSI of the remote BLE device. This API uses a promise to return the result. It can be used only after a connection is set up by calling [connect](#connect). 3903 3904**Required permissions**: ohos.permission.USE_BLUETOOTH 3905 3906**System capability**: SystemCapability.Communication.Bluetooth.Core 3907 3908**Return value** 3909 3910| Type | Description | 3911| --------------------- | --------------------------------- | 3912| Promise<number> | Promise used to return the RSSI, in dBm.| 3913 3914**Error codes** 3915 3916For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 3917 3918| ID| Error Message| 3919| -------- | ---------------------------- | 3920|2900099 | Operation failed. | 3921 3922**Example** 3923 3924```js 3925// promise 3926try { 3927 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 3928 let rssi = gattClient.getRssiValue().then((data) => { 3929 console.info('rssi' + JSON.stringify(data)); 3930 }) 3931} catch (err) { 3932 console.error("errCode:" + err.code + ",errMessage:" + err.message); 3933} 3934``` 3935 3936## ScanMode<a name="ScanMode"></a> 3937 3938Enumerates the scan modes. 3939 3940**System capability**: SystemCapability.Communication.Bluetooth.Core 3941 3942| Name | Value | Description | 3943| ---------------------------------------- | ---- | --------------- | 3944| SCAN_MODE_NONE | 0 | No scan mode. | 3945| SCAN_MODE_CONNECTABLE | 1 | Connectable mode. | 3946| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | General discoverable mode. | 3947| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | Limited discoverable mode. | 3948| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | General connectable and discoverable mode.| 3949| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | Limited connectable and discoverable mode.| 3950 3951## BondState<a name="BondState"></a> 3952 3953Enumerates the pairing states. 3954 3955**System capability**: SystemCapability.Communication.Bluetooth.Core 3956 3957| Name | Value | Description | 3958| ------------------ | ---- | ------ | 3959| BOND_STATE_INVALID | 0 | Invalid pairing.| 3960| BOND_STATE_BONDING | 1 | Pairing. | 3961| BOND_STATE_BONDED | 2 | Paired. | 3962 3963 3964## SppOption<a name="SppOption"></a> 3965 3966Defines the SPP configuration parameters. 3967 3968**System capability**: SystemCapability.Communication.Bluetooth.Core 3969 3970| Name | Type | Readable | Writable | Description | 3971| ------ | ------------------- | ---- | ---- | ----------- | 3972| uuid | string | Yes | Yes | UUID of the SPP.| 3973| secure | boolean | Yes | Yes | Whether it is a secure channel. | 3974| type | [SppType](#spptype) | Yes | Yes | Type of the SPP link. | 3975 3976 3977## SppType<a name="SppType"></a> 3978 3979Enumerates the SPP link types. 3980 3981**System capability**: SystemCapability.Communication.Bluetooth.Core 3982 3983| Name | Value | Description | 3984| ---------- | ---- | ------------- | 3985| SPP_RFCOMM | 0 | Radio frequency communication (RFCOMM) link type.| 3986 3987 3988## GattService 3989 3990Defines the GATT service API parameters. 3991 3992**System capability**: SystemCapability.Communication.Bluetooth.Core 3993 3994| Name | Type | Readable | Writable | Description | 3995| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 3996| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3997| isPrimary | boolean | Yes | Yes | Whether the service is a primary service. The value **true** means a primary service. | 3998| characteristics | Array<[BLECharacteristic](#blecharacteristic)> | Yes | Yes | List of characteristics of the service. | 3999| includeServices | Array<[GattService](#gattservice)> | Yes | Yes | Services on which the service depends. | 4000 4001 4002## BLECharacteristic 4003 4004Defines the characteristic API parameters. 4005 4006**System capability**: SystemCapability.Communication.Bluetooth.Core 4007 4008| Name | Type | Readable | Writable | Description | 4009| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 4010| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4011| characteristicUuid | string | Yes | Yes | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4012| characteristicValue | ArrayBuffer | Yes | Yes | Binary value of the characteristic. | 4013| descriptors | Array<[BLEDescriptor](#bledescriptor)> | Yes | Yes | List of descriptors of the characteristic. | 4014 4015 4016## BLEDescriptor 4017 4018Defines the descriptor API parameters. 4019 4020**System capability**: SystemCapability.Communication.Bluetooth.Core 4021 4022| Name | Type | Readable | Writable | Description | 4023| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 4024| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4025| characteristicUuid | string | Yes | Yes | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4026| descriptorUuid | string | Yes | Yes | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 4027| descriptorValue | ArrayBuffer | Yes | Yes | Binary value of the descriptor. | 4028 4029 4030## NotifyCharacteristic 4031 4032Defines the parameters in the notifications sent when the server characteristic value changes. 4033 4034**System capability**: SystemCapability.Communication.Bluetooth.Core 4035 4036| Name | Type | Readable | Writable | Description | 4037| ------------------- | ----------- | ---- | ---- | ---------------------------------------- | 4038| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4039| characteristicUuid | string | Yes | Yes | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4040| characteristicValue | ArrayBuffer | Yes | Yes | Binary value of the characteristic. | 4041| confirm | boolean | Yes | Yes | Whether the notification needs to be confirmed by the remote end. For a notification, set it to **true**. In this case, the remote end must confirm the receipt of the notification. For an indication, set it to **false**. In this case, the remote end does not need to confirm the receipt of the notification.| 4042 4043 4044## CharacteristicReadRequest 4045 4046Defines the parameters of the **CharacteristicReadReq** event received by the server. 4047 4048**System capability**: SystemCapability.Communication.Bluetooth.Core 4049 4050| Name | Type | Readable | Writable | Description | 4051| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 4052| deviceId | string | Yes | No | Address of the remote device that sends the **CharacteristicReadReq** event, for example, XX:XX:XX:XX:XX:XX.| 4053| transId | number | Yes | No | Transmission ID of the read request. The response returned by the server must use the same transmission ID. | 4054| offset | number | Yes | No | Position from which the characteristic value is read. For example, **k** means to read from the kth byte. The response returned by the server must use the same offset.| 4055| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4056| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4057 4058 4059## CharacteristicWriteRequest 4060 4061Defines the parameters of the **CharacteristicWriteReq** event received by the server. 4062 4063**System capability**: SystemCapability.Communication.Bluetooth.Core 4064 4065| Name | Type | Readable | Writable | Description | 4066| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 4067| deviceId | string | Yes | No | Address of the remote device that sends the **CharacteristicWriteReq** event, for example, XX:XX:XX:XX:XX:XX.| 4068| transId | number | Yes | No | Transmission ID of the write request. The response returned by the server must use the same transmission ID. | 4069| offset | number | Yes | No | Start position for writing the characteristic value. For example, **k** means to write from the kth byte. The response returned by the server must use the same offset.| 4070| descriptorUuid | string | Yes | No | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 4071| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4072| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4073 4074 4075## DescriptorReadRequest 4076 4077Defines the parameters of the **DescriptorReadReq** event received by the server. 4078 4079**System capability**: SystemCapability.Communication.Bluetooth.Core 4080 4081| Name | Type | Readable | Writable | Description | 4082| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 4083| deviceId | string | Yes | No | Address of the remote device that sends a **DescriptorReadReq** event, for example, XX:XX:XX:XX:XX:XX.| 4084| transId | number | Yes | No | Transmission ID of the read request. The response returned by the server must use the same transmission ID. | 4085| offset | number | Yes | No | Position from which the descriptor is read. For example, **k** means to read from the kth byte. The response returned by the server must use the same offset.| 4086| descriptorUuid | string | Yes | No | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 4087| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4088| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4089 4090 4091## DescriptorWriteRequest 4092 4093Defines the parameters of the **DescriptorWriteReq** event received by the server. 4094 4095**System capability**: SystemCapability.Communication.Bluetooth.Core 4096 4097| Name | Type | Readable | Writable | Description | 4098| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 4099| deviceId | string | Yes | No | Address of the remote device that sends a **DescriptorWriteReq** event, for example, XX:XX:XX:XX:XX:XX.| 4100| transId | number | Yes | No | Transmission ID of the write request. The response returned by the server must use the same transmission ID. | 4101| offset | number | Yes | No | Start position for writing the descriptor. For example, **k** means to write from the kth byte. The response returned by the server must use the same offset.| 4102| isPrep | boolean | Yes | No | Whether the write request is executed immediately. | 4103| needRsp | boolean | Yes | No | Whether to send a response to the GATT client. | 4104| value | ArrayBuffer | Yes | No | Binary value of the descriptor to write. | 4105| descriptorUuid | string | Yes | No | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 4106| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 4107| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4108 4109 4110## ServerResponse 4111 4112Defines the parameters of the server's response to the GATT client's read/write request. 4113 4114**System capability**: SystemCapability.Communication.Bluetooth.Core 4115 4116| Name | Type | Readable | Writable | Description | 4117| -------- | ----------- | ---- | ---- | -------------------------------------- | 4118| deviceId | string | Yes | No | Address of the remote device, for example, XX:XX:XX:XX:XX:XX. | 4119| transId | number | Yes | No | Transmission ID of the request. The value must be the same as the ID carried in the read/write request received. | 4120| status | number | Yes | No | Response state. Set this parameter to **0**, which indicates a normal response. | 4121| offset | number | Yes | No | Start read/write position. The value must be the same as the offset carried in the read/write request.| 4122| value | ArrayBuffer | Yes | No | Binary data in the response. | 4123 4124 4125## BLEConnectChangedState 4126 4127Defines the parameters of **BLEConnectChangedState**. 4128 4129**System capability**: SystemCapability.Communication.Bluetooth.Core 4130 4131| Name | Type | Readable| Writable| Description | 4132| -------- | ------------------------------------------------- | ---- | ---- | --------------------------------------------- | 4133| deviceId | string | Yes | No | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 4134| state | [ProfileConnectionState](#profileconnectionstate) | Yes | Yes | BLE connection state. | 4135 4136 4137## ProfileConnectionState 4138 4139Enumerates the profile connection states. 4140 4141**System capability**: SystemCapability.Communication.Bluetooth.Core 4142 4143| Name | Value | Description | 4144| ------------------- | ---- | -------------- | 4145| STATE_DISCONNECTED | 0 | Disconnected. | 4146| STATE_CONNECTING | 1 | Connecting.| 4147| STATE_CONNECTED | 2 | Connected. | 4148| STATE_DISCONNECTING | 3 | Disconnecting.| 4149 4150 4151## ScanFilter 4152 4153Defines the scan filter parameters. 4154 4155**System capability**: SystemCapability.Communication.Bluetooth.Core 4156 4157| Name | Type | Readable| Writable| Description | 4158| ---------------------------------------- | ----------- | ---- | ---- | ------------------------------------------------------------ | 4159| deviceId | string | Yes | Yes | Address of the BLE device to filter, for example, XX:XX:XX:XX:XX:XX. | 4160| name | string | Yes | Yes | Name of the BLE device to filter. | 4161| serviceUuid | string | Yes | Yes | Service UUID of the device to filter, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 4162| serviceUuidMask | string | Yes | Yes | Service UUID mask of the device to filter, for example, **FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF**.| 4163| serviceSolicitationUuid | string | Yes | Yes | Service solicitation UUID of the device to filter, for example, **00001888-0000-1000-8000-00805F9B34FB**.| 4164| serviceSolicitationUuidMask | string | Yes | Yes | Service solicitation UUID mask of the device to filter, for example, **FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF**.| 4165| serviceData | ArrayBuffer | Yes | Yes | Service data of the device to filter, for example, **[0x90, 0x00, 0xF1, 0xF2]**.| 4166| serviceDataMask | ArrayBuffer | Yes | Yes | Service data mask of the device to filter, for example, **[0xFF,0xFF,0xFF,0xFF]**.| 4167| manufactureId | number | Yes | Yes | Manufacturer ID of the device to filter, for example, **0x0006**. | 4168| manufactureData | ArrayBuffer | Yes | Yes | Manufacturer data of the device to filter, for example, **[0x1F,0x2F,0x3F]**.| 4169| manufactureDataMask | ArrayBuffer | Yes | Yes | Manufacturer data mask of the device to filter, for example, **[0xFF, 0xFF, 0xFF]**.| 4170 4171 4172## ScanOptions 4173 4174Defines the scan configuration parameters. 4175 4176**System capability**: SystemCapability.Communication.Bluetooth.Core 4177 4178| Name | Type | Readable | Writable | Description | 4179| --------- | ----------------------- | ---- | ---- | -------------------------------------- | 4180| interval | number | Yes | Yes | Delay in reporting the scan result. The default value is **0**. | 4181| dutyMode | [ScanDuty](#scanduty) | Yes | Yes | Scan duty. The default value is SCAN_MODE_LOW_POWER. | 4182| matchMode | [MatchMode](#matchmode) | Yes | Yes | Hardware filtering match mode. The default value is **MATCH_MODE_AGGRESSIVE**.| 4183 4184 4185## ScanDuty 4186 4187Enumerates the scan duty options. 4188 4189**System capability**: SystemCapability.Communication.Bluetooth.Core 4190 4191| Name | Value | Description | 4192| --------------------- | ---- | ------------ | 4193| SCAN_MODE_LOW_POWER | 0 | Low-power mode, which is the default value.| 4194| SCAN_MODE_BALANCED | 1 | Balanced mode. | 4195| SCAN_MODE_LOW_LATENCY | 2 | Low-latency mode. | 4196 4197 4198## MatchMode 4199 4200Enumerates the hardware match modes of BLE scan filters. 4201 4202**System capability**: SystemCapability.Communication.Bluetooth.Core 4203 4204| Name | Value | Description | 4205| --------------------- | ---- | ---------------------------------------- | 4206| MATCH_MODE_AGGRESSIVE | 1 | Hardware reports the scan result with a lower threshold of signal strength and few number of matches in a duration. This is the default value.| 4207| MATCH_MODE_STICKY | 2 | Hardware reports the scan result with a higher threshold of signal strength and sightings. | 4208 4209 4210## ScanResult 4211 4212Defines the scan result. 4213 4214**System capability**: SystemCapability.Communication.Bluetooth.Core 4215 4216| Name | Type | Readable | Writable | Description | 4217| -------- | ----------- | ---- | ---- | ---------------------------------- | 4218| deviceId | string | Yes | No | Address of the scanned device, for example, XX:XX:XX:XX:XX:XX.| 4219| rssi | number | Yes | No | RSSI of the device. | 4220| data | ArrayBuffer | Yes | No | Advertisement packets sent by the device. | 4221 4222 4223## BluetoothState 4224 4225Enumerates the Bluetooth states. 4226 4227**System capability**: SystemCapability.Communication.Bluetooth.Core 4228 4229| Name | Value | Description | 4230| --------------------- | ---- | ------------------ | 4231| STATE_OFF | 0 | Bluetooth is turned off. | 4232| STATE_TURNING_ON | 1 | Bluetooth is being turned on. | 4233| STATE_ON | 2 | Bluetooth is turned on. | 4234| STATE_TURNING_OFF | 3 | Bluetooth is being turned off. | 4235| STATE_BLE_TURNING_ON | 4 | The LE-only mode is being turned on for Bluetooth.| 4236| STATE_BLE_ON | 5 | Bluetooth is in LE-only mode. | 4237| STATE_BLE_TURNING_OFF | 6 | The LE-only mode is being turned off for Bluetooth.| 4238 4239 4240## AdvertiseSetting 4241 4242Defines the BLE advertising parameters. 4243 4244**System capability**: SystemCapability.Communication.Bluetooth.Core 4245 4246| Name | Type | Readable | Writable | Description | 4247| ----------- | ------- | ---- | ---- | ---------------------------------------- | 4248| interval | number | Yes | Yes | Interval for BLE advertising. The minimum value is **32** slots (20 ms). The maximum value is **16384** slots. The default value is **1600** slots (1s).| 4249| txPower | number | Yes | Yes | Transmit power, in dBm. The value range is -127 to 1. The default value is **-7**. | 4250| connectable | boolean | Yes | Yes | Whether the advertisement is connectable. The default value is **true**. | 4251 4252 4253## AdvertiseData 4254 4255Defines the content of a BLE advertisement packet. 4256 4257**System capability**: SystemCapability.Communication.Bluetooth.Core 4258 4259| Name | Type | Readable | Writable | Description | 4260| --------------- | ---------------------------------------- | ---- | ---- | --------------------------- | 4261| serviceUuids | Array<string> | Yes | Yes | List of service UUIDs to broadcast.| 4262| manufactureData | Array<[ManufactureData](#manufacturedata)> | Yes | Yes | List of manufacturers to broadcast. | 4263| serviceData | Array<[ServiceData](#servicedata)> | Yes | Yes | List of service data to broadcast. | 4264 4265 4266## ManufactureData 4267 4268Defines the content of a BLE advertisement packet. 4269 4270**System capability**: SystemCapability.Communication.Bluetooth.Core 4271 4272| Name | Type | Readable | Writable | Description | 4273| ---------------- | ------------------- | ---- | ---- | ------------------ | 4274| manufactureId | number | Yes | Yes | Manufacturer ID allocated by the Bluetooth SIG.| 4275| manufactureValue | ArrayBuffer | Yes | Yes | Manufacturer data. | 4276 4277 4278## ServiceData 4279 4280Defines the service data contained in an advertisement packet. 4281 4282**System capability**: SystemCapability.Communication.Bluetooth.Core 4283 4284| Name | Type | Readable | Writable | Description | 4285| ------------ | ----------- | ---- | ---- | ---------- | 4286| serviceUuid | string | Yes | Yes | Service UUID.| 4287| serviceValue | ArrayBuffer | Yes | Yes | Service data. | 4288 4289 4290## PinRequiredParam<a name="PinRequiredParam"></a> 4291 4292Defines the pairing request parameters. 4293 4294**System capability**: SystemCapability.Communication.Bluetooth.Core 4295 4296| Name | Type | Readable | Writable | Description | 4297| -------- | ------ | ---- | ---- | ----------- | 4298| deviceId | string | Yes | No | ID of the device to pair.| 4299| pinCode | string | Yes | No | Key for the device pairing. | 4300 4301 4302## BondStateParam<a name="BondStateParam"></a> 4303 4304Defines the pairing state parameters. 4305 4306**System capability**: SystemCapability.Communication.Bluetooth.Core 4307 4308| Name | Type | Readable | Writable | Description | 4309| -------- | ------ | ---- | ---- | ----------- | 4310| deviceId | string | Yes | No | ID of the device to pair.| 4311| state | BondState | Yes | No | State of the device.| 4312 4313 4314## StateChangeParam<a name="StateChangeParam"></a> 4315 4316Defines the profile state change parameters. 4317 4318**System capability**: SystemCapability.Communication.Bluetooth.Core 4319 4320| Name | Type | Readable| Writable| Description | 4321| -------- | ------------------------------------------------- | ---- | ---- | ------------------------------- | 4322| deviceId | string | Yes | No | Address of a Bluetooth device. | 4323| state | [ProfileConnectionState](#profileconnectionstate) | Yes | No | Profile connection state of the device.| 4324 4325 4326## DeviceClass<a name="DeviceClass"></a> 4327 4328Defines the class of a Bluetooth device. 4329 4330**System capability**: SystemCapability.Communication.Bluetooth.Core 4331 4332| Name | Type | Readable | Writable | Description | 4333| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 4334| majorClass | [MajorClass](#majorclass) | Yes | No | Major classes of Bluetooth devices. | 4335| majorMinorClass | [MajorMinorClass](#majorminorclass) | Yes | No | Major and minor classes of Bluetooth devices.| 4336| classOfDevice | number | Yes | No | Class of the device. | 4337 4338 4339 4340## MajorClass<a name="MajorClass"></a> 4341 4342Enumerates the major classes of Bluetooth devices. 4343 4344**System capability**: SystemCapability.Communication.Bluetooth.Core 4345 4346| Name | Value | Description | 4347| ------------------- | ------ | ---------- | 4348| MAJOR_MISC | 0x0000 | Miscellaneous device. | 4349| MAJOR_COMPUTER | 0x0100 | Computer. | 4350| MAJOR_PHONE | 0x0200 | Mobile phone. | 4351| MAJOR_NETWORKING | 0x0300 | Network device. | 4352| MAJOR_AUDIO_VIDEO | 0x0400 | Audio or video device.| 4353| MAJOR_PERIPHERAL | 0x0500 | Peripheral device. | 4354| MAJOR_IMAGING | 0x0600 | Imaging device. | 4355| MAJOR_WEARABLE | 0x0700 | Wearable device. | 4356| MAJOR_TOY | 0x0800 | Toy. | 4357| MAJOR_HEALTH | 0x0900 | Health device. | 4358| MAJOR_UNCATEGORIZED | 0x1F00 | Unclassified device. | 4359 4360 4361## MajorMinorClass<a name="MajorMinorClass"></a> 4362 4363Enumerates the major and minor classes of Bluetooth devices. 4364 4365**System capability**: SystemCapability.Communication.Bluetooth.Core 4366 4367| Name | Value | Description | 4368| ---------------------------------------- | ------ | --------------- | 4369| COMPUTER_UNCATEGORIZED | 0x0100 | Unclassified computer. | 4370| COMPUTER_DESKTOP | 0x0104 | Desktop computer. | 4371| COMPUTER_SERVER | 0x0108 | Server. | 4372| COMPUTER_LAPTOP | 0x010C | Laptop. | 4373| COMPUTER_HANDHELD_PC_PDA | 0x0110 | Hand-held computer. | 4374| COMPUTER_PALM_SIZE_PC_PDA | 0x0114 | Palmtop computer. | 4375| COMPUTER_WEARABLE | 0x0118 | Wearable computer. | 4376| COMPUTER_TABLET | 0x011C | Tablet. | 4377| PHONE_UNCATEGORIZED | 0x0200 | Unclassified mobile phone. | 4378| PHONE_CELLULAR | 0x0204 | Portable phone. | 4379| PHONE_CORDLESS | 0x0208 | Cordless phone. | 4380| PHONE_SMART | 0x020C | Smartphone. | 4381| PHONE_MODEM_OR_GATEWAY | 0x0210 | Modem or gateway phone.| 4382| PHONE_ISDN | 0x0214 | ISDN phone. | 4383| NETWORK_FULLY_AVAILABLE | 0x0300 | Device with network fully available. | 4384| NETWORK_1_TO_17_UTILIZED | 0x0320 | Device used on network 1 to 17. | 4385| NETWORK_17_TO_33_UTILIZED | 0x0340 | Device used on network 17 to 33. | 4386| NETWORK_33_TO_50_UTILIZED | 0x0360 | Device used on network 33 to 50. | 4387| NETWORK_60_TO_67_UTILIZED | 0x0380 | Device used on network 60 to 67. | 4388| NETWORK_67_TO_83_UTILIZED | 0x03A0 | Device used on network 67 to 83. | 4389| NETWORK_83_TO_99_UTILIZED | 0x03C0 | Device used on network 83 to 99. | 4390| NETWORK_NO_SERVICE | 0x03E0 | Device without network service | 4391| AUDIO_VIDEO_UNCATEGORIZED | 0x0400 | Unclassified audio or video device. | 4392| AUDIO_VIDEO_WEARABLE_HEADSET | 0x0404 | Wearable audio or video headset. | 4393| AUDIO_VIDEO_HANDSFREE | 0x0408 | Hands-free audio or video device. | 4394| AUDIO_VIDEO_MICROPHONE | 0x0410 | Audio or video microphone. | 4395| AUDIO_VIDEO_LOUDSPEAKER | 0x0414 | Audio or video loudspeaker. | 4396| AUDIO_VIDEO_HEADPHONES | 0x0418 | Audio or video headphones. | 4397| AUDIO_VIDEO_PORTABLE_AUDIO | 0x041C | Portable audio or video device. | 4398| AUDIO_VIDEO_CAR_AUDIO | 0x0420 | In-vehicle audio or video device. | 4399| AUDIO_VIDEO_SET_TOP_BOX | 0x0424 | Audio or video STB device. | 4400| AUDIO_VIDEO_HIFI_AUDIO | 0x0428 | High-fidelity speaker device. | 4401| AUDIO_VIDEO_VCR | 0x042C | Video cassette recording (VCR) device. | 4402| AUDIO_VIDEO_VIDEO_CAMERA | 0x0430 | Camera. | 4403| AUDIO_VIDEO_CAMCORDER | 0x0434 | Camcorder | 4404| AUDIO_VIDEO_VIDEO_MONITOR | 0x0438 | Audio or video monitor. | 4405| AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER | 0x043C | Video display or loudspeaker. | 4406| AUDIO_VIDEO_VIDEO_CONFERENCING | 0x0440 | Video conferencing device. | 4407| AUDIO_VIDEO_VIDEO_GAMING_TOY | 0x0448 | Audio or video gaming toy. | 4408| PERIPHERAL_NON_KEYBOARD_NON_POINTING | 0x0500 | Non-keyboard or non-pointing peripheral device. | 4409| PERIPHERAL_KEYBOARD | 0x0540 | Keyboard device. | 4410| PERIPHERAL_POINTING_DEVICE | 0x0580 | Pointing peripheral device. | 4411| PERIPHERAL_KEYBOARD_POINTING | 0x05C0 | Keyboard pointing device. | 4412| PERIPHERAL_UNCATEGORIZED | 0x0500 | Unclassified peripheral device. | 4413| PERIPHERAL_JOYSTICK | 0x0504 | Peripheral joystick. | 4414| PERIPHERAL_GAMEPAD | 0x0508 | Peripheral game pad | 4415| PERIPHERAL_REMOTE_CONTROL | 0x05C0 | Peripheral remote control device | 4416| PERIPHERAL_SENSING_DEVICE | 0x0510 | Peripheral sensing device. | 4417| PERIPHERAL_DIGITIZER_TABLET | 0x0514 | Peripheral digitizer tablet.| 4418| PERIPHERAL_CARD_READER | 0x0518 | Peripheral card reader. | 4419| PERIPHERAL_DIGITAL_PEN | 0x051C | Peripheral digital pen. | 4420| PERIPHERAL_SCANNER_RFID | 0x0520 | Peripheral RFID scanner. | 4421| PERIPHERAL_GESTURAL_INPUT | 0x0522 | Gesture input device. | 4422| IMAGING_UNCATEGORIZED | 0x0600 | Unclassified imaging device. | 4423| IMAGING_DISPLAY | 0x0610 | Imaging display device. | 4424| IMAGING_CAMERA | 0x0620 | Imaging camera device. | 4425| IMAGING_SCANNER | 0x0640 | Imaging scanner. | 4426| IMAGING_PRINTER | 0x0680 | Imaging printer. | 4427| WEARABLE_UNCATEGORIZED | 0x0700 | Unclassified wearable device. | 4428| WEARABLE_WRIST_WATCH | 0x0704 | Smart watch. | 4429| WEARABLE_PAGER | 0x0708 | Wearable pager. | 4430| WEARABLE_JACKET | 0x070C | Smart jacket. | 4431| WEARABLE_HELMET | 0x0710 | Wearable helmet. | 4432| WEARABLE_GLASSES | 0x0714 | Wearable glasses. | 4433| TOY_UNCATEGORIZED | 0x0800 | Unclassified toy. | 4434| TOY_ROBOT | 0x0804 | Toy robot. | 4435| TOY_VEHICLE | 0x0808 | Toy vehicle. | 4436| TOY_DOLL_ACTION_FIGURE | 0x080C | Humanoid toy doll. | 4437| TOY_CONTROLLER | 0x0810 | Toy controller. | 4438| TOY_GAME | 0x0814 | Toy gaming device. | 4439| HEALTH_UNCATEGORIZED | 0x0900 | Unclassified health devices. | 4440| HEALTH_BLOOD_PRESSURE | 0x0904 | Blood pressure device. | 4441| HEALTH_THERMOMETER | 0x0908 | Thermometer | 4442| HEALTH_WEIGHING | 0x090C | Body scale. | 4443| HEALTH_GLUCOSE | 0x0910 | Blood glucose monitor. | 4444| HEALTH_PULSE_OXIMETER | 0x0914 | Pulse oximeter. | 4445| HEALTH_PULSE_RATE | 0x0918 | Heart rate monitor. | 4446| HEALTH_DATA_DISPLAY | 0x091C | Health data display. | 4447| HEALTH_STEP_COUNTER | 0x0920 | Step counter. | 4448| HEALTH_BODY_COMPOSITION_ANALYZER | 0x0924 | Body composition analyzer. | 4449| HEALTH_PEAK_FLOW_MONITOR | 0x0928 | Hygrometer. | 4450| HEALTH_MEDICATION_MONITOR | 0x092C | Medication monitor. | 4451| HEALTH_KNEE_PROSTHESIS | 0x0930 | Prosthetic knee. | 4452| HEALTH_ANKLE_PROSTHESIS | 0x0934 | Prosthetic ankle. | 4453| HEALTH_GENERIC_HEALTH_MANAGER | 0x0938 | Generic health management device. | 4454| HEALTH_PERSONAL_MOBILITY_DEVICE | 0x093C | Personal mobility device. | 4455 4456 4457## PlayingState<a name="PlayingState"></a> 4458 4459Enumerates the A2DP playing states. 4460 4461**System capability**: SystemCapability.Communication.Bluetooth.Core 4462 4463| Name | Value | Description | 4464| ----------------- | ------ | ------- | 4465| STATE_NOT_PLAYING | 0x0000 | Not playing. | 4466| STATE_PLAYING | 0x0001 | Playing.| 4467 4468 4469## ProfileId<a name="ProfileId"></a> 4470 4471Enumerates the Bluetooth profiles. API version 9 is added with **PROFILE_HID_HOST** and **PROFILE_PAN_NETWORK**. 4472 4473**System capability**: SystemCapability.Communication.Bluetooth.Core 4474 4475| Name | Value | Description | 4476| -------------------------------- | ------ | --------------- | 4477| PROFILE_A2DP_SOURCE | 1 | A2DP profile.| 4478| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 4 | HFP profile. | 4479| PROFILE_HID_HOST | 6 | Human Interface Device (HID) profile. | 4480| PROFILE_PAN_NETWORK | 7 | PAN profile. | 4481