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

A
Annie_wang 已提交
3 4
The Bluetooth module provides classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising.

A
Annie_wang 已提交
5
> **NOTE**<br>
A
annie_wangli 已提交
6
> 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_wang 已提交
7

Z
zengyawen 已提交
8 9 10 11


## Modules to Import

P
Peter_1988 已提交
12
```js
Z
zengyawen 已提交
13 14 15 16
import bluetooth from '@ohos.bluetooth';
```


A
annie_wangli 已提交
17
## bluetooth.enableBluetooth<sup>8+</sup><a name="enableBluetooth"></a>
Z
zengyawen 已提交
18 19 20 21 22

enableBluetooth(): boolean

Enables Bluetooth.

A
annie_wangli 已提交
23
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
24

A
annie_wangli 已提交
25 26 27 28
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
29 30
| Type     | Description                      |
| ------- | ------------------------ |
A
annie_wangli 已提交
31
| boolean | Returns **true** if Bluetooth is enabled; returns **false** otherwise.|
Z
zengyawen 已提交
32

A
annie_wangli 已提交
33
**Example**
Z
zengyawen 已提交
34

A
annie_wangli 已提交
35
```js
Z
zengyawen 已提交
36 37 38 39
let enable = bluetooth.enableBluetooth();
```


A
annie_wangli 已提交
40
## bluetooth.disableBluetooth<sup>8+</sup><a name="disableBluetooth"></a>
Z
zengyawen 已提交
41 42 43 44 45

disableBluetooth(): boolean

Disables Bluetooth.

A
annie_wangli 已提交
46
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
47

A
annie_wangli 已提交
48 49 50 51
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
52 53
| Type     | Description                      |
| ------- | ------------------------ |
A
annie_wangli 已提交
54
| boolean | Returns **true** if Bluetooth is disabled; returns **false** otherwise.|
Z
zengyawen 已提交
55

A
annie_wangli 已提交
56
**Example**
Z
zengyawen 已提交
57

A
annie_wangli 已提交
58
```js
Z
zengyawen 已提交
59 60 61 62
let disable = bluetooth.disableBluetooth();
```


A
annie_wangli 已提交
63
## bluetooth.getLocalName<sup>8+</sup><a name="getLocalName"></a>
Z
zengyawen 已提交
64 65 66 67 68

getLocalName(): string

Obtains the name of the local Bluetooth device.

A
annie_wangli 已提交
69
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
70

A
annie_wangli 已提交
71 72 73 74
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
75 76
| Type    | Description       |
| ------ | --------- |
A
annie_wangli 已提交
77
| string | Name of the local Bluetooth device obtained.|
Z
zengyawen 已提交
78

A
annie_wangli 已提交
79
**Example**
Z
zengyawen 已提交
80

A
annie_wangli 已提交
81
```js
Z
zengyawen 已提交
82 83 84 85 86 87 88 89 90 91
let localName = bluetooth.getLocalName();
```


## bluetooth.getState

getState(): BluetoothState

Obtains the Bluetooth state.

A
annie_wangli 已提交
92
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
93

A
annie_wangli 已提交
94 95 96 97
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
98 99
| Type                               | Description       |
| --------------------------------- | --------- |
A
annie_wangli 已提交
100
| [BluetoothState](#bluetoothstate) | Bluetooth state obtained.|
Z
zengyawen 已提交
101

A
annie_wangli 已提交
102
**Example**
Z
zengyawen 已提交
103

A
annie_wangli 已提交
104
```js
Z
zengyawen 已提交
105 106 107 108 109 110 111 112 113 114
let state = bluetooth.getState();
```


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

Obtains the profile connection state of this Bluetooth device.

A
annie_wangli 已提交
115
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
116

A
annie_wangli 已提交
117 118 119 120
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
121 122
| Type                                      | Description                 |
| ---------------------------------------- | ------------------- |
A
annie_wangli 已提交
123
| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.|
Z
zengyawen 已提交
124

A
annie_wangli 已提交
125
**Example**
Z
zengyawen 已提交
126

A
annie_wangli 已提交
127
```js
Z
zengyawen 已提交
128 129 130 131
let connectionState = bluetooth.getBtConnectionState();
```


A
annie_wangli 已提交
132
## bluetooth.setLocalName<sup>8+</sup><a name="setLocalName"></a>
Z
zengyawen 已提交
133 134 135 136 137

setLocalName(name: string): boolean

Sets the name of the local Bluetooth device.

A
annie_wangli 已提交
138 139 140 141 142
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
143

A
annie_wangli 已提交
144 145
| Name | Type    | Mandatory  | Description                   |
| ---- | ------ | ---- | --------------------- |
A
annie_wangli 已提交
146
| name | string | Yes   | Bluetooth device name to set. It cannot exceed 248 bytes.|
Z
zengyawen 已提交
147

A
annie_wangli 已提交
148
**Return value**
Z
zengyawen 已提交
149

A
annie_wangli 已提交
150 151
| Type     | Description                            |
| ------- | ------------------------------ |
A
annie_wangli 已提交
152
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
153

A
annie_wangli 已提交
154
**Example**
Z
zengyawen 已提交
155

A
annie_wangli 已提交
156
```js
Z
zengyawen 已提交
157 158 159 160 161 162 163 164 165 166
let ret = bluetooth.setLocalName('device_name');
```


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

Initiates Bluetooth pairing.

A
annie_wangli 已提交
167 168 169
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

A
annie_wangli 已提交
171 172
**Parameters**

A
annie_wangli 已提交
173 174 175
| Name     | Type    | Mandatory  | Description                                 |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | Yes   | Address of the remote device to pair, for example, XX:XX:XX:XX:XX:XX.|
Z
zengyawen 已提交
176

A
annie_wangli 已提交
177
**Return value**
Z
zengyawen 已提交
178

A
annie_wangli 已提交
179 180
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
181
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
182

A
annie_wangli 已提交
183
**Example**
Z
zengyawen 已提交
184

A
annie_wangli 已提交
185 186 187
```js
// The address can be scanned.
let result = bluetooth.pairDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
188
```
A
annie_wangli 已提交
189 190 191 192 193 194


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

getProfileConnState(profileId: ProfileId): ProfileConnectionState

A
Annie_wang 已提交
195
Obtains the connection state of a profile.
A
annie_wangli 已提交
196 197 198 199 200 201 202

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

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

**Parameters**

A
annie_wangli 已提交
203 204
| Name      | Type       | Mandatory  | Description                                   |
| --------- | --------- | ---- | ------------------------------------- |
A
Annie_wang 已提交
205
| ProfileId | profileId | Yes   | ID of the target profile, for example, **PROFILE_A2DP_SOURCE**.|
A
annie_wangli 已提交
206 207 208

**Return value**

A
annie_wangli 已提交
209 210 211
| Type                                             | Description               |
| ------------------------------------------------- | ------------------- |
| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.|
A
annie_wangli 已提交
212 213 214 215

**Example**

```js
A
Annie_wang 已提交
216
let result = bluetooth.getProfileConnState(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Z
zengyawen 已提交
217 218 219
```


A
annie_wangli 已提交
220 221 222 223 224 225
## bluetooth.cancelPairedDevice<sup>8+</sup><a name="cancelPairedDevice"></a>

cancelPairedDevice(deviceId: string): boolean

Cancels a paired remote device.

A
Annie_wang 已提交
226 227
This is a system API.

A
annie_wangli 已提交
228 229 230 231 232 233
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

**Parameters**

A
annie_wangli 已提交
234 235 236
| 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 已提交
237 238 239

**Return value**

A
annie_wangli 已提交
240 241
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| 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 已提交
263 264 265
| Name     | Type    | Mandatory  | Description                               |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
266 267 268

**Return value**

A
annie_wangli 已提交
269 270
| Type    | Description           |
| ------ | ------------- |
A
annie_wangli 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| 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 已提交
292 293 294
| Name     | Type    | Mandatory  | Description                               |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
295 296 297

**Return value**

A
annie_wangli 已提交
298 299
| Type                         | Description      |
| --------------------------- | -------- |
A
annie_wangli 已提交
300 301 302 303 304 305 306 307 308 309
| [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 已提交
310 311 312

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

A
Annie_wang 已提交
313
Obtains the paired devices.
Z
zengyawen 已提交
314

A
annie_wangli 已提交
315
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
316

A
annie_wangli 已提交
317 318 319 320
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
321 322
| Type                 | Description           |
| ------------------- | ------------- |
A
Annie_wang 已提交
323
| Array&lt;string&gt; | Addresses of the paired Bluetooth devices.|
Z
zengyawen 已提交
324

A
annie_wangli 已提交
325
**Example**
Z
zengyawen 已提交
326

A
annie_wangli 已提交
327
```js
Z
zengyawen 已提交
328 329 330 331
let devices = bluetooth.getPairedDevices();
```


A
annie_wangli 已提交
332
## bluetooth.setBluetoothScanMode<sup>8+</sup><a name="setBluetoothScanMode"></a>
Z
zengyawen 已提交
333 334 335

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

A
annie_wangli 已提交
336 337 338
Sets the Bluetooth scan mode so that the device can be discovered by a remote device.

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

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

A
annie_wangli 已提交
342 343
**Parameters**

A
annie_wangli 已提交
344 345 346
| Name     | Type                   | Mandatory  | Description                          |
| -------- | --------------------- | ---- | ---------------------------- |
| mode     | [ScanMode](#scanmode) | Yes   | Bluetooth scan mode to set.                     |
A
Annie_wang 已提交
347
| duration | number                | Yes   | Duration (in ms) in which the device can be discovered. The value **0** indicates unlimited time.|
Z
zengyawen 已提交
348

A
annie_wangli 已提交
349
**Return value**
Z
zengyawen 已提交
350

A
annie_wangli 已提交
351 352
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
353
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
354

A
annie_wangli 已提交
355
**Example**
Z
zengyawen 已提交
356

A
annie_wangli 已提交
357
```js
Z
zengyawen 已提交
358
// The device can be discovered and connected only when the discoverable and connectable mode is used.
A
Annie_wang 已提交
359
let result = bluetooth.setBluetoothScanMode(bluetooth.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
Z
zengyawen 已提交
360 361 362
```


A
annie_wangli 已提交
363
## bluetooth.getBluetoothScanMode<sup>8+</sup><a name="getBluetoothScanMode"></a>
Z
zengyawen 已提交
364 365 366 367 368

getBluetoothScanMode(): ScanMode

Obtains the Bluetooth scan mode.

A
annie_wangli 已提交
369
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
370

A
annie_wangli 已提交
371 372 373 374
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
375 376
| Type                   | Description     |
| --------------------- | ------- |
A
annie_wangli 已提交
377
| [ScanMode](#scanmode) | Bluetooth scan mode obtained.|
Z
zengyawen 已提交
378

A
annie_wangli 已提交
379
**Example**
Z
zengyawen 已提交
380

A
annie_wangli 已提交
381
```js
Z
zengyawen 已提交
382 383 384 385
let scanMode = bluetooth.getBluetoothScanMode();
```


A
annie_wangli 已提交
386
## bluetooth.startBluetoothDiscovery<sup>8+</sup><a name="startBluetoothDiscovery"></a>
Z
zengyawen 已提交
387 388 389

startBluetoothDiscovery(): boolean

A
annie_wangli 已提交
390
Starts Bluetooth scan to discover remote devices.
Z
zengyawen 已提交
391

A
Annie_wang 已提交
392
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH and ohos.permission.LOCATION
Z
zengyawen 已提交
393

A
annie_wangli 已提交
394 395 396 397
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
398 399
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
400
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
401

A
annie_wangli 已提交
402
**Example**
Z
zengyawen 已提交
403

A
annie_wangli 已提交
404
```js
Z
zengyawen 已提交
405 406 407 408 409 410 411 412 413
let deviceId;
function onReceiveEvent(data) {
    deviceId = data;
}
bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
let result = bluetooth.startBluetoothDiscovery();
```


A
annie_wangli 已提交
414
## bluetooth.stopBluetoothDiscovery<sup>8+</sup><a name="stopBluetoothDiscovery"></a>
Z
zengyawen 已提交
415 416 417 418 419

stopBluetoothDiscovery(): boolean

Stops Bluetooth scan.

A
annie_wangli 已提交
420
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
421

A
annie_wangli 已提交
422 423 424 425
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
426 427
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
428
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
429

A
annie_wangli 已提交
430
**Example**
Z
zengyawen 已提交
431

A
annie_wangli 已提交
432
```js
Z
zengyawen 已提交
433 434 435 436
let result = bluetooth.stopBluetoothDiscovery();
```


A
annie_wangli 已提交
437
## bluetooth.setDevicePairingConfirmation<sup>8+</sup><a name="setDevicePairingConfirmation"></a>
Z
zengyawen 已提交
438 439 440 441 442

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

Sets the device pairing confirmation.

A
annie_wangli 已提交
443 444 445
**Required permissions**: ohos.permission.MANAGE_BLUETOOTH

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

A
annie_wangli 已提交
447 448
**Parameters**

A
annie_wangli 已提交
449 450
| Name   | Type     | Mandatory  | Description                              |
| ------ | ------- | ---- | -------------------------------- |
A
Annie_wang 已提交
451
| device | string  | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
452
| 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 已提交
453

A
annie_wangli 已提交
454
**Return value**
Z
zengyawen 已提交
455

A
annie_wangli 已提交
456 457
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
458
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
459

A
annie_wangli 已提交
460
**Example**
Z
zengyawen 已提交
461

A
annie_wangli 已提交
462 463
```js
// Subscribe to the pinRequired event and configure the pairing confirmation after receiving a pairing request from the remote device.
Z
zengyawen 已提交
464 465 466 467 468 469 470 471
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 已提交
472
## bluetooth.on('bluetoothDeviceFind')<sup>8+</sup>
Z
zengyawen 已提交
473 474 475 476 477

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

Subscribes to the Bluetooth device discovery events.

A
annie_wangli 已提交
478
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
479

A
annie_wangli 已提交
480 481 482 483
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
484 485 486 487
| 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 已提交
488

A
annie_wangli 已提交
489
**Return value**
Z
zengyawen 已提交
490

A
annie_wangli 已提交
491
No value is returned.
Z
zengyawen 已提交
492

A
annie_wangli 已提交
493
**Example**
Z
zengyawen 已提交
494

A
annie_wangli 已提交
495
```js
Z
zengyawen 已提交
496 497 498 499 500 501 502
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 已提交
503
## bluetooth.off('bluetoothDeviceFind')<sup>8+</sup>
Z
zengyawen 已提交
504 505 506 507 508

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

Unsubscribes from the Bluetooth device discovery events.

A
annie_wangli 已提交
509
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
510

A
annie_wangli 已提交
511 512 513 514
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
515 516 517 518
| 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 已提交
519

A
annie_wangli 已提交
520
**Return value**
Z
zengyawen 已提交
521

A
annie_wangli 已提交
522
No value is returned.
Z
zengyawen 已提交
523

A
annie_wangli 已提交
524
**Example**
Z
zengyawen 已提交
525

A
annie_wangli 已提交
526
```js
Z
zengyawen 已提交
527 528 529 530 531 532 533 534
function onReceiveEvent(data) {
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
bluetooth.off('bluetoothDeviceFind', onReceiveEvent);
```


A
annie_wangli 已提交
535
## bluetooth.on('pinRequired')<sup>8+</sup>
Z
zengyawen 已提交
536 537 538

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

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

A
annie_wangli 已提交
541
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
542

A
annie_wangli 已提交
543 544 545 546
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
547 548 549 550
| 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 已提交
551

A
annie_wangli 已提交
552
**Return value**
Z
zengyawen 已提交
553

A
annie_wangli 已提交
554
No value is returned.
Z
zengyawen 已提交
555

A
annie_wangli 已提交
556
**Example**
Z
zengyawen 已提交
557

A
annie_wangli 已提交
558
```js
Z
zengyawen 已提交
559 560 561 562 563 564 565
function onReceiveEvent(data) { // data is the pairing request parameter.
    console.info('pin required = '+ JSON.stringify(data));
}
bluetooth.on('pinRequired', onReceiveEvent);
```


A
annie_wangli 已提交
566
## bluetooth.off('pinRequired')<sup>8+</sup>
Z
zengyawen 已提交
567 568 569

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

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

A
annie_wangli 已提交
572
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
573

A
annie_wangli 已提交
574 575 576 577
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
578 579 580 581
| 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 已提交
582

A
annie_wangli 已提交
583
**Return value**
Z
zengyawen 已提交
584

A
annie_wangli 已提交
585
No value is returned.
Z
zengyawen 已提交
586

A
annie_wangli 已提交
587
**Example**
Z
zengyawen 已提交
588

A
annie_wangli 已提交
589
```js
Z
zengyawen 已提交
590 591 592 593 594 595 596 597
function onReceiveEvent(data) {
    console.info('pin required = '+ JSON.stringify(data));
}
bluetooth.on('pinRequired', onReceiveEvent);
bluetooth.off('pinRequired', onReceiveEvent);
```


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

A
annie_wangli 已提交
600
on(type: "bondStateChange", callback: Callback&lt;BondStateParam&gt;): void
Z
zengyawen 已提交
601

A
Annie_wang 已提交
602
Subscribes to the Bluetooth bond state change events.
Z
zengyawen 已提交
603

A
annie_wangli 已提交
604
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
605

A
annie_wangli 已提交
606 607 608 609
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
610 611
| Name     | Type                                      | Mandatory  | Description                                  |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
A
Annie_wang 已提交
612 613
| type     | string                                   | Yes   | Event type. The value **bondStateChange** indicates a Bluetooth bond state change event.|
| callback | Callback&lt;[BondStateParam](#BondStateParam)&gt; | Yes   | Callback invoked to return the bond state. You need to implement this callback.   |
Z
zengyawen 已提交
614

A
annie_wangli 已提交
615
**Return value**
Z
zengyawen 已提交
616

A
annie_wangli 已提交
617
No value is returned.
Z
zengyawen 已提交
618

A
annie_wangli 已提交
619
**Example**
Z
zengyawen 已提交
620

A
annie_wangli 已提交
621
```js
A
Annie_wang 已提交
622
function onReceiveEvent(data) { // data, as the input parameter of the callback, indicates the bond state.
Z
zengyawen 已提交
623 624 625 626 627 628
    console.info('pair state = '+ JSON.stringify(data));
}
bluetooth.on('bondStateChange', onReceiveEvent);
```


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

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

A
Annie_wang 已提交
633
Unsubscribes from the Bluetooth bond state change events.
Z
zengyawen 已提交
634

A
annie_wangli 已提交
635
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
636

A
annie_wangli 已提交
637 638 639 640
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

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

A
annie_wangli 已提交
646
**Return value**
Z
zengyawen 已提交
647

A
annie_wangli 已提交
648
No value is returned.
Z
zengyawen 已提交
649

A
annie_wangli 已提交
650
**Example**
Z
zengyawen 已提交
651

A
annie_wangli 已提交
652
```js
Z
zengyawen 已提交
653 654 655 656 657 658 659 660
function onReceiveEvent(data) {
    console.info('bond state = '+ JSON.stringify(data));
}
bluetooth.on('bondStateChange', onReceiveEvent);
bluetooth.off('bondStateChange', onReceiveEvent);
```


A
annie_wangli 已提交
661
## bluetooth.on('stateChange')<sup>8+</sup>
Z
zengyawen 已提交
662 663 664 665 666

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

Subscribes to the Bluetooth connection state change events.

A
annie_wangli 已提交
667
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
668

A
annie_wangli 已提交
669 670 671 672
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
673 674 675 676
| 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 已提交
677

A
annie_wangli 已提交
678
**Return value**
Z
zengyawen 已提交
679

A
annie_wangli 已提交
680
No value is returned.
Z
zengyawen 已提交
681

A
annie_wangli 已提交
682
**Example**
Z
zengyawen 已提交
683

A
annie_wangli 已提交
684
```js
Z
zengyawen 已提交
685 686 687 688 689 690 691
function onReceiveEvent(data) {
    console.info('bluetooth state = '+ JSON.stringify(data));
}
bluetooth.on('stateChange', onReceiveEvent);
```


A
annie_wangli 已提交
692
## bluetooth.off('stateChange')<sup>8+</sup>
Z
zengyawen 已提交
693 694 695 696 697

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

Unsubscribes from the Bluetooth connection state change events.

A
annie_wangli 已提交
698
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
699

A
annie_wangli 已提交
700 701 702 703
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
704 705 706 707
| 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 已提交
708

A
annie_wangli 已提交
709
**Return value**
Z
zengyawen 已提交
710

A
annie_wangli 已提交
711
No value is returned.
Z
zengyawen 已提交
712

A
annie_wangli 已提交
713
**Example**
Z
zengyawen 已提交
714

A
annie_wangli 已提交
715
```js
Z
zengyawen 已提交
716 717 718 719 720 721 722 723
function onReceiveEvent(data) {
    console.info('bluetooth state = '+ JSON.stringify(data));
}
bluetooth.on('stateChange', onReceiveEvent);
bluetooth.off('stateChange', onReceiveEvent);
```


A
Annie_wang 已提交
724
## bluetooth.sppListen<sup>8+</sup><a name="sppListen"></a>
Z
zengyawen 已提交
725 726 727 728 729

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

Creates a server listening socket.

A
annie_wangli 已提交
730
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
731

A
annie_wangli 已提交
732 733 734 735
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
736 737 738 739 740
| 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 已提交
741

A
annie_wangli 已提交
742
**Example**
Z
zengyawen 已提交
743

A
annie_wangli 已提交
744
```js
Z
zengyawen 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758
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 已提交
759
## bluetooth.sppAccept<sup>8+</sup><a name="sppAccept"></a>
Z
zengyawen 已提交
760 761 762 763 764

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 已提交
765
**System capability**: SystemCapability.Communication.Bluetooth.Core
Z
zengyawen 已提交
766

A
annie_wangli 已提交
767 768
**Parameters**

A
annie_wangli 已提交
769 770 771 772
| 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 已提交
773

A
annie_wangli 已提交
774
**Example**
Z
zengyawen 已提交
775

A
annie_wangli 已提交
776
```js
A
Annie_wang 已提交
777 778 779 780 781 782 783 784
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;
  }
}
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
A
Annie_wang 已提交
819

Z
zengyawen 已提交
820 821 822 823 824 825 826 827 828 829
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 已提交
830
bluetooth.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
Z
zengyawen 已提交
831 832 833
```


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

sppCloseServerSocket(socket: number): void

Closes the listening socket of the server.

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

**Parameters**
Z
zengyawen 已提交
843

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

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

A
annie_wangli 已提交
850
```js
A
Annie_wang 已提交
851 852 853 854 855 856 857 858
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;
  }
}
Z
zengyawen 已提交
859 860 861 862
bluetooth.sppCloseServerSocket(serverNumber);
```


A
annie_wangli 已提交
863
## bluetooth.sppCloseClientSocket<sup>8+</sup><a name="sppCloseClientSocket"></a>
Z
zengyawen 已提交
864 865 866 867 868

sppCloseClientSocket(socket: number): void

Closes the client socket.

A
annie_wangli 已提交
869 870 871
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
872

A
annie_wangli 已提交
873 874 875 876
| Name   | Type    | Mandatory  | Description           |
| ------ | ------ | ---- | ------------- |
| Name   | Type    | Mandatory  | Description           |
| socket | number | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.|
Z
zengyawen 已提交
877

A
annie_wangli 已提交
878
**Example**
Z
zengyawen 已提交
879

A
annie_wangli 已提交
880
```js
A
Annie_wang 已提交
881 882 883 884 885 886 887 888 889
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;
}
Z
zengyawen 已提交
890 891 892 893
bluetooth.sppCloseClientSocket(clientNumber);
```


A
annie_wangli 已提交
894
## bluetooth.sppWrite<sup>8+</sup><a name="sppWrite"></a>
Z
zengyawen 已提交
895 896 897

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

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

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

A
annie_wangli 已提交
902 903
**Parameters**

A
annie_wangli 已提交
904 905 906 907
| 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 已提交
908

A
annie_wangli 已提交
909
**Return value**
Z
zengyawen 已提交
910

A
annie_wangli 已提交
911 912 913
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
914

A
annie_wangli 已提交
915
**Example**
Z
zengyawen 已提交
916

A
annie_wangli 已提交
917
```js
A
Annie_wang 已提交
918 919 920 921 922 923 924 925 926
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;
}
Z
zengyawen 已提交
927 928 929 930 931 932 933 934 935 936 937 938
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 已提交
939
## bluetooth.on('sppRead')<sup>8+</sup>
Z
zengyawen 已提交
940 941 942 943 944

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

Subscribes to the SPP read request events.

A
annie_wangli 已提交
945 946 947
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
948

A
annie_wangli 已提交
949 950 951 952 953
| 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 已提交
954

A
annie_wangli 已提交
955
**Return value**
Z
zengyawen 已提交
956

A
annie_wangli 已提交
957
No value is returned.
Z
zengyawen 已提交
958

A
annie_wangli 已提交
959
**Example**
Z
zengyawen 已提交
960

A
annie_wangli 已提交
961
```js
A
Annie_wang 已提交
962 963 964 965 966 967 968 969 970
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;
}
Z
zengyawen 已提交
971 972 973 974 975 976 977 978
function dataRead(dataBuffer) {
  let data = new Uint8Array(dataBuffer);
  console.log('bluetooth data is: ' + data[0]);
}
bluetooth.on('sppRead', clientNumber, dataRead);
```


A
annie_wangli 已提交
979
## bluetooth.off('sppRead')<sup>8+</sup>
Z
zengyawen 已提交
980 981 982 983 984

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

Unsubscribes from the SPP read request events.

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

A
annie_wangli 已提交
987 988
**Parameters**

A
annie_wangli 已提交
989 990 991 992 993
| 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 已提交
994

A
annie_wangli 已提交
995
**Return value**
Z
zengyawen 已提交
996

A
annie_wangli 已提交
997
No value is returned.
Z
zengyawen 已提交
998

A
annie_wangli 已提交
999
**Example**
Z
zengyawen 已提交
1000

A
annie_wangli 已提交
1001
```js
A
Annie_wang 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010
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;
}
Z
zengyawen 已提交
1011 1012 1013 1014
bluetooth.off('sppRead', clientNumber);
```


A
Annie_wang 已提交
1015
## bluetooth.getProfile<sup>8+</sup><a name="bt-getProfile"></a>
A
annie_wangli 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

Obtains a profile object.

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

**Parameters**

A
annie_wangli 已提交
1025 1026
| Name      | Type       | Mandatory  | Description                                   |
| --------- | --------- | ---- | ------------------------------------- |
A
Annie_wang 已提交
1027
| profileId | [ProfileId](#ProfileId) | Yes   | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.|
A
annie_wangli 已提交
1028 1029 1030

**Return value**

A
Annie_wang 已提交
1031 1032
| Type                                                        | Description                                                        |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
1033
| [A2dpSourceProfile](#a2dpsourceprofile) or [HandsFreeAudioGatewayProfile](#handsfreeaudiogatewayprofile)| Profile object obtained. Only **A2dpSourceProfile** and **HandsFreeAudioGatewayProfile** are supported.|
A
annie_wangli 已提交
1034 1035 1036 1037

**Example**

```js
A
Annie_wang 已提交
1038
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
A
annie_wangli 已提交
1039 1040
```

A
Annie_wang 已提交
1041
## bluetooth.getProfileInst<sup>9+</sup><a name="getProfileInst"></a>
A
Annie_wang 已提交
1042

A
Annie_wang 已提交
1043
getProfileInst(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile | HidHostProfile | PanProfile
A
Annie_wang 已提交
1044

A
Annie_wang 已提交
1045
Obtains a profile instance. API version 9 is added with **HidHostProfile** and **PanProfile**.
A
Annie_wang 已提交
1046 1047 1048 1049 1050 1051 1052

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

**Parameters**

| Name      | Type       | Mandatory  | Description                                   |
| --------- | --------- | ---- | ------------------------------------- |
A
Annie_wang 已提交
1053
| profileId | [ProfileId](#ProfileId) | Yes   | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.|
A
Annie_wang 已提交
1054 1055 1056 1057 1058

**Return value**

| Type                                                        | Description                                                        |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
1059
| [A2dpSourceProfile](#a2dpsourceprofile), [HandsFreeAudioGatewayProfile](#handsfreeaudiogatewayprofile), [HidHostProfile](#hidhostprofile), or [PanProfile](#panprofile)| Profile instance obtained, which can be **A2dpSourceProfile**, **HandsFreeAudioGatewayProfile**, **HidHostProfile**, or **PanProfile**.|
A
Annie_wang 已提交
1060 1061 1062 1063

**Example**

```js
A
Annie_wang 已提交
1064
let hidHost = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1065 1066
```

A
annie_wangli 已提交
1067

Z
zengyawen 已提交
1068 1069 1070 1071 1072 1073 1074 1075
## bluetooth.BLE

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

Creates a **GattServer** instance.

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

A
annie_wangli 已提交
1078 1079
**Return value**

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

A
annie_wangli 已提交
1084
**Example**
Z
zengyawen 已提交
1085

A
annie_wangli 已提交
1086
```js
Z
zengyawen 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
let gattServer = bluetooth.BLE.createGattServer();
```


### bluetooth.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

Creates a **GattClientDevice** instance.

A
annie_wangli 已提交
1097 1098 1099 1100
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1101 1102 1103
| Name     | Type    | Mandatory  | Description                                  |
| -------- | ------ | ---- | ------------------------------------ |
| deviceId | string | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
1104 1105 1106

**Return value**

A
annie_wangli 已提交
1107 1108
| Type                                   | Description                                  |
| ------------------------------------- | ------------------------------------ |
A
annie_wangli 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
| [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 已提交
1129

A
annie_wangli 已提交
1130 1131
| Type                 | Description                 |
| ------------------- | ------------------- |
A
annie_wangli 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
| 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 已提交
1153 1154 1155 1156
| 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 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 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

**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 已提交
1217 1218 1219 1220
| 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 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

**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 已提交
1248 1249 1250 1251
| 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 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

**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_wang 已提交
1289
| Type                 | Description           |
A
annie_wangli 已提交
1290
| ------------------- | ------------- |
A
Annie_wang 已提交
1291
| Array&lt;string&gt; | Addresses of the connected devices.|
A
annie_wangli 已提交
1292

A
Annie_wang 已提交
1293 1294 1295
**Example**

```js
A
Annie_wang 已提交
1296
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE)
A
Annie_wang 已提交
1297 1298
let retArray = a2dpSrc.getConnectionDevices();
```
A
annie_wangli 已提交
1299 1300 1301 1302 1303

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

getDeviceState(device: string): ProfileConnectionState

A
Annie_wang 已提交
1304
Obtains the connection state of the profile.
A
annie_wangli 已提交
1305 1306 1307 1308 1309 1310 1311

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

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

**Parameters**

A
annie_wangli 已提交
1312 1313
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1314
| device | string | Yes   | Address of the target device.|
Z
zengyawen 已提交
1315

A
annie_wangli 已提交
1316
**Return value**
Z
zengyawen 已提交
1317

A
Annie_wang 已提交
1318
| Type                                             | Description                   |
A
annie_wangli 已提交
1319
| ------------------------------------------------- | ----------------------- |
A
Annie_wang 已提交
1320
| [ProfileConnectionState](#profileconnectionstate) | Profile connection state obtained.|
A
annie_wangli 已提交
1321

A
Annie_wang 已提交
1322 1323 1324
**Example**

```js
A
Annie_wang 已提交
1325
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE)
A
Annie_wang 已提交
1326 1327
let ret = a2dpSrc.getDeviceState('XX:XX:XX:XX:XX:XX');
```
A
annie_wangli 已提交
1328 1329 1330 1331 1332 1333

## A2dpSourceProfile

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


A
Annie_wang 已提交
1334
### connect<sup>8+</sup><a name="a2dp-connect"></a>
A
annie_wangli 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

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 已提交
1346 1347
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1348
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1349 1350 1351

**Return value**

A
Annie_wang 已提交
1352
| Type     | Description                 |
A
annie_wangli 已提交
1353
| ------- | ------------------- |
A
Annie_wang 已提交
1354
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
1355 1356 1357 1358

**Example**

```js
A
Annie_wang 已提交
1359
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE)
A
annie_wangli 已提交
1360
let ret = a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1361 1362 1363
```


A
Annie_wang 已提交
1364
### disconnect<sup>8+</sup><a name="a2dp-disconnect"></a>
A
annie_wangli 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

disconnect(device: string): boolean

Disconnects an A2DP connection.

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

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

**Parameters**

A
annie_wangli 已提交
1376 1377
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1378
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1379 1380 1381

**Return value**

A
Annie_wang 已提交
1382
| Type     | Description                 |
A
annie_wangli 已提交
1383
| ------- | ------------------- |
A
Annie_wang 已提交
1384
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
1385 1386 1387 1388

**Example**

```js
A
Annie_wang 已提交
1389
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Z
zengyawen 已提交
1390
let ret = a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1391 1392 1393
```


A
Annie_wang 已提交
1394
### on('connectionStateChange')<sup>8+</sup>
A
annie_wangli 已提交
1395 1396 1397

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

A
Annie_wang 已提交
1398
Subscribes to the A2DP connection state change events.
A
annie_wangli 已提交
1399 1400 1401 1402 1403

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

**Parameters**

A
annie_wangli 已提交
1404 1405 1406 1407
| 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 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('a2dp state = '+ JSON.stringify(data));
}
A
Annie_wang 已提交
1419
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
A
Annie_wang 已提交
1420
a2dpSrc.on('connectionStateChange', onReceiveEvent);
A
annie_wangli 已提交
1421 1422 1423
```


A
Annie_wang 已提交
1424
### off('connectionStateChange')<sup>8+</sup>
A
annie_wangli 已提交
1425

A
Annie_wang 已提交
1426
off(type: "connectionStateChange", callback?: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
A
annie_wangli 已提交
1427

A
Annie_wang 已提交
1428
Unsubscribes from the A2DP connection state change events.
A
annie_wangli 已提交
1429 1430 1431 1432 1433

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

**Parameters**

A
annie_wangli 已提交
1434 1435 1436
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.|
A
Annie_wang 已提交
1437
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | No   | Callback used to return the A2DP connection state change event.                              |
A
annie_wangli 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('a2dp state = '+ JSON.stringify(data));
}
A
Annie_wang 已提交
1449
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
A
Annie_wang 已提交
1450 1451
a2dpSrc.on('connectionStateChange', onReceiveEvent);
a2dpSrc.off('connectionStateChange', onReceiveEvent);
A
annie_wangli 已提交
1452 1453 1454
```


A
Annie_wang 已提交
1455
### getPlayingState<sup>8+</sup>
A
annie_wangli 已提交
1456 1457 1458

getPlayingState(device: string): PlayingState

A
Annie_wang 已提交
1459
Obtains the playing state of a device.
A
annie_wangli 已提交
1460 1461 1462 1463 1464

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

**Parameters**

A
annie_wangli 已提交
1465 1466
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1467
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1468 1469 1470

**Return value**

A
Annie_wang 已提交
1471
| Type                           | Description        |
A
annie_wangli 已提交
1472
| ----------------------------- | ---------- |
A
Annie_wang 已提交
1473
| [PlayingState](#PlayingState) | Playing state of the remote device obtained.|
Z
zengyawen 已提交
1474

A
annie_wangli 已提交
1475
**Example**
Z
zengyawen 已提交
1476

A
annie_wangli 已提交
1477
```js
A
Annie_wang 已提交
1478
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
A
annie_wangli 已提交
1479
let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1480 1481 1482
```


A
annie_wangli 已提交
1483
## HandsFreeAudioGatewayProfile
Z
zengyawen 已提交
1484

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


A
Annie_wang 已提交
1488
### connect<sup>8+</sup><a name="hfp-connect"></a>
Z
zengyawen 已提交
1489

A
annie_wangli 已提交
1490
connect(device: string): boolean
Z
zengyawen 已提交
1491

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

A
annie_wangli 已提交
1494
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1495

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

A
annie_wangli 已提交
1498
**Parameters**
Z
zengyawen 已提交
1499

A
annie_wangli 已提交
1500 1501
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1502
| device | string | Yes   | Address of the target device.|
Z
zengyawen 已提交
1503

A
annie_wangli 已提交
1504
**Return value**
Z
zengyawen 已提交
1505

A
Annie_wang 已提交
1506
| Type     | Description                 |
A
annie_wangli 已提交
1507
| ------- | ------------------- |
A
Annie_wang 已提交
1508
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
1509

A
annie_wangli 已提交
1510
**Example**
Z
zengyawen 已提交
1511

A
annie_wangli 已提交
1512
```js
A
Annie_wang 已提交
1513
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
annie_wangli 已提交
1514
let ret = hfpAg.connect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1515
```
Z
zengyawen 已提交
1516 1517


A
Annie_wang 已提交
1518
### disconnect<sup>8+</sup><a name="hfp-disconnect"></a>
Z
zengyawen 已提交
1519

A
annie_wangli 已提交
1520
disconnect(device: string): boolean
Z
zengyawen 已提交
1521

A
annie_wangli 已提交
1522
Disconnects the HFP connection of a device.
Z
zengyawen 已提交
1523

A
annie_wangli 已提交
1524
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1525

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

A
annie_wangli 已提交
1528 1529
**Parameters**

A
annie_wangli 已提交
1530 1531
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1532
| device | string | Yes   | Address of the target device.|
Z
zengyawen 已提交
1533

A
annie_wangli 已提交
1534
**Return value**
Z
zengyawen 已提交
1535

A
annie_wangli 已提交
1536 1537 1538
| Type     | Description                 |
| ------- | ------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
1539

A
annie_wangli 已提交
1540
**Example**
Z
zengyawen 已提交
1541

A
annie_wangli 已提交
1542
```js
A
Annie_wang 已提交
1543
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
annie_wangli 已提交
1544
let ret = hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1545 1546 1547
```


A
Annie_wang 已提交
1548
### on('connectionStateChange')<sup>8+</sup>
Z
zengyawen 已提交
1549

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

A
Annie_wang 已提交
1552
Subscribes to the HFP connection state change events.
A
annie_wangli 已提交
1553 1554

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

A
annie_wangli 已提交
1556
**Parameters**
Z
zengyawen 已提交
1557

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

A
annie_wangli 已提交
1563
**Return value**
Z
zengyawen 已提交
1564

A
annie_wangli 已提交
1565
No value is returned.
Z
zengyawen 已提交
1566

A
annie_wangli 已提交
1567
**Example**
Z
zengyawen 已提交
1568

A
annie_wangli 已提交
1569
```js
Z
zengyawen 已提交
1570
function onReceiveEvent(data) {
A
annie_wangli 已提交
1571
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1572
}
A
Annie_wang 已提交
1573
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
Annie_wang 已提交
1574
hfpAg.on('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1575 1576 1577
```


A
Annie_wang 已提交
1578
### off('connectionStateChange')<sup>8+</sup>
Z
zengyawen 已提交
1579

A
Annie_wang 已提交
1580
off(type: "connectionStateChange", callback?: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
Z
zengyawen 已提交
1581

A
Annie_wang 已提交
1582
Unsubscribes from the HFP connection state change events.
Z
zengyawen 已提交
1583

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

A
annie_wangli 已提交
1586 1587
**Parameters**

A
annie_wangli 已提交
1588 1589
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
A
annie_wangli 已提交
1590
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an HFP connection state change event.|
A
Annie_wang 已提交
1591
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | No   | Callback used to return the HFP connection state change event.                              |
Z
zengyawen 已提交
1592

A
annie_wangli 已提交
1593
**Return value**
Z
zengyawen 已提交
1594

A
annie_wangli 已提交
1595
No value is returned.
Z
zengyawen 已提交
1596

A
annie_wangli 已提交
1597
**Example**
Z
zengyawen 已提交
1598

A
annie_wangli 已提交
1599
```js
Z
zengyawen 已提交
1600
function onReceiveEvent(data) {
A
annie_wangli 已提交
1601
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1602
}
A
Annie_wang 已提交
1603
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
Annie_wang 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
hfpAg.on('connectionStateChange', onReceiveEvent);
hfpAg.off('connectionStateChange', onReceiveEvent);
```


## HidHostProfile

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


A
Annie_wang 已提交
1614
### connect<sup>9+</sup><a name="HidHost-connect"></a>
A
Annie_wang 已提交
1615 1616 1617 1618 1619

connect(device: string): boolean

Connects to the HidHost service of a device.

A
Annie_wang 已提交
1620 1621
This is a system API.

A
Annie_wang 已提交
1622 1623 1624 1625 1626 1627 1628 1629
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

**Parameters**

| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1630
| device | string | Yes   | Address of the target device.|
A
Annie_wang 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

**Return value**

| Type     | Description                 |
| --------------------- | --------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```js
A
Annie_wang 已提交
1641
let hidHostProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1642 1643 1644 1645
let ret = hidHostProfile.connect('XX:XX:XX:XX:XX:XX');
```


A
Annie_wang 已提交
1646
### disconnect<sup>9+</sup><a name="HidHost-disconnect"></a>
A
Annie_wang 已提交
1647 1648 1649 1650 1651

disconnect(device: string): boolean

Disconnects from the HidHost service of a device.

A
Annie_wang 已提交
1652 1653
This is a system API.

A
Annie_wang 已提交
1654 1655 1656 1657 1658 1659 1660 1661
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

**Parameters**

| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
Annie_wang 已提交
1662
| device | string | Yes   | Address of the target device.|
A
Annie_wang 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672

**Return value**

| Type     | Description                 |
| --------------------- | --------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```js
A
Annie_wang 已提交
1673
let hidHostProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
let ret = hidHostProfile.disconnect('XX:XX:XX:XX:XX:XX');
```


### on('connectionStateChange')<sup>9+</sup>

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

Subscribes to the HidHost connection state change events.

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

**Parameters**

| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates a HidHost connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback invoked to return the HidHost connection state change event.                              |

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('hidHost state = '+ JSON.stringify(data));
}
A
Annie_wang 已提交
1703
let hidHost = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1704 1705 1706 1707 1708 1709
hidHost.on('connectionStateChange', onReceiveEvent);
```


### off('connectionStateChange')<sup>9+</sup>

A
Annie_wang 已提交
1710
off(type: "connectionStateChange", callback?: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
A
Annie_wang 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732

Unsubscribes from the HidHost connection state change events.

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

**Parameters**

| Name  | Type                                                 | Mandatory| Description                                                     |
| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- |
| type     | string                                                | Yes  | Event type. The value **connectionStateChange** indicates a HidHost connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | No  | Callback used to return the HidHost connection state change event.                                     |

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('hidHost state = '+ JSON.stringify(data));
}
A
Annie_wang 已提交
1733
let hidHost = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1734 1735
hidHost.on('connectionStateChange', onReceiveEvent);
hidHost.off('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1736 1737 1738
```


A
Annie_wang 已提交
1739 1740 1741 1742 1743
## PanProfile

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


A
Annie_wang 已提交
1744
### disconnect<sup>9+</sup><a name="PanP-disconnect"></a>
A
Annie_wang 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

disconnect(device: string): boolean

Disconnects from the Personal Area Network (PAN) service of a device.

This is a system API.

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

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

**Parameters**

| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
| device | string | Yes   | Address of the target device.|

**Return value**

| Type     | Description                 |
| --------------------- | --------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```js
let panProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_PAN_NETWORK);
let ret = panProfile.disconnect('XX:XX:XX:XX:XX:XX');
```


### on('connectionStateChange')<sup>9+</sup>

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

Subscribes to the PAN connection state change events.

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

**Parameters**

| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates a PAN connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback used to return the PAN connection state change event.                              |

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('pan state = '+ JSON.stringify(data));
}
let panProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_PAN_NETWORK);
panProfile.on('connectionStateChange', onReceiveEvent);
```


### off('connectionStateChange')<sup>9+</sup>

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

Unsubscribes from the PAN connection state change events.

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

**Parameters**

| Name  | Type                                                 | Mandatory| Description                                                     |
| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- |
| type     | string                                                | Yes  | Event type. The value **connectionStateChange** indicates a PAN connection state change event.|
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | No  | Callback used to return the PAN connection state change event.                                     |

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('pan state = '+ JSON.stringify(data));
}
let panProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_PAN_NETWORK);
panProfile.on('connectionStateChange', onReceiveEvent);
panProfile.off('connectionStateChange', onReceiveEvent);
```


### setTethering<sup>9+</sup><a name="setTethering"></a>

A
Annie_wang 已提交
1839
setTethering(enable: boolean): void
A
Annie_wang 已提交
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 1869 1870 1871 1872

Sets tethering.

This is a system API.

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

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

**Parameters**

| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
| value | boolean | Yes   | Whether to set tethering over a Bluetooth PAN.|

**Return value**

| Type     | Description                 |
| --------------------- | --------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```js
let panProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_PAN_NETWORK);
let ret = panProfile.setTethering(true);
```


### isTetheringOn<sup>9+</sup><a name="isTetheringOn"></a>

isTetheringOn(): boolean

A
Annie_wang 已提交
1873
Obtains the tethering state.
A
Annie_wang 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892

This is a system API.

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

**Return value**

| Type     | Description                 |
| --------------------- | --------------------------------- |
| boolean | Returns **true** if tethering is available over a Bluetooth PAN; return **false** otherwise.|

**Example**

```js
let panProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_PAN_NETWORK);
let ret = panProfile.isTetheringOn();
```


Z
zengyawen 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
## 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 已提交
1904 1905 1906
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

A
annie_wangli 已提交
1908 1909
**Parameters**

A
annie_wangli 已提交
1910 1911 1912 1913 1914
| 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 已提交
1915

A
annie_wangli 已提交
1916
**Return value**
Z
zengyawen 已提交
1917

A
annie_wangli 已提交
1918
No value is returned.
Z
zengyawen 已提交
1919

A
annie_wangli 已提交
1920
**Example**
Z
zengyawen 已提交
1921

A
annie_wangli 已提交
1922
```js
Z
zengyawen 已提交
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 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
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 已提交
1971
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1972

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

A
annie_wangli 已提交
1975
**Return value**
Z
zengyawen 已提交
1976

A
annie_wangli 已提交
1977 1978 1979 1980 1981
No value is returned.

**Example**

```js
Z
zengyawen 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
let server = bluetooth.BLE.createGattServer();
server.stopAdvertising();
```


### addService

addService(service: GattService): boolean

Adds a service to this GATT server.

A
annie_wangli 已提交
1993 1994 1995
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
1997 1998
**Parameters**

A
annie_wangli 已提交
1999 2000 2001
| Name    | Type                         | Mandatory  | Description                      |
| ------- | --------------------------- | ---- | ------------------------ |
| service | [GattService](#gattservice) | Yes   | Service to add. Settings related to BLE advertising.|
Z
zengyawen 已提交
2002

A
annie_wangli 已提交
2003
**Return value**
Z
zengyawen 已提交
2004

A
annie_wangli 已提交
2005 2006
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
2007
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2008

A
annie_wangli 已提交
2009
**Example**
Z
zengyawen 已提交
2010

A
annie_wangli 已提交
2011
```js
Z
zengyawen 已提交
2012 2013 2014 2015 2016 2017 2018
// 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',
A
Annie_wang 已提交
2019
  descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
Z
zengyawen 已提交
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
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',
A
Annie_wang 已提交
2030
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
Z
zengyawen 已提交
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
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 已提交
2052 2053 2054
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2056 2057
**Parameters**

A
annie_wangli 已提交
2058 2059 2060
| 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 已提交
2061

A
annie_wangli 已提交
2062
**Return value**
Z
zengyawen 已提交
2063

A
annie_wangli 已提交
2064 2065
|         |                            |
| ------- | -------------------------- |
A
annie_wangli 已提交
2066 2067
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
2068

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

A
annie_wangli 已提交
2071
```js
Z
zengyawen 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080
let server = bluetooth.BLE.createGattServer();
server.removeService('00001810-0000-1000-8000-00805F9B34FB');
```


### close

close(): void

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

A
annie_wangli 已提交
2083
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2084

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

A
annie_wangli 已提交
2087 2088 2089
**Example**

```js
Z
zengyawen 已提交
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
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 已提交
2101
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2102

A
annie_wangli 已提交
2103 2104 2105 2106
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
2107 2108 2109 2110
| 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 已提交
2111

A
annie_wangli 已提交
2112
**Return value**
Z
zengyawen 已提交
2113

A
annie_wangli 已提交
2114 2115
|         |                          |
| ------- | ------------------------ |
A
annie_wangli 已提交
2116 2117
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
2118

A
annie_wangli 已提交
2119
**Example**
Z
zengyawen 已提交
2120

A
annie_wangli 已提交
2121
```js
A
Annie_wang 已提交
2122 2123 2124 2125 2126 2127 2128
// 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',
A
Annie_wang 已提交
2129
  descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
A
Annie_wang 已提交
2130
descriptors[0] = descriptor;
A
Annie_wang 已提交
2131 2132 2133
let arrayBufferC = new ArrayBuffer(8);
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
Z
zengyawen 已提交
2134
let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
A
Annie_wang 已提交
2135
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false};
Z
zengyawen 已提交
2136
let server = bluetooth.BLE.createGattServer();
A
annie_wangli 已提交
2137
server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic);
Z
zengyawen 已提交
2138 2139 2140 2141 2142 2143 2144 2145 2146
```


### sendResponse

sendResponse(serverResponse: ServerResponse): boolean

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

A
annie_wangli 已提交
2147 2148 2149
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2151 2152
**Parameters**

A
annie_wangli 已提交
2153 2154 2155
| Name           | Type                               | Mandatory  | Description             |
| -------------- | --------------------------------- | ---- | --------------- |
| serverResponse | [ServerResponse](#serverresponse) | Yes   | Response returned by the GATT server.|
Z
zengyawen 已提交
2156

A
annie_wangli 已提交
2157
**Return value**
Z
zengyawen 已提交
2158

A
annie_wangli 已提交
2159 2160
|         |                            |
| ------- | -------------------------- |
A
annie_wangli 已提交
2161 2162
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
2163

A
annie_wangli 已提交
2164
**Example**
Z
zengyawen 已提交
2165

A
annie_wangli 已提交
2166 2167
```js
/* send response */
Z
zengyawen 已提交
2168 2169 2170 2171
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
cccValue[0] = 1123;
let serverResponse = {
A
annie_wangli 已提交
2172
    "deviceId": "XX:XX:XX:XX:XX:XX",
Z
zengyawen 已提交
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
    "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 已提交
2195 2196 2197 2198 2199
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
2200

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

A
annie_wangli 已提交
2206
**Return value**
Z
zengyawen 已提交
2207

A
annie_wangli 已提交
2208
No value is returned.
Z
zengyawen 已提交
2209

A
annie_wangli 已提交
2210
**Example**
Z
zengyawen 已提交
2211

A
annie_wangli 已提交
2212
```js
Z
zengyawen 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
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 已提交
2243 2244 2245
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2247 2248
**Parameters**

A
annie_wangli 已提交
2249 2250 2251 2252
| 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 已提交
2253

A
annie_wangli 已提交
2254
**Return value**
Z
zengyawen 已提交
2255

A
annie_wangli 已提交
2256
No value is returned.
Z
zengyawen 已提交
2257

A
annie_wangli 已提交
2258
**Example**
Z
zengyawen 已提交
2259

A
annie_wangli 已提交
2260
```js
Z
zengyawen 已提交
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
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 已提交
2272
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2273

A
annie_wangli 已提交
2274 2275 2276 2277
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
2278 2279 2280
| Name     | Type                                      | Mandatory  | Description                                    |
| -------- | ---------------------------------------- | ---- | -------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicWrite** indicates a characteristic write request event.|
A
Annie_wang 已提交
2281
| callback | Callback&lt;[CharacteristicWriteReq](#characteristicwritereq)&gt; | Yes   | Callback invoked to return a characteristic write request from the GATT client.            |
Z
zengyawen 已提交
2282

A
annie_wangli 已提交
2283
**Return value**
Z
zengyawen 已提交
2284

A
annie_wangli 已提交
2285
No value is returned.
Z
zengyawen 已提交
2286

A
annie_wangli 已提交
2287
**Example**
Z
zengyawen 已提交
2288

A
annie_wangli 已提交
2289
```js
Z
zengyawen 已提交
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
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 已提交
2323 2324 2325
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2327 2328
**Parameters**

A
annie_wangli 已提交
2329 2330 2331 2332
| 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 已提交
2333

A
annie_wangli 已提交
2334
**Return value**
Z
zengyawen 已提交
2335

A
annie_wangli 已提交
2336
No value is returned.
Z
zengyawen 已提交
2337

A
annie_wangli 已提交
2338
**Example**
Z
zengyawen 已提交
2339

A
annie_wangli 已提交
2340
```js
Z
zengyawen 已提交
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
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 已提交
2352 2353 2354 2355 2356
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
2357

A
annie_wangli 已提交
2358 2359 2360
| Name     | Type                                      | Mandatory  | Description                               |
| -------- | ---------------------------------------- | ---- | --------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorRead** indicates a descriptor read request event.|
A
Annie_wang 已提交
2361
| callback | Callback&lt;[DescriptorReadReq](#descriptorreadreq)&gt; | Yes   | Callback invoked to return a descriptor read request event from the GATT client.       |
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 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
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 已提交
2400 2401 2402
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2404 2405
**Parameters**

A
annie_wangli 已提交
2406 2407 2408 2409
| 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 已提交
2410

A
annie_wangli 已提交
2411
**Return value**
Z
zengyawen 已提交
2412

A
annie_wangli 已提交
2413
No value is returned.
Z
zengyawen 已提交
2414

A
annie_wangli 已提交
2415
**Example**
Z
zengyawen 已提交
2416

A
annie_wangli 已提交
2417
```js
Z
zengyawen 已提交
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
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 已提交
2429
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2430

A
annie_wangli 已提交
2431 2432 2433 2434
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

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

A
annie_wangli 已提交
2440
**Return value**
Z
zengyawen 已提交
2441

A
annie_wangli 已提交
2442
No value is returned.
Z
zengyawen 已提交
2443

A
annie_wangli 已提交
2444
**Example**
Z
zengyawen 已提交
2445

A
annie_wangli 已提交
2446
```js
Z
zengyawen 已提交
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
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 已提交
2480 2481 2482
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2484 2485
**Parameters**

A
annie_wangli 已提交
2486 2487 2488 2489
| 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 已提交
2490

A
annie_wangli 已提交
2491
**Return value**
Z
zengyawen 已提交
2492

A
annie_wangli 已提交
2493
No value is returned.
Z
zengyawen 已提交
2494

A
annie_wangli 已提交
2495
**Example**
Z
zengyawen 已提交
2496

A
annie_wangli 已提交
2497
```js
Z
zengyawen 已提交
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
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 已提交
2508 2509 2510 2511 2512
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
2513

A
annie_wangli 已提交
2514 2515 2516 2517
| 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 已提交
2518

A
annie_wangli 已提交
2519
**Return value**
Z
zengyawen 已提交
2520

A
annie_wangli 已提交
2521
No value is returned.
Z
zengyawen 已提交
2522

A
annie_wangli 已提交
2523
**Example**
Z
zengyawen 已提交
2524

A
annie_wangli 已提交
2525
```js
Z
zengyawen 已提交
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
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 已提交
2542 2543 2544
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2546 2547
**Parameters**

A
annie_wangli 已提交
2548 2549 2550 2551
| 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 已提交
2552

A
annie_wangli 已提交
2553
**Return value**
Z
zengyawen 已提交
2554

A
annie_wangli 已提交
2555
No value is returned.
Z
zengyawen 已提交
2556

A
annie_wangli 已提交
2557
**Example**
Z
zengyawen 已提交
2558

A
annie_wangli 已提交
2559
```js
Z
zengyawen 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
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 已提交
2574
Initiates a connection to the remote BLE device.
Z
zengyawen 已提交
2575

A
annie_wangli 已提交
2576
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2577

A
annie_wangli 已提交
2578 2579 2580 2581
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2582 2583
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
2584
| boolean | Returns **true** if the connection is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2585

A
annie_wangli 已提交
2586
**Example**
Z
zengyawen 已提交
2587

A
annie_wangli 已提交
2588 2589
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2590 2591 2592 2593 2594 2595 2596 2597
let ret = device.connect();
```


### disconnect

disconnect(): boolean

A
annie_wangli 已提交
2598
Disconnects from the remote BLE device.
Z
zengyawen 已提交
2599

A
annie_wangli 已提交
2600
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2601

A
annie_wangli 已提交
2602 2603 2604 2605
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2606 2607
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
2608
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2609

A
annie_wangli 已提交
2610
**Example**
Z
zengyawen 已提交
2611

A
annie_wangli 已提交
2612 2613
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
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 已提交
2624
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2625

A
annie_wangli 已提交
2626 2627 2628 2629
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2630 2631
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
2632
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2633

A
annie_wangli 已提交
2634
**Example**
Z
zengyawen 已提交
2635

A
annie_wangli 已提交
2636 2637
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
let ret = device.close();
```




### getServices

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

A
Annie_wang 已提交
2648
Obtains all services of the remote BLE device. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
2649

A
annie_wangli 已提交
2650
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2651

A
annie_wangli 已提交
2652 2653 2654 2655
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

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

A
annie_wangli 已提交
2660
**Return value**
Z
zengyawen 已提交
2661

A
annie_wangli 已提交
2662
No value is returned.
Z
zengyawen 已提交
2663

A
annie_wangli 已提交
2664
**Example**
Z
zengyawen 已提交
2665

A
annie_wangli 已提交
2666
```js
Z
zengyawen 已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
// 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 已提交
2680
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689
device.connect();
device.getServices(getServices);
```


### getServices

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

A
Annie_wang 已提交
2690
Obtains all services of the remote BLE device. This API uses a promise to return the result.
Z
zengyawen 已提交
2691

A
annie_wangli 已提交
2692
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2693

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

A
annie_wangli 已提交
2696 2697 2698 2699
**Parameters**

**Return value**

A
annie_wangli 已提交
2700 2701
| Type                                      | Description                         |
| ---------------------------------------- | --------------------------- |
A
annie_wangli 已提交
2702
| Promise&lt;Array&lt;[GattService](#gattservice)&gt;&gt; | Promise used to return the services obtained.|
Z
zengyawen 已提交
2703

A
annie_wangli 已提交
2704
**Example**
Z
zengyawen 已提交
2705

A
annie_wangli 已提交
2706
```js
Z
zengyawen 已提交
2707
// Promise
A
Annie_wang 已提交
2708 2709 2710 2711
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
device.connect();
device.getServices().then(result => {
    console.info("getServices successfully:" + JSON.stringify(result));
A
Annie_wang 已提交
2712
});
Z
zengyawen 已提交
2713 2714 2715 2716 2717 2718 2719
```


### readCharacteristicValue

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

A
Annie_wang 已提交
2720
Reads the characteristic value of the specific service of the remote BLE device. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2721 2722

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

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

A
annie_wangli 已提交
2726 2727
**Parameters**

A
annie_wangli 已提交
2728 2729 2730 2731
| 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 已提交
2732

A
annie_wangli 已提交
2733
**Return value**
Z
zengyawen 已提交
2734

A
annie_wangli 已提交
2735
No value is returned.
Z
zengyawen 已提交
2736

A
annie_wangli 已提交
2737
**Example**
Z
zengyawen 已提交
2738

A
annie_wangli 已提交
2739
```js
Z
zengyawen 已提交
2740 2741 2742 2743 2744 2745 2746 2747 2748
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 已提交
2749
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
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_wang 已提交
2774
Reads the characteristic value of the specific service of the remote BLE device. This API uses a promise to return the result.
A
annie_wangli 已提交
2775 2776 2777 2778

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

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

A
annie_wangli 已提交
2780
**Parameters**
Z
zengyawen 已提交
2781

A
annie_wangli 已提交
2782 2783 2784
| Name           | Type                                     | Mandatory  | Description      |
| -------------- | --------------------------------------- | ---- | -------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | Yes   | Characteristic value to read.|
Z
zengyawen 已提交
2785

A
annie_wangli 已提交
2786
**Return value**
Z
zengyawen 已提交
2787

A
annie_wangli 已提交
2788 2789
|                                          |                            |
| ---------------------------------------- | -------------------------- |
A
annie_wangli 已提交
2790 2791
| Type | Description |
| Promise&lt;[BLECharacteristic](#blecharacteristic)&gt; | Promise used to return the characteristic value read. |
Z
zengyawen 已提交
2792

A
annie_wangli 已提交
2793
**Example**
Z
zengyawen 已提交
2794

A
annie_wangli 已提交
2795 2796
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
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_wang 已提交
2821
Reads the descriptor contained in the specific characteristic of the remote BLE device. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2822 2823

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

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

A
annie_wangli 已提交
2827 2828
**Parameters**

A
annie_wangli 已提交
2829 2830 2831 2832
| 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 已提交
2833

A
annie_wangli 已提交
2834
**Return value**
Z
zengyawen 已提交
2835

A
annie_wangli 已提交
2836
No value is returned.
Z
zengyawen 已提交
2837

A
annie_wangli 已提交
2838
**Example**
Z
zengyawen 已提交
2839

A
annie_wangli 已提交
2840
```js
Z
zengyawen 已提交
2841 2842 2843 2844 2845 2846 2847 2848 2849
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 已提交
2850
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
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_wang 已提交
2865
Reads the descriptor contained in the specific characteristic of the remote BLE device. This API uses a promise to return the result.
A
annie_wangli 已提交
2866 2867 2868 2869

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

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

A
annie_wangli 已提交
2871
**Parameters**
Z
zengyawen 已提交
2872

A
annie_wangli 已提交
2873 2874 2875
| Name       | Type                             | Mandatory  | Description      |
| ---------- | ------------------------------- | ---- | -------- |
| descriptor | [BLEDescriptor](#bledescriptor) | Yes   | Descriptor to read.|
Z
zengyawen 已提交
2876

A
annie_wangli 已提交
2877
**Return value**
Z
zengyawen 已提交
2878

A
annie_wangli 已提交
2879 2880
|                                          |                            |
| ---------------------------------------- | -------------------------- |
A
annie_wangli 已提交
2881 2882
| Type | Description |
| Promise&lt;[BLEDescriptor](#bledescriptor)&gt; | Promise used to return the descriptor read. |
Z
zengyawen 已提交
2883

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

A
annie_wangli 已提交
2886 2887
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
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 已提交
2902 2903 2904
Writes a characteristic value to the remote BLE device.

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

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

A
annie_wangli 已提交
2908 2909
**Parameters**

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

A
annie_wangli 已提交
2914
**Return value**
Z
zengyawen 已提交
2915

A
annie_wangli 已提交
2916 2917
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2918
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2919

A
annie_wangli 已提交
2920
**Example**
Z
zengyawen 已提交
2921

A
annie_wangli 已提交
2922 2923
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
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 已提交
2952 2953 2954 2955 2956
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 已提交
2957

A
annie_wangli 已提交
2958
**Parameters**
Z
zengyawen 已提交
2959

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

A
annie_wangli 已提交
2964
**Return value**
Z
zengyawen 已提交
2965

A
annie_wangli 已提交
2966 2967
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2968
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2969

A
annie_wangli 已提交
2970
**Example**
Z
zengyawen 已提交
2971

A
annie_wangli 已提交
2972 2973
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992
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_wang 已提交
2993
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).
A
annie_wangli 已提交
2994 2995 2996 2997

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

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

A
annie_wangli 已提交
2999
**Parameters**
Z
zengyawen 已提交
3000

A
annie_wangli 已提交
3001 3002 3003
| Name | Type    | Mandatory  | Description            |
| ---- | ------ | ---- | -------------- |
| mtu  | number | Yes   | MTU to set, which ranges from 22 to 512 bytes.|
Z
zengyawen 已提交
3004

A
annie_wangli 已提交
3005
**Return value**
Z
zengyawen 已提交
3006

A
annie_wangli 已提交
3007 3008
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
3009
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
3010

A
annie_wangli 已提交
3011
**Example**
Z
zengyawen 已提交
3012

A
annie_wangli 已提交
3013 3014
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3015 3016 3017 3018 3019 3020 3021 3022
device.setBLEMtuSize(128);
```


### setNotifyCharacteristicChanged

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

A
annie_wangli 已提交
3023 3024 3025 3026 3027
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 已提交
3028

A
annie_wangli 已提交
3029
**Parameters**
Z
zengyawen 已提交
3030

A
annie_wangli 已提交
3031 3032 3033 3034
| 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 已提交
3035

A
annie_wangli 已提交
3036
**Return value**
Z
zengyawen 已提交
3037

A
annie_wangli 已提交
3038 3039
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
3040
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
3041

A
annie_wangli 已提交
3042
**Example**
Z
zengyawen 已提交
3043

A
annie_wangli 已提交
3044
```js
A
Annie_wang 已提交
3045 3046 3047 3048 3049 3050 3051
// 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',
A
Annie_wang 已提交
3052
  descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
A
Annie_wang 已提交
3053
descriptors[0] = descriptor;
A
Annie_wang 已提交
3054 3055 3056
let arrayBufferC = new ArrayBuffer(8);
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
A
annie_wangli 已提交
3057
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
A
Annie_wang 已提交
3058
device.setNotifyCharacteristicChanged(characteristic, false);
Z
zengyawen 已提交
3059 3060 3061 3062 3063 3064 3065 3066 3067
```


### 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 已提交
3068 3069 3070 3071 3072
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
3073

A
annie_wangli 已提交
3074 3075 3076 3077
| 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 已提交
3078

A
annie_wangli 已提交
3079
**Return value**
Z
zengyawen 已提交
3080

A
annie_wangli 已提交
3081
No value is returned.
Z
zengyawen 已提交
3082

A
annie_wangli 已提交
3083
**Example**
Z
zengyawen 已提交
3084

A
annie_wangli 已提交
3085
```js
Z
zengyawen 已提交
3086 3087 3088 3089 3090
function CharacteristicChange(CharacteristicChangeReq) {
  let serviceUuid = CharacteristicChangeReq.serviceUuid;
  let characteristicUuid = CharacteristicChangeReq.characteristicUuid;
  let value = new Uint8Array(CharacteristicChangeReq.characteristicValue);
}
A
annie_wangli 已提交
3091
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101
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 已提交
3102 3103 3104
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
3106 3107
**Parameters**

A
annie_wangli 已提交
3108 3109 3110 3111
| 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 已提交
3112

A
annie_wangli 已提交
3113
**Return value**
Z
zengyawen 已提交
3114

A
annie_wangli 已提交
3115
No value is returned.
Z
zengyawen 已提交
3116

A
annie_wangli 已提交
3117
**Example**
Z
zengyawen 已提交
3118

A
annie_wangli 已提交
3119 3120
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3121 3122 3123 3124 3125 3126 3127 3128 3129 3130
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 已提交
3131
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
3132

A
annie_wangli 已提交
3133 3134 3135 3136
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
3137 3138 3139 3140
| 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 已提交
3141

A
annie_wangli 已提交
3142
**Return value**
Z
zengyawen 已提交
3143

A
annie_wangli 已提交
3144
No value is returned.
Z
zengyawen 已提交
3145

A
annie_wangli 已提交
3146
**Example**
Z
zengyawen 已提交
3147

A
annie_wangli 已提交
3148
```js
Z
zengyawen 已提交
3149 3150 3151 3152
function ConnectStateChanged(state) {
  console.log('bluetooth connect state changed');
  let connectState = state.state;
}
A
annie_wangli 已提交
3153
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
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 已提交
3164 3165 3166
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
3168 3169
**Parameters**

A
annie_wangli 已提交
3170 3171 3172 3173
| 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 已提交
3174

A
annie_wangli 已提交
3175
**Return value**
Z
zengyawen 已提交
3176

A
annie_wangli 已提交
3177
No value is returned.
Z
zengyawen 已提交
3178

A
annie_wangli 已提交
3179
**Example**
Z
zengyawen 已提交
3180

A
annie_wangli 已提交
3181 3182
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3183 3184 3185 3186 3187 3188 3189 3190
device.off('BLEConnectionStateChange');
```


### getDeviceName

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

A
Annie_wang 已提交
3191
Obtains the name of the remote BLE device. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3192 3193 3194 3195

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

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

A
annie_wangli 已提交
3197
**Parameters**
Z
zengyawen 已提交
3198

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

A
annie_wangli 已提交
3203
**Return value**
Z
zengyawen 已提交
3204

A
annie_wangli 已提交
3205
No value is returned.
Z
zengyawen 已提交
3206

A
annie_wangli 已提交
3207
**Example**
Z
zengyawen 已提交
3208

A
annie_wangli 已提交
3209 3210 3211
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
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_wang 已提交
3223
Obtains the name of the remote BLE device. This API uses a promise to return the result.
Z
zengyawen 已提交
3224

A
annie_wangli 已提交
3225
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
3226

A
annie_wangli 已提交
3227 3228 3229 3230
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
3231 3232
| Type                   | Description                                |
| --------------------- | ---------------------------------- |
A
annie_wangli 已提交
3233
| Promise&lt;string&gt; | Promise used to return the remote BLE device name.|
Z
zengyawen 已提交
3234

A
annie_wangli 已提交
3235
**Example**
Z
zengyawen 已提交
3236

A
annie_wangli 已提交
3237 3238 3239
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
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_wang 已提交
3251
Obtains the received signal strength indication (RSSI) of the remote BLE device. This API uses an asynchronous callback to return the result. It can be used only after a connection is set up by calling [connect](#connect).
A
annie_wangli 已提交
3252 3253

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

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

A
annie_wangli 已提交
3257 3258
**Parameters**

A
annie_wangli 已提交
3259 3260 3261
| Name     | Type                         | Mandatory  | Description                            |
| -------- | --------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the RSSI, in dBm.|
Z
zengyawen 已提交
3262

A
annie_wangli 已提交
3263
**Return value**
Z
zengyawen 已提交
3264

A
annie_wangli 已提交
3265
No value is returned.
Z
zengyawen 已提交
3266

A
annie_wangli 已提交
3267
**Example**
Z
zengyawen 已提交
3268

A
annie_wangli 已提交
3269 3270 3271
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
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_wang 已提交
3284
Obtains the RSSI of the remote BLE device. This API uses a promise to return the result. It can be used only after a connection is set up by calling [connect](#connect).
Z
zengyawen 已提交
3285

A
annie_wangli 已提交
3286
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
3287

A
annie_wangli 已提交
3288 3289 3290 3291
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
3292 3293
| Type                   | Description                               |
| --------------------- | --------------------------------- |
A
annie_wangli 已提交
3294
| Promise&lt;number&gt; | Promise used to return the RSSI, in dBm.|
Z
zengyawen 已提交
3295

A
annie_wangli 已提交
3296
**Example**
Z
zengyawen 已提交
3297

A
annie_wangli 已提交
3298 3299 3300
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3301 3302 3303 3304 3305
let rssi = gattClient.getRssiValue().then((data) => {
    console.info('rssi' + JSON.stringify(data));
})
```

A
annie_wangli 已提交
3306
## ScanMode<sup>8+</sup><a name="ScanMode"></a>
Z
zengyawen 已提交
3307 3308 3309

Enumerates the scan modes.

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

A
Annie_wang 已提交
3312
| Name                                      | Default Value | Description             |
A
annie_wangli 已提交
3313 3314 3315 3316 3317 3318 3319
| ---------------------------------------- | ---- | --------------- |
| 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 已提交
3320

A
annie_wangli 已提交
3321
## BondState<sup>8+</sup><a name="BondState"></a>
Z
zengyawen 已提交
3322 3323 3324

Enumerates the pairing states.

A
annie_wangli 已提交
3325 3326
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3327
| Name                | Default Value | Description    |
A
annie_wangli 已提交
3328 3329 3330 3331
| ------------------ | ---- | ------ |
| BOND_STATE_INVALID | 0    | Invalid pairing.|
| BOND_STATE_BONDING | 1    | Pairing. |
| BOND_STATE_BONDED  | 2    | Paired.  |
Z
zengyawen 已提交
3332 3333


A
annie_wangli 已提交
3334
## SppOption<sup>8+</sup><a name="SppOption"></a>
Z
zengyawen 已提交
3335 3336 3337

Defines the SPP configuration parameters.

A
annie_wangli 已提交
3338 3339
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3340 3341 3342 3343 3344
| 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 已提交
3345 3346


A
annie_wangli 已提交
3347
## SppType<sup>8+</sup><a name="SppType"></a>
Z
zengyawen 已提交
3348 3349 3350

Enumerates the SPP link types.

A
annie_wangli 已提交
3351 3352
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3353
| Name        | Default Value | Description           |
A
annie_wangli 已提交
3354 3355
| ---------- | ---- | ------------- |
| SPP_RFCOMM | 0    | Radio frequency communication (RFCOMM) link type.|
Z
zengyawen 已提交
3356 3357 3358 3359 3360 3361


## GattService

Defines the GATT service API parameters.

A
annie_wangli 已提交
3362 3363
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3364 3365 3366 3367 3368 3369
| 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 已提交
3370 3371 3372 3373 3374 3375


## BLECharacteristic

Defines the characteristic API parameters.

A
annie_wangli 已提交
3376 3377
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3378 3379 3380 3381 3382 3383
| 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 已提交
3384 3385 3386 3387 3388 3389


## BLEDescriptor

Defines the descriptor API parameters.

A
annie_wangli 已提交
3390 3391
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3392 3393 3394 3395 3396 3397
| 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 已提交
3398 3399 3400 3401 3402 3403


## NotifyCharacteristic

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

A
annie_wangli 已提交
3404 3405
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3406 3407 3408 3409 3410 3411
| 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 已提交
3412 3413 3414 3415 3416 3417


## CharacteristicReadReq

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

A
annie_wangli 已提交
3418 3419
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3420 3421 3422 3423 3424 3425 3426
| 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 已提交
3427 3428 3429 3430 3431 3432


## CharacteristicWriteReq

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

A
annie_wangli 已提交
3433 3434
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3435 3436 3437 3438 3439 3440 3441 3442
| 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 已提交
3443

A
annie_wangli 已提交
3444

Z
zengyawen 已提交
3445 3446 3447 3448
## DescriptorReadReq

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

A
annie_wangli 已提交
3449 3450
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3451 3452 3453 3454 3455 3456 3457 3458
| 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 已提交
3459 3460 3461 3462 3463 3464


## DescriptorWriteReq

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

A
annie_wangli 已提交
3465 3466
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
| 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 已提交
3478 3479 3480 3481 3482 3483


## ServerResponse

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

A
annie_wangli 已提交
3484 3485
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3486 3487 3488 3489 3490 3491 3492
| 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 已提交
3493 3494 3495 3496 3497 3498


## BLEConnectChangedState

Defines the parameters of **BLEConnectChangedState**.

A
annie_wangli 已提交
3499 3500
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3501 3502 3503 3504
| 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 已提交
3505 3506 3507 3508 3509 3510


## ProfileConnectionState

Enumerates the profile connection states.

A
annie_wangli 已提交
3511 3512
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3513 3514 3515 3516 3517 3518
| Name                 | Default Value | Description            |
| ------------------- | ---- | -------------- |
| STATE_DISCONNECTED  | 0    | Disconnected. |
| STATE_CONNECTING    | 1    | Connecting.|
| STATE_CONNECTED     | 2    | Connected. |
| STATE_DISCONNECTING | 3    | Disconnecting.|
Z
zengyawen 已提交
3519 3520 3521 3522 3523 3524


## ScanFilter

Defines the scan filter parameters.

A
annie_wangli 已提交
3525 3526
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
| 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  | Service UUID of the device to filter, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
| serviceUuidMask<sup>9+</sup>             | string      | Yes  | Yes  | Service UUID mask of the device to filter, for example, **FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF**.|
| serviceSolicitationUuid<sup>9+</sup>     | string      | Yes  | Yes  | Service solicitation UUID of the device to filter, for example, **00001888-0000-1000-8000-00805F9B34FB**.|
| serviceSolicitationUuidMask<sup>9+</sup> | string      | Yes  | Yes  | Service solicitation UUID mask of the device to filter, for example, **FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF**.|
| serviceData<sup>9+</sup>                 | ArrayBuffer | Yes  | Yes  | Service data of the device to filter, for example, **[0x90, 0x00, 0xF1, 0xF2]**.|
| serviceDataMask<sup>9+</sup>             | ArrayBuffer | Yes  | Yes  | Service data mask of the device to filter, for example, **[0xFF,0xFF,0xFF,0xFF]**.|
A
Annie_wang 已提交
3537
| manufactureId<sup>9+</sup>               | number      | Yes  | Yes  | Manufacturer ID of the device to filter, for example, **0x0006**.                |
A
Annie_wang 已提交
3538 3539
| manufactureData<sup>9+</sup>             | ArrayBuffer | Yes  | Yes  | Manufacturer data of the device to filter, for example, **[0x1F,0x2F,0x3F]**.|
| manufactureDataMask<sup>9+</sup>         | ArrayBuffer | Yes  | Yes  | Manufacturer data mask of the device to filter, for example, **[0xFF, 0xFF, 0xFF]**.|
Z
zengyawen 已提交
3540 3541 3542 3543 3544 3545


## ScanOptions

Defines the scan configuration parameters.

A
annie_wangli 已提交
3546 3547
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3548 3549 3550 3551 3552
| 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 已提交
3553 3554 3555 3556 3557 3558


## ScanDuty

Enumerates the scan duty options.

A
annie_wangli 已提交
3559 3560
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3561 3562 3563 3564 3565
| 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 已提交
3566 3567 3568 3569 3570 3571


## MatchMode

Enumerates the hardware match modes of BLE scan filters.

A
annie_wangli 已提交
3572 3573
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3574 3575 3576 3577
| 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 已提交
3578 3579 3580 3581 3582 3583


## ScanResult

Defines the scan result.

A
annie_wangli 已提交
3584 3585
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3586 3587 3588 3589 3590
| 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 已提交
3591 3592 3593 3594 3595 3596


## BluetoothState

Enumerates the Bluetooth states.

A
annie_wangli 已提交
3597 3598
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3599 3600 3601 3602 3603 3604 3605 3606 3607
| 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 已提交
3608 3609 3610 3611 3612 3613


## AdvertiseSetting

Defines the BLE advertising parameters.

A
annie_wangli 已提交
3614 3615
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3616 3617
| Name         | Type   | Readable  | Writable  | Description                                      |
| ----------- | ------- | ---- | ---- | ---------------------------------------- |
A
Annie_wang 已提交
3618
| interval    | number  | Yes   | Yes   | Interval for BLE advertising. The minimum value is **32** slots (20 ms). The maximum value is **16384** slots. The default value is **1600** slots (1s).|
A
annie_wangli 已提交
3619 3620
| 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 已提交
3621 3622 3623 3624 3625 3626


## AdvertiseData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3627 3628
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3629 3630 3631 3632 3633
| 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 已提交
3634 3635 3636 3637 3638 3639


## ManufactureData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3640 3641
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3642 3643 3644 3645
| 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 已提交
3646 3647 3648 3649 3650 3651


## ServiceData

Defines the service data contained in an advertisement packet.

A
annie_wangli 已提交
3652 3653
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3654 3655 3656 3657
| Name          | Type       | Readable  | Writable  | Description        |
| ------------ | ----------- | ---- | ---- | ---------- |
| serviceUuid  | string      | Yes   | Yes   | Service UUID.|
| serviceValue | ArrayBuffer | Yes   | Yes   | Service data.   |
Z
zengyawen 已提交
3658 3659


A
annie_wangli 已提交
3660
## PinRequiredParam<sup>8+</sup><a name="PinRequiredParam"></a>
Z
zengyawen 已提交
3661 3662 3663

Defines the pairing request parameters.

A
annie_wangli 已提交
3664 3665
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3666 3667 3668 3669
| 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 已提交
3670 3671


A
Annie_wang 已提交
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
## BondStateParam<sup>8+</sup><a name="BondStateParam"></a>

Defines the bond state parameters.

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

| Name      | Type  | Readable  | Writable  | Description         |
| -------- | ------ | ---- | ---- | ----------- |
| deviceId | string      | Yes   | No   | ID of the device.|
| state    | BondState   | Yes   | No   | State of the device.|


A
annie_wangli 已提交
3684 3685 3686 3687 3688 3689
## StateChangeParam<sup>8+</sup><a name="StateChangeParam"></a>

Defines the profile state change parameters.

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

A
annie_wangli 已提交
3690 3691 3692 3693
| 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 已提交
3694 3695 3696 3697 3698 3699 3700 3701


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

Defines the class of a Bluetooth device.

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

A
annie_wangli 已提交
3702 3703 3704 3705 3706
| 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 已提交
3707 3708 3709 3710 3711 3712 3713 3714 3715



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

Enumerates the major classes of Bluetooth devices.

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

A
annie_wangli 已提交
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728
| 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 已提交
3729 3730 3731 3732 3733 3734 3735 3736


## 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 已提交
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824
| 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 已提交
3825 3826 3827 3828 3829 3830 3831 3832


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

Enumerates the A2DP playing states.

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

A
annie_wangli 已提交
3833 3834 3835 3836
| Name               | Default Value   | Description     |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | Not playing. |
| STATE_PLAYING     | 0x0001 | Playing.|
A
annie_wangli 已提交
3837 3838 3839 3840


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

A
Annie_wang 已提交
3841
Enumerates the Bluetooth profiles. API version 9 is added with **PROFILE_HID_HOST** and **PROFILE_PAN_NETWORK**.
A
annie_wangli 已提交
3842 3843 3844

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

A
annie_wangli 已提交
3845 3846 3847 3848
| Name                              | Default Value   | Description             |
| -------------------------------- | ------ | --------------- |
| PROFILE_A2DP_SOURCE              | 0x0001 | A2DP profile.|
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | HFP profile. |
A
Annie_wang 已提交
3849
| PROFILE_HID_HOST<sup>9+</sup> | 0x0006 | Human Interface Device (HID) profile. |
A
Annie_wang 已提交
3850
| PROFILE_PAN_NETWORK<sup>9+</sup> | 0x0007 | PAN profile. |