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

A
Annie_wang 已提交
3
> **NOTE**<br>
A
annie_wangli 已提交
4
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
Annie_wang 已提交
5

A
Annie_wang 已提交
6
The Bluetooth module provides classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising.
Z
zengyawen 已提交
7 8 9 10


## Modules to Import

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


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

enableBluetooth(): boolean

Enables Bluetooth.

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

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

**Return value**

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

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

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


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

disableBluetooth(): boolean

Disables Bluetooth.

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

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

**Return value**

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

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

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


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

getLocalName(): string

Obtains the name of the local Bluetooth device.

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

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

**Return value**

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

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

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


## bluetooth.getState

getState(): BluetoothState

Obtains the Bluetooth state.

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

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

**Return value**

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

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

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


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

Obtains the profile connection state of this Bluetooth device.

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

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

**Return value**

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

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

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


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

setLocalName(name: string): boolean

Sets the name of the local Bluetooth device.

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

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

**Parameters**
Z
zengyawen 已提交
142

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

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

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

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

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


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

Initiates Bluetooth pairing.

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

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

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

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

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

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

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

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


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

getProfileConnState(profileId: ProfileId): ProfileConnectionState

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

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

cancelPairedDevice(deviceId: string): boolean

Cancels a paired remote device.

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

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

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

**Parameters**

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

**Return value**

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

**Return value**

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

**Return value**

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

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

Obtains the Bluetooth pairing list.

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

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

**Return value**

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

getBluetoothScanMode(): ScanMode

Obtains the Bluetooth scan mode.

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

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

**Return value**

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

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

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


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

startBluetoothDiscovery(): boolean

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

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

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

**Return value**

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

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

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


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

stopBluetoothDiscovery(): boolean

Stops Bluetooth scan.

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

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

**Return value**

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

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

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


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

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

Sets the device pairing confirmation.

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

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

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

A
annie_wangli 已提交
448 449
| Name   | Type     | Mandatory  | Description                              |
| ------ | ------- | ---- | -------------------------------- |
A
Annie_wang 已提交
450
| device | string  | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
451
| accept | boolean | Yes   | Whether to accept the pairing request. The value **true** means to accept the pairing request, and the value **false** means the opposite.       |
Z
zengyawen 已提交
452

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

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

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

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

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

Subscribes to the Bluetooth device discovery events.

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

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

**Parameters**

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

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

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

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

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

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

Unsubscribes from the Bluetooth device discovery events.

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

Subscribes to the Bluetooth pairing state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Unsubscribes from the Bluetooth pairing state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Subscribes to the Bluetooth connection state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Unsubscribes from the Bluetooth connection state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Creates a server listening socket.

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

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

**Parameters**

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

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

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

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

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

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

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

A
annie_wangli 已提交
775
```js
A
Annie_wang 已提交
776 777 778 779 780 781 782 783
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 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796
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 已提交
797
## bluetooth.sppConnect<sup>8+</sup><a name="sppConnect"></a>
Z
zengyawen 已提交
798 799 800

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

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

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

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

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

A
annie_wangli 已提交
809 810 811 812 813
| 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 已提交
814

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

A
annie_wangli 已提交
817
```js
A
Annie_wang 已提交
818

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


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

sppCloseServerSocket(socket: number): void

Closes the listening socket of the server.

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

**Parameters**
Z
zengyawen 已提交
842

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

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

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


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

sppCloseClientSocket(socket: number): void

Closes the client socket.

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

**Parameters**
Z
zengyawen 已提交
871

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

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

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


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

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

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

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

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

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

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

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

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

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

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

Subscribes to the SPP read request events.

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

**Parameters**
Z
zengyawen 已提交
947

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

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

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

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

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


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

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

Unsubscribes from the SPP read request events.

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

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

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

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

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

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

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


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

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

Obtains a profile object.

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

**Parameters**

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

**Return value**

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

**Example**

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

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

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

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

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

**Parameters**

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

**Return value**

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

**Example**

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

A
annie_wangli 已提交
1066

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

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

Creates a **GattServer** instance.

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

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

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

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

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


### bluetooth.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

Creates a **GattClientDevice** instance.

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

**Parameters**

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

**Return value**

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

A
annie_wangli 已提交
1129 1130
| Type                 | Description                 |
| ------------------- | ------------------- |
A
annie_wangli 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
| 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 已提交
1152 1153 1154 1155
| 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 已提交
1156 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

**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 已提交
1216 1217 1218 1219
| 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 已提交
1220 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

**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 已提交
1247 1248 1249 1250
| 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 已提交
1251 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

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

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

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

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

getDeviceState(device: string): ProfileConnectionState

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

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

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

**Parameters**

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

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

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

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

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

## A2dpSourceProfile

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


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

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

**Return value**

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

**Example**

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


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

disconnect(device: string): boolean

Disconnects an A2DP connection.

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

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

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

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

**Parameters**

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

**Return value**

No value is returned.

**Example**

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


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

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

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

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

**Parameters**

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

**Return value**

No value is returned.

**Example**

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


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

getPlayingState(device: string): PlayingState

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

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

**Parameters**

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

**Return value**

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

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

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


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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

A
annie_wangli 已提交
1598
```js
Z
zengyawen 已提交
1599
function onReceiveEvent(data) {
A
annie_wangli 已提交
1600
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1601
}
A
Annie_wang 已提交
1602
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
Annie_wang 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
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 已提交
1613
### connect<sup>9+</sup><a name="HidHost-connect"></a>
A
Annie_wang 已提交
1614 1615 1616 1617 1618

connect(device: string): boolean

Connects to the HidHost service of a device.

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

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

disconnect(device: string): boolean

Disconnects from the HidHost service of a device.

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

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

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

**Parameters**

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

**Return value**

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

**Example**

```js
A
Annie_wang 已提交
1672
let hidHostProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1673 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
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 已提交
1702
let hidHost = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1703 1704 1705 1706 1707 1708
hidHost.on('connectionStateChange', onReceiveEvent);
```


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

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

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 已提交
1732
let hidHost = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
A
Annie_wang 已提交
1733 1734
hidHost.on('connectionStateChange', onReceiveEvent);
hidHost.off('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1735 1736 1737
```


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

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


A
Annie_wang 已提交
1743
### disconnect<sup>9+</sup><a name="PanP-disconnect"></a>
A
Annie_wang 已提交
1744 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

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 已提交
1838
setTethering(enable: boolean): void
A
Annie_wang 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871

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 已提交
1872
Obtains the tethering state.
A
Annie_wang 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891

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

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

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

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

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

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

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

A
annie_wangli 已提交
1921
```js
Z
zengyawen 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
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 已提交
1970
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1971

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

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

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

**Example**

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


### addService

addService(service: GattService): boolean

Adds a service to this GATT server.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


### close

close(): void

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

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

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

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

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

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

**Parameters**

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

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

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

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

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


### sendResponse

sendResponse(serverResponse: ServerResponse): boolean

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

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

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

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

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

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

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

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

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

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

**Parameters**
Z
zengyawen 已提交
2199

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

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

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

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

A
annie_wangli 已提交
2211
```js
Z
zengyawen 已提交
2212 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
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 已提交
2242 2243 2244
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

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

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

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

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

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

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

**Parameters**

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

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

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

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

A
annie_wangli 已提交
2288
```js
Z
zengyawen 已提交
2289 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
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 已提交
2322 2323 2324
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

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

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

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

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

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

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

**Parameters**
Z
zengyawen 已提交
2356

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

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

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

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

A
annie_wangli 已提交
2368
```js
Z
zengyawen 已提交
2369 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
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 已提交
2399 2400 2401
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

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

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

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

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

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

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

**Parameters**

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

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

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

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

A
annie_wangli 已提交
2445
```js
Z
zengyawen 已提交
2446 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
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 已提交
2479 2480 2481
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

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

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

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

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

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

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

**Parameters**
Z
zengyawen 已提交
2512

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

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

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

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

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

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

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

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

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

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

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

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

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

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

**Return value**

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

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

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


### disconnect

disconnect(): boolean

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

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

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

**Return value**

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

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

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

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

**Return value**

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

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

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




### getServices

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

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

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

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

**Parameters**

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

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

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

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

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


### getServices

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

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

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

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

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

**Return value**

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

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

A
annie_wangli 已提交
2705
```js
Z
zengyawen 已提交
2706
// Promise
A
Annie_wang 已提交
2707 2708 2709 2710
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 已提交
2711
});
Z
zengyawen 已提交
2712 2713 2714 2715 2716 2717 2718
```


### readCharacteristicValue

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A
annie_wangli 已提交
2921 2922
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2923 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
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 已提交
2951 2952 2953 2954 2955
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 已提交
2956

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

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

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

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

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

A
annie_wangli 已提交
2971 2972
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991
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 已提交
2992
Sets the maximum transmission unit (MTU) that can be transmitted between the GATT client and its remote BLE device. This API can be used only after a connection is set up by calling [connect](#connect).
A
annie_wangli 已提交
2993 2994 2995 2996

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

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

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

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

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

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

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

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


### setNotifyCharacteristicChanged

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

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

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

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

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

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

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

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


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

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

**Parameters**
Z
zengyawen 已提交
3072

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

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

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

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

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

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

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

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

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

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

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

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

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

**Parameters**

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

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

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

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

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

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

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

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

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

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

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

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


### getDeviceName

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

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

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

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

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

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

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

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

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

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

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

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

**Return value**

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

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

A
annie_wangli 已提交
3236 3237 3238
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
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 已提交
3250
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 已提交
3251 3252

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

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

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

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

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

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

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

A
annie_wangli 已提交
3268 3269 3270
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
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 已提交
3283
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 已提交
3284

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

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

**Return value**

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

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

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

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

Enumerates the scan modes.

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

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

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

Enumerates the pairing states.

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

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


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

Defines the SPP configuration parameters.

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

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


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

Enumerates the SPP link types.

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

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


## GattService

Defines the GATT service API parameters.

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

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


## BLECharacteristic

Defines the characteristic API parameters.

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

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


## BLEDescriptor

Defines the descriptor API parameters.

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

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


## NotifyCharacteristic

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

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

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


## CharacteristicReadReq

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

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

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


## CharacteristicWriteReq

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

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

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

A
annie_wangli 已提交
3443

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

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

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

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


## DescriptorWriteReq

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

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

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


## ServerResponse

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

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

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


## BLEConnectChangedState

Defines the parameters of **BLEConnectChangedState**.

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

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


## ProfileConnectionState

Enumerates the profile connection states.

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

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


## ScanFilter

Defines the scan filter parameters.

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

A
Annie_wang 已提交
3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
| 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 已提交
3536
| manufactureId<sup>9+</sup>               | number      | Yes  | Yes  | Manufacturer ID of the device to filter, for example, **0x0006**.                |
A
Annie_wang 已提交
3537 3538
| 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 已提交
3539 3540 3541 3542 3543 3544


## ScanOptions

Defines the scan configuration parameters.

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

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


## ScanDuty

Enumerates the scan duty options.

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

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


## MatchMode

Enumerates the hardware match modes of BLE scan filters.

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

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


## ScanResult

Defines the scan result.

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

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


## BluetoothState

Enumerates the Bluetooth states.

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

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


## AdvertiseSetting

Defines the BLE advertising parameters.

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

A
annie_wangli 已提交
3615 3616
| Name         | Type   | Readable  | Writable  | Description                                      |
| ----------- | ------- | ---- | ---- | ---------------------------------------- |
A
Annie_wang 已提交
3617
| 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 已提交
3618 3619
| 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 已提交
3620 3621 3622 3623 3624 3625


## AdvertiseData

Defines the content of a BLE advertisement packet.

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

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


## ManufactureData

Defines the content of a BLE advertisement packet.

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

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


## ServiceData

Defines the service data contained in an advertisement packet.

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

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


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

Defines the pairing request parameters.

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

A
annie_wangli 已提交
3665 3666 3667 3668
| 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 已提交
3669 3670 3671 3672 3673 3674 3675 3676


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

Defines the profile state change parameters.

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

A
annie_wangli 已提交
3677 3678 3679 3680
| 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 已提交
3681 3682 3683 3684 3685 3686 3687 3688


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

Defines the class of a Bluetooth device.

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

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



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

Enumerates the major classes of Bluetooth devices.

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

A
annie_wangli 已提交
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
| 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 已提交
3716 3717 3718 3719 3720 3721 3722 3723


## 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 已提交
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 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
| 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 已提交
3812 3813 3814 3815 3816 3817 3818 3819


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

Enumerates the A2DP playing states.

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

A
annie_wangli 已提交
3820 3821 3822 3823
| Name               | Default Value   | Description     |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | Not playing. |
| STATE_PLAYING     | 0x0001 | Playing.|
A
annie_wangli 已提交
3824 3825 3826 3827


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

A
Annie_wang 已提交
3828
Enumerates the Bluetooth profiles. API version 9 is added with **PROFILE_HID_HOST** and **PROFILE_PAN_NETWORK**.
A
annie_wangli 已提交
3829 3830 3831

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

A
annie_wangli 已提交
3832 3833 3834 3835
| Name                              | Default Value   | Description             |
| -------------------------------- | ------ | --------------- |
| PROFILE_A2DP_SOURCE              | 0x0001 | A2DP profile.|
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | HFP profile. |
A
Annie_wang 已提交
3836
| PROFILE_HID_HOST<sup>9+</sup> | 0x0006 | Human Interface Device (HID) profile. |
A
Annie_wang 已提交
3837
| PROFILE_PAN_NETWORK<sup>9+</sup> | 0x0007 | PAN profile. |