js-apis-bluetooth.md 122.8 KB
Newer Older
A
annie_wangli 已提交
1 2 3 4
# Bluetooth

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> 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.
A
annie_wangli 已提交
5
>
Z
zengyawen 已提交
6
> The Bluetooth module provides Classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising.
Z
zengyawen 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26


## Modules to Import

```
import bluetooth from '@ohos.bluetooth';
```


## Required Permissions

ohos.permission.USE_BLUETOOTH

ohos.permission.MANAGE_BLUETOOTH

ohos.permission.DISCOVER_BLUETOOTH

ohos.permission.LOCATION


A
annie_wangli 已提交
27
## bluetooth.enableBluetooth<sup>8+</sup><a name="enableBluetooth"></a>
Z
zengyawen 已提交
28 29 30 31 32

enableBluetooth(): boolean

Enables Bluetooth.

A
annie_wangli 已提交
33
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
34

A
annie_wangli 已提交
35 36 37 38
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
39 40
| Type     | Description                      |
| ------- | ------------------------ |
A
annie_wangli 已提交
41
| boolean | Returns **true** if Bluetooth is enabled; returns **false** otherwise.|
Z
zengyawen 已提交
42

A
annie_wangli 已提交
43
**Example**
Z
zengyawen 已提交
44

A
annie_wangli 已提交
45
```js
Z
zengyawen 已提交
46 47 48 49
let enable = bluetooth.enableBluetooth();
```


A
annie_wangli 已提交
50
## bluetooth.disableBluetooth<sup>8+</sup><a name="disableBluetooth"></a>
Z
zengyawen 已提交
51 52 53 54 55

disableBluetooth(): boolean

Disables Bluetooth.

A
annie_wangli 已提交
56
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
57

A
annie_wangli 已提交
58 59 60 61
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
62 63
| Type     | Description                      |
| ------- | ------------------------ |
A
annie_wangli 已提交
64
| boolean | Returns **true** if Bluetooth is disabled; returns **false** otherwise.|
Z
zengyawen 已提交
65

A
annie_wangli 已提交
66
**Example**
Z
zengyawen 已提交
67

A
annie_wangli 已提交
68
```js
Z
zengyawen 已提交
69 70 71 72
let disable = bluetooth.disableBluetooth();
```


A
annie_wangli 已提交
73
## bluetooth.getLocalName<sup>8+</sup><a name="getLocalName"></a>
Z
zengyawen 已提交
74 75 76 77 78

getLocalName(): string

Obtains the name of the local Bluetooth device.

A
annie_wangli 已提交
79
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
80

A
annie_wangli 已提交
81 82 83 84
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
85 86
| Type    | Description       |
| ------ | --------- |
A
annie_wangli 已提交
87
| string | Name of the local Bluetooth device obtained.|
Z
zengyawen 已提交
88

A
annie_wangli 已提交
89
**Example**
Z
zengyawen 已提交
90

A
annie_wangli 已提交
91
```js
Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101
let localName = bluetooth.getLocalName();
```


## bluetooth.getState

getState(): BluetoothState

Obtains the Bluetooth state.

A
annie_wangli 已提交
102
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
103

A
annie_wangli 已提交
104 105 106 107
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
108 109
| Type                               | Description       |
| --------------------------------- | --------- |
A
annie_wangli 已提交
110
| [BluetoothState](#bluetoothstate) | Bluetooth state obtained.|
Z
zengyawen 已提交
111

A
annie_wangli 已提交
112
**Example**
Z
zengyawen 已提交
113

A
annie_wangli 已提交
114
```js
Z
zengyawen 已提交
115 116 117 118 119 120 121 122 123 124
let state = bluetooth.getState();
```


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

Obtains the profile connection state of this Bluetooth device.

A
annie_wangli 已提交
125
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
126

A
annie_wangli 已提交
127 128 129 130
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
131 132
| Type                                      | Description                 |
| ---------------------------------------- | ------------------- |
A
annie_wangli 已提交
133
| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.|
Z
zengyawen 已提交
134

A
annie_wangli 已提交
135
**Example**
Z
zengyawen 已提交
136

A
annie_wangli 已提交
137
```js
Z
zengyawen 已提交
138 139 140 141
let connectionState = bluetooth.getBtConnectionState();
```


A
annie_wangli 已提交
142
## bluetooth.setLocalName<sup>8+</sup><a name="setLocalName"></a>
Z
zengyawen 已提交
143 144 145 146 147

setLocalName(name: string): boolean

Sets the name of the local Bluetooth device.

A
annie_wangli 已提交
148 149 150 151 152
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
153

A
annie_wangli 已提交
154 155
| Name | Type    | Mandatory  | Description                   |
| ---- | ------ | ---- | --------------------- |
A
annie_wangli 已提交
156
| name | string | Yes   | Bluetooth device name to set. It cannot exceed 248 bytes.|
Z
zengyawen 已提交
157

A
annie_wangli 已提交
158
**Return value**
Z
zengyawen 已提交
159

A
annie_wangli 已提交
160 161
| Type     | Description                            |
| ------- | ------------------------------ |
A
annie_wangli 已提交
162
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
163

A
annie_wangli 已提交
164
**Example**
Z
zengyawen 已提交
165

A
annie_wangli 已提交
166
```js
Z
zengyawen 已提交
167 168 169 170 171 172 173 174 175 176
let ret = bluetooth.setLocalName('device_name');
```


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

Initiates Bluetooth pairing.

A
annie_wangli 已提交
177 178 179
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
180

A
annie_wangli 已提交
181 182
**Parameters**

A
annie_wangli 已提交
183 184 185
| Name     | Type    | Mandatory  | Description                                 |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes   | Address of the remote device to pair, for example, XX:XX:XX:XX:XX:XX.|
Z
zengyawen 已提交
186

A
annie_wangli 已提交
187
**Return value**
Z
zengyawen 已提交
188

A
annie_wangli 已提交
189 190
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
191
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
192

A
annie_wangli 已提交
193
**Example**
Z
zengyawen 已提交
194

A
annie_wangli 已提交
195 196 197
```js
// The address can be scanned.
let result = bluetooth.pairDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
198
```
A
annie_wangli 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212


## bluetooth.getProfileConnState<sup>8+</sup><a name="getProfileConnState"></a>

getProfileConnState(profileId: ProfileId): ProfileConnectionState

Obtains the connection status of a profile.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
213 214 215
| Name      | Type       | Mandatory  | Description                                   |
| --------- | --------- | ---- | ------------------------------------- |
| ProfileId | profileId | Yes   | ID of the target profile, for example, **PROFILE_A2DP_SOURCE**.|
A
annie_wangli 已提交
216 217 218

**Return value**

A
annie_wangli 已提交
219 220
| Type                                      | Description           |
| ---------------------------------------- | ------------- |
A
annie_wangli 已提交
221 222 223 224 225 226
| [ProfileConnectionState](#ProfileConnectionState) | Profile connection state obtained.|

**Example**

```js
let result = bluetooth.getProfileConnState(PROFILE_A2DP_SOURCE);
Z
zengyawen 已提交
227 228 229
```


A
annie_wangli 已提交
230 231 232 233 234 235 236 237 238 239 240 241
## bluetooth.cancelPairedDevice<sup>8+</sup><a name="cancelPairedDevice"></a>

cancelPairedDevice(deviceId: string): boolean

Cancels a paired remote device.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
242 243 244
| Name     | Type    | Mandatory  | Description                                   |
| -------- | ------ | ---- | ------------------------------------- |
| deviceId | string | Yes   | Address of the remote device to cancel, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
245 246 247

**Return value**

A
annie_wangli 已提交
248 249
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```js
let result = bluetooth.cancelPairedDevice("XX:XX:XX:XX:XX:XX");
```


## bluetooth.getRemoteDeviceName<sup>8+</sup><a name="getRemoteDeviceName"></a>

getRemoteDeviceName(deviceId: string): string

Obtains the name of the remote Bluetooth device.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
271 272 273
| Name     | Type    | Mandatory  | Description                               |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
274 275 276

**Return value**

A
annie_wangli 已提交
277 278
| Type    | Description           |
| ------ | ------------- |
A
annie_wangli 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| string | Device name (a string) obtained.|

**Example**

```js
let remoteDeviceName = bluetooth.getRemoteDeviceName("XX:XX:XX:XX:XX:XX");
```


## bluetooth.getRemoteDeviceClass<sup>8+</sup><a name="getRemoteDeviceClass"></a>

getRemoteDeviceClass(deviceId: string): DeviceClass

Obtains the type of the remote Bluetooth device.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
300 301 302
| Name     | Type    | Mandatory  | Description                               |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
303 304 305

**Return value**

A
annie_wangli 已提交
306 307
| Type                         | Description      |
| --------------------------- | -------- |
A
annie_wangli 已提交
308 309 310 311 312 313 314 315 316 317
| [DeviceClass](#deviceclass) | Type of a remote device obtained.|

**Example**

```js
let remoteDeviceClass = bluetooth.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX");
```


## bluetooth.getPairedDevices<sup>8+</sup><a name="getPairedDevices"></a>
Z
zengyawen 已提交
318 319 320 321 322

getPairedDevices(): Array&lt;string&gt;

Obtains the Bluetooth pairing list.

A
annie_wangli 已提交
323
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
324

A
annie_wangli 已提交
325 326 327 328
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
329 330
| Type                 | Description           |
| ------------------- | ------------- |
A
annie_wangli 已提交
331
| Array&lt;string&gt; | List of the addresses of the paired Bluetooth devices.|
Z
zengyawen 已提交
332

A
annie_wangli 已提交
333
**Example**
Z
zengyawen 已提交
334

A
annie_wangli 已提交
335
```js
Z
zengyawen 已提交
336 337 338 339
let devices = bluetooth.getPairedDevices();
```


A
annie_wangli 已提交
340
## bluetooth.setBluetoothScanMode<sup>8+</sup><a name="setBluetoothScanMode"></a>
Z
zengyawen 已提交
341 342 343

setBluetoothScanMode(mode: ScanMode, duration: number): boolean

A
annie_wangli 已提交
344 345 346
Sets the Bluetooth scan mode so that the device can be discovered by a remote device.

**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
347

A
annie_wangli 已提交
348
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
349

A
annie_wangli 已提交
350 351
**Parameters**

A
annie_wangli 已提交
352 353 354 355
| Name     | Type                   | Mandatory  | Description                          |
| -------- | --------------------- | ---- | ---------------------------- |
| mode     | [ScanMode](#scanmode) | Yes   | Bluetooth scan mode to set.                     |
| duration | number                | Yes   | Duration (in seconds) in which the device can be discovered. The value **0** indicates unlimited time.|
Z
zengyawen 已提交
356

A
annie_wangli 已提交
357
**Return value**
Z
zengyawen 已提交
358

A
annie_wangli 已提交
359 360
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
361
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
362

A
annie_wangli 已提交
363
**Example**
Z
zengyawen 已提交
364

A
annie_wangli 已提交
365
```js
Z
zengyawen 已提交
366 367 368 369 370
// The device can be discovered and connected only when the discoverable and connectable mode is used.
let result = bluetooth.setBluetoothScanMode(ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
```


A
annie_wangli 已提交
371
## bluetooth.getBluetoothScanMode<sup>8+</sup><a name="getBluetoothScanMode"></a>
Z
zengyawen 已提交
372 373 374 375 376

getBluetoothScanMode(): ScanMode

Obtains the Bluetooth scan mode.

A
annie_wangli 已提交
377
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
378

A
annie_wangli 已提交
379 380 381 382
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
383 384
| Type                   | Description     |
| --------------------- | ------- |
A
annie_wangli 已提交
385
| [ScanMode](#scanmode) | Bluetooth scan mode obtained.|
Z
zengyawen 已提交
386

A
annie_wangli 已提交
387
**Example**
Z
zengyawen 已提交
388

A
annie_wangli 已提交
389
```js
Z
zengyawen 已提交
390 391 392 393
let scanMode = bluetooth.getBluetoothScanMode();
```


A
annie_wangli 已提交
394
## bluetooth.startBluetoothDiscovery<sup>8+</sup><a name="startBluetoothDiscovery"></a>
Z
zengyawen 已提交
395 396 397

startBluetoothDiscovery(): boolean

A
annie_wangli 已提交
398
Starts Bluetooth scan to discover remote devices.
Z
zengyawen 已提交
399

A
annie_wangli 已提交
400
**Required permissions**: ohos.permission.USE_BLUETOOTH and ohos.permission.LOCATION
Z
zengyawen 已提交
401

A
annie_wangli 已提交
402 403 404 405
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
406 407
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
408
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
409

A
annie_wangli 已提交
410
**Example**
Z
zengyawen 已提交
411

A
annie_wangli 已提交
412
```js
Z
zengyawen 已提交
413 414 415 416 417 418 419 420 421
let deviceId;
function onReceiveEvent(data) {
    deviceId = data;
}
bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
let result = bluetooth.startBluetoothDiscovery();
```


A
annie_wangli 已提交
422
## bluetooth.stopBluetoothDiscovery<sup>8+</sup><a name="stopBluetoothDiscovery"></a>
Z
zengyawen 已提交
423 424 425 426 427

stopBluetoothDiscovery(): boolean

Stops Bluetooth scan.

A
annie_wangli 已提交
428
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
429

A
annie_wangli 已提交
430 431 432 433
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
434 435
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
436
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
437

A
annie_wangli 已提交
438
**Example**
Z
zengyawen 已提交
439

A
annie_wangli 已提交
440
```js
Z
zengyawen 已提交
441 442 443 444
let result = bluetooth.stopBluetoothDiscovery();
```


A
annie_wangli 已提交
445
## bluetooth.setDevicePairingConfirmation<sup>8+</sup><a name="setDevicePairingConfirmation"></a>
Z
zengyawen 已提交
446 447 448 449 450

setDevicePairingConfirmation(device: string, accept: boolean): boolean

Sets the device pairing confirmation.

A
annie_wangli 已提交
451 452 453
**Required permissions**: ohos.permission.MANAGE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
454

A
annie_wangli 已提交
455 456
**Parameters**

A
annie_wangli 已提交
457 458 459 460
| Name   | Type     | Mandatory  | Description                              |
| ------ | ------- | ---- | -------------------------------- |
| device | string  | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
| 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.       |
Z
zengyawen 已提交
461

A
annie_wangli 已提交
462
**Return value**
Z
zengyawen 已提交
463

A
annie_wangli 已提交
464 465
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
466
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
467

A
annie_wangli 已提交
468
**Example**
Z
zengyawen 已提交
469

A
annie_wangli 已提交
470 471
```js
// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device.
Z
zengyawen 已提交
472 473 474 475 476 477 478 479
function onReceivePinRequiredEvent(data) { // data is the input parameter for the pairing request.
    console.info('pin required  = '+ JSON.stringify(data));
    bluetooth.setDevicePairingConfirmation(data.deviceId, true);
}
bluetooth.on("pinRequired", onReceivePinRequiredEvent);
```


A
annie_wangli 已提交
480
## bluetooth.on('bluetoothDeviceFind')<sup>8+</sup>
Z
zengyawen 已提交
481 482 483 484 485

on(type: "bluetoothDeviceFind", callback: Callback&lt;Array&lt;string&gt;&gt;): void

Subscribes to the Bluetooth device discovery events.

A
annie_wangli 已提交
486
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
487

A
annie_wangli 已提交
488 489 490 491
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
492 493 494 495
| Name     | Type                                 | Mandatory  | Description                                    |
| -------- | ----------------------------------- | ---- | -------------------------------------- |
| type     | string                              | Yes   | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered.|
| callback | Callback&lt;Array&lt;string&gt;&gt; | Yes   | Callback invoked to return the discovered devices. You need to implement this callback.   |
Z
zengyawen 已提交
496

A
annie_wangli 已提交
497
**Return value**
Z
zengyawen 已提交
498

A
annie_wangli 已提交
499
No value is returned.
Z
zengyawen 已提交
500

A
annie_wangli 已提交
501
**Example**
Z
zengyawen 已提交
502

A
annie_wangli 已提交
503
```js
Z
zengyawen 已提交
504 505 506 507 508 509 510
function onReceiveEvent(data) { // data is a set of Bluetooth device addresses.
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
```


A
annie_wangli 已提交
511
## bluetooth.off('bluetoothDeviceFind')<sup>8+</sup>
Z
zengyawen 已提交
512 513 514 515 516

off(type: "bluetoothDeviceFind", callback?: Callback&lt;Array&lt;string&gt;&gt;): void

Unsubscribes from the Bluetooth device discovery events.

A
annie_wangli 已提交
517
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
518

A
annie_wangli 已提交
519 520 521 522
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
523 524 525 526
| Name     | Type                                 | Mandatory  | Description                                      |
| -------- | ----------------------------------- | ---- | ---------------------------------------- |
| type     | string                              | Yes   | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered.  |
| callback | Callback&lt;Array&lt;string&gt;&gt; | No   | Callback used to report the discovered devices. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
527

A
annie_wangli 已提交
528
**Return value**
Z
zengyawen 已提交
529

A
annie_wangli 已提交
530
No value is returned.
Z
zengyawen 已提交
531

A
annie_wangli 已提交
532
**Example**
Z
zengyawen 已提交
533

A
annie_wangli 已提交
534
```js
Z
zengyawen 已提交
535 536 537 538 539 540 541 542
function onReceiveEvent(data) {
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
bluetooth.off('bluetoothDeviceFind', onReceiveEvent);
```


A
annie_wangli 已提交
543
## bluetooth.on('pinRequired')<sup>8+</sup>
Z
zengyawen 已提交
544 545 546

on(type: "pinRequired", callback: Callback&lt;PinRequiredParam&gt;): void

A
annie_wangli 已提交
547
Subscribes to the pairing request events of the remote Bluetooth device.
Z
zengyawen 已提交
548

A
annie_wangli 已提交
549
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
550

A
annie_wangli 已提交
551 552 553 554
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
555 556 557 558
| Name     | Type                                      | Mandatory  | Description                              |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| type     | string                                   | Yes   | Event type. The value **pinRequired** indicates a pairing request event.    |
| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | Yes   | Callback invoked to return the pairing request. You need to implement this callback.|
Z
zengyawen 已提交
559

A
annie_wangli 已提交
560
**Return value**
Z
zengyawen 已提交
561

A
annie_wangli 已提交
562
No value is returned.
Z
zengyawen 已提交
563

A
annie_wangli 已提交
564
**Example**
Z
zengyawen 已提交
565

A
annie_wangli 已提交
566
```js
Z
zengyawen 已提交
567 568 569 570 571 572 573
function onReceiveEvent(data) { // data is the pairing request parameter.
    console.info('pin required = '+ JSON.stringify(data));
}
bluetooth.on('pinRequired', onReceiveEvent);
```


A
annie_wangli 已提交
574
## bluetooth.off('pinRequired')<sup>8+</sup>
Z
zengyawen 已提交
575 576 577

off(type: "pinRequired", callback?: Callback&lt;PinRequiredParam&gt;): void

A
annie_wangli 已提交
578
Unsubscribes from the pairing request events of the remote Bluetooth device.
Z
zengyawen 已提交
579

A
annie_wangli 已提交
580
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
581

A
annie_wangli 已提交
582 583 584 585
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
586 587 588 589
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **pinRequired** indicates a pairing request event.            |
| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | No   | Callback used to report the Bluetooth pairing request. The input parameter is the pairing request parameter. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
590

A
annie_wangli 已提交
591
**Return value**
Z
zengyawen 已提交
592

A
annie_wangli 已提交
593
No value is returned.
Z
zengyawen 已提交
594

A
annie_wangli 已提交
595
**Example**
Z
zengyawen 已提交
596

A
annie_wangli 已提交
597
```js
Z
zengyawen 已提交
598 599 600 601 602 603 604 605
function onReceiveEvent(data) {
    console.info('pin required = '+ JSON.stringify(data));
}
bluetooth.on('pinRequired', onReceiveEvent);
bluetooth.off('pinRequired', onReceiveEvent);
```


A
annie_wangli 已提交
606
## bluetooth.on('bondStateChange')<sup>8+</sup>
Z
zengyawen 已提交
607

A
annie_wangli 已提交
608
on(type: "bondStateChange", callback: Callback&lt;BondStateParam&gt;): void
Z
zengyawen 已提交
609 610 611

Subscribes to the Bluetooth pairing state change events.

A
annie_wangli 已提交
612
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
613

A
annie_wangli 已提交
614 615 616 617
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
618 619 620 621
| Name     | Type                                      | Mandatory  | Description                                  |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
| type     | string                                   | Yes   | Event type. The value **bondStateChange** indicates a Bluetooth pairing state change event.|
| callback | Callback&lt;[BondStateParam](#bondstate)&gt; | Yes   | Callback invoked to return the pairing state. You need to implement this callback.   |
Z
zengyawen 已提交
622

A
annie_wangli 已提交
623
**Return value**
Z
zengyawen 已提交
624

A
annie_wangli 已提交
625
No value is returned.
Z
zengyawen 已提交
626

A
annie_wangli 已提交
627
**Example**
Z
zengyawen 已提交
628

A
annie_wangli 已提交
629
```js
Z
zengyawen 已提交
630 631 632 633 634 635 636
function onReceiveEvent(data) { // data, as the input parameter of the callback, indicates the pairing state.
    console.info('pair state = '+ JSON.stringify(data));
}
bluetooth.on('bondStateChange', onReceiveEvent);
```


A
annie_wangli 已提交
637
## bluetooth.off('bondStateChange')<sup>8+</sup>
Z
zengyawen 已提交
638

A
annie_wangli 已提交
639
off(type: "bondStateChange", callback?: Callback&lt;BondStateParam&gt;): void
Z
zengyawen 已提交
640 641 642

Unsubscribes from the Bluetooth pairing state change events.

A
annie_wangli 已提交
643
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
644

A
annie_wangli 已提交
645 646 647 648
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
649 650 651 652
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **bondStateChange** indicates a Bluetooth pairing state change event.    |
| callback | Callback&lt;[BondStateParam](#bondstate)&gt; | No   | Callback used to report the change of the Bluetooth pairing state. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
653

A
annie_wangli 已提交
654
**Return value**
Z
zengyawen 已提交
655

A
annie_wangli 已提交
656
No value is returned.
Z
zengyawen 已提交
657

A
annie_wangli 已提交
658
**Example**
Z
zengyawen 已提交
659

A
annie_wangli 已提交
660
```js
Z
zengyawen 已提交
661 662 663 664 665 666 667 668
function onReceiveEvent(data) {
    console.info('bond state = '+ JSON.stringify(data));
}
bluetooth.on('bondStateChange', onReceiveEvent);
bluetooth.off('bondStateChange', onReceiveEvent);
```


A
annie_wangli 已提交
669
## bluetooth.on('stateChange')<sup>8+</sup>
Z
zengyawen 已提交
670 671 672 673 674

on(type: "stateChange", callback: Callback&lt;BluetoothState&gt;): void

Subscribes to the Bluetooth connection state change events.

A
annie_wangli 已提交
675
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
676

A
annie_wangli 已提交
677 678 679 680
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
681 682 683 684
| Name     | Type                                      | Mandatory  | Description                              |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| type     | string                                   | Yes   | Event type. The value **stateChange** indicates a Bluetooth connection state change event.  |
| callback | Callback&lt;[BluetoothState](#bluetoothstate)&gt; | Yes   | Callback invoked to return the Bluetooth connection state. You need to implement this callback.|
Z
zengyawen 已提交
685

A
annie_wangli 已提交
686
**Return value**
Z
zengyawen 已提交
687

A
annie_wangli 已提交
688
No value is returned.
Z
zengyawen 已提交
689

A
annie_wangli 已提交
690
**Example**
Z
zengyawen 已提交
691

A
annie_wangli 已提交
692
```js
Z
zengyawen 已提交
693 694 695 696 697 698 699
function onReceiveEvent(data) {
    console.info('bluetooth state = '+ JSON.stringify(data));
}
bluetooth.on('stateChange', onReceiveEvent);
```


A
annie_wangli 已提交
700
## bluetooth.off('stateChange')<sup>8+</sup>
Z
zengyawen 已提交
701 702 703 704 705

off(type: "stateChange", callback?: Callback&lt;BluetoothState&gt;): void

Unsubscribes from the Bluetooth connection state change events.

A
annie_wangli 已提交
706
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
707

A
annie_wangli 已提交
708 709 710 711
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
712 713 714 715
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **stateChange** indicates a Bluetooth connection state change event.          |
| callback | Callback&lt;[BluetoothState](#bluetoothstate)&gt; | No   | Callback used to report the Bluetooth connection state. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
716

A
annie_wangli 已提交
717
**Return value**
Z
zengyawen 已提交
718

A
annie_wangli 已提交
719
No value is returned.
Z
zengyawen 已提交
720

A
annie_wangli 已提交
721
**Example**
Z
zengyawen 已提交
722

A
annie_wangli 已提交
723
```js
Z
zengyawen 已提交
724 725 726 727 728 729 730 731
function onReceiveEvent(data) {
    console.info('bluetooth state = '+ JSON.stringify(data));
}
bluetooth.on('stateChange', onReceiveEvent);
bluetooth.off('stateChange', onReceiveEvent);
```


A
annie_wangli 已提交
732
## bluetooth.sppListen<sup>8+</sup><a name="sppListen<"></a>
Z
zengyawen 已提交
733 734 735 736 737

sppListen(name: string, option: SppOption, callback: AsyncCallback&lt;number&gt;): void

Creates a server listening socket.

A
annie_wangli 已提交
738
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
739

A
annie_wangli 已提交
740 741 742 743
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
744 745 746 747 748
| Name     | Type                         | Mandatory  | Description                     |
| -------- | --------------------------- | ---- | ----------------------- |
| name     | string                      | Yes   | Name of the service.                 |
| option   | [SppOption](#sppoption)     | Yes   | Serial port profile (SPP) listening configuration.             |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the server socket ID.|
Z
zengyawen 已提交
749

A
annie_wangli 已提交
750
**Example**
Z
zengyawen 已提交
751

A
annie_wangli 已提交
752
```js
Z
zengyawen 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766
let serverNumber = -1;
function serverSocket(code, number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}

let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
bluetooth.sppListen('server1', sppOption, serverSocket);
```


A
annie_wangli 已提交
767
## bluetooth.sppAccept<sup>8+</sup><a name="sppAccept"></a>
Z
zengyawen 已提交
768 769 770 771 772

sppAccept(serverSocket: number, callback: AsyncCallback&lt;number&gt;): void

Listens for a connection to be made to this socket from the client and accepts it.

A
annie_wangli 已提交
773
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
774

A
annie_wangli 已提交
775 776
**Parameters**

A
annie_wangli 已提交
777 778 779 780
| Name         | Type                         | Mandatory  | Description                     |
| ------------ | --------------------------- | ---- | ----------------------- |
| serverSocket | number                      | Yes   | Server socket ID.          |
| callback     | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the client socket ID.|
Z
zengyawen 已提交
781

A
annie_wangli 已提交
782
**Example**
Z
zengyawen 已提交
783

A
annie_wangli 已提交
784
```js
Z
zengyawen 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797
let clientNumber = -1;
function acceptClientSocket(code, number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth clientSocket Number: ' + number);
    // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the server.
    clientNumber = number;
  }
}
bluetooth.sppAccept(serverNumber, acceptClientSocket);
```


A
annie_wangli 已提交
798
## bluetooth.sppConnect<sup>8+</sup><a name="sppConnect"></a>
Z
zengyawen 已提交
799 800 801

sppConnect(device: string, option: SppOption, callback: AsyncCallback&lt;number&gt;): void

A
annie_wangli 已提交
802 803 804
Initiates an SPP connection to a remote device from the client.

**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
805

A
annie_wangli 已提交
806
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
807

A
annie_wangli 已提交
808 809
**Parameters**

A
annie_wangli 已提交
810 811 812 813 814
| Name     | Type                         | Mandatory  | Description                            |
| -------- | --------------------------- | ---- | ------------------------------ |
| device   | string                      | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
| option   | [SppOption](#sppoption)     | Yes   | Configuration for connecting to the SPP client.                 |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the client socket ID.       |
Z
zengyawen 已提交
815

A
annie_wangli 已提交
816
**Example**
Z
zengyawen 已提交
817

A
annie_wangli 已提交
818
```js
Z
zengyawen 已提交
819 820 821 822 823 824 825 826 827 828
let clientNumber = -1;
function clientSocket(code, number) {
  if (code.code != 0) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // The obtained clientNumber is used as the socket ID for subsequent read/write operations on the client.
  clientNumber = number;
}
let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
A
annie_wangli 已提交
829
bluetooth.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
Z
zengyawen 已提交
830 831 832
```


A
annie_wangli 已提交
833
## bluetooth.sppCloseServerSocket<sup>8+</sup><a name="sppCloseServerSocket"></a>
Z
zengyawen 已提交
834 835 836 837 838

sppCloseServerSocket(socket: number): void

Closes the listening socket of the server.

A
annie_wangli 已提交
839 840 841
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
842

A
annie_wangli 已提交
843 844 845
| Name   | Type    | Mandatory  | Description             |
| ------ | ------ | ---- | --------------- |
| socket | number | Yes   | ID of the listening socket on the server. The ID is obtained by **sppListen**.|
Z
zengyawen 已提交
846

A
annie_wangli 已提交
847
**Example**
Z
zengyawen 已提交
848

A
annie_wangli 已提交
849
```js
Z
zengyawen 已提交
850 851 852 853
bluetooth.sppCloseServerSocket(serverNumber);
```


A
annie_wangli 已提交
854
## bluetooth.sppCloseClientSocket<sup>8+</sup><a name="sppCloseClientSocket"></a>
Z
zengyawen 已提交
855 856 857 858 859

sppCloseClientSocket(socket: number): void

Closes the client socket.

A
annie_wangli 已提交
860 861 862
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
863

A
annie_wangli 已提交
864 865 866 867
| Name   | Type    | Mandatory  | Description           |
| ------ | ------ | ---- | ------------- |
| Name   | Type    | Mandatory  | Description           |
| socket | number | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.|
Z
zengyawen 已提交
868

A
annie_wangli 已提交
869
**Example**
Z
zengyawen 已提交
870

A
annie_wangli 已提交
871
```js
Z
zengyawen 已提交
872 873 874 875
bluetooth.sppCloseClientSocket(clientNumber);
```


A
annie_wangli 已提交
876
## bluetooth.sppWrite<sup>8+</sup><a name="sppWrite"></a>
Z
zengyawen 已提交
877 878 879

sppWrite(clientSocket: number, data: ArrayBuffer): boolean

A
annie_wangli 已提交
880
Writes data to the remote device through the socket.
Z
zengyawen 已提交
881

A
annie_wangli 已提交
882
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
883

A
annie_wangli 已提交
884 885
**Parameters**

A
annie_wangli 已提交
886 887 888 889
| Name         | Type         | Mandatory  | Description           |
| ------------ | ----------- | ---- | ------------- |
| clientSocket | number      | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.|
| data         | ArrayBuffer | Yes   | Data to write.       |
Z
zengyawen 已提交
890

A
annie_wangli 已提交
891
**Return value**
Z
zengyawen 已提交
892

A
annie_wangli 已提交
893 894 895
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
896

A
annie_wangli 已提交
897
**Example**
Z
zengyawen 已提交
898

A
annie_wangli 已提交
899
```js
Z
zengyawen 已提交
900 901 902 903 904 905 906 907 908 909 910 911
let arrayBuffer = new ArrayBuffer(8);
let data = new Uint8Array(arrayBuffer);
data[0] = 123;
let ret = bluetooth.sppWrite(clientNumber, arrayBuffer);
if (ret) {
  console.log('spp write successfully');
} else {
  console.log('spp write failed');
}
```


A
annie_wangli 已提交
912
## bluetooth.on('sppRead')<sup>8+</sup>
Z
zengyawen 已提交
913 914 915 916 917

on(type: "sppRead", clientSocket: number, callback: Callback&lt;ArrayBuffer&gt;): void

Subscribes to the SPP read request events.

A
annie_wangli 已提交
918 919 920
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
921

A
annie_wangli 已提交
922 923 924 925 926
| Name         | Type                         | Mandatory  | Description                        |
| ------------ | --------------------------- | ---- | -------------------------- |
| type         | string                      | Yes   | Event type. The value **sppRead** indicates an SPP read request event.|
| clientSocket | number                      | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.             |
| callback     | Callback&lt;ArrayBuffer&gt; | Yes   | Callback invoked to return the data read.         |
Z
zengyawen 已提交
927

A
annie_wangli 已提交
928
**Return value**
Z
zengyawen 已提交
929

A
annie_wangli 已提交
930
No value is returned.
Z
zengyawen 已提交
931

A
annie_wangli 已提交
932
**Example**
Z
zengyawen 已提交
933

A
annie_wangli 已提交
934
```js
Z
zengyawen 已提交
935 936 937 938 939 940 941 942
function dataRead(dataBuffer) {
  let data = new Uint8Array(dataBuffer);
  console.log('bluetooth data is: ' + data[0]);
}
bluetooth.on('sppRead', clientNumber, dataRead);
```


A
annie_wangli 已提交
943
## bluetooth.off('sppRead')<sup>8+</sup>
Z
zengyawen 已提交
944 945 946 947 948

off(type: "sppRead", clientSocket: number, callback?: Callback&lt;ArrayBuffer&gt;): void

Unsubscribes from the SPP read request events.

A
annie_wangli 已提交
949
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
950

A
annie_wangli 已提交
951 952
**Parameters**

A
annie_wangli 已提交
953 954 955 956 957
| Name         | Type                         | Mandatory  | Description                                      |
| ------------ | --------------------------- | ---- | ---------------------------------------- |
| type         | string                      | Yes   | Event type. The value **sppRead** indicates an SPP read request event.              |
| clientSocket | number                      | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.                           |
| callback     | Callback&lt;ArrayBuffer&gt; | No   | Callback used to report an SPP read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
958

A
annie_wangli 已提交
959
**Return value**
Z
zengyawen 已提交
960

A
annie_wangli 已提交
961
No value is returned.
Z
zengyawen 已提交
962

A
annie_wangli 已提交
963
**Example**
Z
zengyawen 已提交
964

A
annie_wangli 已提交
965
```js
Z
zengyawen 已提交
966 967 968 969
bluetooth.off('sppRead', clientNumber);
```


A
annie_wangli 已提交
970 971 972 973 974 975 976 977 978 979
## bluetooth.getProfile<sup>8+</sup><a name="getProfile"></a>

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

Obtains a profile object.

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
980 981
| Name      | Type       | Mandatory  | Description                                   |
| --------- | --------- | ---- | ------------------------------------- |
A
annie_wangli 已提交
982
| ProfileId | profileId | Yes   | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.|
A
annie_wangli 已提交
983 984 985

**Return value**

A
annie_wangli 已提交
986 987 988
| Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- |
| A2dpSourceProfile or HandsFreeAudioGatewayProfile| Profile object obtained. Only **A2dpSourceProfile** and **HandsFreeAudioGatewayProfile** are supported.|
A
annie_wangli 已提交
989 990 991 992

**Example**

```js
A
annie_wangli 已提交
993
let a2dpSrc = bluetooth.getProfile(PROFILE_A2DP_SOURCE);
A
annie_wangli 已提交
994 995 996
```


Z
zengyawen 已提交
997 998 999 1000 1001 1002 1003 1004
## bluetooth.BLE

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

Creates a **GattServer** instance.

A
annie_wangli 已提交
1005
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1006

A
annie_wangli 已提交
1007 1008
**Return value**

A
annie_wangli 已提交
1009 1010
| Type                       | Description                                  |
| ------------------------- | ------------------------------------ |
A
annie_wangli 已提交
1011
| [GattServer](#gattserver) | **GattServer** instance created. Before using a method of the server, you must create a **GattSever** instance.|
Z
zengyawen 已提交
1012

A
annie_wangli 已提交
1013
**Example**
Z
zengyawen 已提交
1014

A
annie_wangli 已提交
1015
```js
Z
zengyawen 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
let gattServer = bluetooth.BLE.createGattServer();
```


### bluetooth.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

Creates a **GattClientDevice** instance.

A
annie_wangli 已提交
1026 1027 1028 1029
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1030 1031 1032
| Name     | Type    | Mandatory  | Description                                  |
| -------- | ------ | ---- | ------------------------------------ |
| deviceId | string | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
1033 1034 1035

**Return value**

A
annie_wangli 已提交
1036 1037
| Type                                   | Description                                  |
| ------------------------------------- | ------------------------------------ |
A
annie_wangli 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
| [GattClientDevice](#gattclientdevice) | **GattClientDevice** instance created. Before using a method of the client, you must create a **GattClientDevice** instance.|

**Example**

```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
```


### bluetooth.BLE.getConnectedBLEDevices

getConnectedBLEDevices(): Array&lt;string&gt;

Obtains the BLE devices connected to this device.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**
Z
zengyawen 已提交
1058

A
annie_wangli 已提交
1059 1060
| Type                 | Description                 |
| ------------------- | ------------------- |
A
annie_wangli 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
| Array&lt;string&gt; | Addresses of the BLE devices connected to this device.|

**Example**

```js
let result = bluetooth.BLE.getConnectedBLEDevices();
```


### bluetooth.BLE.startBLEScan

startBLEScan(filters: Array&lt;ScanFilter&gt;, options?: ScanOptions): void

Starts a BLE scan.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH, ohos.permission.MANAGE_BLUETOOTH, and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1082 1083 1084 1085
| Name    | Type                                    | Mandatory  | Description                                 |
| ------- | -------------------------------------- | ---- | ----------------------------------- |
| filters | Array&lt;[ScanFilter](#scanfilter)&gt; | Yes   | Criteria for filtering the scan result. Set this parameter to **null** if you do not want to filter the scan result.|
| options | [ScanOptions](#scanoptions)            | No   | Scan options.                    |
A
annie_wangli 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('BLE scan device find result = '+ JSON.stringify(data));
}
bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent);
bluetooth.BLE.startBLEScan(
    [{
        deviceId:"XX:XX:XX:XX:XX:XX",
        name:"test",
        serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"
    }],
    {
        interval: 500,
        dutyMode: bluetooth.ScanDuty.SCAN_MODE_LOW_POWER,
        matchMode: bluetooth.MatchMode.MATCH_MODE_AGGRESSIVE,
    }
);
```


### bluetooth.BLE.stopBLEScan

stopBLEScan(): void

Stops the BLE scan.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

No value is returned.

**Example**

```js
bluetooth.BLE.stopBLEScan();
```


### bluetooth.BLE.on('BLEDeviceFind')

on(type: "BLEDeviceFind", callback: Callback&lt;Array&lt;ScanResult&gt;&gt;): void

Subscribe to the BLE device discovery events.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1146 1147 1148 1149
| Name     | Type                                      | Mandatory  | Description                                 |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered.  |
| callback | Callback&lt;Array&lt;[ScanResult](#scanresult)&gt;&gt; | Yes   | Callback invoked to return the discovered devices. You need to implement this callback.|
A
annie_wangli 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
bluetooth.BLE.on('BLEDeviceFind', onReceiveEvent);
```


### bluetooth.BLE.off('BLEDeviceFind')

off(type: "BLEDeviceFind", callback?: Callback&lt;Array&lt;ScanResult&gt;&gt;): void

Unsubscribes from the BLE device discovery events.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1177 1178 1179 1180
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered.       |
| callback | Callback&lt;Array&lt;[ScanResult](#scanresult)&gt;&gt; | No   | Callback used to report the discovered devices. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
A
annie_wangli 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
bluetooth.BLE.on('BLEDeviceFind', onReceiveEvent);
bluetooth.BLE.off('BLEDeviceFind', onReceiveEvent);
```


## BaseProfile

Provides the profile base class.


### getConnectionDevices<sup>8+</sup><a name="getConnectionDevices"></a>

getConnectionDevices(): Array&lt;string&gt;

Obtains the connected devices.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

No value is returned.

**Return value**

A
annie_wangli 已提交
1218 1219 1220
|                     |               |
| ------------------- | ------------- |
| Type                  | Description            |
A
annie_wangli 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
| Array&lt;string&gt; | List of addresses of the connected devices. |


### getDeviceState<sup>8+</sup><a name="getDeviceState"></a>

getDeviceState(device: string): ProfileConnectionState

Obtains the connection status of the profile.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1236 1237 1238
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
| device | string | Yes   | Address of the remote device.|
A
annie_wangli 已提交
1239
|
Z
zengyawen 已提交
1240

A
annie_wangli 已提交
1241
**Return value**
Z
zengyawen 已提交
1242

A
annie_wangli 已提交
1243 1244
|                                          |                 |
| ---------------------------------------- | --------------- |
Z
zengyawen 已提交
1245
| Type | Description |
A
annie_wangli 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
| [ProfileConnectionState](#profileconnectionState) | Profile connection state obtained. |


## A2dpSourceProfile

Before using a method of **A2dpSourceProfile**, you need to create an instance of this class by using the **getProfile()** method.


### connect<sup>8+</sup><a name="connect"></a>

connect(device: string): boolean

Sets up an Advanced Audio Distribution Profile (A2DP) connection.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1266 1267
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1268
| device | string | Yes   | Address of the remote device to connect.|
A
annie_wangli 已提交
1269 1270 1271 1272
|

**Return value**

A
annie_wangli 已提交
1273 1274
|         |                     |
| ------- | ------------------- |
A
annie_wangli 已提交
1275 1276 1277 1278 1279 1280
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |

**Example**

```js
A
annie_wangli 已提交
1281 1282
let a2dpSrc = bluetooth.getProfile(PROFILE_A2DP_SOURCE)
let ret = a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
```


### disconnect<sup>8+</sup><a name="disconnect"></a>

disconnect(device: string): boolean

Disconnects an A2DP connection.

**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1298 1299
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1300
| device | string | Yes   | Address of the remote device to disconnect.|
A
annie_wangli 已提交
1301 1302 1303 1304
|

**Return value**

A
annie_wangli 已提交
1305 1306
|         |                     |
| ------- | ------------------- |
A
annie_wangli 已提交
1307 1308 1309 1310 1311 1312
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |

**Example**

```js
A
annie_wangli 已提交
1313 1314
let a2dpSrc = bluetooth.getProfile(PROFILE_A2DP_SOURCE);
let boolean ret = a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
```


### A2dpSourceProfile.on('connectionStateChange')<sup>8+</sup>

on(type: "connectionStateChange", callback: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void

Subscribes to the A2DP connection status change events.

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1328 1329 1330 1331
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback invoked to return the A2DP connection state change event.                              |
A
annie_wangli 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('a2dp state = '+ JSON.stringify(data));
}
A2dpSourceProfile.on('connectionStateChange', onReceiveEvent);
```


### A2dpSourceProfile.off('connectionStateChange')<sup>8+</sup>

off(type: "connectionStateChange", callback: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void

Unsubscribes from the A2DP connection status change events.

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1357 1358 1359
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.|
A
annie_wangli 已提交
1360
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback used to return the A2DP connection state change event.                              |
A
annie_wangli 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('a2dp state = '+ JSON.stringify(data));
}
A2dpSourceProfile.off('connectionStateChange', onReceiveEvent);
```


### getPlayingState

getPlayingState(device: string): PlayingState

Obtains the playing status of a device.

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1386 1387
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1388
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1389 1390 1391

**Return value**

A
annie_wangli 已提交
1392 1393
|                               |            |
| ----------------------------- | ---------- |
A
annie_wangli 已提交
1394 1395
| Type | Description |
| [PlayingState](#PlayingState) | Playing status obtained. |
Z
zengyawen 已提交
1396

A
annie_wangli 已提交
1397
**Example**
Z
zengyawen 已提交
1398

A
annie_wangli 已提交
1399
```js
A
annie_wangli 已提交
1400 1401
let a2dpSrc = bluetooth.getProfile(PROFILE_A2DP_SOURCE);
let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1402 1403 1404
```


A
annie_wangli 已提交
1405
## HandsFreeAudioGatewayProfile
Z
zengyawen 已提交
1406

A
annie_wangli 已提交
1407
Before using a method of **HandsFreeAudioGatewayProfile**, you need to create an instance of this class by using the **getProfile()** method.
Z
zengyawen 已提交
1408 1409


A
annie_wangli 已提交
1410
### connect<sup>8+</sup><a name="connect"></a>
Z
zengyawen 已提交
1411

A
annie_wangli 已提交
1412
connect(device: string): boolean
Z
zengyawen 已提交
1413

A
annie_wangli 已提交
1414
Sets up a Hands-free Profile (HFP) connection of a device.
Z
zengyawen 已提交
1415

A
annie_wangli 已提交
1416
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1417

A
annie_wangli 已提交
1418
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1419

A
annie_wangli 已提交
1420
**Parameters**
Z
zengyawen 已提交
1421

A
annie_wangli 已提交
1422 1423
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1424
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1425
|
Z
zengyawen 已提交
1426

A
annie_wangli 已提交
1427
**Return value**
Z
zengyawen 已提交
1428

A
annie_wangli 已提交
1429 1430
|         |                     |
| ------- | ------------------- |
A
annie_wangli 已提交
1431 1432
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1433

A
annie_wangli 已提交
1434
**Example**
Z
zengyawen 已提交
1435

A
annie_wangli 已提交
1436
```js
A
annie_wangli 已提交
1437 1438
let hfpAg = bluetooth.getProfile(PROFILE_HANDS_FREE_AUDIO_GATEWAY);
let ret = hfpAg.connect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1439
```
Z
zengyawen 已提交
1440 1441


A
annie_wangli 已提交
1442
### disconnect<sup>8+</sup><a name="disconnect"></a>
Z
zengyawen 已提交
1443

A
annie_wangli 已提交
1444
disconnect(device: string): boolean
Z
zengyawen 已提交
1445

A
annie_wangli 已提交
1446
Disconnects the HFP connection of a device.
Z
zengyawen 已提交
1447

A
annie_wangli 已提交
1448
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1449

A
annie_wangli 已提交
1450
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1451

A
annie_wangli 已提交
1452 1453
**Parameters**

A
annie_wangli 已提交
1454 1455
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1456
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1457
|
Z
zengyawen 已提交
1458

A
annie_wangli 已提交
1459
**Return value**
Z
zengyawen 已提交
1460

A
annie_wangli 已提交
1461 1462 1463
| Type     | Description                 |
| ------- | ------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
1464

A
annie_wangli 已提交
1465
**Example**
Z
zengyawen 已提交
1466

A
annie_wangli 已提交
1467
```js
A
annie_wangli 已提交
1468 1469
let hfpAg = bluetooth.getProfile(PROFILE_HANDS_FREE_AUDIO_GATEWAY);
let ret = hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1470 1471 1472
```


A
annie_wangli 已提交
1473
### HandsFreeAudioGatewayProfile.on('connectionStateChange')<sup>8+</sup>
Z
zengyawen 已提交
1474

A
annie_wangli 已提交
1475
on(type: "connectionStateChange", callback: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
Z
zengyawen 已提交
1476

A
annie_wangli 已提交
1477 1478 1479
Subscribes to the HFP connection status change events.

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1480

A
annie_wangli 已提交
1481
**Parameters**
Z
zengyawen 已提交
1482

A
annie_wangli 已提交
1483 1484
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
A
annie_wangli 已提交
1485 1486
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an HFP connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback invoked to return the HFP connection state change event.                              |
Z
zengyawen 已提交
1487

A
annie_wangli 已提交
1488
**Return value**
Z
zengyawen 已提交
1489

A
annie_wangli 已提交
1490
No value is returned.
Z
zengyawen 已提交
1491

A
annie_wangli 已提交
1492
**Example**
Z
zengyawen 已提交
1493

A
annie_wangli 已提交
1494
```js
Z
zengyawen 已提交
1495
function onReceiveEvent(data) {
A
annie_wangli 已提交
1496
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1497
}
A
annie_wangli 已提交
1498
HandsFreeAudioGatewayProfile.on('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1499 1500 1501
```


A
annie_wangli 已提交
1502
### HandsFreeAudioGatewayProfile.off('connectionStateChange')<sup>8+</sup>
Z
zengyawen 已提交
1503

A
annie_wangli 已提交
1504
off(type: "connectionStateChange", callback: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
Z
zengyawen 已提交
1505

A
annie_wangli 已提交
1506
Unsubscribes from the HFP connection status change events.
Z
zengyawen 已提交
1507

A
annie_wangli 已提交
1508
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1509

A
annie_wangli 已提交
1510 1511
**Parameters**

A
annie_wangli 已提交
1512 1513
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
A
annie_wangli 已提交
1514 1515
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an HFP connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback used to return the HFP connection state change event.                              |
Z
zengyawen 已提交
1516

A
annie_wangli 已提交
1517
**Return value**
Z
zengyawen 已提交
1518

A
annie_wangli 已提交
1519
No value is returned.
Z
zengyawen 已提交
1520

A
annie_wangli 已提交
1521
**Example**
Z
zengyawen 已提交
1522

A
annie_wangli 已提交
1523
```js
Z
zengyawen 已提交
1524
function onReceiveEvent(data) {
A
annie_wangli 已提交
1525
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1526
}
A
annie_wangli 已提交
1527
HandsFreeAudioGatewayProfile.off('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
```


## GattServer

Implements the Generic Attribute Profile (GATT) server. Before using a method of this class, you need to create a **GattServer** instance using the **createGattServer()** method.


### startAdvertising

startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void

Starts BLE advertising.

A
annie_wangli 已提交
1542 1543 1544
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1545

A
annie_wangli 已提交
1546 1547
**Parameters**

A
annie_wangli 已提交
1548 1549 1550 1551 1552
| Name        | Type                                   | Mandatory  | Description            |
| ----------- | ------------------------------------- | ---- | -------------- |
| setting     | [AdvertiseSetting](#advertisesetting) | Yes   | Settings related to BLE advertising.   |
| advData     | [AdvertiseData](#advertisedata)       | Yes   | Content of the BLE advertisement packet.     |
| advResponse | [AdvertiseData](#advertisedata)       | No   | Response to the BLE scan request.|
Z
zengyawen 已提交
1553

A
annie_wangli 已提交
1554
**Return value**
Z
zengyawen 已提交
1555

A
annie_wangli 已提交
1556
No value is returned.
Z
zengyawen 已提交
1557

A
annie_wangli 已提交
1558
**Example**
Z
zengyawen 已提交
1559

A
annie_wangli 已提交
1560
```js
Z
zengyawen 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
let manufactureValueBuffer = new Uint8Array(4);
manufactureValueBuffer[0] = 1;
manufactureValueBuffer[1] = 2;
manufactureValueBuffer[2] = 3;
manufactureValueBuffer[3] = 4;

let serviceValueBuffer = new Uint8Array(4);
serviceValueBuffer[0] = 4;
serviceValueBuffer[1] = 6;
serviceValueBuffer[2] = 7;
serviceValueBuffer[3] = 8;
console.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer));
console.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer));
let gattServer = bluetooth.BLE.createGattServer();
gattServer.startAdvertising({
            interval:150,
            txPower:60,
            connectable:true,
        },{
            serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"],
            manufactureData:[{
                 manufactureId:4567,
                 manufactureValue:manufactureValueBuffer.buffer
            }],
            serviceData:[{
                 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb",
                 serviceValue:serviceValueBuffer.buffer
            }],
        },{
            serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"],
            manufactureData:[{
                 manufactureId:1789,
                 manufactureValue:manufactureValueBuffer.buffer
            }],
            serviceData:[{
                 serviceUuid:"00001889-0000-1000-8000-00805f9b34fb",
                 serviceValue:serviceValueBuffer.buffer
            }],
});
```


### stopAdvertising

stopAdvertising(): void

Stops BLE advertising.

A
annie_wangli 已提交
1609
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1610

A
annie_wangli 已提交
1611
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1612

A
annie_wangli 已提交
1613
**Return value**
Z
zengyawen 已提交
1614

A
annie_wangli 已提交
1615 1616 1617 1618 1619
No value is returned.

**Example**

```js
Z
zengyawen 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
let server = bluetooth.BLE.createGattServer();
server.stopAdvertising();
```


### addService

addService(service: GattService): boolean

Adds a service to this GATT server.

A
annie_wangli 已提交
1631 1632 1633
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1634

A
annie_wangli 已提交
1635 1636
**Parameters**

A
annie_wangli 已提交
1637 1638 1639
| Name    | Type                         | Mandatory  | Description                      |
| ------- | --------------------------- | ---- | ------------------------ |
| service | [GattService](#gattservice) | Yes   | Service to add. Settings related to BLE advertising.|
Z
zengyawen 已提交
1640

A
annie_wangli 已提交
1641
**Return value**
Z
zengyawen 已提交
1642

A
annie_wangli 已提交
1643 1644
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
1645
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
1646

A
annie_wangli 已提交
1647
**Example**
Z
zengyawen 已提交
1648

A
annie_wangli 已提交
1649
```js
Z
zengyawen 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
// Create descriptors.
let descriptors = [];
let arrayBuffer = new ArrayBuffer(8);
let descV = new Uint8Array(arrayBuffer);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
descriptors[0] = descriptor;

// Create characteristics.
let characteristics = [];
let arrayBufferC = new ArrayBuffer(8);
let cccV = new Uint8Array(arrayBufferC);
cccV[0] = 1;
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
let characteristicN = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptorsN};
characteristics[0] = characteristic;

// Create a gattService instance.
let gattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]};

let gattServer = bluetooth.BLE.createGattServer();
let ret = gattServer.addService(gattService);
if (ret) {
   console.log("add service successfully");
} else {
   console.log("add service failed");
}
```


### removeService

removeService(serviceUuid: string): boolean

Removes a service from this GATT server.

A
annie_wangli 已提交
1690 1691 1692
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1693

A
annie_wangli 已提交
1694 1695
**Parameters**

A
annie_wangli 已提交
1696 1697 1698
| Name        | Type    | Mandatory  | Description                                      |
| ----------- | ------ | ---- | ---------------------------------------- |
| serviceUuid | string | Yes   | Universally unique identifier (UUID) of the service to remove, for example, **00001810-0000-1000-8000-00805F9B34FB**.|
Z
zengyawen 已提交
1699

A
annie_wangli 已提交
1700
**Return value**
Z
zengyawen 已提交
1701

A
annie_wangli 已提交
1702 1703
|         |                            |
| ------- | -------------------------- |
A
annie_wangli 已提交
1704 1705
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1706

A
annie_wangli 已提交
1707
**Example**
Z
zengyawen 已提交
1708

A
annie_wangli 已提交
1709
```js
Z
zengyawen 已提交
1710 1711 1712 1713 1714 1715 1716 1717 1718
let server = bluetooth.BLE.createGattServer();
server.removeService('00001810-0000-1000-8000-00805F9B34FB');
```


### close

close(): void

A
annie_wangli 已提交
1719
Closes this GATT server to unregister it from the protocol stack. After this method is called, this [GattServer](#gattserver) cannot be used.
Z
zengyawen 已提交
1720

A
annie_wangli 已提交
1721
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
1722

A
annie_wangli 已提交
1723
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1724

A
annie_wangli 已提交
1725 1726 1727
**Example**

```js
Z
zengyawen 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
let server = bluetooth.BLE.createGattServer();
server.close();
```


### notifyCharacteristicChanged

notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): boolean

Notifies the connected client device when a characteristic value changes.

A
annie_wangli 已提交
1739
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
1740

A
annie_wangli 已提交
1741 1742 1743 1744
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1745 1746 1747 1748
| Name                 | Type                                      | Mandatory  | Description                                     |
| -------------------- | ---------------------------------------- | ---- | --------------------------------------- |
| deviceId             | string                                   | Yes   | Address of the client that receives notifications, for example, XX:XX:XX:XX:XX:XX.|
| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristic) | Yes   | New characteristic value.                              |
Z
zengyawen 已提交
1749

A
annie_wangli 已提交
1750
**Return value**
Z
zengyawen 已提交
1751

A
annie_wangli 已提交
1752 1753
|         |                          |
| ------- | ------------------------ |
A
annie_wangli 已提交
1754 1755
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1756

A
annie_wangli 已提交
1757
**Example**
Z
zengyawen 已提交
1758

A
annie_wangli 已提交
1759
```js
Z
zengyawen 已提交
1760 1761 1762
let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue:  notifyCcc.characteristicValue, confirm: false};
let server = bluetooth.BLE.createGattServer();
A
annie_wangli 已提交
1763
server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic);
Z
zengyawen 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772
```


### sendResponse

sendResponse(serverResponse: ServerResponse): boolean

Sends a response to a read or write request from the GATT client.

A
annie_wangli 已提交
1773 1774 1775
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1776

A
annie_wangli 已提交
1777 1778
**Parameters**

A
annie_wangli 已提交
1779 1780 1781
| Name           | Type                               | Mandatory  | Description             |
| -------------- | --------------------------------- | ---- | --------------- |
| serverResponse | [ServerResponse](#serverresponse) | Yes   | Response returned by the GATT server.|
Z
zengyawen 已提交
1782

A
annie_wangli 已提交
1783
**Return value**
Z
zengyawen 已提交
1784

A
annie_wangli 已提交
1785 1786
|         |                            |
| ------- | -------------------------- |
A
annie_wangli 已提交
1787 1788
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1789

A
annie_wangli 已提交
1790
**Example**
Z
zengyawen 已提交
1791

A
annie_wangli 已提交
1792 1793
```js
/* send response */
Z
zengyawen 已提交
1794 1795 1796 1797
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
cccValue[0] = 1123;
let serverResponse = {
A
annie_wangli 已提交
1798
    "deviceId": "XX:XX:XX:XX:XX:XX",
Z
zengyawen 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
    "transId": 0,
    "status": 0,
    "offset": 0,
    "value": arrayBufferCCC,
};

let gattServer = bluetooth.BLE.createGattServer();
let ret = gattServer.sendResponse(serverResponse);
if (ret) {
  console.log('bluetooth sendResponse successfully');
} else {
  console.log('bluetooth sendResponse failed');
}
```


### on('characteristicRead')

on(type: "characteristicRead", callback: Callback&lt;CharacteristicReadReq&gt;): void

Subscribes to the characteristic read request events.

A
annie_wangli 已提交
1821 1822 1823 1824 1825
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
1826

A
annie_wangli 已提交
1827 1828 1829 1830
| Name     | Type                                      | Mandatory  | Description                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicRead** indicates a characteristic read request event.|
| callback | Callback&lt;[CharacteristicReadReq](#characteristicreadreq)&gt; | Yes   | Callback invoked to return a characteristic read request from the GATT client.           |
Z
zengyawen 已提交
1831

A
annie_wangli 已提交
1832
**Return value**
Z
zengyawen 已提交
1833

A
annie_wangli 已提交
1834
No value is returned.
Z
zengyawen 已提交
1835

A
annie_wangli 已提交
1836
**Example**
Z
zengyawen 已提交
1837

A
annie_wangli 已提交
1838
```js
Z
zengyawen 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
cccValue[0] = 1123;
function ReadCharacteristicReq(CharacteristicReadReq) {
  let deviceId = CharacteristicReadReq.deviceId;
  let transId = CharacteristicReadReq.transId;
  let offset = CharacteristicReadReq.offset;
  let characteristicUuid = CharacteristicReadReq.characteristicUuid;

  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
  
  let ret = gattServer.sendResponse(serverResponse);
  if (ret) {
    console.log('bluetooth sendResponse successfully');
  } else {
    console.log('bluetooth sendResponse failed');
  }
}

let gattServer = bluetooth.BLE.createGattServer();
gattServer.on("characteristicRead", ReadCharacteristicReq);
```


### off('characteristicRead')

off(type: "characteristicRead", callback?: Callback&lt;CharacteristicReadReq&gt;): void

Unsubscribes from the characteristic read request events.

A
annie_wangli 已提交
1869 1870 1871
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1872

A
annie_wangli 已提交
1873 1874
**Parameters**

A
annie_wangli 已提交
1875 1876 1877 1878
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicRead** indicates a characteristic read request event.   |
| callback | Callback&lt;[CharacteristicReadReq](#characteristicreadreq)&gt; | No   | Callback used to report a characteristic read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
1879

A
annie_wangli 已提交
1880
**Return value**
Z
zengyawen 已提交
1881

A
annie_wangli 已提交
1882
No value is returned.
Z
zengyawen 已提交
1883

A
annie_wangli 已提交
1884
**Example**
Z
zengyawen 已提交
1885

A
annie_wangli 已提交
1886
```js
Z
zengyawen 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("characteristicRead");
```


### on('characteristicWrite')

on(type: "characteristicWrite", callback: Callback&lt;CharacteristicWriteReq&gt;): void

Subscribes to the characteristic write request events.

A
annie_wangli 已提交
1898
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
1899

A
annie_wangli 已提交
1900 1901 1902 1903
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1904 1905 1906 1907
| Name     | Type                                      | Mandatory  | Description                                    |
| -------- | ---------------------------------------- | ---- | -------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicWrite** indicates a characteristic write request event.|
| callback | Callback&lt;[DescriptorWriteReq](#descriptorwritereq)&gt; | Yes   | Callback invoked to return a characteristic write request from the GATT client.            |
Z
zengyawen 已提交
1908

A
annie_wangli 已提交
1909
**Return value**
Z
zengyawen 已提交
1910

A
annie_wangli 已提交
1911
No value is returned.
Z
zengyawen 已提交
1912

A
annie_wangli 已提交
1913
**Example**
Z
zengyawen 已提交
1914

A
annie_wangli 已提交
1915
```js
Z
zengyawen 已提交
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
function WriteCharacteristicReq(CharacteristicWriteReq) {
  let deviceId = CharacteristicWriteReq.deviceId;
  let transId = CharacteristicWriteReq.transId;
  let offset = CharacteristicWriteReq.offset;
  let isPrep = CharacteristicWriteReq.isPrep;
  let needRsp = CharacteristicWriteReq.needRsp;
  let value =  new Uint8Array(CharacteristicWriteReq.value);
  let characteristicUuid = CharacteristicWriteReq.characteristicUuid;
  
  cccValue[0] = value[0];
  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
  
  let ret = gattServer.sendResponse(serverResponse);
  if (ret) {
    console.log('bluetooth sendResponse successfully');
  } else {
    console.log('bluetooth sendResponse failed');
  }
}

let gattServer = bluetooth.BLE.createGattServer();
gattServer.on("characteristicWrite", WriteCharacteristicReq);
```


### off('characteristicWrite')

off(type: "characteristicWrite", callback?: Callback&lt;CharacteristicWriteReq&gt;): void

Unsubscribes from the characteristic write request events.

A
annie_wangli 已提交
1949 1950 1951
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
1952

A
annie_wangli 已提交
1953 1954
**Parameters**

A
annie_wangli 已提交
1955 1956 1957 1958
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicWrite** indicates a characteristic write request event.  |
| callback | Callback&lt;[CharacteristicWriteReq](#characteristicwritereq)&gt; | No   | Callback used to report a characteristic write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
1959

A
annie_wangli 已提交
1960
**Return value**
Z
zengyawen 已提交
1961

A
annie_wangli 已提交
1962
No value is returned.
Z
zengyawen 已提交
1963

A
annie_wangli 已提交
1964
**Example**
Z
zengyawen 已提交
1965

A
annie_wangli 已提交
1966
```js
Z
zengyawen 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("characteristicWrite");
```


### on('descriptorRead')

on(type: "descriptorRead", callback: Callback&lt;DescriptorReadReq&gt;): void

Subscribes to the descriptor read request events.

A
annie_wangli 已提交
1978 1979 1980 1981 1982
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
1983

A
annie_wangli 已提交
1984 1985 1986
| Name     | Type                                      | Mandatory  | Description                               |
| -------- | ---------------------------------------- | ---- | --------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorRead** indicates a descriptor read request event.|
A
annie_wangli 已提交
1987
| callback | Callback&lt;[DescriptorReadReq](#descriptorreadreq)&gt; | Yes   | Callback invoked to return a descriptor read request event from the GATT client.       |
Z
zengyawen 已提交
1988

A
annie_wangli 已提交
1989
**Return value**
Z
zengyawen 已提交
1990

A
annie_wangli 已提交
1991
No value is returned.
Z
zengyawen 已提交
1992

A
annie_wangli 已提交
1993
**Example**
Z
zengyawen 已提交
1994

A
annie_wangli 已提交
1995
```js
Z
zengyawen 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
let arrayBufferDesc = new ArrayBuffer(8);
let descValue = new Uint8Array(arrayBufferDesc);
descValue[0] = 1101;
function ReadDescriptorReq(DescriptorReadReq) {
  let deviceId = DescriptorReadReq.deviceId;
  let transId = DescriptorReadReq.transId;
  let offset = DescriptorReadReq.offset;
  let descriptorUuid = DescriptorReadReq.descriptorUuid;

  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc};
  
  let ret = gattServer.sendResponse(serverResponse);
  if (ret) {
    console.log('bluetooth sendResponse successfully');
  } else {
    console.log('bluetooth sendResponse failed');
  }
}

let gattServer = bluetooth.BLE.createGattServer();
gattServer.on("descriptorRead", ReadDescriptorReq);
```


### off('descriptorRead')

off(type: "descriptorRead", callback?: Callback&lt;DescriptorReadReq&gt;): void

Unsubscribes from the descriptor read request events.

A
annie_wangli 已提交
2026 2027 2028
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2029

A
annie_wangli 已提交
2030 2031
**Parameters**

A
annie_wangli 已提交
2032 2033 2034 2035
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorRead** indicates a descriptor read request event.       |
| callback | Callback&lt;[DescriptorReadReq](#descriptorreadreq)&gt; | No   | Callback used to report a descriptor read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2036

A
annie_wangli 已提交
2037
**Return value**
Z
zengyawen 已提交
2038

A
annie_wangli 已提交
2039
No value is returned.
Z
zengyawen 已提交
2040

A
annie_wangli 已提交
2041
**Example**
Z
zengyawen 已提交
2042

A
annie_wangli 已提交
2043
```js
Z
zengyawen 已提交
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("descriptorRead");
```


### on('descriptorWrite')

on(type: "descriptorWrite", callback: Callback&lt;DescriptorWriteReq&gt;): void

Subscribes to the descriptor write request events.

A
annie_wangli 已提交
2055
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2056

A
annie_wangli 已提交
2057 2058 2059 2060
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
2061 2062 2063
| Name     | Type                                      | Mandatory  | Description                                |
| -------- | ---------------------------------------- | ---- | ---------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorWrite** indicates a descriptor write request event.|
A
annie_wangli 已提交
2064
| callback | Callback&lt;[DescriptorWriteReq](#descriptorwritereq)&gt; | Yes   | Callback invoked to return a descriptor write request from the GATT client.        |
Z
zengyawen 已提交
2065

A
annie_wangli 已提交
2066
**Return value**
Z
zengyawen 已提交
2067

A
annie_wangli 已提交
2068
No value is returned.
Z
zengyawen 已提交
2069

A
annie_wangli 已提交
2070
**Example**
Z
zengyawen 已提交
2071

A
annie_wangli 已提交
2072
```js
Z
zengyawen 已提交
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
let arrayBufferDesc = new ArrayBuffer(8);
let descValue = new Uint8Array(arrayBufferDesc);
function WriteDescriptorReq(DescriptorWriteReq) {
  let deviceId = DescriptorWriteReq.deviceId;
  let transId = DescriptorWriteReq.transId;
  let offset = DescriptorWriteReq.offset;
  let isPrep = DescriptorWriteReq.isPrep;
  let needRsp = DescriptorWriteReq.needRsp;
  let value = new Uint8Array(DescriptorWriteReq.value);
  let descriptorUuid = DescriptorWriteReq.descriptorUuid;

  descValue[0] = value[0];
  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc};
  
  let ret = gattServer.sendResponse(serverResponse);
  if (ret) {
    console.log('bluetooth sendResponse successfully');
  } else {
    console.log('bluetooth sendResponse failed');
  }
}

let gattServer = bluetooth.BLE.createGattServer();
gattServer.on("descriptorRead", WriteDescriptorReq);
```


### off('descriptorWrite')

off(type: "descriptorWrite", callback?: Callback&lt;DescriptorWriteReq&gt;): void

Unsubscribes from the descriptor write request events.

A
annie_wangli 已提交
2106 2107 2108
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2109

A
annie_wangli 已提交
2110 2111
**Parameters**

A
annie_wangli 已提交
2112 2113 2114 2115
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorWrite** indicates a descriptor write request event.      |
| callback | Callback&lt;[DescriptorWriteReq](#descriptorwritereq)&gt; | No   | Callback used to report a descriptor write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2116

A
annie_wangli 已提交
2117
**Return value**
Z
zengyawen 已提交
2118

A
annie_wangli 已提交
2119
No value is returned.
Z
zengyawen 已提交
2120

A
annie_wangli 已提交
2121
**Example**
Z
zengyawen 已提交
2122

A
annie_wangli 已提交
2123
```js
Z
zengyawen 已提交
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("descriptorWrite");
```


### on('connectStateChange')

on(type: "connectStateChange", callback: Callback&lt;BLEConnectChangedState&gt;): void

Subscribes to the BLE connection state change events.

A
annie_wangli 已提交
2135 2136 2137 2138 2139
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
2140

A
annie_wangli 已提交
2141 2142 2143 2144
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectStateChange** indicates a BLE connection state change event.|
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | Yes   | Callback invoked to return the BLE connection state.                         |
Z
zengyawen 已提交
2145

A
annie_wangli 已提交
2146
**Return value**
Z
zengyawen 已提交
2147

A
annie_wangli 已提交
2148
No value is returned.
Z
zengyawen 已提交
2149

A
annie_wangli 已提交
2150
**Example**
Z
zengyawen 已提交
2151

A
annie_wangli 已提交
2152
```js
Z
zengyawen 已提交
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
function Connected(BLEConnectChangedState) {
  let deviceId = BLEConnectChangedState.deviceId;
  let status = BLEConnectChangedState.state;
}

let gattServer = bluetooth.BLE.createGattServer();
gattServer.on("connectStateChange", Connected);
```


### off('connectStateChange')

off(type: "connectStateChange", callback?: Callback&lt;BLEConnectChangedState&gt;): void

Unsubscribes from the BLE connection state change events.

A
annie_wangli 已提交
2169 2170 2171
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2172

A
annie_wangli 已提交
2173 2174
**Parameters**

A
annie_wangli 已提交
2175 2176 2177 2178
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectStateChange** indicates a BLE connection state change event.|
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | No   | Callback used to report the BLE connection state. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2179

A
annie_wangli 已提交
2180
**Return value**
Z
zengyawen 已提交
2181

A
annie_wangli 已提交
2182
No value is returned.
Z
zengyawen 已提交
2183

A
annie_wangli 已提交
2184
**Example**
Z
zengyawen 已提交
2185

A
annie_wangli 已提交
2186
```js
Z
zengyawen 已提交
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("connectStateChange");
```


## GattClientDevice

Implements the GATT client. Before using a method of this class, you must create a **GattClientDevice** instance using the **createGattClientDevice(deviceId: string)** method.


### connect

connect(): boolean

A
annie_wangli 已提交
2201
Initiates a connection to the remote BLE device.
Z
zengyawen 已提交
2202

A
annie_wangli 已提交
2203
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2204

A
annie_wangli 已提交
2205 2206 2207 2208
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2209 2210
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
2211
| boolean | Returns **true** if the connection is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2212

A
annie_wangli 已提交
2213
**Example**
Z
zengyawen 已提交
2214

A
annie_wangli 已提交
2215 2216
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2217 2218 2219 2220 2221 2222 2223 2224
let ret = device.connect();
```


### disconnect

disconnect(): boolean

A
annie_wangli 已提交
2225
Disconnects from the remote BLE device.
Z
zengyawen 已提交
2226

A
annie_wangli 已提交
2227
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2228

A
annie_wangli 已提交
2229 2230 2231 2232
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2233 2234
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
2235
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2236

A
annie_wangli 已提交
2237
**Example**
Z
zengyawen 已提交
2238

A
annie_wangli 已提交
2239 2240
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
let ret = device.disconnect();
```


### close

close(): boolean

Closes this GATT client to unregister it from the protocol stack. After this method is called, this [GattClientDevice](#gattclientdevice) instance cannot be used.

A
annie_wangli 已提交
2251
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2252

A
annie_wangli 已提交
2253 2254 2255 2256
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2257 2258
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
2259
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2260

A
annie_wangli 已提交
2261
**Example**
Z
zengyawen 已提交
2262

A
annie_wangli 已提交
2263 2264
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
let ret = device.close();
```




### getServices

getServices(callback: AsyncCallback&lt;Array&lt;GattService&gt;&gt;): void

A
annie_wangli 已提交
2275
Obtains all services of the remote BLE device. This method uses an asynchronous callback to return the result.
Z
zengyawen 已提交
2276

A
annie_wangli 已提交
2277
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2278

A
annie_wangli 已提交
2279 2280 2281 2282
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
2283 2284 2285
| Name     | Type                                      | Mandatory  | Description                      |
| -------- | ---------------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;Array&lt;[GattService](#gattservice)&gt;&gt; | Yes   | Callback invoked to return the services obtained.|
Z
zengyawen 已提交
2286

A
annie_wangli 已提交
2287
**Return value**
Z
zengyawen 已提交
2288

A
annie_wangli 已提交
2289
No value is returned.
Z
zengyawen 已提交
2290

A
annie_wangli 已提交
2291
**Example**
Z
zengyawen 已提交
2292

A
annie_wangli 已提交
2293
```js
Z
zengyawen 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
// Callback
function getServices(code, gattServices) {
  if (code.code == 0) {
      let services = gattServices;
      console.log('bluetooth code is ' + code.code);
      console.log("bluetooth services size is ", services.length);

      for (let i = 0; i < services.length; i++) {
        console.log('bluetooth serviceUuid is ' + services[i].serviceUuid);
      }
  }
}

A
annie_wangli 已提交
2307
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316
device.connect();
device.getServices(getServices);
```


### getServices

getServices(): Promise&lt;Array&lt;GattService&gt;&gt;

A
annie_wangli 已提交
2317
Obtains all services of the remote BLE device. This method uses a promise to return the result.
Z
zengyawen 已提交
2318

A
annie_wangli 已提交
2319
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2320

A
annie_wangli 已提交
2321
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2322

A
annie_wangli 已提交
2323 2324 2325 2326
**Parameters**

**Return value**

A
annie_wangli 已提交
2327 2328
| Type                                      | Description                         |
| ---------------------------------------- | --------------------------- |
A
annie_wangli 已提交
2329
| Promise&lt;Array&lt;[GattService](#gattservice)&gt;&gt; | Promise used to return the services obtained.|
Z
zengyawen 已提交
2330

A
annie_wangli 已提交
2331
**Example**
Z
zengyawen 已提交
2332

A
annie_wangli 已提交
2333
```js
Z
zengyawen 已提交
2334
// Promise
A
annie_wangli 已提交
2335
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
device.connect();
let services = device.getServices();
console.log("bluetooth services size is ", services.length);

for (let i = 0; i < services.length; i++) {
    console.log('bluetooth serviceUuid is ' + services[i].serviceUuid);
}
```


### readCharacteristicValue

readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback&lt;BLECharacteristic&gt;): void

A
annie_wangli 已提交
2350 2351 2352
Reads the characteristic value of the specific service of the remote BLE device. This method uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2353

A
annie_wangli 已提交
2354
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2355

A
annie_wangli 已提交
2356 2357
**Parameters**

A
annie_wangli 已提交
2358 2359 2360 2361
| Name           | Type                                      | Mandatory  | Description                     |
| -------------- | ---------------------------------------- | ---- | ----------------------- |
| characteristic | [BLECharacteristic](#blecharacteristic)  | Yes   | Characteristic value to read.               |
| callback       | AsyncCallback&lt;[BLECharacteristic](#blecharacteristic)&gt; | Yes   | Callback invoked to return the characteristic value read.|
Z
zengyawen 已提交
2362

A
annie_wangli 已提交
2363
**Return value**
Z
zengyawen 已提交
2364

A
annie_wangli 已提交
2365
No value is returned.
Z
zengyawen 已提交
2366

A
annie_wangli 已提交
2367
**Example**
Z
zengyawen 已提交
2368

A
annie_wangli 已提交
2369
```js
Z
zengyawen 已提交
2370 2371 2372 2373 2374 2375 2376 2377 2378
function readCcc(code, BLECharacteristic) {
  if (code.code != 0) {
      return;
  }
  console.log('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid);
  let value = new Uint8Array(BLECharacteristic.characteristicValue);
  console.log('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]);
}

A
annie_wangli 已提交
2379
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
let descriptors = [];
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
descriptors[0] = descriptor;

let bufferCCC = new ArrayBuffer(8);
let cccV = new Uint8Array(bufferCCC);
cccV[0] = 1;
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
characteristicValue: bufferCCC, descriptors:descriptors};

device.readCharacteristicValue(characteristic, readCcc);
```


### readCharacteristicValue

readCharacteristicValue(characteristic: BLECharacteristic): Promise&lt;BLECharacteristic&gt;

A
annie_wangli 已提交
2404 2405 2406 2407 2408
Reads the characteristic value of the specific service of the remote BLE device. This method uses a promise to return the result.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2409

A
annie_wangli 已提交
2410
**Parameters**
Z
zengyawen 已提交
2411

A
annie_wangli 已提交
2412 2413 2414
| Name           | Type                                     | Mandatory  | Description      |
| -------------- | --------------------------------------- | ---- | -------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | Yes   | Characteristic value to read.|
Z
zengyawen 已提交
2415

A
annie_wangli 已提交
2416
**Return value**
Z
zengyawen 已提交
2417

A
annie_wangli 已提交
2418 2419
|                                          |                            |
| ---------------------------------------- | -------------------------- |
A
annie_wangli 已提交
2420 2421
| Type | Description |
| Promise&lt;[BLECharacteristic](#blecharacteristic)&gt; | Promise used to return the characteristic value read. |
Z
zengyawen 已提交
2422

A
annie_wangli 已提交
2423
**Example**
Z
zengyawen 已提交
2424

A
annie_wangli 已提交
2425 2426
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
let descriptors = [];
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
descriptors[0] = descriptor;

let bufferCCC = new ArrayBuffer(8);
let cccV = new Uint8Array(bufferCCC);
cccV[0] = 1;
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
characteristicValue: bufferCCC, descriptors:descriptors};

device.readCharacteristicValue(characteristic);
```


### readDescriptorValue

readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback&lt;BLEDescriptor&gt;): void

A
annie_wangli 已提交
2451 2452 2453
Reads the descriptor contained in the specific characteristic of the remote BLE device. This method uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2454

A
annie_wangli 已提交
2455
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2456

A
annie_wangli 已提交
2457 2458
**Parameters**

A
annie_wangli 已提交
2459 2460 2461 2462
| Name       | Type                                      | Mandatory  | Description                     |
| ---------- | ---------------------------------------- | ---- | ----------------------- |
| descriptor | [BLEDescriptor](#bledescriptor)          | Yes   | Descriptor to read.               |
| callback   | AsyncCallback&lt;[BLECharacteristic](#blecharacteristic)&gt; | Yes   | Callback invoked to return the descriptor read.|
Z
zengyawen 已提交
2463

A
annie_wangli 已提交
2464
**Return value**
Z
zengyawen 已提交
2465

A
annie_wangli 已提交
2466
No value is returned.
Z
zengyawen 已提交
2467

A
annie_wangli 已提交
2468
**Example**
Z
zengyawen 已提交
2469

A
annie_wangli 已提交
2470
```js
Z
zengyawen 已提交
2471 2472 2473 2474 2475 2476 2477 2478 2479
function readDesc(code, BLEDescriptor) {
  if (code.code != 0) {
      return;
  }
  console.log('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid);
  let value = new Uint8Array(BLEDescriptor.descriptorValue);
  console.log('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]);
}

A
annie_wangli 已提交
2480
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
device.readDescriptorValue(descriptor, readDesc);
```


### readDescriptorValue

readDescriptorValue(descriptor: BLEDescriptor): Promise&lt;BLEDescriptor&gt;

A
annie_wangli 已提交
2495 2496 2497 2498 2499
Reads the descriptor contained in the specific characteristic of the remote BLE device. This method uses a promise to return the result.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2500

A
annie_wangli 已提交
2501
**Parameters**
Z
zengyawen 已提交
2502

A
annie_wangli 已提交
2503 2504 2505
| Name       | Type                             | Mandatory  | Description      |
| ---------- | ------------------------------- | ---- | -------- |
| descriptor | [BLEDescriptor](#bledescriptor) | Yes   | Descriptor to read.|
Z
zengyawen 已提交
2506

A
annie_wangli 已提交
2507
**Return value**
Z
zengyawen 已提交
2508

A
annie_wangli 已提交
2509 2510
|                                          |                            |
| ---------------------------------------- | -------------------------- |
A
annie_wangli 已提交
2511 2512
| Type | Description |
| Promise&lt;[BLEDescriptor](#bledescriptor)&gt; | Promise used to return the descriptor read. |
Z
zengyawen 已提交
2513

A
annie_wangli 已提交
2514
**Example**
Z
zengyawen 已提交
2515

A
annie_wangli 已提交
2516 2517
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
device.readDescriptorValue(descriptor);
```


### writeCharacteristicValue

writeCharacteristicValue(characteristic: BLECharacteristic): boolean

A
annie_wangli 已提交
2532 2533 2534
Writes a characteristic value to the remote BLE device.

**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2535

A
annie_wangli 已提交
2536
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2537

A
annie_wangli 已提交
2538 2539
**Parameters**

A
annie_wangli 已提交
2540 2541 2542
| Name           | Type                                     | Mandatory  | Description                 |
| -------------- | --------------------------------------- | ---- | ------------------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | Yes   | Binary value and other parameters of the BLE device characteristic.|
Z
zengyawen 已提交
2543

A
annie_wangli 已提交
2544
**Return value**
Z
zengyawen 已提交
2545

A
annie_wangli 已提交
2546 2547
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2548
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2549

A
annie_wangli 已提交
2550
**Example**
Z
zengyawen 已提交
2551

A
annie_wangli 已提交
2552 2553
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
let descriptors = [];
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
descriptors[0] = descriptor;

let bufferCCC = new ArrayBuffer(8);
let cccV = new Uint8Array(bufferCCC);
cccV[0] = 1;
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  characteristicValue: bufferCCC, descriptors:descriptors};
let retWriteCcc = device.writeCharacteristicValue(characteristic);
if (retWriteCcc) {
  console.log('write characteristic successfully');
} else {
  console.log('write characteristic failed');
}
```


### writeDescriptorValue

writeDescriptorValue(descriptor: BLEDescriptor): boolean

A
annie_wangli 已提交
2582 2583 2584 2585 2586
Writes binary data to the specific descriptor of the remote BLE device.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2587

A
annie_wangli 已提交
2588
**Parameters**
Z
zengyawen 已提交
2589

A
annie_wangli 已提交
2590 2591 2592
| Name       | Type                             | Mandatory  | Description                |
| ---------- | ------------------------------- | ---- | ------------------ |
| descriptor | [BLEDescriptor](#bledescriptor) | Yes   | Binary value and other parameters of the BLE device descriptor.|
Z
zengyawen 已提交
2593

A
annie_wangli 已提交
2594
**Return value**
Z
zengyawen 已提交
2595

A
annie_wangli 已提交
2596 2597
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2598
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2599

A
annie_wangli 已提交
2600
**Example**
Z
zengyawen 已提交
2601

A
annie_wangli 已提交
2602 2603
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 22;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
let retWriteDesc = device.writeDescriptorValue(descriptor);
if (retWriteDesc) {
  console.log('bluetooth write descriptor successfully');
} else {
  console.log('bluetooth write descriptor failed');
}
```


### setBLEMtuSize

setBLEMtuSize(mtu: number): boolean

A
annie_wangli 已提交
2623 2624 2625 2626 2627
Sets the maximum transmission unit (MTU) that can be transmitted between the GATT client and its remote BLE device. This method can be used only after a connection is set up by calling [connect](#connect).

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2628

A
annie_wangli 已提交
2629
**Parameters**
Z
zengyawen 已提交
2630

A
annie_wangli 已提交
2631 2632 2633
| Name | Type    | Mandatory  | Description            |
| ---- | ------ | ---- | -------------- |
| mtu  | number | Yes   | MTU to set, which ranges from 22 to 512 bytes.|
Z
zengyawen 已提交
2634

A
annie_wangli 已提交
2635
**Return value**
Z
zengyawen 已提交
2636

A
annie_wangli 已提交
2637 2638
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
2639
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2640

A
annie_wangli 已提交
2641
**Example**
Z
zengyawen 已提交
2642

A
annie_wangli 已提交
2643 2644
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2645 2646 2647 2648 2649 2650 2651 2652
device.setBLEMtuSize(128);
```


### setNotifyCharacteristicChanged

setNotifyCharacteristicChanged(characteristic: BLECharacteristic, enable: boolean): boolean

A
annie_wangli 已提交
2653 2654 2655 2656 2657
Sets the function of notifying the GATT client when the characteristic value of the remote BLE device changes.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2658

A
annie_wangli 已提交
2659
**Parameters**
Z
zengyawen 已提交
2660

A
annie_wangli 已提交
2661 2662 2663 2664
| Name           | Type                                     | Mandatory  | Description                           |
| -------------- | --------------------------------------- | ---- | ----------------------------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | Yes   | BLE characteristic to listen for.                     |
| 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.|
Z
zengyawen 已提交
2665

A
annie_wangli 已提交
2666
**Return value**
Z
zengyawen 已提交
2667

A
annie_wangli 已提交
2668 2669
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
2670
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2671

A
annie_wangli 已提交
2672
**Example**
Z
zengyawen 已提交
2673

A
annie_wangli 已提交
2674 2675
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
device.setNotifyCharacteristicChanged(notifyCcc, false);
```


### on('BLECharacteristicChange')

on(type: "BLECharacteristicChange", callback: Callback&lt;BLECharacteristic&gt;): void

Subscribes to the BLE characteristic change events. The client can receive a notification from the server only after the **setNotifyCharacteristicChanged** method is called.

A
annie_wangli 已提交
2686 2687 2688 2689 2690
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
2691

A
annie_wangli 已提交
2692 2693 2694 2695
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLECharacteristicChange** indicates a characteristic value change event.|
| callback | Callback&lt;[BLECharacteristic](#blecharacteristic)&gt; | Yes   | Callback invoked to return the characteristic value changes.                 |
Z
zengyawen 已提交
2696

A
annie_wangli 已提交
2697
**Return value**
Z
zengyawen 已提交
2698

A
annie_wangli 已提交
2699
No value is returned.
Z
zengyawen 已提交
2700

A
annie_wangli 已提交
2701
**Example**
Z
zengyawen 已提交
2702

A
annie_wangli 已提交
2703
```js
Z
zengyawen 已提交
2704 2705 2706 2707 2708
function CharacteristicChange(CharacteristicChangeReq) {
  let serviceUuid = CharacteristicChangeReq.serviceUuid;
  let characteristicUuid = CharacteristicChangeReq.characteristicUuid;
  let value = new Uint8Array(CharacteristicChangeReq.characteristicValue);
}
A
annie_wangli 已提交
2709
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
device.on('BLECharacteristicChange', CharacteristicChange);
```


### off('BLECharacteristicChange')

off(type: "BLECharacteristicChange", callback?: Callback&lt;BLECharacteristic&gt;): void

Unsubscribes from the BLE characteristic change events.

A
annie_wangli 已提交
2720 2721 2722
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2723

A
annie_wangli 已提交
2724 2725
**Parameters**

A
annie_wangli 已提交
2726 2727 2728 2729
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLECharacteristicChange** indicates a characteristic value change event.|
| callback | Callback&lt;[BLECharacteristic](#blecharacteristic)&gt; | No   | Callback used to report the characteristic value. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2730

A
annie_wangli 已提交
2731
**Return value**
Z
zengyawen 已提交
2732

A
annie_wangli 已提交
2733
No value is returned.
Z
zengyawen 已提交
2734

A
annie_wangli 已提交
2735
**Example**
Z
zengyawen 已提交
2736

A
annie_wangli 已提交
2737 2738
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
device.off('BLECharacteristicChange');
```


### on('BLEConnectionStateChange')

on(type: "BLEConnectionStateChange", callback: Callback&lt;BLEConnectChangedState&gt;): void

Subscribes to the BLE connection state change events.

A
annie_wangli 已提交
2749
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2750

A
annie_wangli 已提交
2751 2752 2753 2754
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
2755 2756 2757 2758
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLEConnectionStateChange** indicates a BLE connection state change event.|
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | Yes   | Callback invoked to return the BLE connection state.                          |
Z
zengyawen 已提交
2759

A
annie_wangli 已提交
2760
**Return value**
Z
zengyawen 已提交
2761

A
annie_wangli 已提交
2762
No value is returned.
Z
zengyawen 已提交
2763

A
annie_wangli 已提交
2764
**Example**
Z
zengyawen 已提交
2765

A
annie_wangli 已提交
2766
```js
Z
zengyawen 已提交
2767 2768 2769 2770
function ConnectStateChanged(state) {
  console.log('bluetooth connect state changed');
  let connectState = state.state;
}
A
annie_wangli 已提交
2771
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
device.on('BLEConnectionStateChange', ConnectStateChanged);
```


### off('BLEConnectionStateChange')

off(type: "BLEConnectionStateChange", callback?: Callback&lt;BLEConnectChangedState&gt;): void

Unsubscribes from the BLE connection state change events.

A
annie_wangli 已提交
2782 2783 2784
**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2785

A
annie_wangli 已提交
2786 2787
**Parameters**

A
annie_wangli 已提交
2788 2789 2790 2791
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLEConnectionStateChange** indicates a BLE connection state change event.|
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | No   | Callback used to report the BLE connection state. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2792

A
annie_wangli 已提交
2793
**Return value**
Z
zengyawen 已提交
2794

A
annie_wangli 已提交
2795
No value is returned.
Z
zengyawen 已提交
2796

A
annie_wangli 已提交
2797
**Example**
Z
zengyawen 已提交
2798

A
annie_wangli 已提交
2799 2800
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2801 2802 2803 2804 2805 2806 2807 2808
device.off('BLEConnectionStateChange');
```


### getDeviceName

getDeviceName(callback: AsyncCallback&lt;string&gt;): void

A
annie_wangli 已提交
2809 2810 2811 2812 2813
Obtains the name of the remote BLE device. This method uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.USE_BLUETOOTH

**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2814

A
annie_wangli 已提交
2815
**Parameters**
Z
zengyawen 已提交
2816

A
annie_wangli 已提交
2817 2818 2819
| Name     | Type                         | Mandatory  | Description                             |
| -------- | --------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback&lt;string&gt; | Yes   | Callback invoked to return the remote BLE device name obtained.|
Z
zengyawen 已提交
2820

A
annie_wangli 已提交
2821
**Return value**
Z
zengyawen 已提交
2822

A
annie_wangli 已提交
2823
No value is returned.
Z
zengyawen 已提交
2824

A
annie_wangli 已提交
2825
**Example**
Z
zengyawen 已提交
2826

A
annie_wangli 已提交
2827 2828 2829
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
let deviceName = gattClient.getDeviceName((err, data)=> {
    console.info('device name err ' + JSON.stringify(err));
    console.info('device name' + JSON.stringify(data));
})
```


### getDeviceName

getDeviceName(): Promise&lt;string&gt;

A
annie_wangli 已提交
2841
Obtains the name of the remote BLE device. This method uses a promise to return the result.
Z
zengyawen 已提交
2842

A
annie_wangli 已提交
2843
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2844

A
annie_wangli 已提交
2845 2846 2847 2848
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2849 2850
| Type                   | Description                                |
| --------------------- | ---------------------------------- |
A
annie_wangli 已提交
2851
| Promise&lt;string&gt; | Promise used to return the remote BLE device name.|
Z
zengyawen 已提交
2852

A
annie_wangli 已提交
2853
**Example**
Z
zengyawen 已提交
2854

A
annie_wangli 已提交
2855 2856 2857
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
let ret = device.connect();
let deviceName = gattClient.getDeviceName().then((data) => {
    console.info('device name' + JSON.stringify(data));
})
```


### getRssiValue

getRssiValue(callback: AsyncCallback&lt;number&gt;): void

A
annie_wangli 已提交
2869 2870 2871
Obtains the received signal strength indication (RSSI) of the remote BLE device. This method uses an asynchronous callback to return the result. It can be used only after a connection is set up by calling [connect](#connect).

**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2872

A
annie_wangli 已提交
2873
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2874

A
annie_wangli 已提交
2875 2876
**Parameters**

A
annie_wangli 已提交
2877 2878 2879
| Name     | Type                         | Mandatory  | Description                            |
| -------- | --------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the RSSI, in dBm.|
Z
zengyawen 已提交
2880

A
annie_wangli 已提交
2881
**Return value**
Z
zengyawen 已提交
2882

A
annie_wangli 已提交
2883
No value is returned.
Z
zengyawen 已提交
2884

A
annie_wangli 已提交
2885
**Example**
Z
zengyawen 已提交
2886

A
annie_wangli 已提交
2887 2888 2889
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
let ret = device.connect();
let rssi = gattClient.getRssiValue((err, data)=> {
    console.info('rssi err ' + JSON.stringify(err));
    console.info('rssi value' + JSON.stringify(data));
})
```


### getRssiValue

getRssiValue(): Promise&lt;number&gt;

A
annie_wangli 已提交
2902
Obtains the RSSI of the remote BLE device. This method uses a promise to return the result. It can be used only after a connection is set up by calling [connect](#connect).
Z
zengyawen 已提交
2903

A
annie_wangli 已提交
2904
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2905

A
annie_wangli 已提交
2906 2907 2908 2909
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2910 2911
| Type                   | Description                               |
| --------------------- | --------------------------------- |
A
annie_wangli 已提交
2912
| Promise&lt;number&gt; | Promise used to return the RSSI, in dBm.|
Z
zengyawen 已提交
2913

A
annie_wangli 已提交
2914
**Example**
Z
zengyawen 已提交
2915

A
annie_wangli 已提交
2916 2917 2918
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2919 2920 2921 2922 2923
let rssi = gattClient.getRssiValue().then((data) => {
    console.info('rssi' + JSON.stringify(data));
})
```

A
annie_wangli 已提交
2924
## ScanMode<sup>8+</sup><a name="ScanMode"></a>
Z
zengyawen 已提交
2925 2926 2927

Enumerates the scan modes.

A
annie_wangli 已提交
2928
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
2929

A
annie_wangli 已提交
2930 2931 2932 2933 2934 2935 2936 2937
| Name                                      | Default Value | Description             |
| ---------------------------------------- | ---- | --------------- |
| SCAN_MODE_NONE                           | 0    | No scan mode.        |
| SCAN_MODE_CONNECTABLE                    | 1    | Connectable mode.       |
| SCAN_MODE_GENERAL_DISCOVERABLE           | 2    | General discoverable mode.   |
| SCAN_MODE_LIMITED_DISCOVERABLE           | 3    | Limited discoverable mode.   |
| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4    | General connectable and discoverable mode.|
| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5    | Limited connectable and discoverable mode.|
Z
zengyawen 已提交
2938

A
annie_wangli 已提交
2939
## BondState<sup>8+</sup><a name="BondState"></a>
Z
zengyawen 已提交
2940 2941 2942

Enumerates the pairing states.

A
annie_wangli 已提交
2943 2944
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
2945 2946 2947 2948 2949
| Name                | Default Value | Description    |
| ------------------ | ---- | ------ |
| BOND_STATE_INVALID | 0    | Invalid pairing.|
| BOND_STATE_BONDING | 1    | Pairing. |
| BOND_STATE_BONDED  | 2    | Paired.  |
Z
zengyawen 已提交
2950 2951


A
annie_wangli 已提交
2952
## SppOption<sup>8+</sup><a name="SppOption"></a>
Z
zengyawen 已提交
2953 2954 2955

Defines the SPP configuration parameters.

A
annie_wangli 已提交
2956 2957
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
2958 2959 2960 2961 2962
| Name    | Type               | Readable  | Writable  | Description         |
| ------ | ------------------- | ---- | ---- | ----------- |
| uuid   | string              | Yes   | Yes   | UUID of the SPP.|
| secure | boolean             | Yes   | Yes   | Whether it is a secure channel.   |
| type   | [SppType](#spptype) | Yes   | Yes   | Type of the SPP link.   |
Z
zengyawen 已提交
2963 2964


A
annie_wangli 已提交
2965
## SppType<sup>8+</sup><a name="SppType"></a>
Z
zengyawen 已提交
2966 2967 2968

Enumerates the SPP link types.

A
annie_wangli 已提交
2969 2970
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
2971 2972 2973
| Name        | Default Value | Description           |
| ---------- | ---- | ------------- |
| SPP_RFCOMM | 0    | Radio frequency communication (RFCOMM) link type.|
Z
zengyawen 已提交
2974 2975 2976 2977 2978 2979


## GattService

Defines the GATT service API parameters.

A
annie_wangli 已提交
2980 2981
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
2982 2983 2984 2985 2986 2987
| Name             | Type                                    | Readable  | Writable  | Description                                      |
| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| serviceUuid     | string                                   | Yes   | Yes   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
| isPrimary       | boolean                                  | Yes   | Yes   | Whether the service is a primary service. The value **true** means a primary service.               |
| characteristics | Array&lt;[BLECharacteristic](#blecharacteristic)&gt; | Yes   | Yes   | List of characteristics of the service.                            |
| includeServices | Array&lt;[GattService](#gattservice)&gt; | Yes   | Yes   | Services on which the service depends.                            |
Z
zengyawen 已提交
2988 2989 2990 2991 2992 2993


## BLECharacteristic

Defines the characteristic API parameters.

A
annie_wangli 已提交
2994 2995
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
2996 2997 2998 2999 3000 3001
| Name                 | Type                                    | Readable  | Writable  | Description                                      |
| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| serviceUuid         | string                                   | Yes   | Yes   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
| characteristicUuid  | string                                   | Yes   | Yes   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| characteristicValue | ArrayBuffer                              | Yes   | Yes   | Binary value of the characteristic.                              |
| descriptors         | Array&lt;[BLEDescriptor](#bledescriptor)&gt; | Yes   | Yes   | List of descriptors of the characteristic.                             |
Z
zengyawen 已提交
3002 3003 3004 3005 3006 3007


## BLEDescriptor

Defines the descriptor API parameters.

A
annie_wangli 已提交
3008 3009
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3010 3011 3012 3013 3014 3015
| Name                | Type       | Readable  | Writable  | Description                                      |
| ------------------ | ----------- | ---- | ---- | ---------------------------------------- |
| serviceUuid        | string      | Yes   | Yes   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
| characteristicUuid | string      | Yes   | Yes   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| descriptorUuid     | string      | Yes   | Yes   | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.|
| descriptorValue    | ArrayBuffer | Yes   | Yes   | Binary value of the descriptor.                             |
Z
zengyawen 已提交
3016 3017 3018 3019 3020 3021


## NotifyCharacteristic

Defines the parameters in the notifications sent when the server characteristic value changes.

A
annie_wangli 已提交
3022 3023
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3024 3025 3026 3027 3028 3029
| Name                 | Type       | Readable  | Writable  | Description                                      |
| ------------------- | ----------- | ---- | ---- | ---------------------------------------- |
| serviceUuid         | string      | Yes   | Yes   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
| characteristicUuid  | string      | Yes   | Yes   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| characteristicValue | ArrayBuffer | Yes   | Yes   | Binary value of the characteristic.                              |
| 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.|
Z
zengyawen 已提交
3030 3031 3032 3033 3034 3035


## CharacteristicReadReq

Defines the parameters of the **CharacteristicReadReq** event received by the server.

A
annie_wangli 已提交
3036 3037
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3038 3039 3040 3041 3042 3043 3044
| Name                | Type  | Readable  | Writable  | Description                                      |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| deviceId           | string | Yes   | No   | Address of the remote device that sends the **CharacteristicReadReq** event, for example, XX:XX:XX:XX:XX:XX.|
| transId            | number | Yes   | No   | Transmission ID of the read request. The response returned by the server must use the same transmission ID.      |
| 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.|
| characteristicUuid | string | Yes   | No   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| serviceUuid        | string | Yes   | No   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
Z
zengyawen 已提交
3045 3046 3047 3048 3049 3050


## CharacteristicWriteReq

Defines the parameters of the **CharacteristicWriteReq** event received by the server.

A
annie_wangli 已提交
3051 3052
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3053 3054 3055 3056 3057 3058 3059 3060
| Name                | Type  | Readable  | Writable  | Description                                      |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| deviceId           | string | Yes   | No   | Address of the remote device that sends the **CharacteristicWriteReq** event, for example, XX:XX:XX:XX:XX:XX.|
| transId            | number | Yes   | No   | Transmission ID of the write request. The response returned by the server must use the same transmission ID.      |
| 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.|
| descriptorUuid     | string | Yes   | No   | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.|
| characteristicUuid | string | Yes   | No   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| serviceUuid        | string | Yes   | No   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
Z
zengyawen 已提交
3061

A
annie_wangli 已提交
3062

Z
zengyawen 已提交
3063 3064 3065 3066
## DescriptorReadReq

Defines the parameters of the **DescriptorReadReq** event received by the server.

A
annie_wangli 已提交
3067 3068
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3069 3070 3071 3072 3073 3074 3075 3076
| Name                | Type  | Readable  | Writable  | Description                                      |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| deviceId           | string | Yes   | No   | Address of the remote device that sends a **DescriptorReadReq** event, for example, XX:XX:XX:XX:XX:XX.|
| transId            | number | Yes   | No   | Transmission ID of the read request. The response returned by the server must use the same transmission ID.      |
| 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.|
| descriptorUuid     | string | Yes   | No   | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.|
| characteristicUuid | string | Yes   | No   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| serviceUuid        | string | Yes   | No   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
Z
zengyawen 已提交
3077 3078 3079 3080 3081 3082


## DescriptorWriteReq

Defines the parameters of the **DescriptorWriteReq** event received by the server.

A
annie_wangli 已提交
3083 3084
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
| Name                | Type       | Readable  | Writable  | Description                                      |
| ------------------ | ----------- | ---- | ---- | ---------------------------------------- |
| deviceId           | string      | Yes   | No   | Address of the remote device that sends a **DescriptorWriteReq** event, for example, XX:XX:XX:XX:XX:XX.|
| transId            | number      | Yes   | No   | Transmission ID of the write request. The response returned by the server must use the same transmission ID.      |
| 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.|
| isPrep             | boolean     | Yes   | No   | Whether the write request is executed immediately.                            |
| needRsp            | boolean     | Yes   | No   | Whether to send a response to the GATT client.                      |
| value              | ArrayBuffer | Yes   | No   | Binary value of the descriptor to write.                          |
| descriptorUuid     | string      | Yes   | No   | UUID of the descriptor, for example, **00002902-0000-1000-8000-00805f9b34fb**.|
| characteristicUuid | string      | Yes   | No   | UUID of the characteristic, for example, **00002a11-0000-1000-8000-00805f9b34fb**.|
| serviceUuid        | string      | Yes   | No   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
Z
zengyawen 已提交
3096 3097 3098 3099 3100 3101


## ServerResponse

Defines the parameters of the server's response to the GATT client's read/write request.

A
annie_wangli 已提交
3102 3103
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3104 3105 3106 3107 3108 3109 3110
| Name      | Type       | Readable  | Writable  | Description                                    |
| -------- | ----------- | ---- | ---- | -------------------------------------- |
| deviceId | string      | Yes   | No   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.      |
| 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.       |
| status   | number      | Yes   | No   | Response state. Set this parameter to **0**, which indicates a normal response.                  |
| offset   | number      | Yes   | No   | Start read/write position. The value must be the same as the offset carried in the read/write request.|
| value    | ArrayBuffer | Yes   | No   | Binary data in the response.                         |
Z
zengyawen 已提交
3111 3112 3113 3114 3115 3116


## BLEConnectChangedState

Defines the parameters of **BLEConnectChangedState**.

A
annie_wangli 已提交
3117 3118
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3119 3120 3121 3122
| Name      | Type                                    | Readable  | Writable  | Description                              |
| -------- | ---------------------------------------- | ---- | ---- | -------------------------------- |
| deviceId | string                                   | Yes   | No   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
| state    | [ProfileConnectionState](#profileconnectionState) | Yes   | Yes   | BLE connection state.                   |
Z
zengyawen 已提交
3123 3124 3125 3126 3127 3128


## ProfileConnectionState

Enumerates the profile connection states.

A
annie_wangli 已提交
3129 3130
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3131 3132 3133 3134 3135 3136
| Name                 | Default Value | Description            |
| ------------------- | ---- | -------------- |
| STATE_DISCONNECTED  | 0    | Disconnected. |
| STATE_CONNECTING    | 1    | Connecting.|
| STATE_CONNECTED     | 2    | Connected. |
| STATE_DISCONNECTING | 3    | Disconnecting.|
Z
zengyawen 已提交
3137 3138 3139 3140 3141 3142


## ScanFilter

Defines the scan filter parameters.

A
annie_wangli 已提交
3143 3144
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3145 3146 3147 3148 3149
| Name         | Type  | Readable  | Writable  | Description                                      |
| ----------- | ------ | ---- | ---- | ---------------------------------------- |
| deviceId    | string | Yes   | Yes   | Address of the BLE device to filter, for example, XX:XX:XX:XX:XX:XX.    |
| name        | string | Yes   | Yes   | Name of the BLE device to filter.                            |
| serviceUuid | string | Yes   | Yes   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
Z
zengyawen 已提交
3150 3151 3152 3153 3154 3155


## ScanOptions

Defines the scan configuration parameters.

A
annie_wangli 已提交
3156 3157
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3158 3159 3160 3161 3162
| Name       | Type                   | Readable  | Writable  | Description                                    |
| --------- | ----------------------- | ---- | ---- | -------------------------------------- |
| interval  | number                  | Yes   | Yes   | Delay in reporting the scan result. The default value is **0**.                   |
| dutyMode  | [ScanDuty](#scanduty)   | Yes   | Yes   | Scan duty. The default value is **SCAN_MODE_LOW_POWER**.       |
| matchMode | [MatchMode](#matchmode) | Yes   | Yes   | Hardware filtering match mode. The default value is **MATCH_MODE_AGGRESSIVE**.|
Z
zengyawen 已提交
3163 3164 3165 3166 3167 3168


## ScanDuty

Enumerates the scan duty options.

A
annie_wangli 已提交
3169 3170
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3171 3172 3173 3174 3175
| Name                   | Default Value | Description          |
| --------------------- | ---- | ------------ |
| SCAN_MODE_LOW_POWER   | 0    | Low-power mode, which is the default value.|
| SCAN_MODE_BALANCED    | 1    | Balanced mode.     |
| SCAN_MODE_LOW_LATENCY | 2    | Low-latency mode.    |
Z
zengyawen 已提交
3176 3177 3178 3179 3180 3181


## MatchMode

Enumerates the hardware match modes of BLE scan filters.

A
annie_wangli 已提交
3182 3183
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3184 3185 3186 3187
| Name                   | Default Value | Description                                      |
| --------------------- | ---- | ---------------------------------------- |
| 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.|
| MATCH_MODE_STICKY     | 2    | Hardware reports the scan result with a higher threshold of signal strength and sightings.      |
Z
zengyawen 已提交
3188 3189 3190 3191 3192 3193


## ScanResult

Defines the scan result.

A
annie_wangli 已提交
3194 3195
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3196 3197 3198 3199 3200
| Name      | Type       | Readable  | Writable  | Description                                |
| -------- | ----------- | ---- | ---- | ---------------------------------- |
| deviceId | string      | Yes   | No   | Address of the scanned device, for example, XX:XX:XX:XX:XX:XX.|
| rssi     | number      | Yes   | No   | RSSI of the device.                   |
| data     | ArrayBuffer | Yes   | No   | Advertisement packets sent by the device.                   |
Z
zengyawen 已提交
3201 3202 3203 3204 3205 3206


## BluetoothState

Enumerates the Bluetooth states.

A
annie_wangli 已提交
3207 3208
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3209 3210 3211 3212 3213 3214 3215 3216 3217
| Name                   | Default Value | Description                |
| --------------------- | ---- | ------------------ |
| STATE_OFF             | 0    | Bluetooth is turned off.          |
| STATE_TURNING_ON      | 1    | Bluetooth is being turned on.         |
| STATE_ON              | 2    | Bluetooth is turned on.          |
| STATE_TURNING_OFF     | 3    | Bluetooth is being turned off.         |
| STATE_BLE_TURNING_ON  | 4    | The LE-only mode is being turned on for Bluetooth.|
| STATE_BLE_ON          | 5    | Bluetooth is in LE-only mode. |
| STATE_BLE_TURNING_OFF | 6    | The LE-only mode is being turned off for Bluetooth.|
Z
zengyawen 已提交
3218 3219 3220 3221 3222 3223


## AdvertiseSetting

Defines the BLE advertising parameters.

A
annie_wangli 已提交
3224 3225
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3226 3227 3228 3229 3230
| Name         | Type   | Readable  | Writable  | Description                                      |
| ----------- | ------- | ---- | ---- | ---------------------------------------- |
| interval    | number  | Yes   | Yes   | Interval for BLE advertising. The minimum value is **32** slots (20 ms). The maximum value is **16777215** slots. The default value is **1600** slots (1s).|
| txPower     | number  | Yes   | Yes   | Transmit power, in dBm. The value range is -127 to 1. The default value is **-7**.  |
| connectable | boolean | Yes   | Yes   | Whether the advertisement is connectable. The default value is **true**.                  |
Z
zengyawen 已提交
3231 3232 3233 3234 3235 3236


## AdvertiseData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3237 3238
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3239 3240 3241 3242 3243
| Name             | Type                                    | Readable  | Writable  | Description                         |
| --------------- | ---------------------------------------- | ---- | ---- | --------------------------- |
| serviceUuids    | Array&lt;string&gt;                      | Yes   | Yes   | List of service UUIDs to broadcast.|
| manufactureData | Array&lt;[ManufactureData](#manufacturedata)&gt; | Yes   | Yes   | List of manufacturers to broadcast.          |
| serviceData     | Array&lt;[ServiceData](#servicedata)&gt; | Yes   | Yes   | List of service data to broadcast.              |
Z
zengyawen 已提交
3244 3245 3246 3247 3248 3249


## ManufactureData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3250 3251
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3252 3253 3254 3255
| Name              | Type               | Readable  | Writable  | Description                |
| ---------------- | ------------------- | ---- | ---- | ------------------ |
| manufactureId    | Array&lt;string&gt; | Yes   | Yes   | Manufacturer ID allocated by the Bluetooth SIG.|
| manufactureValue | ArrayBuffer         | Yes   | Yes   | Manufacturer data.    |
Z
zengyawen 已提交
3256 3257 3258 3259 3260 3261


## ServiceData

Defines the service data contained in an advertisement packet.

A
annie_wangli 已提交
3262 3263
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3264 3265 3266 3267
| Name          | Type       | Readable  | Writable  | Description        |
| ------------ | ----------- | ---- | ---- | ---------- |
| serviceUuid  | string      | Yes   | Yes   | Service UUID.|
| serviceValue | ArrayBuffer | Yes   | Yes   | Service data.   |
Z
zengyawen 已提交
3268 3269


A
annie_wangli 已提交
3270
## PinRequiredParam<sup>8+</sup><a name="PinRequiredParam"></a>
Z
zengyawen 已提交
3271 3272 3273

Defines the pairing request parameters.

A
annie_wangli 已提交
3274 3275
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3276 3277 3278 3279
| Name      | Type  | Readable  | Writable  | Description         |
| -------- | ------ | ---- | ---- | ----------- |
| deviceId | string | Yes   | No   | ID of the device to pair.|
| pinCode  | string | Yes   | No   | Key for the device pairing.  |
A
annie_wangli 已提交
3280 3281 3282 3283 3284 3285 3286 3287


## StateChangeParam<sup>8+</sup><a name="StateChangeParam"></a>

Defines the profile state change parameters.

**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3288 3289 3290 3291
| Name      | Type                                    | Readable  | Writable  | Description                 |
| -------- | ---------------------------------------- | ---- | ---- | ------------------- |
| deviceId | string                                   | Yes   | No   | Address of a Bluetooth device.          |
| state    | [ProfileConnectionState](#ProfileConnectionState) | Yes   | No   | Profile connection state of the device.|
A
annie_wangli 已提交
3292 3293 3294 3295 3296 3297 3298 3299


## DeviceClass<sup>8+</sup><a name="DeviceClass"></a>

Defines the class of a Bluetooth device.

**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3300 3301 3302 3303 3304
| Name             | Type                               | Readable  | Writable  | Description              |
| --------------- | ----------------------------------- | ---- | ---- | ---------------- |
| majorClass      | [MajorClass](#majorclass)           | Yes   | No   | Major classes of Bluetooth devices.  |
| majorMinorClass | [MajorMinorClass](#majorminorclass) | Yes   | No   | Major and minor classes of Bluetooth devices.|
| classOfDevice   | number                              | Yes   | No   | Class of the device.         |
A
annie_wangli 已提交
3305 3306 3307 3308 3309 3310 3311 3312 3313



## MajorClass<sup>8+</sup><a name="MajorClass"></a>

Enumerates the major classes of Bluetooth devices.

**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
| Name                 | Default Value   | Description        |
| ------------------- | ------ | ---------- |
| MAJOR_MISC          | 0x0000 | Miscellaneous device.   |
| MAJOR_COMPUTER      | 0x0100 | Computer.  |
| MAJOR_PHONE         | 0x0200 | Mobile phone.   |
| MAJOR_NETWORKING    | 0x0300 | Network device.   |
| MAJOR_AUDIO_VIDEO   | 0x0400 | Audio or video device.|
| MAJOR_PERIPHERAL    | 0x0500 | Peripheral device.   |
| MAJOR_IMAGING       | 0x0600 | Imaging device.   |
| MAJOR_WEARABLE      | 0x0700 | Wearable device.  |
| MAJOR_TOY           | 0x0800 | Toy.   |
| MAJOR_HEALTH        | 0x0900 | Health device.   |
| MAJOR_UNCATEGORIZED | 0x1F00 | Unclassified device.  |
A
annie_wangli 已提交
3327 3328 3329 3330 3331 3332 3333 3334


## MajorMinorClass<sup>8+</sup><a name="MajorMinorClass"></a>

Enumerates the major and minor classes of Bluetooth devices.

**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
| Name                                      | Default Value   | Description             |
| ---------------------------------------- | ------ | --------------- |
| COMPUTER_UNCATEGORIZED                   | 0x0100 | Unclassified computer.    |
| COMPUTER_DESKTOP                         | 0x0104 | Desktop computer.     |
| COMPUTER_SERVER                          | 0x0108 | Server.       |
| COMPUTER_LAPTOP                          | 0x010C | Laptop.    |
| COMPUTER_HANDHELD_PC_PDA                 | 0x0110 | Hand-held computer.    |
| COMPUTER_PALM_SIZE_PC_PDA                | 0x0114 | Palmtop computer.      |
| COMPUTER_WEARABLE                        | 0x0118 | Wearable computer.    |
| COMPUTER_TABLET                          | 0x011C | Tablet.      |
| PHONE_UNCATEGORIZED                      | 0x0200 | Unclassified mobile phone.     |
| PHONE_CELLULAR                           | 0x0204 | Portable phone.     |
| PHONE_CORDLESS                           | 0x0208 | Cordless phone.      |
| PHONE_SMART                              | 0x020C | Smartphone.      |
| PHONE_MODEM_OR_GATEWAY                   | 0x0210 | Modem or gateway phone.|
| PHONE_ISDN                               | 0x0214 | ISDN phone.    |
| NETWORK_FULLY_AVAILABLE                  | 0x0300 | Device with network fully available.    |
| NETWORK_1_TO_17_UTILIZED                 | 0x0320 | Device used on network 1 to 17.  |
| NETWORK_17_TO_33_UTILIZED                | 0x0340 | Device used on network 17 to 33. |
| NETWORK_33_TO_50_UTILIZED                | 0x0360 | Device used on network 33 to 50. |
| NETWORK_60_TO_67_UTILIZED                | 0x0380 | Device used on network 60 to 67. |
| NETWORK_67_TO_83_UTILIZED                | 0x03A0 | Device used on network 67 to 83. |
| NETWORK_83_TO_99_UTILIZED                | 0x03C0 | Device used on network 83 to 99. |
| NETWORK_NO_SERVICE                       | 0x03E0 | Device without network service     |
| AUDIO_VIDEO_UNCATEGORIZED                | 0x0400 | Unclassified audio or video device.   |
| AUDIO_VIDEO_WEARABLE_HEADSET             | 0x0404 | Wearable audio or video headset.  |
| AUDIO_VIDEO_HANDSFREE                    | 0x0408 | Hands-free audio or video device.    |
| AUDIO_VIDEO_MICROPHONE                   | 0x0410 | Audio or video microphone.   |
| AUDIO_VIDEO_LOUDSPEAKER                  | 0x0414 | Audio or video loudspeaker.   |
| AUDIO_VIDEO_HEADPHONES                   | 0x0418 | Audio or video headphones.   |
| AUDIO_VIDEO_PORTABLE_AUDIO               | 0x041C | Portable audio or video device.   |
| AUDIO_VIDEO_CAR_AUDIO                    | 0x0420 | In-vehicle audio or video device.    |
| AUDIO_VIDEO_SET_TOP_BOX                  | 0x0424 | Audio or video STB device.   |
| AUDIO_VIDEO_HIFI_AUDIO                   | 0x0428 | High-fidelity speaker device.     |
| AUDIO_VIDEO_VCR                          | 0x042C | Video cassette recording (VCR) device.   |
| AUDIO_VIDEO_VIDEO_CAMERA                 | 0x0430 | Camera.   |
| AUDIO_VIDEO_CAMCORDER                    | 0x0434 | Camcorder   |
| AUDIO_VIDEO_VIDEO_MONITOR                | 0x0438 | Audio or video monitor.   |
| AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER | 0x043C | Video display or loudspeaker. |
| AUDIO_VIDEO_VIDEO_CONFERENCING           | 0x0440 | Video conferencing device.    |
| AUDIO_VIDEO_VIDEO_GAMING_TOY             | 0x0448 | Audio or video gaming toy.  |
| PERIPHERAL_NON_KEYBOARD_NON_POINTING     | 0x0500 | Non-keyboard or non-pointing peripheral device.  |
| PERIPHERAL_KEYBOARD                      | 0x0540 | Keyboard device.      |
| PERIPHERAL_POINTING_DEVICE               | 0x0580 | Pointing peripheral device.    |
| PERIPHERAL_KEYBOARD_POINTING             | 0x05C0 | Keyboard pointing device.    |
| PERIPHERAL_UNCATEGORIZED                 | 0x0500 | Unclassified peripheral device.     |
| PERIPHERAL_JOYSTICK                      | 0x0504 | Peripheral joystick.     |
| PERIPHERAL_GAMEPAD                       | 0x0508 | Peripheral game pad     |
| PERIPHERAL_REMOTE_CONTROL                | 0x05C0 | Peripheral remote control device    |
| PERIPHERAL_SENSING_DEVICE                | 0x0510 | Peripheral sensing device.    |
| PERIPHERAL_DIGITIZER_TABLET              | 0x0514 | Peripheral digitizer tablet.|
| PERIPHERAL_CARD_READER                   | 0x0518 | Peripheral card reader.     |
| PERIPHERAL_DIGITAL_PEN                   | 0x051C | Peripheral digital pen.     |
| PERIPHERAL_SCANNER_RFID                  | 0x0520 | Peripheral RFID scanner. |
| PERIPHERAL_GESTURAL_INPUT                | 0x0522 | Gesture input device.    |
| IMAGING_UNCATEGORIZED                    | 0x0600 | Unclassified imaging device.    |
| IMAGING_DISPLAY                          | 0x0610 | Imaging display device.      |
| IMAGING_CAMERA                           | 0x0620 | Imaging camera device.     |
| IMAGING_SCANNER                          | 0x0640 | Imaging scanner.     |
| IMAGING_PRINTER                          | 0x0680 | Imaging printer.     |
| WEARABLE_UNCATEGORIZED                   | 0x0700 | Unclassified wearable device.   |
| WEARABLE_WRIST_WATCH                     | 0x0704 | Smart watch.     |
| WEARABLE_PAGER                           | 0x0708 | Wearable pager.    |
| WEARABLE_JACKET                          | 0x070C | Smart jacket.     |
| WEARABLE_HELMET                          | 0x0710 | Wearable helmet.     |
| WEARABLE_GLASSES                         | 0x0714 | Wearable glasses.     |
| TOY_UNCATEGORIZED                        | 0x0800 | Unclassified toy.    |
| TOY_ROBOT                                | 0x0804 | Toy robot.     |
| TOY_VEHICLE                              | 0x0808 | Toy vehicle.       |
| TOY_DOLL_ACTION_FIGURE                   | 0x080C | Humanoid toy doll.    |
| TOY_CONTROLLER                           | 0x0810 | Toy controller.     |
| TOY_GAME                                 | 0x0814 | Toy gaming device.      |
| HEALTH_UNCATEGORIZED                     | 0x0900 | Unclassified health devices.     |
| HEALTH_BLOOD_PRESSURE                    | 0x0904 | Blood pressure device.      |
| HEALTH_THERMOMETER                       | 0x0908 | Thermometer     |
| HEALTH_WEIGHING                          | 0x090C | Body scale.      |
| HEALTH_GLUCOSE                           | 0x0910 | Blood glucose monitor.     |
| HEALTH_PULSE_OXIMETER                    | 0x0914 | Pulse oximeter.   |
| HEALTH_PULSE_RATE                        | 0x0918 | Heart rate monitor.     |
| HEALTH_DATA_DISPLAY                      | 0x091C | Health data display.    |
| HEALTH_STEP_COUNTER                      | 0x0920 | Step counter.   |
| HEALTH_BODY_COMPOSITION_ANALYZER         | 0x0924 | Body composition analyzer. |
| HEALTH_PEAK_FLOW_MOITOR                  | 0x0928 | Hygrometer.     |
| HEALTH_MEDICATION_MONITOR                | 0x092C | Medication monitor.   |
| HEALTH_KNEE_PROSTHESIS                   | 0x0930 | Prosthetic knee.    |
| HEALTH_ANKLE_PROSTHESIS                  | 0x0934 | Prosthetic ankle.    |
| HEALTH_GENERIC_HEALTH_MANAGER            | 0x0938 | Generic health management device.    |
| HEALTH_PERSONAL_MOBILITY_DEVICE          | 0x093C | Personal mobility device.    |
A
annie_wangli 已提交
3423 3424 3425 3426 3427 3428 3429 3430


## PlayingState<sup>8+</sup><a name="PlayingState"></a>

Enumerates the A2DP playing states.

**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3431 3432 3433 3434
| Name               | Default Value   | Description     |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | Not playing. |
| STATE_PLAYING     | 0x0001 | Playing.|
A
annie_wangli 已提交
3435 3436 3437 3438 3439 3440 3441 3442


## ProfileId<sup>8+</sup><a name="ProfileId"></a>

Enumerates the Bluetooth profile IDs.

**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3443 3444 3445 3446
| Name                              | Default Value   | Description             |
| -------------------------------- | ------ | --------------- |
| PROFILE_A2DP_SOURCE              | 0x0001 | A2DP profile.|
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | HFP profile. |