1# Bluetooth 2 3> **NOTE**<br> 4> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 5 6The **Bluetooth** module provides classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising. 7 8 9## Modules to Import 10 11```js 12import bluetooth from '@ohos.bluetooth'; 13``` 14 15 16## bluetooth.enableBluetooth<sup>8+</sup><a name="enableBluetooth"></a> 17 18enableBluetooth(): boolean 19 20Enables Bluetooth. 21 22**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 23 24**System capability**: SystemCapability.Communication.Bluetooth.Core 25 26**Return value** 27 28| Type | Description | 29| ------- | ------------------------ | 30| boolean | Returns **true** if Bluetooth is enabled; returns **false** otherwise.| 31 32**Example** 33 34```js 35let enable = bluetooth.enableBluetooth(); 36``` 37 38 39## bluetooth.disableBluetooth<sup>8+</sup><a name="disableBluetooth"></a> 40 41disableBluetooth(): boolean 42 43Disables Bluetooth. 44 45**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 46 47**System capability**: SystemCapability.Communication.Bluetooth.Core 48 49**Return value** 50 51| Type | Description | 52| ------- | ------------------------ | 53| boolean | Returns **true** if Bluetooth is disabled; returns **false** otherwise.| 54 55**Example** 56 57```js 58let disable = bluetooth.disableBluetooth(); 59``` 60 61 62## bluetooth.getLocalName<sup>8+</sup><a name="getLocalName"></a> 63 64getLocalName(): string 65 66Obtains the name of the local Bluetooth device. 67 68**Required permissions**: ohos.permission.USE_BLUETOOTH 69 70**System capability**: SystemCapability.Communication.Bluetooth.Core 71 72**Return value** 73 74| Type | Description | 75| ------ | --------- | 76| string | Name of the local Bluetooth device obtained.| 77 78**Example** 79 80```js 81let localName = bluetooth.getLocalName(); 82``` 83 84 85## bluetooth.getState 86 87getState(): BluetoothState 88 89Obtains the Bluetooth state. 90 91**Required permissions**: ohos.permission.USE_BLUETOOTH 92 93**System capability**: SystemCapability.Communication.Bluetooth.Core 94 95**Return value** 96 97| Type | Description | 98| --------------------------------- | --------- | 99| [BluetoothState](#bluetoothstate) | Bluetooth state obtained.| 100 101**Example** 102 103```js 104let state = bluetooth.getState(); 105``` 106 107 108## bluetooth.getBtConnectionState 109 110getBtConnectionState(): ProfileConnectionState 111 112Obtains the profile connection state of this Bluetooth device. 113 114**Required permissions**: ohos.permission.USE_BLUETOOTH 115 116**System capability**: SystemCapability.Communication.Bluetooth.Core 117 118**Return value** 119 120| Type | Description | 121| ---------------------------------------- | ------------------- | 122| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.| 123 124**Example** 125 126```js 127let connectionState = bluetooth.getBtConnectionState(); 128``` 129 130 131## bluetooth.setLocalName<sup>8+</sup><a name="setLocalName"></a> 132 133setLocalName(name: string): boolean 134 135Sets the name of the local Bluetooth device. 136 137**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 138 139**System capability**: SystemCapability.Communication.Bluetooth.Core 140 141**Parameters** 142 143| Name | Type | Mandatory | Description | 144| ---- | ------ | ---- | --------------------- | 145| name | string | Yes | Bluetooth device name to set. It cannot exceed 248 bytes.| 146 147**Return value** 148 149| Type | Description | 150| ------- | ------------------------------ | 151| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 152 153**Example** 154 155```js 156let ret = bluetooth.setLocalName('device_name'); 157``` 158 159 160## bluetooth.pairDevice 161 162pairDevice(deviceId: string): boolean 163 164Initiates Bluetooth pairing. 165 166**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 167 168**System capability**: SystemCapability.Communication.Bluetooth.Core 169 170**Parameters** 171 172| Name | Type | Mandatory | Description | 173| -------- | ------ | ---- | ----------------------------------- | 174| deviceId | string | Yes | Address of the remote device to pair, for example, XX:XX:XX:XX:XX:XX.| 175 176**Return value** 177 178| Type | Description | 179| ------- | -------------------------- | 180| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 181 182**Example** 183 184```js 185// The address can be scanned. 186let result = bluetooth.pairDevice("XX:XX:XX:XX:XX:XX"); 187``` 188 189 190## bluetooth.getProfileConnState<sup>8+</sup><a name="getProfileConnState"></a> 191 192getProfileConnState(profileId: ProfileId): ProfileConnectionState 193 194Obtains the connection state of a profile. 195 196**Required permissions**: ohos.permission.USE_BLUETOOTH 197 198**System capability**: SystemCapability.Communication.Bluetooth.Core 199 200**Parameters** 201 202| Name | Type | Mandatory | Description | 203| --------- | --------- | ---- | ------------------------------------- | 204| ProfileId | profileId | Yes | ID of the target profile, for example, **PROFILE_A2DP_SOURCE**.| 205 206**Return value** 207 208| Type | Description | 209| ------------------------------------------------- | ------------------- | 210| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.| 211 212**Example** 213 214```js 215let result = bluetooth.getProfileConnState(bluetooth.ProfileId.PROFILE_A2DP_SOURCE); 216``` 217 218 219## bluetooth.cancelPairedDevice<sup>8+</sup><a name="cancelPairedDevice"></a> 220 221cancelPairedDevice(deviceId: string): boolean 222 223Cancels a paired remote device. 224 225This is a system API. 226 227**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 228 229**System capability**: SystemCapability.Communication.Bluetooth.Core 230 231**Parameters** 232 233| Name | Type | Mandatory | Description | 234| -------- | ------ | ---- | ------------------------------------- | 235| deviceId | string | Yes | Address of the remote device to cancel, for example, XX:XX:XX:XX:XX:XX.| 236 237**Return value** 238 239| Type | Description | 240| ------- | -------------------------- | 241| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 242 243**Example** 244 245```js 246let result = bluetooth.cancelPairedDevice("XX:XX:XX:XX:XX:XX"); 247``` 248 249 250## bluetooth.getRemoteDeviceName<sup>8+</sup><a name="getRemoteDeviceName"></a> 251 252getRemoteDeviceName(deviceId: string): string 253 254Obtains the name of the remote Bluetooth device. 255 256**Required permissions**: ohos.permission.USE_BLUETOOTH 257 258**System capability**: SystemCapability.Communication.Bluetooth.Core 259 260**Parameters** 261 262| Name | Type | Mandatory | Description | 263| -------- | ------ | ---- | --------------------------------- | 264| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.| 265 266**Return value** 267 268| Type | Description | 269| ------ | ------------- | 270| string | Device name (a string) obtained.| 271 272**Example** 273 274```js 275let remoteDeviceName = bluetooth.getRemoteDeviceName("XX:XX:XX:XX:XX:XX"); 276``` 277 278 279## bluetooth.getRemoteDeviceClass<sup>8+</sup><a name="getRemoteDeviceClass"></a> 280 281getRemoteDeviceClass(deviceId: string): DeviceClass 282 283Obtains the class of the remote Bluetooth device. 284 285**Required permissions**: ohos.permission.USE_BLUETOOTH 286 287**System capability**: SystemCapability.Communication.Bluetooth.Core 288 289**Parameters** 290 291| Name | Type | Mandatory | Description | 292| -------- | ------ | ---- | --------------------------------- | 293| deviceId | string | Yes | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.| 294 295**Return value** 296 297| Type | Description | 298| --------------------------- | -------- | 299| [DeviceClass](#deviceclass) | Class of the remote device obtained.| 300 301**Example** 302 303```js 304let remoteDeviceClass = bluetooth.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX"); 305``` 306 307 308## bluetooth.getPairedDevices<sup>8+</sup><a name="getPairedDevices"></a> 309 310getPairedDevices(): Array<string> 311 312Obtains the paired devices. 313 314**Required permissions**: ohos.permission.USE_BLUETOOTH 315 316**System capability**: SystemCapability.Communication.Bluetooth.Core 317 318**Return value** 319 320| Type | Description | 321| ------------------- | ------------- | 322| Array<string> | Addresses of the paired Bluetooth devices.| 323 324**Example** 325 326```js 327let devices = bluetooth.getPairedDevices(); 328``` 329 330 331## bluetooth.setBluetoothScanMode<sup>8+</sup><a name="setBluetoothScanMode"></a> 332 333setBluetoothScanMode(mode: ScanMode, duration: number): boolean 334 335Sets the Bluetooth scan mode so that the device can be discovered by a remote device. 336 337**Required permissions**: ohos.permission.USE_BLUETOOTH 338 339**System capability**: SystemCapability.Communication.Bluetooth.Core 340 341**Parameters** 342 343| Name | Type | Mandatory | Description | 344| -------- | --------------------- | ---- | ---------------------------- | 345| mode | [ScanMode](#scanmode) | Yes | Bluetooth scan mode to set. | 346| duration | number | Yes | Duration (in ms) in which the device can be discovered. The value **0** indicates unlimited time.| 347 348**Return value** 349 350| Type | Description | 351| ------- | -------------------------- | 352| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 353 354**Example** 355 356```js 357// The device can be discovered and connected only when the discoverable and connectable mode is used. 358let result = bluetooth.setBluetoothScanMode(bluetooth.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 359``` 360 361 362## bluetooth.getBluetoothScanMode<sup>8+</sup><a name="getBluetoothScanMode"></a> 363 364getBluetoothScanMode(): ScanMode 365 366Obtains the Bluetooth scan mode. 367 368**Required permissions**: ohos.permission.USE_BLUETOOTH 369 370**System capability**: SystemCapability.Communication.Bluetooth.Core 371 372**Return value** 373 374| Type | Description | 375| --------------------- | ------- | 376| [ScanMode](#scanmode) | Bluetooth scan mode obtained.| 377 378**Example** 379 380```js 381let scanMode = bluetooth.getBluetoothScanMode(); 382``` 383 384 385## bluetooth.startBluetoothDiscovery<sup>8+</sup><a name="startBluetoothDiscovery"></a> 386 387startBluetoothDiscovery(): boolean 388 389Starts Bluetooth scan to discover remote devices. 390 391**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION 392 393**System capability**: SystemCapability.Communication.Bluetooth.Core 394 395**Return value** 396 397| Type | Description | 398| ------- | -------------------------- | 399| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 400 401**Example** 402 403```js 404let deviceId; 405function onReceiveEvent(data) { 406 deviceId = data; 407} 408bluetooth.on('bluetoothDeviceFind', onReceiveEvent); 409let result = bluetooth.startBluetoothDiscovery(); 410``` 411 412 413## bluetooth.stopBluetoothDiscovery<sup>8+</sup><a name="stopBluetoothDiscovery"></a> 414 415stopBluetoothDiscovery(): boolean 416 417Stops Bluetooth scan. 418 419**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 420 421**System capability**: SystemCapability.Communication.Bluetooth.Core 422 423**Return value** 424 425| Type | Description | 426| ------- | -------------------------- | 427| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 428 429**Example** 430 431```js 432let result = bluetooth.stopBluetoothDiscovery(); 433``` 434 435 436## bluetooth.setDevicePairingConfirmation<sup>8+</sup><a name="setDevicePairingConfirmation"></a> 437 438setDevicePairingConfirmation(device: string, accept: boolean): boolean 439 440Sets the device pairing confirmation. 441 442**Required permissions**: ohos.permission.MANAGE_BLUETOOTH 443 444**System capability**: SystemCapability.Communication.Bluetooth.Core 445 446**Parameters** 447 448| Name | Type | Mandatory | Description | 449| ------ | ------- | ---- | -------------------------------- | 450| device | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 451| 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. | 452 453**Return value** 454 455| Type | Description | 456| ------- | ---------------------------- | 457| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 458 459**Example** 460 461```js 462// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device. 463function onReceivePinRequiredEvent(data) { // data is the input parameter for the pairing request. 464 console.info('pin required = '+ JSON.stringify(data)); 465 bluetooth.setDevicePairingConfirmation(data.deviceId, true); 466} 467bluetooth.on("pinRequired", onReceivePinRequiredEvent); 468``` 469 470 471## bluetooth.on('bluetoothDeviceFind')<sup>8+</sup> 472 473on(type: "bluetoothDeviceFind", callback: Callback<Array<string>>): void 474 475Subscribes to the Bluetooth device discovery events. 476 477**Required permissions**: ohos.permission.USE_BLUETOOTH 478 479**System capability**: SystemCapability.Communication.Bluetooth.Core 480 481**Parameters** 482 483| Name | Type | Mandatory | Description | 484| -------- | ----------------------------------- | ---- | -------------------------------------- | 485| type | string | Yes | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered.| 486| callback | Callback<Array<string>> | Yes | Callback invoked to return the discovered devices. You need to implement this callback. | 487 488**Return value** 489 490No value is returned. 491 492**Example** 493 494```js 495function onReceiveEvent(data) { // data is a set of Bluetooth device addresses. 496 console.info('bluetooth device find = '+ JSON.stringify(data)); 497} 498bluetooth.on('bluetoothDeviceFind', onReceiveEvent); 499``` 500 501 502## bluetooth.off('bluetoothDeviceFind')<sup>8+</sup> 503 504off(type: "bluetoothDeviceFind", callback?: Callback<Array<string>>): void 505 506Unsubscribes from the Bluetooth device discovery events. 507 508**Required permissions**: ohos.permission.USE_BLUETOOTH 509 510**System capability**: SystemCapability.Communication.Bluetooth.Core 511 512**Parameters** 513 514| Name | Type | Mandatory | Description | 515| -------- | ----------------------------------- | ---- | ---------------------------------------- | 516| type | string | Yes | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered. | 517| 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**. | 518 519**Return value** 520 521No value is returned. 522 523**Example** 524 525```js 526function onReceiveEvent(data) { 527 console.info('bluetooth device find = '+ JSON.stringify(data)); 528} 529bluetooth.on('bluetoothDeviceFind', onReceiveEvent); 530bluetooth.off('bluetoothDeviceFind', onReceiveEvent); 531``` 532 533 534## bluetooth.on('pinRequired')<sup>8+</sup> 535 536on(type: "pinRequired", callback: Callback<PinRequiredParam>): void 537 538Subscribes to the pairing request events of the remote Bluetooth device. 539 540**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 541 542**System capability**: SystemCapability.Communication.Bluetooth.Core 543 544**Parameters** 545 546| Name | Type | Mandatory | Description | 547| -------- | ---------------------------------------- | ---- | -------------------------------- | 548| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 549| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | Yes | Callback invoked to return the pairing request. You need to implement this callback.| 550 551**Return value** 552 553No value is returned. 554 555**Example** 556 557```js 558function onReceiveEvent(data) { // data is the pairing request parameter. 559 console.info('pin required = '+ JSON.stringify(data)); 560} 561bluetooth.on('pinRequired', onReceiveEvent); 562``` 563 564 565## bluetooth.off('pinRequired')<sup>8+</sup> 566 567off(type: "pinRequired", callback?: Callback<PinRequiredParam>): void 568 569Unsubscribes from the pairing request events of the remote Bluetooth device. 570 571**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 572 573**System capability**: SystemCapability.Communication.Bluetooth.Core 574 575**Parameters** 576 577| Name | Type | Mandatory | Description | 578| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 579| type | string | Yes | Event type. The value **pinRequired** indicates a pairing request event. | 580| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | No | Callback for the **pinRequired** event. The input parameter is the pairing request parameter. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 581 582**Return value** 583 584No value is returned. 585 586**Example** 587 588```js 589function onReceiveEvent(data) { 590 console.info('pin required = '+ JSON.stringify(data)); 591} 592bluetooth.on('pinRequired', onReceiveEvent); 593bluetooth.off('pinRequired', onReceiveEvent); 594``` 595 596 597## bluetooth.on('bondStateChange')<sup>8+</sup> 598 599on(type: "bondStateChange", callback: Callback<BondStateParam>): void 600 601Subscribes to the Bluetooth paring change events. 602 603**Required permissions**: ohos.permission.USE_BLUETOOTH 604 605**System capability**: SystemCapability.Communication.Bluetooth.Core 606 607**Parameters** 608 609| Name | Type | Mandatory | Description | 610| -------- | ---------------------------------------- | ---- | ------------------------------------ | 611| type | string | Yes | Event type. The value **bondStateChange** indicates a Bluetooth paring state change event. | 612| callback | Callback<[BondStateParam](#bondstateparam8)> | Yes | Callback invoked to return the paring state. You need to implement this callback. | 613 614**Return value** 615 616No value is returned. 617 618**Example** 619 620```js 621function onReceiveEvent(data) { // data, as the input parameter of the callback, indicates the pairing state. 622 console.info('pair state = '+ JSON.stringify(data)); 623} 624bluetooth.on('bondStateChange', onReceiveEvent); 625``` 626 627 628## bluetooth.off('bondStateChange')<sup>8+</sup> 629 630off(type: "bondStateChange", callback?: Callback<BondStateParam>): void 631 632Unsubscribes from the Bluetooth pairing state change events. 633 634**Required permissions**: ohos.permission.USE_BLUETOOTH 635 636**System capability**: SystemCapability.Communication.Bluetooth.Core 637 638**Parameters** 639 640| Name | Type | Mandatory | Description | 641| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 642| type | string | Yes | Event type. The value **bondStateChange** indicates a Bluetooth pairing state change event. | 643| callback | Callback<[BondStateParam](#bondstateparam8)> | No | Callback for the pairing state change event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 644 645**Return value** 646 647No value is returned. 648 649**Example** 650 651```js 652function onReceiveEvent(data) { 653 console.info('bond state = '+ JSON.stringify(data)); 654} 655bluetooth.on('bondStateChange', onReceiveEvent); 656bluetooth.off('bondStateChange', onReceiveEvent); 657``` 658 659 660## bluetooth.on('stateChange')<sup>8+</sup> 661 662on(type: "stateChange", callback: Callback<BluetoothState>): void 663 664Subscribes to the Bluetooth connection state change events. 665 666**Required permissions**: ohos.permission.USE_BLUETOOTH 667 668**System capability**: SystemCapability.Communication.Bluetooth.Core 669 670**Parameters** 671 672| Name | Type | Mandatory | Description | 673| -------- | ---------------------------------------- | ---- | -------------------------------- | 674| type | string | Yes | Event type. The value **stateChange** indicates a Bluetooth connection state change event. | 675| callback | Callback<[BluetoothState](#bluetoothstate)> | Yes | Callback invoked to return the Bluetooth connection state. You need to implement this callback.| 676 677**Return value** 678 679No value is returned. 680 681**Example** 682 683```js 684function onReceiveEvent(data) { 685 console.info('bluetooth state = '+ JSON.stringify(data)); 686} 687bluetooth.on('stateChange', onReceiveEvent); 688``` 689 690 691## bluetooth.off('stateChange')<sup>8+</sup> 692 693off(type: "stateChange", callback?: Callback<BluetoothState>): void 694 695Unsubscribes from the Bluetooth connection state change events. 696 697**Required permissions**: ohos.permission.USE_BLUETOOTH 698 699**System capability**: SystemCapability.Communication.Bluetooth.Core 700 701**Parameters** 702 703| Name | Type | Mandatory | Description | 704| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 705| type | string | Yes | Event type. The value **stateChange** indicates a Bluetooth connection state change event. | 706| callback | Callback<[BluetoothState](#bluetoothstate)> | No | Callback for the Bluetooth connection state event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 707 708**Return value** 709 710No value is returned. 711 712**Example** 713 714```js 715function onReceiveEvent(data) { 716 console.info('bluetooth state = '+ JSON.stringify(data)); 717} 718bluetooth.on('stateChange', onReceiveEvent); 719bluetooth.off('stateChange', onReceiveEvent); 720``` 721 722 723## bluetooth.sppListen<sup>8+</sup><a name="sppListen"></a> 724 725sppListen(name: string, option: SppOption, callback: AsyncCallback<number>): void 726 727Creates a server listening socket. 728 729**Required permissions**: ohos.permission.USE_BLUETOOTH 730 731**System capability**: SystemCapability.Communication.Bluetooth.Core 732 733**Parameters** 734 735| Name | Type | Mandatory | Description | 736| -------- | --------------------------- | ---- | ----------------------- | 737| name | string | Yes | Name of the service. | 738| option | [SppOption](#sppoption) | Yes | Serial port profile (SPP) listening configuration. | 739| callback | AsyncCallback<number> | Yes | Callback invoked to return the server socket ID.| 740 741**Example** 742 743```js 744let serverNumber = -1; 745function serverSocket(code, number) { 746 console.log('bluetooth error code: ' + code.code); 747 if (code.code == 0) { 748 console.log('bluetooth serverSocket Number: ' + number); 749 serverNumber = number; 750 } 751} 752 753let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0}; 754bluetooth.sppListen('server1', sppOption, serverSocket); 755``` 756 757 758## bluetooth.sppAccept<sup>8+</sup><a name="sppAccept"></a> 759 760sppAccept(serverSocket: number, callback: AsyncCallback<number>): void 761 762Listens for a connection to be made to this socket from the client and accepts it. 763 764**System capability**: SystemCapability.Communication.Bluetooth.Core 765 766**Parameters** 767 768| Name | Type | Mandatory | Description | 769| ------------ | --------------------------- | ---- | ----------------------- | 770| serverSocket | number | Yes | Server socket ID. | 771| callback | AsyncCallback<number> | Yes | Callback invoked to return the client socket ID.| 772 773**Example** 774 775```js 776let serverNumber = -1; 777function serverSocket(code, number) { 778 console.log('bluetooth error code: ' + code.code); 779 if (code.code == 0) { 780 console.log('bluetooth serverSocket Number: ' + number); 781 serverNumber = number; 782 } 783} 784let clientNumber = -1; 785function acceptClientSocket(code, number) { 786 console.log('bluetooth error code: ' + code.code); 787 if (code.code == 0) { 788 console.log('bluetooth clientSocket Number: ' + number); 789 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the server. 790 clientNumber = number; 791 } 792} 793bluetooth.sppAccept(serverNumber, acceptClientSocket); 794``` 795 796 797## bluetooth.sppConnect<sup>8+</sup><a name="sppConnect"></a> 798 799sppConnect(device: string, option: SppOption, callback: AsyncCallback<number>): void 800 801Initiates an SPP connection to a remote device from the client. 802 803**Required permissions**: ohos.permission.USE_BLUETOOTH 804 805**System capability**: SystemCapability.Communication.Bluetooth.Core 806 807**Parameters** 808 809| Name | Type | Mandatory | Description | 810| -------- | --------------------------- | ---- | ------------------------------ | 811| device | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 812| option | [SppOption](#sppoption) | Yes | Configuration for connecting to the SPP client. | 813| callback | AsyncCallback<number> | Yes | Callback invoked to return the client socket ID. | 814 815**Example** 816 817```js 818let clientNumber = -1; 819function clientSocket(code, number) { 820 if (code.code != 0) { 821 return; 822 } 823 console.log('bluetooth serverSocket Number: ' + number); 824 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 825 clientNumber = number; 826} 827let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0}; 828bluetooth.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket); 829``` 830 831 832## bluetooth.sppCloseServerSocket<sup>8+</sup><a name="sppCloseServerSocket"></a> 833 834sppCloseServerSocket(socket: number): void 835 836Closes the listening socket of the server. 837 838**System capability**: SystemCapability.Communication.Bluetooth.Core 839 840**Parameters** 841 842| Name | Type | Mandatory | Description | 843| ------ | ------ | ---- | --------------- | 844| socket | number | Yes | ID of the listening socket on the server. The ID is obtained by **sppListen**.| 845 846**Example** 847 848```js 849let serverNumber = -1; 850function serverSocket(code, number) { 851 console.log('bluetooth error code: ' + code.code); 852 if (code.code == 0) { 853 console.log('bluetooth serverSocket Number: ' + number); 854 serverNumber = number; 855 } 856} 857bluetooth.sppCloseServerSocket(serverNumber); 858``` 859 860 861## bluetooth.sppCloseClientSocket<sup>8+</sup><a name="sppCloseClientSocket"></a> 862 863sppCloseClientSocket(socket: number): void 864 865Closes the client socket. 866 867**System capability**: SystemCapability.Communication.Bluetooth.Core 868 869**Parameters** 870 871| Name | Type | Mandatory | Description | 872| ------ | ------ | ---- | ------------- | 873| Name | Type | Mandatory | Description | 874| socket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.| 875 876**Example** 877 878```js 879let clientNumber = -1; 880function clientSocket(code, number) { 881 if (code.code != 0) { 882 return; 883 } 884 console.log('bluetooth serverSocket Number: ' + number); 885 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 886 clientNumber = number; 887} 888bluetooth.sppCloseClientSocket(clientNumber); 889``` 890 891 892## bluetooth.sppWrite<sup>8+</sup><a name="sppWrite"></a> 893 894sppWrite(clientSocket: number, data: ArrayBuffer): boolean 895 896Writes data to the remote device through the socket. 897 898**System capability**: SystemCapability.Communication.Bluetooth.Core 899 900**Parameters** 901 902| Name | Type | Mandatory | Description | 903| ------------ | ----------- | ---- | ------------- | 904| clientSocket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.| 905| data | ArrayBuffer | Yes | Data to write. | 906 907**Return value** 908 909| Type | Description | 910| ------- | ------------------------- | 911| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 912 913**Example** 914 915```js 916let clientNumber = -1; 917function clientSocket(code, number) { 918 if (code.code != 0) { 919 return; 920 } 921 console.log('bluetooth serverSocket Number: ' + number); 922 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 923 clientNumber = number; 924} 925let arrayBuffer = new ArrayBuffer(8); 926let data = new Uint8Array(arrayBuffer); 927data[0] = 123; 928let ret = bluetooth.sppWrite(clientNumber, arrayBuffer); 929if (ret) { 930 console.log('spp write successfully'); 931} else { 932 console.log('spp write failed'); 933} 934``` 935 936 937## bluetooth.on('sppRead')<sup>8+</sup> 938 939on(type: "sppRead", clientSocket: number, callback: Callback<ArrayBuffer>): void 940 941Subscribes to the SPP read request events. 942 943**System capability**: SystemCapability.Communication.Bluetooth.Core 944 945**Parameters** 946 947| Name | Type | Mandatory | Description | 948| ------------ | --------------------------- | ---- | -------------------------- | 949| type | string | Yes | Event type. The value **sppRead** indicates an SPP read request event.| 950| clientSocket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**. | 951| callback | Callback<ArrayBuffer> | Yes | Callback invoked to return the data read. | 952 953**Return value** 954 955No value is returned. 956 957**Example** 958 959```js 960let clientNumber = -1; 961function clientSocket(code, number) { 962 if (code.code != 0) { 963 return; 964 } 965 console.log('bluetooth serverSocket Number: ' + number); 966 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 967 clientNumber = number; 968} 969function dataRead(dataBuffer) { 970 let data = new Uint8Array(dataBuffer); 971 console.log('bluetooth data is: ' + data[0]); 972} 973bluetooth.on('sppRead', clientNumber, dataRead); 974``` 975 976 977## bluetooth.off('sppRead')<sup>8+</sup> 978 979off(type: "sppRead", clientSocket: number, callback?: Callback<ArrayBuffer>): void 980 981Unsubscribes from the SPP read request events. 982 983**System capability**: SystemCapability.Communication.Bluetooth.Core 984 985**Parameters** 986 987| Name | Type | Mandatory | Description | 988| ------------ | --------------------------- | ---- | ---------------------------------------- | 989| type | string | Yes | Event type. The value **sppRead** indicates an SPP read request event. | 990| clientSocket | number | Yes | Client socket ID, which is obtained by **sppAccept** or **sppConnect**. | 991| 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**. | 992 993**Return value** 994 995No value is returned. 996 997**Example** 998 999```js 1000let clientNumber = -1; 1001function clientSocket(code, number) { 1002 if (code.code != 0) { 1003 return; 1004 } 1005 console.log('bluetooth serverSocket Number: ' + number); 1006 // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client. 1007 clientNumber = number; 1008} 1009bluetooth.off('sppRead', clientNumber); 1010``` 1011 1012 1013## bluetooth.getProfile<sup>8+</sup><a name="getProfile"></a> 1014 1015getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile 1016 1017Obtains a profile object. 1018 1019**System capability**: SystemCapability.Communication.Bluetooth.Core 1020 1021**Parameters** 1022 1023| Name | Type | Mandatory | Description | 1024| --------- | --------- | ---- | ------------------------------------- | 1025| ProfileId | profileId | Yes | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.| 1026 1027**Return value** 1028 1029| Type | Description | 1030| ---------------------------------------- | ---------------------------------------- | 1031| A2dpSourceProfile or HandsFreeAudioGatewayProfile| Profile object obtained. Only **A2dpSourceProfile** and **HandsFreeAudioGatewayProfile** are supported.| 1032 1033**Example** 1034 1035```js 1036let a2dpSrc = bluetooth.getProfile(PROFILE_A2DP_SOURCE); 1037``` 1038 1039 1040## bluetooth.BLE 1041 1042### bluetooth.BLE.createGattServer 1043 1044createGattServer(): GattServer 1045 1046Creates a **GattServer** instance. 1047 1048**System capability**: SystemCapability.Communication.Bluetooth.Core 1049 1050**Return value** 1051 1052| Type | Description | 1053| ------------------------- | ------------------------------------ | 1054| [GattServer](#gattserver) | **GattServer** instance created. Before using a method of the server, you must create a **GattSever** instance.| 1055 1056**Example** 1057 1058```js 1059let gattServer = bluetooth.BLE.createGattServer(); 1060``` 1061 1062 1063### bluetooth.BLE.createGattClientDevice 1064 1065createGattClientDevice(deviceId: string): GattClientDevice 1066 1067Creates a **GattClientDevice** instance. 1068 1069**System capability**: SystemCapability.Communication.Bluetooth.Core 1070 1071**Parameters** 1072 1073| Name | Type | Mandatory | Description | 1074| -------- | ------ | ---- | ------------------------------------ | 1075| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 1076 1077**Return value** 1078 1079| Type | Description | 1080| ------------------------------------- | ------------------------------------ | 1081| [GattClientDevice](#gattclientdevice) | **GattClientDevice** instance created. Before using a method of the client, you must create a **GattClientDevice** instance.| 1082 1083**Example** 1084 1085```js 1086let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 1087``` 1088 1089 1090### bluetooth.BLE.getConnectedBLEDevices 1091 1092getConnectedBLEDevices(): Array<string> 1093 1094Obtains the BLE devices connected to this device. 1095 1096**Required permissions**: ohos.permission.USE_BLUETOOTH 1097 1098**System capability**: SystemCapability.Communication.Bluetooth.Core 1099 1100**Return value** 1101 1102| Type | Description | 1103| ------------------- | ------------------- | 1104| Array<string> | Addresses of the BLE devices connected to this device.| 1105 1106**Example** 1107 1108```js 1109let result = bluetooth.BLE.getConnectedBLEDevices(); 1110``` 1111 1112 1113### bluetooth.BLE.startBLEScan 1114 1115startBLEScan(filters: Array<ScanFilter>, options?: ScanOptions): void 1116 1117Starts a BLE scan. 1118 1119**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH, ohos.permission.MANAGE_BLUETOOTH, and ohos.permission.LOCATION 1120 1121**System capability**: SystemCapability.Communication.Bluetooth.Core 1122 1123**Parameters** 1124 1125| Name | Type | Mandatory | Description | 1126| ------- | -------------------------------------- | ---- | ----------------------------------- | 1127| 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.| 1128| options | [ScanOptions](#scanoptions) | No | Scan options. | 1129 1130**Return value** 1131 1132No value is returned. 1133 1134**Example** 1135 1136```js 1137function onReceiveEvent(data) { 1138 console.info('BLE scan device find result = '+ JSON.stringify(data)); 1139} 1140bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent); 1141bluetooth.BLE.startBLEScan( 1142 [{ 1143 deviceId:"XX:XX:XX:XX:XX:XX", 1144 name:"test", 1145 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb" 1146 }], 1147 { 1148 interval: 500, 1149 dutyMode: bluetooth.ScanDuty.SCAN_MODE_LOW_POWER, 1150 matchMode: bluetooth.MatchMode.MATCH_MODE_AGGRESSIVE, 1151 } 1152); 1153``` 1154 1155 1156### bluetooth.BLE.stopBLEScan 1157 1158stopBLEScan(): void 1159 1160Stops the BLE scan. 1161 1162**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1163 1164**System capability**: SystemCapability.Communication.Bluetooth.Core 1165 1166**Return value** 1167 1168No value is returned. 1169 1170**Example** 1171 1172```js 1173bluetooth.BLE.stopBLEScan(); 1174``` 1175 1176 1177### bluetooth.BLE.on('BLEDeviceFind') 1178 1179on(type: "BLEDeviceFind", callback: Callback<Array<ScanResult>>): void 1180 1181Subscribe to the BLE device discovery events. 1182 1183**Required permissions**: ohos.permission.USE_BLUETOOTH 1184 1185**System capability**: SystemCapability.Communication.Bluetooth.Core 1186 1187**Parameters** 1188 1189| Name | Type | Mandatory | Description | 1190| -------- | ---------------------------------------- | ---- | ----------------------------------- | 1191| type | string | Yes | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered. | 1192| callback | Callback<Array<[ScanResult](#scanresult)>> | Yes | Callback invoked to return the discovered devices. You need to implement this callback.| 1193 1194**Return value** 1195 1196No value is returned. 1197 1198**Example** 1199 1200```js 1201function onReceiveEvent(data) { 1202 console.info('bluetooth device find = '+ JSON.stringify(data)); 1203} 1204bluetooth.BLE.on('BLEDeviceFind', onReceiveEvent); 1205``` 1206 1207 1208### bluetooth.BLE.off('BLEDeviceFind') 1209 1210off(type: "BLEDeviceFind", callback?: Callback<Array<ScanResult>>): void 1211 1212Unsubscribes from the BLE device discovery events. 1213 1214**Required permissions**: ohos.permission.USE_BLUETOOTH 1215 1216**System capability**: SystemCapability.Communication.Bluetooth.Core 1217 1218**Parameters** 1219 1220| Name | Type | Mandatory | Description | 1221| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1222| type | string | Yes | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered. | 1223| 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**. | 1224 1225**Return value** 1226 1227No value is returned. 1228 1229**Example** 1230 1231```js 1232function onReceiveEvent(data) { 1233 console.info('bluetooth device find = '+ JSON.stringify(data)); 1234} 1235bluetooth.BLE.on('BLEDeviceFind', onReceiveEvent); 1236bluetooth.BLE.off('BLEDeviceFind', onReceiveEvent); 1237``` 1238 1239 1240## BaseProfile 1241 1242Provides the profile base class. 1243 1244 1245### getConnectionDevices<sup>8+</sup><a name="getConnectionDevices"></a> 1246 1247getConnectionDevices(): Array<string> 1248 1249Obtains the connected devices. 1250 1251**Required permissions**: ohos.permission.USE_BLUETOOTH 1252 1253**System capability**: SystemCapability.Communication.Bluetooth.Core 1254 1255**Parameters** 1256 1257No value is returned. 1258 1259**Return value** 1260 1261| Type | Description | 1262| ------------------- | ------------- | 1263| Array<string> | Addresses of the connected devices.| 1264 1265 1266### getDeviceState<sup>8+</sup><a name="getDeviceState"></a> 1267 1268getDeviceState(device: string): ProfileConnectionState 1269 1270Obtains the connection state of the profile. 1271 1272**Required permissions**: ohos.permission.USE_BLUETOOTH 1273 1274**System capability**: SystemCapability.Communication.Bluetooth.Core 1275 1276**Parameters** 1277 1278| Name | Type | Mandatory | Description | 1279| ------ | ------ | ---- | ------- | 1280| device | string | Yes | Address of the target device.| 1281 1282**Return value** 1283 1284| Type | Description | 1285| ------------------------------------------------- | ----------------------- | 1286| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.| 1287 1288 1289## A2dpSourceProfile 1290 1291Before using a method of **A2dpSourceProfile**, you need to create an instance of this class by using the **getProfile()** method. 1292 1293 1294### connect<sup>8+</sup><a name="a2dp-connect"></a> 1295 1296connect(device: string): boolean 1297 1298Sets up an Advanced Audio Distribution Profile (A2DP) connection. 1299 1300**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1301 1302**System capability**: SystemCapability.Communication.Bluetooth.Core 1303 1304**Parameters** 1305 1306| Name | Type | Mandatory | Description | 1307| ------ | ------ | ---- | ------- | 1308| device | string | Yes | Address of the target device.| 1309 1310**Return value** 1311 1312| Type | Description | 1313| ------- | ------------------- | 1314| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1315 1316**Example** 1317 1318```js 1319let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) 1320let ret = a2dpSrc.connect('XX:XX:XX:XX:XX:XX'); 1321``` 1322 1323 1324### disconnect<sup>8+</sup><a name="a2dp-disconnect"></a> 1325 1326disconnect(device: string): boolean 1327 1328Disconnects an A2DP connection. 1329 1330**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1331 1332**System capability**: SystemCapability.Communication.Bluetooth.Core 1333 1334**Parameters** 1335 1336| Name | Type | Mandatory | Description | 1337| ------ | ------ | ---- | ------- | 1338| device | string | Yes | Address of the target device.| 1339 1340 1341**Return value** 1342 1343| Type | Description | 1344| ------- | ------------------- | 1345| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1346 1347**Example** 1348 1349```js 1350let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE); 1351let ret = a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX'); 1352``` 1353 1354 1355### A2dpSourceProfile.on('connectionStateChange')<sup>8+</sup> 1356 1357on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#StateChangeParam)>): void 1358 1359Subscribes to the A2DP connection state change events. 1360 1361**System capability**: SystemCapability.Communication.Bluetooth.Core 1362 1363**Parameters** 1364 1365| Name | Type | Mandatory | Description | 1366| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1367| type | string | Yes | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.| 1368| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback invoked to return the A2DP connection state change event. | 1369 1370**Return value** 1371 1372No value is returned. 1373 1374**Example** 1375 1376```js 1377function onReceiveEvent(data) { 1378 console.info('a2dp state = '+ JSON.stringify(data)); 1379} 1380A2dpSourceProfile.on('connectionStateChange', onReceiveEvent); 1381``` 1382 1383 1384### A2dpSourceProfile.off('connectionStateChange')<sup>8+</sup> 1385 1386off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 1387 1388Unsubscribes from the A2DP connection state change events. 1389 1390**System capability**: SystemCapability.Communication.Bluetooth.Core 1391 1392**Parameters** 1393 1394| Name | Type | Mandatory | Description | 1395| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1396| type | string | Yes | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.| 1397| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback for the A2DP connection state change event. | 1398 1399**Return value** 1400 1401No value is returned. 1402 1403**Example** 1404 1405```js 1406function onReceiveEvent(data) { 1407 console.info('a2dp state = '+ JSON.stringify(data)); 1408} 1409A2dpSourceProfile.off('connectionStateChange', onReceiveEvent); 1410``` 1411 1412 1413### getPlayingState<sup>9+</sup> 1414 1415getPlayingState(device: string): PlayingState 1416 1417Obtains the playing state of a device. 1418 1419**Required permissions**: ohos.permission.USE_BLUETOOTH 1420 1421**System capability**: SystemCapability.Communication.Bluetooth.Core 1422 1423**Parameters** 1424 1425| Name | Type | Mandatory | Description | 1426| ------ | ------ | ---- | ------- | 1427| device | string | Yes | Address of the target device.| 1428 1429**Return value** 1430 1431| Type | Description | 1432| ----------------------------- | ---------- | 1433| [PlayingState](#PlayingState) | Playing state obtained.| 1434 1435**Example** 1436 1437```js 1438let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE); 1439let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX'); 1440``` 1441 1442 1443## HandsFreeAudioGatewayProfile 1444 1445Before using a method of **HandsFreeAudioGatewayProfile**, you need to create an instance of this class by using the **getProfile()** method. 1446 1447 1448### connect<sup>8+</sup><a name="hfp-connect"></a> 1449 1450connect(device: string): boolean 1451 1452Sets up a Hands-free Profile (HFP) connection of a device. 1453 1454**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1455 1456**System capability**: SystemCapability.Communication.Bluetooth.Core 1457 1458**Parameters** 1459 1460| Name | Type | Mandatory | Description | 1461| ------ | ------ | ---- | ------- | 1462| device | string | Yes | Address of the target device.| 1463 1464 1465**Return value** 1466 1467| Type | Description | 1468| ------- | ------------------- | 1469| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1470 1471**Example** 1472 1473```js 1474let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY); 1475let ret = hfpAg.connect('XX:XX:XX:XX:XX:XX'); 1476``` 1477 1478 1479### disconnect<sup>8+</sup><a name="hfp-disconnect"></a> 1480 1481disconnect(device: string): boolean 1482 1483Disconnects the HFP connection of a device. 1484 1485**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1486 1487**System capability**: SystemCapability.Communication.Bluetooth.Core 1488 1489**Parameters** 1490 1491| Name | Type | Mandatory | Description | 1492| ------ | ------ | ---- | ------- | 1493| device | string | Yes | Address of the target device.| 1494 1495 1496**Return value** 1497 1498| Type | Description | 1499| ------- | ------------------- | 1500| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1501 1502**Example** 1503 1504```js 1505let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY); 1506let ret = hfpAg.disconnect('XX:XX:XX:XX:XX:XX'); 1507``` 1508 1509 1510### HandsFreeAudioGatewayProfile.on('connectionStateChange')<sup>8+</sup> 1511 1512on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#StateChangeParam)>): void 1513 1514Subscribes to the HFP connection state change events. 1515 1516**System capability**: SystemCapability.Communication.Bluetooth.Core 1517 1518**Parameters** 1519 1520| Name | Type | Mandatory | Description | 1521| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1522| type | string | Yes | Event type. The value **connectionStateChange** indicates an HFP connection state change event.| 1523| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback used to return the HFP connection state change event. | 1524 1525**Return value** 1526 1527No value is returned. 1528 1529**Example** 1530 1531```js 1532function onReceiveEvent(data) { 1533 console.info('hfp state = '+ JSON.stringify(data)); 1534} 1535HandsFreeAudioGatewayProfile.on('connectionStateChange', onReceiveEvent); 1536``` 1537 1538 1539### HandsFreeAudioGatewayProfile.off('connectionStateChange')<sup>8+</sup> 1540 1541off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 1542 1543Unsubscribes from the HFP connection state change events. 1544 1545**System capability**: SystemCapability.Communication.Bluetooth.Core 1546 1547**Parameters** 1548 1549| Name | Type | Mandatory | Description | 1550| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1551| type | string | Yes | Event type. The value **connectionStateChange** indicates an HFP connection state change event.| 1552| callback | Callback<[StateChangeParam](#StateChangeParam)> | Yes | Callback for the HFP connection state change event. | 1553 1554**Return value** 1555 1556No value is returned. 1557 1558**Example** 1559 1560```js 1561function onReceiveEvent(data) { 1562 console.info('hfp state = '+ JSON.stringify(data)); 1563} 1564HandsFreeAudioGatewayProfile.off('connectionStateChange', onReceiveEvent); 1565``` 1566 1567 1568## GattServer 1569 1570Implements the Generic Attribute Profile (GATT) server. Before using a method of this class, you need to create a **GattServer** instance using the **createGattServer()** method. 1571 1572 1573### startAdvertising 1574 1575startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void 1576 1577Starts BLE advertising. 1578 1579**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1580 1581**System capability**: SystemCapability.Communication.Bluetooth.Core 1582 1583**Parameters** 1584 1585| Name | Type | Mandatory | Description | 1586| ----------- | ------------------------------------- | ---- | -------------- | 1587| setting | [AdvertiseSetting](#advertisesetting) | Yes | Settings related to BLE advertising. | 1588| advData | [AdvertiseData](#advertisedata) | Yes | Content of the BLE advertisement packet. | 1589| advResponse | [AdvertiseData](#advertisedata) | No | Response to the BLE scan request.| 1590 1591**Return value** 1592 1593No value is returned. 1594 1595**Example** 1596 1597```js 1598let manufactureValueBuffer = new Uint8Array(4); 1599manufactureValueBuffer[0] = 1; 1600manufactureValueBuffer[1] = 2; 1601manufactureValueBuffer[2] = 3; 1602manufactureValueBuffer[3] = 4; 1603 1604let serviceValueBuffer = new Uint8Array(4); 1605serviceValueBuffer[0] = 4; 1606serviceValueBuffer[1] = 6; 1607serviceValueBuffer[2] = 7; 1608serviceValueBuffer[3] = 8; 1609console.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 1610console.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 1611let gattServer = bluetooth.BLE.createGattServer(); 1612gattServer.startAdvertising({ 1613 interval:150, 1614 txPower:60, 1615 connectable:true, 1616 },{ 1617 serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 1618 manufactureData:[{ 1619 manufactureId:4567, 1620 manufactureValue:manufactureValueBuffer.buffer 1621 }], 1622 serviceData:[{ 1623 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 1624 serviceValue:serviceValueBuffer.buffer 1625 }], 1626 },{ 1627 serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"], 1628 manufactureData:[{ 1629 manufactureId:1789, 1630 manufactureValue:manufactureValueBuffer.buffer 1631 }], 1632 serviceData:[{ 1633 serviceUuid:"00001889-0000-1000-8000-00805f9b34fb", 1634 serviceValue:serviceValueBuffer.buffer 1635 }], 1636}); 1637``` 1638 1639 1640### stopAdvertising 1641 1642stopAdvertising(): void 1643 1644Stops BLE advertising. 1645 1646**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH 1647 1648**System capability**: SystemCapability.Communication.Bluetooth.Core 1649 1650**Return value** 1651 1652No value is returned. 1653 1654**Example** 1655 1656```js 1657let server = bluetooth.BLE.createGattServer(); 1658server.stopAdvertising(); 1659``` 1660 1661 1662### addService 1663 1664addService(service: GattService): boolean 1665 1666Adds a service to this GATT server. 1667 1668**Required permissions**: ohos.permission.USE_BLUETOOTH 1669 1670**System capability**: SystemCapability.Communication.Bluetooth.Core 1671 1672**Parameters** 1673 1674| Name | Type | Mandatory | Description | 1675| ------- | --------------------------- | ---- | ------------------------ | 1676| service | [GattService](#gattservice) | Yes | Service to add. Settings related to BLE advertising.| 1677 1678**Return value** 1679 1680| Type | Description | 1681| ------- | -------------------------- | 1682| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1683 1684**Example** 1685 1686```js 1687// Create descriptors. 1688let descriptors = []; 1689let arrayBuffer = new ArrayBuffer(8); 1690let descV = new Uint8Array(arrayBuffer); 1691descV[0] = 11; 1692let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1693 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1694 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 1695descriptors[0] = descriptor; 1696 1697// Create characteristics. 1698let characteristics = []; 1699let arrayBufferC = new ArrayBuffer(8); 1700let cccV = new Uint8Array(arrayBufferC); 1701cccV[0] = 1; 1702let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1703 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 1704let characteristicN = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1705 characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 1706characteristics[0] = characteristic; 1707 1708// Create a gattService instance. 1709let gattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]}; 1710 1711let gattServer = bluetooth.BLE.createGattServer(); 1712let ret = gattServer.addService(gattService); 1713if (ret) { 1714 console.log("add service successfully"); 1715} else { 1716 console.log("add service failed"); 1717} 1718``` 1719 1720 1721### removeService 1722 1723removeService(serviceUuid: string): boolean 1724 1725Removes a service from this GATT server. 1726 1727**Required permissions**: ohos.permission.USE_BLUETOOTH 1728 1729**System capability**: SystemCapability.Communication.Bluetooth.Core 1730 1731**Parameters** 1732 1733| Name | Type | Mandatory | Description | 1734| ----------- | ------ | ---- | ---------------------------------------- | 1735| serviceUuid | string | Yes | Universally unique identifier (UUID) of the service to remove, for example, **00001810-0000-1000-8000-00805F9B34FB**.| 1736 1737**Return value** 1738 1739| Type | Description | 1740| ------- | -------------------------- | 1741| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1742 1743**Example** 1744 1745```js 1746let server = bluetooth.BLE.createGattServer(); 1747server.removeService('00001810-0000-1000-8000-00805F9B34FB'); 1748``` 1749 1750 1751### close 1752 1753close(): void 1754 1755Closes this GATT server to unregister it from the protocol stack. After this method is called, this [GattServer](#gattserver) cannot be used. 1756 1757**Required permissions**: ohos.permission.USE_BLUETOOTH 1758 1759**System capability**: SystemCapability.Communication.Bluetooth.Core 1760 1761**Example** 1762 1763```js 1764let server = bluetooth.BLE.createGattServer(); 1765server.close(); 1766``` 1767 1768 1769### notifyCharacteristicChanged 1770 1771notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): boolean 1772 1773Notifies the connected client device when a characteristic value changes. 1774 1775**Required permissions**: ohos.permission.USE_BLUETOOTH 1776 1777**System capability**: SystemCapability.Communication.Bluetooth.Core 1778 1779**Parameters** 1780 1781| Name | Type | Mandatory | Description | 1782| -------------------- | ---------------------------------------- | ---- | --------------------------------------- | 1783| deviceId | string | Yes | Address of the client that receives notifications, for example, XX:XX:XX:XX:XX:XX.| 1784| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristic) | Yes | New characteristic value. | 1785 1786**Return value** 1787 1788| Type | Description | 1789| ------- | ------------------------ | 1790| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1791 1792**Example** 1793 1794```js 1795// Create descriptors. 1796let descriptors = []; 1797let arrayBuffer = new ArrayBuffer(8); 1798let descV = new Uint8Array(arrayBuffer); 1799descV[0] = 11; 1800let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1801 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1802 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 1803descriptors[0] = descriptor; 1804 1805let arrayBufferC = new ArrayBuffer(8); 1806let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1807characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 1808let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1809characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false}; 1810let server = bluetooth.BLE.createGattServer(); 1811server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic); 1812``` 1813 1814 1815### sendResponse 1816 1817sendResponse(serverResponse: ServerResponse): boolean 1818 1819Sends a response to a read or write request from the GATT client. 1820 1821**Required permissions**: ohos.permission.USE_BLUETOOTH 1822 1823**System capability**: SystemCapability.Communication.Bluetooth.Core 1824 1825**Parameters** 1826 1827| Name | Type | Mandatory | Description | 1828| -------------- | --------------------------------- | ---- | --------------- | 1829| serverResponse | [ServerResponse](#serverresponse) | Yes | Response returned by the GATT server.| 1830 1831**Return value** 1832 1833| Type | Description | 1834| ------- | -------------------------- | 1835| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1836 1837**Example** 1838 1839```js 1840/* send response */ 1841let arrayBufferCCC = new ArrayBuffer(8); 1842let cccValue = new Uint8Array(arrayBufferCCC); 1843cccValue[0] = 1123; 1844let serverResponse = { 1845 "deviceId": "XX:XX:XX:XX:XX:XX", 1846 "transId": 0, 1847 "status": 0, 1848 "offset": 0, 1849 "value": arrayBufferCCC, 1850}; 1851 1852let gattServer = bluetooth.BLE.createGattServer(); 1853let ret = gattServer.sendResponse(serverResponse); 1854if (ret) { 1855 console.log('bluetooth sendResponse successfully'); 1856} else { 1857 console.log('bluetooth sendResponse failed'); 1858} 1859``` 1860 1861 1862### on('characteristicRead') 1863 1864on(type: "characteristicRead", callback: Callback<CharacteristicReadReq>): void 1865 1866Subscribes to the characteristic read request events. 1867 1868**Required permissions**: ohos.permission.USE_BLUETOOTH 1869 1870**System capability**: SystemCapability.Communication.Bluetooth.Core 1871 1872**Parameters** 1873 1874| Name | Type | Mandatory | Description | 1875| -------- | ---------------------------------------- | ---- | ------------------------------------- | 1876| type | string | Yes | Event type. The value **characteristicRead** indicates a characteristic read request event.| 1877| callback | Callback<[CharacteristicReadReq](#characteristicreadreq)> | Yes | Callback invoked to return a characteristic read request event from the GATT client. | 1878 1879**Return value** 1880 1881No value is returned. 1882 1883**Example** 1884 1885```js 1886let arrayBufferCCC = new ArrayBuffer(8); 1887let cccValue = new Uint8Array(arrayBufferCCC); 1888cccValue[0] = 1123; 1889function ReadCharacteristicReq(CharacteristicReadReq) { 1890 let deviceId = CharacteristicReadReq.deviceId; 1891 let transId = CharacteristicReadReq.transId; 1892 let offset = CharacteristicReadReq.offset; 1893 let characteristicUuid = CharacteristicReadReq.characteristicUuid; 1894 1895 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 1896 1897 let ret = gattServer.sendResponse(serverResponse); 1898 if (ret) { 1899 console.log('bluetooth sendResponse successfully'); 1900 } else { 1901 console.log('bluetooth sendResponse failed'); 1902 } 1903} 1904 1905let gattServer = bluetooth.BLE.createGattServer(); 1906gattServer.on("characteristicRead", ReadCharacteristicReq); 1907``` 1908 1909 1910### off('characteristicRead') 1911 1912off(type: "characteristicRead", callback?: Callback<CharacteristicReadReq>): void 1913 1914Unsubscribes from the characteristic read request events. 1915 1916**Required permissions**: ohos.permission.USE_BLUETOOTH 1917 1918**System capability**: SystemCapability.Communication.Bluetooth.Core 1919 1920**Parameters** 1921 1922| Name | Type | Mandatory | Description | 1923| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1924| type | string | Yes | Event type. The value **characteristicRead** indicates a characteristic read request event. | 1925| callback | Callback<[CharacteristicReadReq](#characteristicreadreq)> | No | Callback for the characteristic read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 1926 1927**Return value** 1928 1929No value is returned. 1930 1931**Example** 1932 1933```js 1934let gattServer = bluetooth.BLE.createGattServer(); 1935gattServer.off("characteristicRead"); 1936``` 1937 1938 1939### on('characteristicWrite') 1940 1941on(type: "characteristicWrite", callback: Callback<CharacteristicWriteReq>): void 1942 1943Subscribes to the characteristic write request events. 1944 1945**Required permissions**: ohos.permission.USE_BLUETOOTH 1946 1947**System capability**: SystemCapability.Communication.Bluetooth.Core 1948 1949**Parameters** 1950 1951| Name | Type | Mandatory | Description | 1952| -------- | ---------------------------------------- | ---- | -------------------------------------- | 1953| type | string | Yes | Event type. The value **characteristicWrite** indicates a characteristic write request event.| 1954| callback | Callback<[CharacteristicWriteReq](#characteristicwritereq)> | Yes | Callback invoked to return a characteristic write request from the GATT client. | 1955 1956**Return value** 1957 1958No value is returned. 1959 1960**Example** 1961 1962```js 1963let arrayBufferCCC = new ArrayBuffer(8); 1964let cccValue = new Uint8Array(arrayBufferCCC); 1965function WriteCharacteristicReq(CharacteristicWriteReq) { 1966 let deviceId = CharacteristicWriteReq.deviceId; 1967 let transId = CharacteristicWriteReq.transId; 1968 let offset = CharacteristicWriteReq.offset; 1969 let isPrep = CharacteristicWriteReq.isPrep; 1970 let needRsp = CharacteristicWriteReq.needRsp; 1971 let value = new Uint8Array(CharacteristicWriteReq.value); 1972 let characteristicUuid = CharacteristicWriteReq.characteristicUuid; 1973 1974 cccValue[0] = value[0]; 1975 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 1976 1977 let ret = gattServer.sendResponse(serverResponse); 1978 if (ret) { 1979 console.log('bluetooth sendResponse successfully'); 1980 } else { 1981 console.log('bluetooth sendResponse failed'); 1982 } 1983} 1984 1985let gattServer = bluetooth.BLE.createGattServer(); 1986gattServer.on("characteristicWrite", WriteCharacteristicReq); 1987``` 1988 1989 1990### off('characteristicWrite') 1991 1992off(type: "characteristicWrite", callback?: Callback<CharacteristicWriteReq>): void 1993 1994Unsubscribes from the characteristic write request events. 1995 1996**Required permissions**: ohos.permission.USE_BLUETOOTH 1997 1998**System capability**: SystemCapability.Communication.Bluetooth.Core 1999 2000**Parameters** 2001 2002| Name | Type | Mandatory | Description | 2003| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2004| type | string | Yes | Event type. The value **characteristicWrite** indicates a characteristic write request event. | 2005| callback | Callback<[CharacteristicWriteReq](#characteristicwritereq)> | No | Callback for the characteristic write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 2006 2007**Return value** 2008 2009No value is returned. 2010 2011**Example** 2012 2013```js 2014let gattServer = bluetooth.BLE.createGattServer(); 2015gattServer.off("characteristicWrite"); 2016``` 2017 2018 2019### on('descriptorRead') 2020 2021on(type: "descriptorRead", callback: Callback<DescriptorReadReq>): void 2022 2023Subscribes to the descriptor read request events. 2024 2025**Required permissions**: ohos.permission.USE_BLUETOOTH 2026 2027**System capability**: SystemCapability.Communication.Bluetooth.Core 2028 2029**Parameters** 2030 2031| Name | Type | Mandatory | Description | 2032| -------- | ---------------------------------------- | ---- | --------------------------------- | 2033| type | string | Yes | Event type. The value **descriptorRead** indicates a descriptor read request event.| 2034| callback | Callback<[DescriptorReadReq](#descriptorreadreq)> | Yes | Callback invoked to return a descriptor read request event from the GATT client. | 2035 2036**Return value** 2037 2038No value is returned. 2039 2040**Example** 2041 2042```js 2043let arrayBufferDesc = new ArrayBuffer(8); 2044let descValue = new Uint8Array(arrayBufferDesc); 2045descValue[0] = 1101; 2046function ReadDescriptorReq(DescriptorReadReq) { 2047 let deviceId = DescriptorReadReq.deviceId; 2048 let transId = DescriptorReadReq.transId; 2049 let offset = DescriptorReadReq.offset; 2050 let descriptorUuid = DescriptorReadReq.descriptorUuid; 2051 2052 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 2053 2054 let ret = gattServer.sendResponse(serverResponse); 2055 if (ret) { 2056 console.log('bluetooth sendResponse successfully'); 2057 } else { 2058 console.log('bluetooth sendResponse failed'); 2059 } 2060} 2061 2062let gattServer = bluetooth.BLE.createGattServer(); 2063gattServer.on("descriptorRead", ReadDescriptorReq); 2064``` 2065 2066 2067### off('descriptorRead') 2068 2069off(type: "descriptorRead", callback?: Callback<DescriptorReadReq>): void 2070 2071Unsubscribes from the descriptor read request events. 2072 2073**Required permissions**: ohos.permission.USE_BLUETOOTH 2074 2075**System capability**: SystemCapability.Communication.Bluetooth.Core 2076 2077**Parameters** 2078 2079| Name | Type | Mandatory | Description | 2080| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2081| type | string | Yes | Event type. The value **descriptorRead** indicates a descriptor read request event. | 2082| callback | Callback<[DescriptorReadReq](#descriptorreadreq)> | No | Callback for the descriptor read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 2083 2084**Return value** 2085 2086No value is returned. 2087 2088**Example** 2089 2090```js 2091let gattServer = bluetooth.BLE.createGattServer(); 2092gattServer.off("descriptorRead"); 2093``` 2094 2095 2096### on('descriptorWrite') 2097 2098on(type: "descriptorWrite", callback: Callback<DescriptorWriteReq>): void 2099 2100Subscribes to the descriptor write request events. 2101 2102**Required permissions**: ohos.permission.USE_BLUETOOTH 2103 2104**System capability**: SystemCapability.Communication.Bluetooth.Core 2105 2106**Parameters** 2107 2108| Name | Type | Mandatory | Description | 2109| -------- | ---------------------------------------- | ---- | ---------------------------------- | 2110| type | string | Yes | Event type. The value **descriptorWrite** indicates a descriptor write request event.| 2111| callback | Callback<[DescriptorWriteReq](#descriptorwritereq)> | Yes | Callback invoked to return a descriptor write request from the GATT client. | 2112 2113**Return value** 2114 2115No value is returned. 2116 2117**Example** 2118 2119```js 2120let arrayBufferDesc = new ArrayBuffer(8); 2121let descValue = new Uint8Array(arrayBufferDesc); 2122function WriteDescriptorReq(DescriptorWriteReq) { 2123 let deviceId = DescriptorWriteReq.deviceId; 2124 let transId = DescriptorWriteReq.transId; 2125 let offset = DescriptorWriteReq.offset; 2126 let isPrep = DescriptorWriteReq.isPrep; 2127 let needRsp = DescriptorWriteReq.needRsp; 2128 let value = new Uint8Array(DescriptorWriteReq.value); 2129 let descriptorUuid = DescriptorWriteReq.descriptorUuid; 2130 2131 descValue[0] = value[0]; 2132 let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 2133 2134 let ret = gattServer.sendResponse(serverResponse); 2135 if (ret) { 2136 console.log('bluetooth sendResponse successfully'); 2137 } else { 2138 console.log('bluetooth sendResponse failed'); 2139 } 2140} 2141 2142let gattServer = bluetooth.BLE.createGattServer(); 2143gattServer.on("descriptorRead", WriteDescriptorReq); 2144``` 2145 2146 2147### off('descriptorWrite') 2148 2149off(type: "descriptorWrite", callback?: Callback<DescriptorWriteReq>): void 2150 2151Unsubscribes from the descriptor write request events. 2152 2153**Required permissions**: ohos.permission.USE_BLUETOOTH 2154 2155**System capability**: SystemCapability.Communication.Bluetooth.Core 2156 2157**Parameters** 2158 2159| Name | Type | Mandatory | Description | 2160| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2161| type | string | Yes | Event type. The value **descriptorWrite** indicates a descriptor write request event. | 2162| callback | Callback<[DescriptorWriteReq](#descriptorwritereq)> | No | Callback for the descriptor write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 2163 2164**Return value** 2165 2166No value is returned. 2167 2168**Example** 2169 2170```js 2171let gattServer = bluetooth.BLE.createGattServer(); 2172gattServer.off("descriptorWrite"); 2173``` 2174 2175 2176### on('connectStateChange') 2177 2178on(type: "connectStateChange", callback: Callback<BLEConnectChangedState>): void 2179 2180Subscribes to the BLE connection state change events. 2181 2182**Required permissions**: ohos.permission.USE_BLUETOOTH 2183 2184**System capability**: SystemCapability.Communication.Bluetooth.Core 2185 2186**Parameters** 2187 2188| Name | Type | Mandatory | Description | 2189| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2190| type | string | Yes | Event type. The value **connectStateChange** indicates a BLE connection state change event.| 2191| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | Yes | Callback invoked to return the BLE connection state. | 2192 2193**Return value** 2194 2195No value is returned. 2196 2197**Example** 2198 2199```js 2200function Connected(BLEConnectChangedState) { 2201 let deviceId = BLEConnectChangedState.deviceId; 2202 let status = BLEConnectChangedState.state; 2203} 2204 2205let gattServer = bluetooth.BLE.createGattServer(); 2206gattServer.on("connectStateChange", Connected); 2207``` 2208 2209 2210### off('connectStateChange') 2211 2212off(type: "connectStateChange", callback?: Callback<BLEConnectChangedState>): void 2213 2214Unsubscribes from the BLE connection state change events. 2215 2216**Required permissions**: ohos.permission.USE_BLUETOOTH 2217 2218**System capability**: SystemCapability.Communication.Bluetooth.Core 2219 2220**Parameters** 2221 2222| Name | Type | Mandatory | Description | 2223| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2224| type | string | Yes | Event type. The value **connectStateChange** indicates a BLE connection state change event.| 2225| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | No | Callback for the BLE connection state event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**. | 2226 2227**Return value** 2228 2229No value is returned. 2230 2231**Example** 2232 2233```js 2234let gattServer = bluetooth.BLE.createGattServer(); 2235gattServer.off("connectStateChange"); 2236``` 2237 2238 2239## GattClientDevice 2240 2241Implements the GATT client. Before using a method of this class, you must create a **GattClientDevice** instance using the **createGattClientDevice(deviceId: string)** method. 2242 2243 2244### connect 2245 2246connect(): boolean 2247 2248Initiates a connection to the remote BLE device. 2249 2250**Required permissions**: ohos.permission.USE_BLUETOOTH 2251 2252**System capability**: SystemCapability.Communication.Bluetooth.Core 2253 2254**Return value** 2255 2256| Type | Description | 2257| ------- | ------------------------- | 2258| boolean | Returns **true** if the connection is successful; returns **false** otherwise.| 2259 2260**Example** 2261 2262```js 2263let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2264let ret = device.connect(); 2265``` 2266 2267 2268### disconnect 2269 2270disconnect(): boolean 2271 2272Disconnects from the remote BLE device. 2273 2274**Required permissions**: ohos.permission.USE_BLUETOOTH 2275 2276**System capability**: SystemCapability.Communication.Bluetooth.Core 2277 2278**Return value** 2279 2280| Type | Description | 2281| ------- | ---------------------------- | 2282| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2283 2284**Example** 2285 2286```js 2287let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2288let ret = device.disconnect(); 2289``` 2290 2291 2292### close 2293 2294close(): boolean 2295 2296Closes this GATT client to unregister it from the protocol stack. After this method is called, this [GattClientDevice](#gattclientdevice) instance cannot be used. 2297 2298**Required permissions**: ohos.permission.USE_BLUETOOTH 2299 2300**System capability**: SystemCapability.Communication.Bluetooth.Core 2301 2302**Return value** 2303 2304| Type | Description | 2305| ------- | -------------------------- | 2306| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2307 2308**Example** 2309 2310```js 2311let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2312let ret = device.close(); 2313``` 2314 2315 2316 2317 2318### getServices 2319 2320getServices(callback: AsyncCallback<Array<GattService>>): void 2321 2322Obtains all services of the remote BLE device. This API uses an asynchronous callback to return the result. 2323 2324**Required permissions**: ohos.permission.USE_BLUETOOTH 2325 2326**System capability**: SystemCapability.Communication.Bluetooth.Core 2327 2328**Parameters** 2329 2330| Name | Type | Mandatory | Description | 2331| -------- | ---------------------------------------- | ---- | ------------------------ | 2332| callback | AsyncCallback<Array<[GattService](#gattservice)>> | Yes | Callback invoked to return the services obtained.| 2333 2334**Return value** 2335 2336No value is returned. 2337 2338**Example** 2339 2340```js 2341// Callback 2342function getServices(code, gattServices) { 2343 if (code.code == 0) { 2344 let services = gattServices; 2345 console.log('bluetooth code is ' + code.code); 2346 console.log("bluetooth services size is ", services.length); 2347 2348 for (let i = 0; i < services.length; i++) { 2349 console.log('bluetooth serviceUuid is ' + services[i].serviceUuid); 2350 } 2351 } 2352} 2353 2354let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2355device.connect(); 2356device.getServices(getServices); 2357``` 2358 2359 2360### getServices 2361 2362getServices(): Promise<Array<GattService>> 2363 2364Obtains all services of the remote BLE device. This API uses a promise to return the result. 2365 2366**Required permissions**: ohos.permission.USE_BLUETOOTH 2367 2368**System capability**: SystemCapability.Communication.Bluetooth.Core 2369 2370**Parameters** 2371 2372**Return value** 2373 2374| Type | Description | 2375| ---------------------------------------- | --------------------------- | 2376| Promise<Array<[GattService](#gattservice)>> | Promise used to return the services obtained.| 2377 2378**Example** 2379 2380```js 2381// Promise 2382let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2383device.connect(); 2384device.getServices().then(result => { 2385 console.info("getServices successfully:" + JSON.stringify(result)); 2386}); 2387``` 2388 2389 2390### readCharacteristicValue 2391 2392readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback<BLECharacteristic>): void 2393 2394Reads the characteristic value of the specific service of the remote BLE device. This API uses an asynchronous callback to return the result. 2395 2396**Required permissions**: ohos.permission.USE_BLUETOOTH 2397 2398**System capability**: SystemCapability.Communication.Bluetooth.Core 2399 2400**Parameters** 2401 2402| Name | Type | Mandatory | Description | 2403| -------------- | ---------------------------------------- | ---- | ----------------------- | 2404| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | Characteristic value to read. | 2405| callback | AsyncCallback<[BLECharacteristic](#blecharacteristic)> | Yes | Callback invoked to return the characteristic value read.| 2406 2407**Return value** 2408 2409No value is returned. 2410 2411**Example** 2412 2413```js 2414function readCcc(code, BLECharacteristic) { 2415 if (code.code != 0) { 2416 return; 2417 } 2418 console.log('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid); 2419 let value = new Uint8Array(BLECharacteristic.characteristicValue); 2420 console.log('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 2421} 2422 2423let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2424let descriptors = []; 2425let bufferDesc = new ArrayBuffer(8); 2426let descV = new Uint8Array(bufferDesc); 2427descV[0] = 11; 2428let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2429characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2430descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2431descriptors[0] = descriptor; 2432 2433let bufferCCC = new ArrayBuffer(8); 2434let cccV = new Uint8Array(bufferCCC); 2435cccV[0] = 1; 2436let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2437characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2438characteristicValue: bufferCCC, descriptors:descriptors}; 2439 2440device.readCharacteristicValue(characteristic, readCcc); 2441``` 2442 2443 2444### readCharacteristicValue 2445 2446readCharacteristicValue(characteristic: BLECharacteristic): Promise<BLECharacteristic> 2447 2448Reads the characteristic value of the specific service of the remote BLE device. This API uses a promise to return the result. 2449 2450**Required permissions**: ohos.permission.USE_BLUETOOTH 2451 2452**System capability**: SystemCapability.Communication.Bluetooth.Core 2453 2454**Parameters** 2455 2456| Name | Type | Mandatory | Description | 2457| -------------- | --------------------------------------- | ---- | -------- | 2458| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | Characteristic value to read.| 2459 2460**Return value** 2461 2462| Type | Description | 2463| ---------------------------------------- | -------------------------- | 2464| Promise<[BLECharacteristic](#blecharacteristic)> | Promise used to return the characteristic value read.| 2465 2466**Example** 2467 2468```js 2469let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2470let descriptors = []; 2471let bufferDesc = new ArrayBuffer(8); 2472let descV = new Uint8Array(bufferDesc); 2473descV[0] = 11; 2474let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2475characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2476descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2477descriptors[0] = descriptor; 2478 2479let bufferCCC = new ArrayBuffer(8); 2480let cccV = new Uint8Array(bufferCCC); 2481cccV[0] = 1; 2482let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2483characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2484characteristicValue: bufferCCC, descriptors:descriptors}; 2485 2486device.readCharacteristicValue(characteristic); 2487``` 2488 2489 2490### readDescriptorValue 2491 2492readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<BLEDescriptor>): void 2493 2494Reads the descriptor contained in the specific characteristic of the remote BLE device. This API uses an asynchronous callback to return the result. 2495 2496**Required permissions**: ohos.permission.USE_BLUETOOTH 2497 2498**System capability**: SystemCapability.Communication.Bluetooth.Core 2499 2500**Parameters** 2501 2502| Name | Type | Mandatory | Description | 2503| ---------- | ---------------------------------------- | ---- | ----------------------- | 2504| descriptor | [BLEDescriptor](#bledescriptor) | Yes | Descriptor to read. | 2505| callback | AsyncCallback<[BLECharacteristic](#blecharacteristic)> | Yes | Callback invoked to return the descriptor read.| 2506 2507**Return value** 2508 2509No value is returned. 2510 2511**Example** 2512 2513```js 2514function readDesc(code, BLEDescriptor) { 2515 if (code.code != 0) { 2516 return; 2517 } 2518 console.log('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid); 2519 let value = new Uint8Array(BLEDescriptor.descriptorValue); 2520 console.log('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 2521} 2522 2523let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2524let bufferDesc = new ArrayBuffer(8); 2525let descV = new Uint8Array(bufferDesc); 2526descV[0] = 11; 2527let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2528 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2529 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2530device.readDescriptorValue(descriptor, readDesc); 2531``` 2532 2533 2534### readDescriptorValue 2535 2536readDescriptorValue(descriptor: BLEDescriptor): Promise<BLEDescriptor> 2537 2538Reads the descriptor contained in the specific characteristic of the remote BLE device. This API uses a promise to return the result. 2539 2540**Required permissions**: ohos.permission.USE_BLUETOOTH 2541 2542**System capability**: SystemCapability.Communication.Bluetooth.Core 2543 2544**Parameters** 2545 2546| Name | Type | Mandatory | Description | 2547| ---------- | ------------------------------- | ---- | -------- | 2548| descriptor | [BLEDescriptor](#bledescriptor) | Yes | Descriptor to read.| 2549 2550**Return value** 2551 2552| Type | Description | 2553| ---------------------------------------- | -------------------------- | 2554| Promise<[BLEDescriptor](#bledescriptor)> | Promise used to return the descriptor read.| 2555 2556**Example** 2557 2558```js 2559let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2560let bufferDesc = new ArrayBuffer(8); 2561let descV = new Uint8Array(bufferDesc); 2562descV[0] = 11; 2563let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2564 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2565 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2566device.readDescriptorValue(descriptor); 2567``` 2568 2569 2570### writeCharacteristicValue 2571 2572writeCharacteristicValue(characteristic: BLECharacteristic): boolean 2573 2574Writes a characteristic value to the remote BLE device. 2575 2576**Required permissions**: ohos.permission.USE_BLUETOOTH 2577 2578**System capability**: SystemCapability.Communication.Bluetooth.Core 2579 2580**Parameters** 2581 2582| Name | Type | Mandatory | Description | 2583| -------------- | --------------------------------------- | ---- | ------------------- | 2584| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | Binary value and other parameters of the BLE device characteristic.| 2585 2586**Return value** 2587 2588| Type | Description | 2589| ------- | --------------------------- | 2590| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2591 2592**Example** 2593 2594```js 2595let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2596let descriptors = []; 2597let bufferDesc = new ArrayBuffer(8); 2598let descV = new Uint8Array(bufferDesc); 2599descV[0] = 11; 2600let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2601 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2602 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2603descriptors[0] = descriptor; 2604 2605let bufferCCC = new ArrayBuffer(8); 2606let cccV = new Uint8Array(bufferCCC); 2607cccV[0] = 1; 2608let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2609 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2610 characteristicValue: bufferCCC, descriptors:descriptors}; 2611let retWriteCcc = device.writeCharacteristicValue(characteristic); 2612if (retWriteCcc) { 2613 console.log('write characteristic successfully'); 2614} else { 2615 console.log('write characteristic failed'); 2616} 2617``` 2618 2619 2620### writeDescriptorValue 2621 2622writeDescriptorValue(descriptor: BLEDescriptor): boolean 2623 2624Writes binary data to the specific descriptor of the remote BLE device. 2625 2626**Required permissions**: ohos.permission.USE_BLUETOOTH 2627 2628**System capability**: SystemCapability.Communication.Bluetooth.Core 2629 2630**Parameters** 2631 2632| Name | Type | Mandatory | Description | 2633| ---------- | ------------------------------- | ---- | ------------------ | 2634| descriptor | [BLEDescriptor](#bledescriptor) | Yes | Binary value and other parameters of the BLE device descriptor.| 2635 2636**Return value** 2637 2638| Type | Description | 2639| ------- | --------------------------- | 2640| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2641 2642**Example** 2643 2644```js 2645let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2646let bufferDesc = new ArrayBuffer(8); 2647let descV = new Uint8Array(bufferDesc); 2648descV[0] = 22; 2649let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2650 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2651 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2652let retWriteDesc = device.writeDescriptorValue(descriptor); 2653if (retWriteDesc) { 2654 console.log('bluetooth write descriptor successfully'); 2655} else { 2656 console.log('bluetooth write descriptor failed'); 2657} 2658``` 2659 2660 2661### setBLEMtuSize 2662 2663setBLEMtuSize(mtu: number): boolean 2664 2665Sets 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). 2666 2667**Required permissions**: ohos.permission.USE_BLUETOOTH 2668 2669**System capability**: SystemCapability.Communication.Bluetooth.Core 2670 2671**Parameters** 2672 2673| Name | Type | Mandatory | Description | 2674| ---- | ------ | ---- | -------------- | 2675| mtu | number | Yes | MTU to set, which ranges from 22 to 512 bytes.| 2676 2677**Return value** 2678 2679| Type | Description | 2680| ------- | ---------------------------- | 2681| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2682 2683**Example** 2684 2685```js 2686let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2687device.setBLEMtuSize(128); 2688``` 2689 2690 2691### setNotifyCharacteristicChanged 2692 2693setNotifyCharacteristicChanged(characteristic: BLECharacteristic, enable: boolean): boolean 2694 2695Sets the function of notifying the GATT client when the characteristic value of the remote BLE device changes. 2696 2697**Required permissions**: ohos.permission.USE_BLUETOOTH 2698 2699**System capability**: SystemCapability.Communication.Bluetooth.Core 2700 2701**Parameters** 2702 2703| Name | Type | Mandatory | Description | 2704| -------------- | --------------------------------------- | ---- | ----------------------------- | 2705| characteristic | [BLECharacteristic](#blecharacteristic) | Yes | BLE characteristic to listen for. | 2706| 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.| 2707 2708**Return value** 2709 2710| Type | Description | 2711| ------- | ------------------------- | 2712| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2713 2714**Example** 2715 2716```js 2717// Create descriptors. 2718let descriptors = []; 2719let arrayBuffer = new ArrayBuffer(8); 2720let descV = new Uint8Array(arrayBuffer); 2721descV[0] = 11; 2722let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2723 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2724 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 2725descriptors[0] = descriptor; 2726 2727let arrayBufferC = new ArrayBuffer(8); 2728let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2729 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2730let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2731device.setNotifyCharacteristicChanged(characteristic, false); 2732``` 2733 2734 2735### on('BLECharacteristicChange') 2736 2737on(type: "BLECharacteristicChange", callback: Callback<BLECharacteristic>): void 2738 2739Subscribes to the BLE characteristic change events. The client can receive a notification from the server only after the **setNotifyCharacteristicChanged** method is called. 2740 2741**Required permissions**: ohos.permission.USE_BLUETOOTH 2742 2743**System capability**: SystemCapability.Communication.Bluetooth.Core 2744 2745**Parameters** 2746 2747| Name | Type | Mandatory | Description | 2748| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2749| type | string | Yes | Event type. The value **BLECharacteristicChange** indicates a characteristic value change event.| 2750| callback | Callback<[BLECharacteristic](#blecharacteristic)> | Yes | Callback invoked to return the characteristic value changes. | 2751 2752**Return value** 2753 2754No value is returned. 2755 2756**Example** 2757 2758```js 2759function CharacteristicChange(CharacteristicChangeReq) { 2760 let serviceUuid = CharacteristicChangeReq.serviceUuid; 2761 let characteristicUuid = CharacteristicChangeReq.characteristicUuid; 2762 let value = new Uint8Array(CharacteristicChangeReq.characteristicValue); 2763} 2764let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2765device.on('BLECharacteristicChange', CharacteristicChange); 2766``` 2767 2768 2769### off('BLECharacteristicChange') 2770 2771off(type: "BLECharacteristicChange", callback?: Callback<BLECharacteristic>): void 2772 2773Unsubscribes from the BLE characteristic change events. 2774 2775**Required permissions**: ohos.permission.USE_BLUETOOTH 2776 2777**System capability**: SystemCapability.Communication.Bluetooth.Core 2778 2779**Parameters** 2780 2781| Name | Type | Mandatory | Description | 2782| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2783| type | string | Yes | Event type. The value **BLECharacteristicChange** indicates a characteristic value change event.| 2784| callback | Callback<[BLECharacteristic](#blecharacteristic)> | No | Callback used to report the characteristic value. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 2785 2786**Return value** 2787 2788No value is returned. 2789 2790**Example** 2791 2792```js 2793let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2794device.off('BLECharacteristicChange'); 2795``` 2796 2797 2798### on('BLEConnectionStateChange') 2799 2800on(type: "BLEConnectionStateChange", callback: Callback<BLEConnectChangedState>): void 2801 2802Subscribes to the BLE connection state change events. 2803 2804**Required permissions**: ohos.permission.USE_BLUETOOTH 2805 2806**System capability**: SystemCapability.Communication.Bluetooth.Core 2807 2808**Parameters** 2809 2810| Name | Type | Mandatory | Description | 2811| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2812| type | string | Yes | Event type. The value **BLEConnectionStateChange** indicates a BLE connection state change event.| 2813| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | Yes | Callback invoked to return the BLE connection state. | 2814 2815**Return value** 2816 2817No value is returned. 2818 2819**Example** 2820 2821```js 2822function ConnectStateChanged(state) { 2823 console.log('bluetooth connect state changed'); 2824 let connectState = state.state; 2825} 2826let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2827device.on('BLEConnectionStateChange', ConnectStateChanged); 2828``` 2829 2830 2831### off('BLEConnectionStateChange') 2832 2833off(type: "BLEConnectionStateChange", callback?: Callback<BLEConnectChangedState>): void 2834 2835Unsubscribes from the BLE connection state change events. 2836 2837**Required permissions**: ohos.permission.USE_BLUETOOTH 2838 2839**System capability**: SystemCapability.Communication.Bluetooth.Core 2840 2841**Parameters** 2842 2843| Name | Type | Mandatory | Description | 2844| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2845| type | string | Yes | Event type. The value **BLEConnectionStateChange** indicates a BLE connection state change event.| 2846| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstate)> | No | Callback used to report the BLE connection state. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.| 2847 2848**Return value** 2849 2850No value is returned. 2851 2852**Example** 2853 2854```js 2855let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2856device.off('BLEConnectionStateChange'); 2857``` 2858 2859 2860### getDeviceName 2861 2862getDeviceName(callback: AsyncCallback<string>): void 2863 2864Obtains the name of the remote BLE device. This API uses an asynchronous callback to return the result. 2865 2866**Required permissions**: ohos.permission.USE_BLUETOOTH 2867 2868**System capability**: SystemCapability.Communication.Bluetooth.Core 2869 2870**Parameters** 2871 2872| Name | Type | Mandatory | Description | 2873| -------- | --------------------------- | ---- | ------------------------------- | 2874| callback | AsyncCallback<string> | Yes | Callback invoked to return the remote BLE device name obtained.| 2875 2876**Return value** 2877 2878No value is returned. 2879 2880**Example** 2881 2882```js 2883// callback 2884let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 2885let deviceName = gattClient.getDeviceName((err, data)=> { 2886 console.info('device name err ' + JSON.stringify(err)); 2887 console.info('device name' + JSON.stringify(data)); 2888}) 2889``` 2890 2891 2892### getDeviceName 2893 2894getDeviceName(): Promise<string> 2895 2896Obtains the name of the remote BLE device. This API uses a promise to return the result. 2897 2898**Required permissions**: ohos.permission.USE_BLUETOOTH 2899 2900**System capability**: SystemCapability.Communication.Bluetooth.Core 2901 2902**Return value** 2903 2904| Type | Description | 2905| --------------------- | ---------------------------------- | 2906| Promise<string> | Promise used to return the remote BLE device name.| 2907 2908**Example** 2909 2910```js 2911// promise 2912let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 2913let ret = device.connect(); 2914let deviceName = gattClient.getDeviceName().then((data) => { 2915 console.info('device name' + JSON.stringify(data)); 2916}) 2917``` 2918 2919 2920### getRssiValue 2921 2922getRssiValue(callback: AsyncCallback<number>): void 2923 2924Obtains 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). 2925 2926**Required permissions**: ohos.permission.USE_BLUETOOTH 2927 2928**System capability**: SystemCapability.Communication.Bluetooth.Core 2929 2930**Parameters** 2931 2932| Name | Type | Mandatory | Description | 2933| -------- | --------------------------- | ---- | ------------------------------ | 2934| callback | AsyncCallback<number> | Yes | Callback invoked to return the RSSI, in dBm.| 2935 2936**Return value** 2937 2938No value is returned. 2939 2940**Example** 2941 2942```js 2943// callback 2944let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 2945let ret = device.connect(); 2946let rssi = gattClient.getRssiValue((err, data)=> { 2947 console.info('rssi err ' + JSON.stringify(err)); 2948 console.info('rssi value' + JSON.stringify(data)); 2949}) 2950``` 2951 2952 2953### getRssiValue 2954 2955getRssiValue(): Promise<number> 2956 2957Obtains 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). 2958 2959**Required permissions**: ohos.permission.USE_BLUETOOTH 2960 2961**System capability**: SystemCapability.Communication.Bluetooth.Core 2962 2963**Return value** 2964 2965| Type | Description | 2966| --------------------- | --------------------------------- | 2967| Promise<number> | Promise used to return the RSSI, in dBm.| 2968 2969**Example** 2970 2971```js 2972// promise 2973let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 2974let rssi = gattClient.getRssiValue().then((data) => { 2975 console.info('rssi' + JSON.stringify(data)); 2976}) 2977``` 2978 2979## ScanMode<sup>8+</sup><a name="ScanMode"></a> 2980 2981Enumerates the scan modes. 2982 2983**System capability**: SystemCapability.Communication.Bluetooth.Core 2984 2985| Name | Default Value | Description | 2986| ---------------------------------------- | ---- | --------------- | 2987| SCAN_MODE_NONE | 0 | No scan mode. | 2988| SCAN_MODE_CONNECTABLE | 1 | Connectable mode. | 2989| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | General discoverable mode. | 2990| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | Limited discoverable mode. | 2991| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | General connectable and discoverable mode.| 2992| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | Limited connectable and discoverable mode.| 2993 2994## BondState<sup>8+</sup><a name="BondState"></a> 2995 2996Enumerates the pairing states. 2997 2998**System capability**: SystemCapability.Communication.Bluetooth.Core 2999 3000| Name | Default Value | Description | 3001| ------------------ | ---- | ------ | 3002| BOND_STATE_INVALID | 0 | Invalid pairing.| 3003| BOND_STATE_BONDING | 1 | Pairing. | 3004| BOND_STATE_BONDED | 2 | Paired. | 3005 3006 3007## SppOption<sup>8+</sup><a name="SppOption"></a> 3008 3009Defines the SPP configuration parameters. 3010 3011**System capability**: SystemCapability.Communication.Bluetooth.Core 3012 3013| Name | Type | Readable | Writable | Description | 3014| ------ | ------------------- | ---- | ---- | ----------- | 3015| uuid | string | Yes | Yes | UUID of the SPP.| 3016| secure | boolean | Yes | Yes | Whether it is a secure channel. | 3017| type | [SppType](#spptype) | Yes | Yes | Type of the SPP link. | 3018 3019 3020## SppType<sup>8+</sup><a name="SppType"></a> 3021 3022Enumerates the SPP link types. 3023 3024**System capability**: SystemCapability.Communication.Bluetooth.Core 3025 3026| Name | Default Value | Description | 3027| ---------- | ---- | ------------- | 3028| SPP_RFCOMM | 0 | Radio frequency communication (RFCOMM) link type.| 3029 3030 3031## GattService 3032 3033Defines the GATT service API parameters. 3034 3035**System capability**: SystemCapability.Communication.Bluetooth.Core 3036 3037| Name | Type | Readable | Writable | Description | 3038| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 3039| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3040| isPrimary | boolean | Yes | Yes | Whether the service is a primary service. The value **true** means a primary service. | 3041| characteristics | Array<[BLECharacteristic](#blecharacteristic)> | Yes | Yes | List of characteristics of the service. | 3042| includeServices | Array<[GattService](#gattservice)> | Yes | Yes | Services on which the service depends. | 3043 3044 3045## BLECharacteristic 3046 3047Defines the characteristic API parameters. 3048 3049**System capability**: SystemCapability.Communication.Bluetooth.Core 3050 3051| Name | Type | Readable | Writable | Description | 3052| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 3053| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3054| characteristicUuid | string | Yes | Yes | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3055| characteristicValue | ArrayBuffer | Yes | Yes | Binary value of the characteristic. | 3056| descriptors | Array<[BLEDescriptor](#bledescriptor)> | Yes | Yes | List of descriptors of the characteristic. | 3057 3058 3059## BLEDescriptor 3060 3061Defines the descriptor API parameters. 3062 3063**System capability**: SystemCapability.Communication.Bluetooth.Core 3064 3065| Name | Type | Readable | Writable | Description | 3066| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 3067| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3068| characteristicUuid | string | Yes | Yes | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3069| descriptorUuid | string | Yes | Yes | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 3070| descriptorValue | ArrayBuffer | Yes | Yes | Binary value of the descriptor. | 3071 3072 3073## NotifyCharacteristic 3074 3075Defines the parameters in the notifications sent when the server characteristic value changes. 3076 3077**System capability**: SystemCapability.Communication.Bluetooth.Core 3078 3079| Name | Type | Readable | Writable | Description | 3080| ------------------- | ----------- | ---- | ---- | ---------------------------------------- | 3081| serviceUuid | string | Yes | Yes | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3082| characteristicUuid | string | Yes | Yes | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3083| characteristicValue | ArrayBuffer | Yes | Yes | Binary value of the characteristic. | 3084| 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.| 3085 3086 3087## CharacteristicReadReq 3088 3089Defines the parameters of the **CharacteristicReadReq** event received by the server. 3090 3091**System capability**: SystemCapability.Communication.Bluetooth.Core 3092 3093| Name | Type | Readable | Writable | Description | 3094| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 3095| deviceId | string | Yes | No | Address of the remote device that sends the **CharacteristicReadReq** event, for example, XX:XX:XX:XX:XX:XX.| 3096| transId | number | Yes | No | Transmission ID of the read request. The response returned by the server must use the same transmission ID. | 3097| 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.| 3098| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3099| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3100 3101 3102## CharacteristicWriteReq 3103 3104Defines the parameters of the **CharacteristicWriteReq** event received by the server. 3105 3106**System capability**: SystemCapability.Communication.Bluetooth.Core 3107 3108| Name | Type | Readable | Writable | Description | 3109| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 3110| deviceId | string | Yes | No | Address of the remote device that sends the **CharacteristicWriteReq** event, for example, XX:XX:XX:XX:XX:XX.| 3111| transId | number | Yes | No | Transmission ID of the write request. The response returned by the server must use the same transmission ID. | 3112| 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.| 3113| descriptorUuid | string | Yes | No | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 3114| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3115| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3116 3117 3118## DescriptorReadReq 3119 3120Defines the parameters of the **DescriptorReadReq** event received by the server. 3121 3122**System capability**: SystemCapability.Communication.Bluetooth.Core 3123 3124| Name | Type | Readable | Writable | Description | 3125| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 3126| deviceId | string | Yes | No | Address of the remote device that sends a **DescriptorReadReq** event, for example, XX:XX:XX:XX:XX:XX.| 3127| transId | number | Yes | No | Transmission ID of the read request. The response returned by the server must use the same transmission ID. | 3128| 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.| 3129| descriptorUuid | string | Yes | No | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 3130| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3131| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3132 3133 3134## DescriptorWriteReq 3135 3136Defines the parameters of the **DescriptorWriteReq** event received by the server. 3137 3138**System capability**: SystemCapability.Communication.Bluetooth.Core 3139 3140| Name | Type | Readable | Writable | Description | 3141| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 3142| deviceId | string | Yes | No | Address of the remote device that sends a **DescriptorWriteReq** event, for example, XX:XX:XX:XX:XX:XX.| 3143| transId | number | Yes | No | Transmission ID of the write request. The response returned by the server must use the same transmission ID. | 3144| 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.| 3145| isPrep | boolean | Yes | No | Whether the write request is executed immediately. | 3146| needRsp | boolean | Yes | No | Whether to send a response to the GATT client. | 3147| value | ArrayBuffer | Yes | No | Binary value of the descriptor to write. | 3148| descriptorUuid | string | Yes | No | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.| 3149| characteristicUuid | string | Yes | No | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.| 3150| serviceUuid | string | Yes | No | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3151 3152 3153## ServerResponse 3154 3155Defines the parameters of the server's response to the GATT client's read/write request. 3156 3157**System capability**: SystemCapability.Communication.Bluetooth.Core 3158 3159| Name | Type | Readable | Writable | Description | 3160| -------- | ----------- | ---- | ---- | -------------------------------------- | 3161| deviceId | string | Yes | No | Address of the remote device, for example, XX:XX:XX:XX:XX:XX. | 3162| 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. | 3163| status | number | Yes | No | Response state. Set this parameter to **0**, which indicates a normal response. | 3164| offset | number | Yes | No | Start read/write position. The value must be the same as the offset carried in the read/write request.| 3165| value | ArrayBuffer | Yes | No | Binary data in the response. | 3166 3167 3168## BLEConnectChangedState 3169 3170Defines the parameters of **BLEConnectChangedState**. 3171 3172**System capability**: SystemCapability.Communication.Bluetooth.Core 3173 3174| Name | Type | Readable| Writable| Description | 3175| -------- | ------------------------------------------------- | ---- | ---- | --------------------------------------------- | 3176| deviceId | string | Yes | No | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 3177| state | [ProfileConnectionState](#profileconnectionstate) | Yes | Yes | BLE connection state. | 3178 3179 3180## ProfileConnectionState 3181 3182Enumerates the profile connection states. 3183 3184**System capability**: SystemCapability.Communication.Bluetooth.Core 3185 3186| Name | Default Value | Description | 3187| ------------------- | ---- | -------------- | 3188| STATE_DISCONNECTED | 0 | Disconnected. | 3189| STATE_CONNECTING | 1 | Connecting.| 3190| STATE_CONNECTED | 2 | Connected. | 3191| STATE_DISCONNECTING | 3 | Disconnecting.| 3192 3193 3194## ScanFilter 3195 3196Defines the scan filter parameters. 3197 3198**System capability**: SystemCapability.Communication.Bluetooth.Core 3199 3200| Name | Type | Readable | Writable | Description | 3201| ----------- | ------ | ---- | ---- | ---------------------------------------- | 3202| deviceId | string | Yes | Yes | Address of the BLE device to filter, for example, XX:XX:XX:XX:XX:XX. | 3203| name | string | Yes | Yes | Name of the BLE device to filter. | 3204| serviceUuid | string | Yes | Yes | Service UUID of the device to filter, for example, **00001888-0000-1000-8000-00805f9b34fb**.| 3205 3206 3207## ScanOptions 3208 3209Defines the scan configuration parameters. 3210 3211**System capability**: SystemCapability.Communication.Bluetooth.Core 3212 3213| Name | Type | Readable | Writable | Description | 3214| --------- | ----------------------- | ---- | ---- | -------------------------------------- | 3215| interval | number | Yes | Yes | Delay in reporting the scan result. The default value is **0**. | 3216| dutyMode | [ScanDuty](#scanduty) | Yes | Yes | Scan duty. The default value is **SCAN_MODE_LOW_POWER**. | 3217| matchMode | [MatchMode](#matchmode) | Yes | Yes | Hardware filtering match mode. The default value is **MATCH_MODE_AGGRESSIVE**.| 3218 3219 3220## ScanDuty 3221 3222Enumerates the scan duty options. 3223 3224**System capability**: SystemCapability.Communication.Bluetooth.Core 3225 3226| Name | Default Value | Description | 3227| --------------------- | ---- | ------------ | 3228| SCAN_MODE_LOW_POWER | 0 | Low-power mode, which is the default value.| 3229| SCAN_MODE_BALANCED | 1 | Balanced mode. | 3230| SCAN_MODE_LOW_LATENCY | 2 | Low-latency mode. | 3231 3232 3233## MatchMode 3234 3235Enumerates the hardware match modes of BLE scan filters. 3236 3237**System capability**: SystemCapability.Communication.Bluetooth.Core 3238 3239| Name | Default Value | Description | 3240| --------------------- | ---- | ---------------------------------------- | 3241| 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.| 3242| MATCH_MODE_STICKY | 2 | Hardware reports the scan result with a higher threshold of signal strength and sightings. | 3243 3244 3245## ScanResult 3246 3247Defines the scan result. 3248 3249**System capability**: SystemCapability.Communication.Bluetooth.Core 3250 3251| Name | Type | Readable | Writable | Description | 3252| -------- | ----------- | ---- | ---- | ---------------------------------- | 3253| deviceId | string | Yes | No | Address of the scanned device, for example, XX:XX:XX:XX:XX:XX.| 3254| rssi | number | Yes | No | RSSI of the device. | 3255| data | ArrayBuffer | Yes | No | Advertisement packets sent by the device. | 3256 3257 3258## BluetoothState 3259 3260Enumerates the Bluetooth states. 3261 3262**System capability**: SystemCapability.Communication.Bluetooth.Core 3263 3264| Name | Default Value | Description | 3265| --------------------- | ---- | ------------------ | 3266| STATE_OFF | 0 | Bluetooth is turned off. | 3267| STATE_TURNING_ON | 1 | Bluetooth is being turned on. | 3268| STATE_ON | 2 | Bluetooth is turned on. | 3269| STATE_TURNING_OFF | 3 | Bluetooth is being turned off. | 3270| STATE_BLE_TURNING_ON | 4 | The LE-only mode is being turned on for Bluetooth.| 3271| STATE_BLE_ON | 5 | Bluetooth is in LE-only mode. | 3272| STATE_BLE_TURNING_OFF | 6 | The LE-only mode is being turned off for Bluetooth.| 3273 3274 3275## AdvertiseSetting 3276 3277Defines the BLE advertising parameters. 3278 3279**System capability**: SystemCapability.Communication.Bluetooth.Core 3280 3281| Name | Type | Readable | Writable | Description | 3282| ----------- | ------- | ---- | ---- | ---------------------------------------- | 3283| 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).| 3284| txPower | number | Yes | Yes | Transmit power, in dBm. The value range is -127 to 1. The default value is **-7**. | 3285| connectable | boolean | Yes | Yes | Whether the advertisement is connectable. The default value is **true**. | 3286 3287 3288## AdvertiseData 3289 3290Defines the content of a BLE advertisement packet. 3291 3292**System capability**: SystemCapability.Communication.Bluetooth.Core 3293 3294| Name | Type | Readable | Writable | Description | 3295| --------------- | ---------------------------------------- | ---- | ---- | --------------------------- | 3296| serviceUuids | Array<string> | Yes | Yes | List of service UUIDs to broadcast.| 3297| manufactureData | Array<[ManufactureData](#manufacturedata)> | Yes | Yes | List of manufacturers to broadcast. | 3298| serviceData | Array<[ServiceData](#servicedata)> | Yes | Yes | List of service data to broadcast. | 3299 3300 3301## ManufactureData 3302 3303Defines the content of a BLE advertisement packet. 3304 3305**System capability**: SystemCapability.Communication.Bluetooth.Core 3306 3307| Name | Type | Readable | Writable | Description | 3308| ---------------- | ------------------- | ---- | ---- | ------------------ | 3309| manufactureId | Array<string> | Yes | Yes | Manufacturer ID allocated by the Bluetooth SIG.| 3310| manufactureValue | ArrayBuffer | Yes | Yes | Manufacturer data. | 3311 3312 3313## ServiceData 3314 3315Defines the service data contained in an advertisement packet. 3316 3317**System capability**: SystemCapability.Communication.Bluetooth.Core 3318 3319| Name | Type | Readable | Writable | Description | 3320| ------------ | ----------- | ---- | ---- | ---------- | 3321| serviceUuid | string | Yes | Yes | Service UUID.| 3322| serviceValue | ArrayBuffer | Yes | Yes | Service data. | 3323 3324 3325## PinRequiredParam<sup>8+</sup><a name="PinRequiredParam"></a> 3326 3327Defines the pairing request parameters. 3328 3329**System capability**: SystemCapability.Communication.Bluetooth.Core 3330 3331| Name | Type | Readable | Writable | Description | 3332| -------- | ------ | ---- | ---- | ----------- | 3333| deviceId | string | Yes | No | ID of the device to pair.| 3334| pinCode | string | Yes | No | Key for the device pairing. | 3335 3336 3337## BondStateParam<sup>8+</sup><a name="BondStateParam"></a> 3338 3339Defines the pairing state parameters. 3340 3341**System capability**: SystemCapability.Communication.Bluetooth.Core 3342 3343| Name | Type | Readable | Writable | Description | 3344| -------- | ------ | ---- | ---- | ----------- | 3345| deviceId | string | Yes | No | ID of the device.| 3346| state | BondState | Yes | No | State of the device.| 3347 3348 3349## StateChangeParam<sup>8+</sup><a name="StateChangeParam"></a> 3350 3351Defines the profile state change parameters. 3352 3353**System capability**: SystemCapability.Communication.Bluetooth.Core 3354 3355| Name | Type | Readable| Writable| Description | 3356| -------- | ------------------------------------------------- | ---- | ---- | ------------------------------- | 3357| deviceId | string | Yes | No | Address of a Bluetooth device. | 3358| state | [ProfileConnectionState](#profileconnectionstate) | Yes | No | Profile connection state of the device.| 3359 3360 3361## DeviceClass<sup>8+</sup><a name="DeviceClass"></a> 3362 3363Defines the class of a Bluetooth device. 3364 3365**System capability**: SystemCapability.Communication.Bluetooth.Core 3366 3367| Name | Type | Readable | Writable | Description | 3368| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 3369| majorClass | [MajorClass](#majorclass) | Yes | No | Major classes of Bluetooth devices. | 3370| majorMinorClass | [MajorMinorClass](#majorminorclass) | Yes | No | Major and minor classes of Bluetooth devices.| 3371| classOfDevice | number | Yes | No | Class of the device. | 3372 3373 3374 3375## MajorClass<sup>8+</sup><a name="MajorClass"></a> 3376 3377Enumerates the major classes of Bluetooth devices. 3378 3379**System capability**: SystemCapability.Communication.Bluetooth.Core 3380 3381| Name | Default Value | Description | 3382| ------------------- | ------ | ---------- | 3383| MAJOR_MISC | 0x0000 | Miscellaneous device. | 3384| MAJOR_COMPUTER | 0x0100 | Computer. | 3385| MAJOR_PHONE | 0x0200 | Mobile phone. | 3386| MAJOR_NETWORKING | 0x0300 | Network device. | 3387| MAJOR_AUDIO_VIDEO | 0x0400 | Audio or video device.| 3388| MAJOR_PERIPHERAL | 0x0500 | Peripheral device. | 3389| MAJOR_IMAGING | 0x0600 | Imaging device. | 3390| MAJOR_WEARABLE | 0x0700 | Wearable device. | 3391| MAJOR_TOY | 0x0800 | Toy. | 3392| MAJOR_HEALTH | 0x0900 | Health device. | 3393| MAJOR_UNCATEGORIZED | 0x1F00 | Unclassified device. | 3394 3395 3396## MajorMinorClass<sup>8+</sup><a name="MajorMinorClass"></a> 3397 3398Enumerates the major and minor classes of Bluetooth devices. 3399 3400**System capability**: SystemCapability.Communication.Bluetooth.Core 3401 3402| Name | Default Value | Description | 3403| ---------------------------------------- | ------ | --------------- | 3404| COMPUTER_UNCATEGORIZED | 0x0100 | Unclassified computer. | 3405| COMPUTER_DESKTOP | 0x0104 | Desktop computer. | 3406| COMPUTER_SERVER | 0x0108 | Server. | 3407| COMPUTER_LAPTOP | 0x010C | Laptop. | 3408| COMPUTER_HANDHELD_PC_PDA | 0x0110 | Hand-held computer. | 3409| COMPUTER_PALM_SIZE_PC_PDA | 0x0114 | Palmtop computer. | 3410| COMPUTER_WEARABLE | 0x0118 | Wearable computer. | 3411| COMPUTER_TABLET | 0x011C | Tablet. | 3412| PHONE_UNCATEGORIZED | 0x0200 | Unclassified mobile phone. | 3413| PHONE_CELLULAR | 0x0204 | Portable phone. | 3414| PHONE_CORDLESS | 0x0208 | Cordless phone. | 3415| PHONE_SMART | 0x020C | Smartphone. | 3416| PHONE_MODEM_OR_GATEWAY | 0x0210 | Modem or gateway phone.| 3417| PHONE_ISDN | 0x0214 | ISDN phone. | 3418| NETWORK_FULLY_AVAILABLE | 0x0300 | Device with network fully available. | 3419| NETWORK_1_TO_17_UTILIZED | 0x0320 | Device used on network 1 to 17. | 3420| NETWORK_17_TO_33_UTILIZED | 0x0340 | Device used on network 17 to 33. | 3421| NETWORK_33_TO_50_UTILIZED | 0x0360 | Device used on network 33 to 50. | 3422| NETWORK_60_TO_67_UTILIZED | 0x0380 | Device used on network 60 to 67. | 3423| NETWORK_67_TO_83_UTILIZED | 0x03A0 | Device used on network 67 to 83. | 3424| NETWORK_83_TO_99_UTILIZED | 0x03C0 | Device used on network 83 to 99. | 3425| NETWORK_NO_SERVICE | 0x03E0 | Device without network service | 3426| AUDIO_VIDEO_UNCATEGORIZED | 0x0400 | Unclassified audio or video device. | 3427| AUDIO_VIDEO_WEARABLE_HEADSET | 0x0404 | Wearable audio or video headset. | 3428| AUDIO_VIDEO_HANDSFREE | 0x0408 | Hands-free audio or video device. | 3429| AUDIO_VIDEO_MICROPHONE | 0x0410 | Audio or video microphone. | 3430| AUDIO_VIDEO_LOUDSPEAKER | 0x0414 | Audio or video loudspeaker. | 3431| AUDIO_VIDEO_HEADPHONES | 0x0418 | Audio or video headphones. | 3432| AUDIO_VIDEO_PORTABLE_AUDIO | 0x041C | Portable audio or video device. | 3433| AUDIO_VIDEO_CAR_AUDIO | 0x0420 | In-vehicle audio or video device. | 3434| AUDIO_VIDEO_SET_TOP_BOX | 0x0424 | Audio or video STB device. | 3435| AUDIO_VIDEO_HIFI_AUDIO | 0x0428 | High-fidelity speaker device. | 3436| AUDIO_VIDEO_VCR | 0x042C | Video cassette recording (VCR) device. | 3437| AUDIO_VIDEO_VIDEO_CAMERA | 0x0430 | Camera. | 3438| AUDIO_VIDEO_CAMCORDER | 0x0434 | Camcorder | 3439| AUDIO_VIDEO_VIDEO_MONITOR | 0x0438 | Audio or video monitor. | 3440| AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER | 0x043C | Video display or loudspeaker. | 3441| AUDIO_VIDEO_VIDEO_CONFERENCING | 0x0440 | Video conferencing device. | 3442| AUDIO_VIDEO_VIDEO_GAMING_TOY | 0x0448 | Audio or video gaming toy. | 3443| PERIPHERAL_NON_KEYBOARD_NON_POINTING | 0x0500 | Non-keyboard or non-pointing peripheral device. | 3444| PERIPHERAL_KEYBOARD | 0x0540 | Keyboard device. | 3445| PERIPHERAL_POINTING_DEVICE | 0x0580 | Pointing peripheral device. | 3446| PERIPHERAL_KEYBOARD_POINTING | 0x05C0 | Keyboard pointing device. | 3447| PERIPHERAL_UNCATEGORIZED | 0x0500 | Unclassified peripheral device. | 3448| PERIPHERAL_JOYSTICK | 0x0504 | Peripheral joystick. | 3449| PERIPHERAL_GAMEPAD | 0x0508 | Peripheral game pad | 3450| PERIPHERAL_REMOTE_CONTROL | 0x05C0 | Peripheral remote control device | 3451| PERIPHERAL_SENSING_DEVICE | 0x0510 | Peripheral sensing device. | 3452| PERIPHERAL_DIGITIZER_TABLET | 0x0514 | Peripheral digitizer tablet.| 3453| PERIPHERAL_CARD_READER | 0x0518 | Peripheral card reader. | 3454| PERIPHERAL_DIGITAL_PEN | 0x051C | Peripheral digital pen. | 3455| PERIPHERAL_SCANNER_RFID | 0x0520 | Peripheral RFID scanner. | 3456| PERIPHERAL_GESTURAL_INPUT | 0x0522 | Gesture input device. | 3457| IMAGING_UNCATEGORIZED | 0x0600 | Unclassified imaging device. | 3458| IMAGING_DISPLAY | 0x0610 | Imaging display device. | 3459| IMAGING_CAMERA | 0x0620 | Imaging camera device. | 3460| IMAGING_SCANNER | 0x0640 | Imaging scanner. | 3461| IMAGING_PRINTER | 0x0680 | Imaging printer. | 3462| WEARABLE_UNCATEGORIZED | 0x0700 | Unclassified wearable device. | 3463| WEARABLE_WRIST_WATCH | 0x0704 | Smart watch. | 3464| WEARABLE_PAGER | 0x0708 | Wearable pager. | 3465| WEARABLE_JACKET | 0x070C | Smart jacket. | 3466| WEARABLE_HELMET | 0x0710 | Wearable helmet. | 3467| WEARABLE_GLASSES | 0x0714 | Wearable glasses. | 3468| TOY_UNCATEGORIZED | 0x0800 | Unclassified toy. | 3469| TOY_ROBOT | 0x0804 | Toy robot. | 3470| TOY_VEHICLE | 0x0808 | Toy vehicle. | 3471| TOY_DOLL_ACTION_FIGURE | 0x080C | Humanoid toy doll. | 3472| TOY_CONTROLLER | 0x0810 | Toy controller. | 3473| TOY_GAME | 0x0814 | Toy gaming device. | 3474| HEALTH_UNCATEGORIZED | 0x0900 | Unclassified health devices. | 3475| HEALTH_BLOOD_PRESSURE | 0x0904 | Blood pressure device. | 3476| HEALTH_THERMOMETER | 0x0908 | Thermometer | 3477| HEALTH_WEIGHING | 0x090C | Body scale. | 3478| HEALTH_GLUCOSE | 0x0910 | Blood glucose monitor. | 3479| HEALTH_PULSE_OXIMETER | 0x0914 | Pulse oximeter. | 3480| HEALTH_PULSE_RATE | 0x0918 | Heart rate monitor. | 3481| HEALTH_DATA_DISPLAY | 0x091C | Health data display. | 3482| HEALTH_STEP_COUNTER | 0x0920 | Step counter. | 3483| HEALTH_BODY_COMPOSITION_ANALYZER | 0x0924 | Body composition analyzer. | 3484| HEALTH_PEAK_FLOW_MOITOR | 0x0928 | Hygrometer. | 3485| HEALTH_MEDICATION_MONITOR | 0x092C | Medication monitor. | 3486| HEALTH_KNEE_PROSTHESIS | 0x0930 | Prosthetic knee. | 3487| HEALTH_ANKLE_PROSTHESIS | 0x0934 | Prosthetic ankle. | 3488| HEALTH_GENERIC_HEALTH_MANAGER | 0x0938 | Generic health management device. | 3489| HEALTH_PERSONAL_MOBILITY_DEVICE | 0x093C | Personal mobility device. | 3490 3491 3492## PlayingState<sup>8+</sup><a name="PlayingState"></a> 3493 3494Enumerates the A2DP playing states. 3495 3496**System capability**: SystemCapability.Communication.Bluetooth.Core 3497 3498| Name | Default Value | Description | 3499| ----------------- | ------ | ------- | 3500| STATE_NOT_PLAYING | 0x0000 | Not playing. | 3501| STATE_PLAYING | 0x0001 | Playing.| 3502 3503 3504## ProfileId<sup>8+</sup><a name="ProfileId"></a> 3505 3506Enumerates the Bluetooth profiles. 3507 3508**System capability**: SystemCapability.Communication.Bluetooth.Core 3509 3510| Name | Default Value | Description | 3511| -------------------------------- | ------ | --------------- | 3512| PROFILE_A2DP_SOURCE | 0x0001 | A2DP profile.| 3513| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | HFP profile. | 3514