js-apis-bluetooth.md 138.8 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.bluetooth (Bluetooth)
A
annie_wangli 已提交
2

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

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

Z
zengyawen 已提交
9 10 11 12


## Modules to Import

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


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

enableBluetooth(): boolean

Enables Bluetooth.

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

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

**Return value**

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

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

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


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

disableBluetooth(): boolean

Disables Bluetooth.

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

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

**Return value**

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

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

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


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

getLocalName(): string

Obtains the name of the local Bluetooth device.

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

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

**Return value**

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

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

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


## bluetooth.getState

getState(): BluetoothState

Obtains the Bluetooth state.

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

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

**Return value**

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

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

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


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

Obtains the profile connection state of this Bluetooth device.

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

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

**Return value**

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

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

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


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

setLocalName(name: string): boolean

Sets the name of the local Bluetooth device.

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

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

**Parameters**
Z
zengyawen 已提交
144

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

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

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

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

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


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

Initiates Bluetooth pairing.

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

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

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

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

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

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

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

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


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

getProfileConnState(profileId: ProfileId): ProfileConnectionState

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

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

cancelPairedDevice(deviceId: string): boolean

Cancels a paired remote device.

A
Annie_wang 已提交
227
**System API**: This is a system API.
A
Annie_wang 已提交
228

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

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

**Parameters**

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

**Return value**

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

**Return value**

A
annie_wangli 已提交
270 271
| Type    | Description           |
| ------ | ------------- |
A
annie_wangli 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284
| 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

A
Annie_wang 已提交
285
Obtains the class of the remote Bluetooth device.
A
annie_wangli 已提交
286 287 288 289 290 291 292

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

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

**Parameters**

A
annie_wangli 已提交
293 294 295
| Name     | Type    | Mandatory  | Description                               |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | Yes   | Address of the target remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
296 297 298

**Return value**

A
annie_wangli 已提交
299 300
| Type                         | Description      |
| --------------------------- | -------- |
A
Annie_wang 已提交
301
| [DeviceClass](#deviceclass) | Class of the remote device obtained.|
A
annie_wangli 已提交
302 303 304 305 306 307 308 309 310

**Example**

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


## bluetooth.getPairedDevices<sup>8+</sup><a name="getPairedDevices"></a>
Z
zengyawen 已提交
311 312 313

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

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

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

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

**Return value**

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

getBluetoothScanMode(): ScanMode

Obtains the Bluetooth scan mode.

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

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

**Return value**

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

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

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


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

startBluetoothDiscovery(): boolean

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

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

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

**Return value**

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

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

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


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

stopBluetoothDiscovery(): boolean

Stops Bluetooth scan.

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

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

**Return value**

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

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

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


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

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

Sets the device pairing confirmation.

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

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

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

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

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

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

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

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

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

Subscribes to the Bluetooth device discovery events.

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

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

**Parameters**

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

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

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

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

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

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

Unsubscribes from the Bluetooth device discovery events.

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

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

**Parameters**

A
annie_wangli 已提交
516 517 518
| Name     | Type                                 | Mandatory  | Description                                      |
| -------- | ----------------------------------- | ---- | ---------------------------------------- |
| type     | string                              | Yes   | Event type. The value **bluetoothDeviceFind** indicates an event reported when a Bluetooth device is discovered.  |
A
Annie_wang 已提交
519
| callback | Callback&lt;Array&lt;string&gt;&gt; | No   | Callback for the **bluetoothDeviceFind** event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
520

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

Subscribes to the Bluetooth connection state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Unsubscribes from the Bluetooth connection state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Creates a server listening socket.

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

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

**Parameters**

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A
annie_wangli 已提交
819
```js
A
Annie_wang 已提交
820

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


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

sppCloseServerSocket(socket: number): void

Closes the listening socket of the server.

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

**Parameters**
Z
zengyawen 已提交
844

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

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

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


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

sppCloseClientSocket(socket: number): void

Closes the client socket.

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

**Parameters**
Z
zengyawen 已提交
873

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

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

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


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

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

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

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

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

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

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

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

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

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

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

Subscribes to the SPP read request events.

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

**Parameters**
Z
zengyawen 已提交
949

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

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

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

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

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


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

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

Unsubscribes from the SPP read request events.

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

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

A
annie_wangli 已提交
990 991 992 993
| Name         | Type                         | Mandatory  | Description                                      |
| ------------ | --------------------------- | ---- | ---------------------------------------- |
| type         | string                      | Yes   | Event type. The value **sppRead** indicates an SPP read request event.              |
| clientSocket | number                      | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.                           |
A
Annie_wang 已提交
994
| callback     | Callback&lt;ArrayBuffer&gt; | No   | Callback for the SPP read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
995

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

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

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

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


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

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

Obtains a profile object.

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

**Parameters**

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

**Return value**

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

**Example**

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

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

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

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

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

**Parameters**

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

**Return value**

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

**Example**

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

A
annie_wangli 已提交
1068

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

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

Creates a **GattServer** instance.

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

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

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

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

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


### bluetooth.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

Creates a **GattClientDevice** instance.

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

**Parameters**

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

**Return value**

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

A
annie_wangli 已提交
1131 1132
| Type                 | Description                 |
| ------------------- | ------------------- |
A
annie_wangli 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
| 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 已提交
1154 1155 1156 1157
| 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 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

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

**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 已提交
1249 1250 1251
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered.       |
A
Annie_wang 已提交
1252
| callback | Callback&lt;Array&lt;[ScanResult](#scanresult)&gt;&gt; | No   | Callback for the **BLEDeviceFind** event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
A
annie_wangli 已提交
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

**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

**Return value**

A
Annie_wang 已提交
1286
| Type                 | Description           |
A
annie_wangli 已提交
1287
| ------------------- | ------------- |
A
Annie_wang 已提交
1288
| Array&lt;string&gt; | Addresses of the connected devices.|
A
annie_wangli 已提交
1289

A
Annie_wang 已提交
1290 1291 1292
**Example**

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

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

getDeviceState(device: string): ProfileConnectionState

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

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

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

**Parameters**

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

A
annie_wangli 已提交
1313
**Return value**
Z
zengyawen 已提交
1314

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

A
Annie_wang 已提交
1319 1320 1321
**Example**

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

## A2dpSourceProfile

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


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

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

**Return value**

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

**Example**

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


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

disconnect(device: string): boolean

Disconnects an A2DP connection.

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

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

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

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

**Parameters**

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

**Return value**

No value is returned.

**Example**

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


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

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

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

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

**Parameters**

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

**Return value**

No value is returned.

**Example**

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


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

getPlayingState(device: string): PlayingState

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

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

**Parameters**

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

**Return value**

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

A
annie_wangli 已提交
1472
**Example**
Z
zengyawen 已提交
1473

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


A
annie_wangli 已提交
1480
## HandsFreeAudioGatewayProfile
Z
zengyawen 已提交
1481

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


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

A
annie_wangli 已提交
1487
connect(device: string): boolean
Z
zengyawen 已提交
1488

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

A
annie_wangli 已提交
1491
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1492

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

A
annie_wangli 已提交
1495
**Parameters**
Z
zengyawen 已提交
1496

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

A
annie_wangli 已提交
1501
**Return value**
Z
zengyawen 已提交
1502

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

A
annie_wangli 已提交
1507
**Example**
Z
zengyawen 已提交
1508

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


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

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

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

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

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

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

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

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

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

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

A
annie_wangli 已提交
1540
```js
A
Annie_wang 已提交
1541 1542
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as
  bluetooth.HandsFreeAudioGatewayProfile;
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 invoked 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 1573
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as
  bluetooth.HandsFreeAudioGatewayProfile;
A
Annie_wang 已提交
1574
hfpAg.on('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1575 1576 1577
```


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

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

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

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

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

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

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

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

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

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

connect(device: string): boolean

Connects to the HidHost service of a device.

A
Annie_wang 已提交
1621
**System API**: This is a system API.
A
Annie_wang 已提交
1622

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

disconnect(device: string): boolean

Disconnects from the HidHost service of a device.

A
Annie_wang 已提交
1653
**System API**: This is a system API.
A
Annie_wang 已提交
1654

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

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

**Parameters**

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

**Return value**

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

**Example**

```js
A
Annie_wang 已提交
1674
let hidHostProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST) as bluetooth.HidHostProfile;
A
Annie_wang 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
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 已提交
1704
let hidHost = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST) as bluetooth.HidHostProfile;
A
Annie_wang 已提交
1705 1706 1707 1708 1709 1710
hidHost.on('connectionStateChange', onReceiveEvent);
```


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

A
Annie_wang 已提交
1711
off(type: "connectionStateChange", callback?: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
A
Annie_wang 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721

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.|
A
Annie_wang 已提交
1722
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | No  | Callback for the HidHost connection state change event.                                     |
A
Annie_wang 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733

**Return value**

No value is returned.

**Example**

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


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

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


A
Annie_wang 已提交
1745
### disconnect<sup>9+</sup><a name="PanP-disconnect"></a>
A
Annie_wang 已提交
1746 1747 1748 1749 1750

disconnect(device: string): boolean

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

A
Annie_wang 已提交
1751
**System API**: This is a system API.
A
Annie_wang 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771

**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
A
Annie_wang 已提交
1772
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
A
Annie_wang 已提交
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
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.|
A
Annie_wang 已提交
1790
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback invoked to return the PAN connection state change event.                              |
A
Annie_wang 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('pan state = '+ JSON.stringify(data));
}
A
Annie_wang 已提交
1802
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
A
Annie_wang 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
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.|
A
Annie_wang 已提交
1820
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | No  | Callback for the PAN connection state change event.                                     |
A
Annie_wang 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831

**Return value**

No value is returned.

**Example**

```js
function onReceiveEvent(data) {
    console.info('pan state = '+ JSON.stringify(data));
}
A
Annie_wang 已提交
1832
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
A
Annie_wang 已提交
1833 1834 1835 1836 1837 1838 1839
panProfile.on('connectionStateChange', onReceiveEvent);
panProfile.off('connectionStateChange', onReceiveEvent);
```


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

A
Annie_wang 已提交
1840
setTethering(enable: boolean): void
A
Annie_wang 已提交
1841 1842 1843

Sets tethering.

A
Annie_wang 已提交
1844
**System API**: This is a system API.
A
Annie_wang 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864

**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
A
Annie_wang 已提交
1865
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
A
Annie_wang 已提交
1866 1867 1868 1869 1870 1871 1872 1873
let ret = panProfile.setTethering(true);
```


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

isTetheringOn(): boolean

A
Annie_wang 已提交
1874
Obtains the tethering state.
A
Annie_wang 已提交
1875

A
Annie_wang 已提交
1876
**System API**: This is a system API.
A
Annie_wang 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888

**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
A
Annie_wang 已提交
1889
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
A
Annie_wang 已提交
1890 1891 1892 1893
let ret = panProfile.isTetheringOn();
```


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

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

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

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

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

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

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

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

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

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

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

**Example**

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


### addService

addService(service: GattService): boolean

Adds a service to this GATT server.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


### close

close(): void

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

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

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

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

```js
Z
zengyawen 已提交
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
let server = bluetooth.BLE.createGattServer();
server.close();
```


### notifyCharacteristicChanged

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

Notifies the connected client device when a characteristic value changes.

A
annie_wangli 已提交
2101
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2102

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

**Parameters**

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

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

A
Annie_wang 已提交
2114
| Type     | Description                      |
A
annie_wangli 已提交
2115
| ------- | ------------------------ |
A
Annie_wang 已提交
2116
| 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_wang 已提交
2158
| Type     | Description                        |
A
annie_wangli 已提交
2159
| ------- | -------------------------- |
A
Annie_wang 已提交
2160
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2161

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

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

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

**Parameters**
Z
zengyawen 已提交
2198

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

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

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

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

A
annie_wangli 已提交
2210
```js
Z
zengyawen 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
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};
A
Annie_wang 已提交
2221

Z
zengyawen 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
  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 已提交
2241 2242 2243
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

A
annie_wangli 已提交
2247 2248 2249
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicRead** indicates a characteristic read request event.   |
A
Annie_wang 已提交
2250
| callback | Callback&lt;[CharacteristicReadReq](#characteristicreadreq)&gt; | No   | Callback for the characteristic read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2251

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

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

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

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

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

**Parameters**

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

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

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

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

A
annie_wangli 已提交
2287
```js
Z
zengyawen 已提交
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
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;
A
Annie_wang 已提交
2298

Z
zengyawen 已提交
2299 2300
  cccValue[0] = value[0];
  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
A
Annie_wang 已提交
2301

Z
zengyawen 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
  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 已提交
2321 2322 2323
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

A
annie_wangli 已提交
2327 2328 2329
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicWrite** indicates a characteristic write request event.  |
A
Annie_wang 已提交
2330
| callback | Callback&lt;[CharacteristicWriteReq](#characteristicwritereq)&gt; | No   | Callback for the characteristic write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2331

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

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

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

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

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

**Parameters**
Z
zengyawen 已提交
2355

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

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

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

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

A
annie_wangli 已提交
2367
```js
Z
zengyawen 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
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};
A
Annie_wang 已提交
2378

Z
zengyawen 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
  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 已提交
2398 2399 2400
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

A
annie_wangli 已提交
2404 2405 2406
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorRead** indicates a descriptor read request event.       |
A
Annie_wang 已提交
2407
| callback | Callback&lt;[DescriptorReadReq](#descriptorreadreq)&gt; | No   | Callback for the descriptor read request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2408

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

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

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

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

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

**Parameters**

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

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

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

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

A
annie_wangli 已提交
2444
```js
Z
zengyawen 已提交
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
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};
A
Annie_wang 已提交
2458

Z
zengyawen 已提交
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
  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 已提交
2478 2479 2480
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

A
annie_wangli 已提交
2484 2485 2486
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **descriptorWrite** indicates a descriptor write request event.      |
A
Annie_wang 已提交
2487
| callback | Callback&lt;[DescriptorWriteReq](#descriptorwritereq)&gt; | No   | Callback for the descriptor write request event. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
Z
zengyawen 已提交
2488

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

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

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

A
annie_wangli 已提交
2495
```js
Z
zengyawen 已提交
2496 2497 2498 2499
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("descriptorWrite");
```

A
Annie_wang 已提交
2500

Z
zengyawen 已提交
2501 2502 2503 2504 2505 2506
### 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
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectStateChange** indicates a BLE connection state change event.|
A
Annie_wang 已提交
2550
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | No   | Callback for the BLE connection state change event. 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
**Return value**

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

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

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


### readCharacteristicValue

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

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

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

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

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

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

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

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

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

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

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

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

A
annie_wangli 已提交
2777
**Parameters**
Z
zengyawen 已提交
2778

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

A
annie_wangli 已提交
2783
**Return value**
Z
zengyawen 已提交
2784

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

A
annie_wangli 已提交
2789
**Example**
Z
zengyawen 已提交
2790

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

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

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

A
annie_wangli 已提交
2823 2824
**Parameters**

A
annie_wangli 已提交
2825 2826 2827
| Name       | Type                                      | Mandatory  | Description                     |
| ---------- | ---------------------------------------- | ---- | ----------------------- |
| descriptor | [BLEDescriptor](#bledescriptor)          | Yes   | Descriptor to read.               |
A
Annie_wang 已提交
2828
| callback   | AsyncCallback&lt;[BLEDescriptor](#bledescriptor)&gt; | Yes   | Callback invoked to return the descriptor read.|
Z
zengyawen 已提交
2829

A
annie_wangli 已提交
2830
**Return value**
Z
zengyawen 已提交
2831

A
annie_wangli 已提交
2832
No value is returned.
Z
zengyawen 已提交
2833

A
annie_wangli 已提交
2834
**Example**
Z
zengyawen 已提交
2835

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

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

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

A
annie_wangli 已提交
2867
**Parameters**
Z
zengyawen 已提交
2868

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

A
annie_wangli 已提交
2873
**Return value**
Z
zengyawen 已提交
2874

A
Annie_wang 已提交
2875
| Type                                      | Description                        |
A
annie_wangli 已提交
2876
| ---------------------------------------- | -------------------------- |
A
Annie_wang 已提交
2877
| Promise&lt;[BLEDescriptor](#bledescriptor)&gt; | Promise used to return the descriptor read.|
Z
zengyawen 已提交
2878

A
annie_wangli 已提交
2879
**Example**
Z
zengyawen 已提交
2880

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

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

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

A
annie_wangli 已提交
2903 2904
**Parameters**

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

A
annie_wangli 已提交
2909
**Return value**
Z
zengyawen 已提交
2910

A
annie_wangli 已提交
2911 2912
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2913
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2914

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

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

A
annie_wangli 已提交
2953
**Parameters**
Z
zengyawen 已提交
2954

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

A
annie_wangli 已提交
2959
**Return value**
Z
zengyawen 已提交
2960

A
annie_wangli 已提交
2961 2962
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2963
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2964

A
annie_wangli 已提交
2965
**Example**
Z
zengyawen 已提交
2966

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

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

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

A
annie_wangli 已提交
2994
**Parameters**
Z
zengyawen 已提交
2995

A
annie_wangli 已提交
2996 2997 2998
| Name | Type    | Mandatory  | Description            |
| ---- | ------ | ---- | -------------- |
| mtu  | number | Yes   | MTU to set, which ranges from 22 to 512 bytes.|
Z
zengyawen 已提交
2999

A
annie_wangli 已提交
3000
**Return value**
Z
zengyawen 已提交
3001

A
annie_wangli 已提交
3002 3003
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
3004
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
3005

A
annie_wangli 已提交
3006
**Example**
Z
zengyawen 已提交
3007

A
annie_wangli 已提交
3008 3009
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3010 3011 3012 3013 3014 3015 3016 3017
device.setBLEMtuSize(128);
```


### setNotifyCharacteristicChanged

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

A
annie_wangli 已提交
3018 3019 3020 3021 3022
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 已提交
3023

A
annie_wangli 已提交
3024
**Parameters**
Z
zengyawen 已提交
3025

A
annie_wangli 已提交
3026 3027 3028 3029
| 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 已提交
3030

A
annie_wangli 已提交
3031
**Return value**
Z
zengyawen 已提交
3032

A
annie_wangli 已提交
3033 3034
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
3035
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
3036

A
annie_wangli 已提交
3037
**Example**
Z
zengyawen 已提交
3038

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


### 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 已提交
3063 3064 3065 3066 3067
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
3068

A
annie_wangli 已提交
3069 3070 3071 3072
| 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 已提交
3073

A
annie_wangli 已提交
3074
**Return value**
Z
zengyawen 已提交
3075

A
annie_wangli 已提交
3076
No value is returned.
Z
zengyawen 已提交
3077

A
annie_wangli 已提交
3078
**Example**
Z
zengyawen 已提交
3079

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

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

A
annie_wangli 已提交
3101 3102
**Parameters**

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

A
annie_wangli 已提交
3108
**Return value**
Z
zengyawen 已提交
3109

A
annie_wangli 已提交
3110
No value is returned.
Z
zengyawen 已提交
3111

A
annie_wangli 已提交
3112
**Example**
Z
zengyawen 已提交
3113

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

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

**Parameters**

A
annie_wangli 已提交
3132 3133 3134 3135
| 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 已提交
3136

A
annie_wangli 已提交
3137
**Return value**
Z
zengyawen 已提交
3138

A
annie_wangli 已提交
3139
No value is returned.
Z
zengyawen 已提交
3140

A
annie_wangli 已提交
3141
**Example**
Z
zengyawen 已提交
3142

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

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

A
annie_wangli 已提交
3163 3164
**Parameters**

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

A
annie_wangli 已提交
3170
**Return value**
Z
zengyawen 已提交
3171

A
annie_wangli 已提交
3172
No value is returned.
Z
zengyawen 已提交
3173

A
annie_wangli 已提交
3174
**Example**
Z
zengyawen 已提交
3175

A
annie_wangli 已提交
3176 3177
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3178 3179 3180 3181 3182 3183 3184 3185
device.off('BLEConnectionStateChange');
```


### getDeviceName

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

A
Annie_wang 已提交
3186
Obtains the name of the remote BLE device. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3187 3188 3189 3190

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

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

A
annie_wangli 已提交
3192
**Parameters**
Z
zengyawen 已提交
3193

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

A
annie_wangli 已提交
3198
**Return value**
Z
zengyawen 已提交
3199

A
annie_wangli 已提交
3200
No value is returned.
Z
zengyawen 已提交
3201

A
annie_wangli 已提交
3202
**Example**
Z
zengyawen 已提交
3203

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

A
annie_wangli 已提交
3220
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
3221

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

**Return value**

A
annie_wangli 已提交
3226 3227
| Type                   | Description                                |
| --------------------- | ---------------------------------- |
A
annie_wangli 已提交
3228
| Promise&lt;string&gt; | Promise used to return the remote BLE device name.|
Z
zengyawen 已提交
3229

A
annie_wangli 已提交
3230
**Example**
Z
zengyawen 已提交
3231

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

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

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

A
annie_wangli 已提交
3252 3253
**Parameters**

A
annie_wangli 已提交
3254 3255 3256
| Name     | Type                         | Mandatory  | Description                            |
| -------- | --------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the RSSI, in dBm.|
Z
zengyawen 已提交
3257

A
annie_wangli 已提交
3258
**Return value**
Z
zengyawen 已提交
3259

A
annie_wangli 已提交
3260
No value is returned.
Z
zengyawen 已提交
3261

A
annie_wangli 已提交
3262
**Example**
Z
zengyawen 已提交
3263

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

A
annie_wangli 已提交
3281
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
3282

A
annie_wangli 已提交
3283 3284 3285 3286
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
3287 3288
| Type                   | Description                               |
| --------------------- | --------------------------------- |
A
annie_wangli 已提交
3289
| Promise&lt;number&gt; | Promise used to return the RSSI, in dBm.|
Z
zengyawen 已提交
3290

A
annie_wangli 已提交
3291
**Example**
Z
zengyawen 已提交
3292

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

A
annie_wangli 已提交
3301
## ScanMode<sup>8+</sup><a name="ScanMode"></a>
Z
zengyawen 已提交
3302 3303 3304

Enumerates the scan modes.

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

A
Annie_wang 已提交
3307
| Name                                      | Value | Description             |
A
annie_wangli 已提交
3308 3309 3310 3311 3312 3313 3314
| ---------------------------------------- | ---- | --------------- |
| 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 已提交
3315

A
annie_wangli 已提交
3316
## BondState<sup>8+</sup><a name="BondState"></a>
Z
zengyawen 已提交
3317 3318 3319

Enumerates the pairing states.

A
annie_wangli 已提交
3320 3321
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3322
| Name                | Value | Description    |
A
annie_wangli 已提交
3323 3324 3325 3326
| ------------------ | ---- | ------ |
| BOND_STATE_INVALID | 0    | Invalid pairing.|
| BOND_STATE_BONDING | 1    | Pairing. |
| BOND_STATE_BONDED  | 2    | Paired.  |
Z
zengyawen 已提交
3327 3328


A
annie_wangli 已提交
3329
## SppOption<sup>8+</sup><a name="SppOption"></a>
Z
zengyawen 已提交
3330 3331 3332

Defines the SPP configuration parameters.

A
annie_wangli 已提交
3333 3334
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3335 3336 3337 3338 3339
| 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 已提交
3340 3341


A
annie_wangli 已提交
3342
## SppType<sup>8+</sup><a name="SppType"></a>
Z
zengyawen 已提交
3343 3344 3345

Enumerates the SPP link types.

A
annie_wangli 已提交
3346 3347
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3348
| Name        | Value | Description           |
A
annie_wangli 已提交
3349 3350
| ---------- | ---- | ------------- |
| SPP_RFCOMM | 0    | Radio frequency communication (RFCOMM) link type.|
Z
zengyawen 已提交
3351 3352 3353 3354 3355 3356


## GattService

Defines the GATT service API parameters.

A
annie_wangli 已提交
3357 3358
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3359 3360 3361 3362 3363 3364
| 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 已提交
3365 3366 3367 3368 3369 3370


## BLECharacteristic

Defines the characteristic API parameters.

A
annie_wangli 已提交
3371 3372
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3373 3374 3375 3376 3377 3378
| 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 已提交
3379 3380 3381 3382 3383 3384


## BLEDescriptor

Defines the descriptor API parameters.

A
annie_wangli 已提交
3385 3386
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3387 3388 3389 3390 3391 3392
| 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 已提交
3393 3394 3395 3396 3397 3398


## NotifyCharacteristic

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

A
annie_wangli 已提交
3399 3400
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3401 3402 3403 3404 3405 3406
| 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 已提交
3407 3408 3409 3410 3411 3412


## CharacteristicReadReq

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

A
annie_wangli 已提交
3413 3414
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3415 3416 3417 3418 3419 3420 3421
| 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 已提交
3422 3423 3424 3425 3426 3427


## CharacteristicWriteReq

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

A
annie_wangli 已提交
3428 3429
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3430 3431 3432 3433 3434 3435 3436 3437
| 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 已提交
3438

A
annie_wangli 已提交
3439

Z
zengyawen 已提交
3440 3441 3442 3443
## DescriptorReadReq

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

A
annie_wangli 已提交
3444 3445
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3446 3447 3448 3449 3450 3451 3452 3453
| 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 已提交
3454 3455 3456 3457 3458 3459


## DescriptorWriteReq

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

A
annie_wangli 已提交
3460 3461
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
| 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 已提交
3473 3474 3475 3476 3477 3478


## ServerResponse

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

A
annie_wangli 已提交
3479 3480
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3481 3482 3483 3484 3485 3486 3487
| 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 已提交
3488 3489 3490 3491 3492 3493


## BLEConnectChangedState

Defines the parameters of **BLEConnectChangedState**.

A
annie_wangli 已提交
3494 3495
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3496 3497 3498 3499
| 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 已提交
3500 3501 3502 3503 3504 3505


## ProfileConnectionState

Enumerates the profile connection states.

A
annie_wangli 已提交
3506 3507
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3508
| Name                 | Value | Description            |
A
annie_wangli 已提交
3509 3510 3511 3512 3513
| ------------------- | ---- | -------------- |
| STATE_DISCONNECTED  | 0    | Disconnected. |
| STATE_CONNECTING    | 1    | Connecting.|
| STATE_CONNECTED     | 2    | Connected. |
| STATE_DISCONNECTING | 3    | Disconnecting.|
Z
zengyawen 已提交
3514 3515 3516 3517 3518 3519


## ScanFilter

Defines the scan filter parameters.

A
annie_wangli 已提交
3520 3521
**System capability**: SystemCapability.Communication.Bluetooth.Core

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


## ScanOptions

Defines the scan configuration parameters.

A
annie_wangli 已提交
3541 3542
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3543 3544 3545 3546 3547
| 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 已提交
3548 3549 3550 3551 3552 3553


## ScanDuty

Enumerates the scan duty options.

A
annie_wangli 已提交
3554 3555
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3556
| Name                   | Value | Description          |
A
annie_wangli 已提交
3557 3558 3559 3560
| --------------------- | ---- | ------------ |
| 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 已提交
3561 3562 3563 3564 3565 3566


## MatchMode

Enumerates the hardware match modes of BLE scan filters.

A
annie_wangli 已提交
3567 3568
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3569
| Name                   | Value | Description                                      |
A
annie_wangli 已提交
3570 3571 3572
| --------------------- | ---- | ---------------------------------------- |
| 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 已提交
3573 3574 3575 3576 3577 3578


## ScanResult

Defines the scan result.

A
annie_wangli 已提交
3579 3580
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3581 3582 3583 3584 3585
| 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 已提交
3586 3587 3588 3589 3590 3591


## BluetoothState

Enumerates the Bluetooth states.

A
annie_wangli 已提交
3592 3593
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
Annie_wang 已提交
3594
| Name                   | Value | Description                |
A
annie_wangli 已提交
3595 3596 3597 3598 3599 3600 3601 3602
| --------------------- | ---- | ------------------ |
| 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 已提交
3603 3604 3605 3606 3607 3608


## AdvertiseSetting

Defines the BLE advertising parameters.

A
annie_wangli 已提交
3609 3610
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3611 3612
| Name         | Type   | Readable  | Writable  | Description                                      |
| ----------- | ------- | ---- | ---- | ---------------------------------------- |
A
Annie_wang 已提交
3613
| 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 已提交
3614 3615
| 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 已提交
3616 3617 3618 3619 3620 3621


## AdvertiseData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3622 3623
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3624 3625 3626 3627 3628
| 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 已提交
3629 3630 3631 3632 3633 3634


## ManufactureData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3635 3636
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3637 3638
| Name              | Type               | Readable  | Writable  | Description                |
| ---------------- | ------------------- | ---- | ---- | ------------------ |
A
Annie_wang 已提交
3639
| manufactureId    | number  | Yes   | Yes   | Manufacturer ID allocated by the Bluetooth SIG.|
A
annie_wangli 已提交
3640
| manufactureValue | ArrayBuffer         | Yes   | Yes   | Manufacturer data.    |
Z
zengyawen 已提交
3641 3642 3643 3644 3645 3646


## ServiceData

Defines the service data contained in an advertisement packet.

A
annie_wangli 已提交
3647 3648
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3649 3650 3651 3652
| Name          | Type       | Readable  | Writable  | Description        |
| ------------ | ----------- | ---- | ---- | ---------- |
| serviceUuid  | string      | Yes   | Yes   | Service UUID.|
| serviceValue | ArrayBuffer | Yes   | Yes   | Service data.   |
Z
zengyawen 已提交
3653 3654


A
annie_wangli 已提交
3655
## PinRequiredParam<sup>8+</sup><a name="PinRequiredParam"></a>
Z
zengyawen 已提交
3656 3657 3658

Defines the pairing request parameters.

A
annie_wangli 已提交
3659 3660
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3661 3662 3663 3664
| 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 已提交
3665 3666


A
Annie_wang 已提交
3667 3668
## BondStateParam<sup>8+</sup><a name="BondStateParam"></a>

A
Annie_wang 已提交
3669
Defines the pairing state parameters.
A
Annie_wang 已提交
3670 3671 3672 3673 3674

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

| Name      | Type  | Readable  | Writable  | Description         |
| -------- | ------ | ---- | ---- | ----------- |
A
Annie_wang 已提交
3675
| deviceId | string      | Yes   | No   | ID of the device to pair.|
A
Annie_wang 已提交
3676 3677 3678
| state    | BondState   | Yes   | No   | State of the device.|


A
annie_wangli 已提交
3679 3680 3681 3682 3683 3684
## StateChangeParam<sup>8+</sup><a name="StateChangeParam"></a>

Defines the profile state change parameters.

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

A
annie_wangli 已提交
3685 3686 3687 3688
| 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 已提交
3689 3690 3691 3692 3693 3694 3695 3696


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

Defines the class of a Bluetooth device.

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

A
annie_wangli 已提交
3697 3698 3699 3700 3701
| 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 已提交
3702 3703 3704 3705 3706 3707 3708 3709 3710



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

Enumerates the major classes of Bluetooth devices.

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

A
Annie_wang 已提交
3711
| Name                 | Value   | Description        |
A
annie_wangli 已提交
3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723
| ------------------- | ------ | ---------- |
| 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 已提交
3724 3725 3726 3727 3728 3729 3730 3731


## 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_wang 已提交
3732
| Name                                      | Value   | Description             |
A
annie_wangli 已提交
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 3812 3813 3814 3815 3816 3817 3818 3819
| ---------------------------------------- | ------ | --------------- |
| 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 已提交
3820 3821 3822 3823 3824 3825 3826 3827


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

Enumerates the A2DP playing states.

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

A
Annie_wang 已提交
3828
| Name               | Value   | Description     |
A
annie_wangli 已提交
3829 3830 3831
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | Not playing. |
| STATE_PLAYING     | 0x0001 | Playing.|
A
annie_wangli 已提交
3832 3833 3834 3835


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

A
Annie_wang 已提交
3836
Enumerates the Bluetooth profiles. API version 9 is added with **PROFILE_HID_HOST** and **PROFILE_PAN_NETWORK**.
A
annie_wangli 已提交
3837 3838 3839

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

A
Annie_wang 已提交
3840
| Name                              | Value   | Description             |
A
annie_wangli 已提交
3841
| -------------------------------- | ------ | --------------- |
A
Annie_wang 已提交
3842 3843 3844 3845
| PROFILE_A2DP_SOURCE              | 1 | A2DP profile.|
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 4 | HFP profile. |
| PROFILE_HID_HOST<sup>9+</sup> | 6 | Human Interface Device (HID) profile. |
| PROFILE_PAN_NETWORK<sup>9+</sup> | 7 | PAN profile. |