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

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

Provides Classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising.
Z
zengyawen 已提交
7 8 9 10


## Modules to Import

P
Peter_1988 已提交
11
```js
Z
zengyawen 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import bluetooth from '@ohos.bluetooth';
```


## Required Permissions

ohos.permission.USE_BLUETOOTH

ohos.permission.MANAGE_BLUETOOTH

ohos.permission.DISCOVER_BLUETOOTH

ohos.permission.LOCATION


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

enableBluetooth(): boolean

Enables Bluetooth.

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

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

**Return value**

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

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

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


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

disableBluetooth(): boolean

Disables Bluetooth.

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

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

**Return value**

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

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

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


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

getLocalName(): string

Obtains the name of the local Bluetooth device.

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

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

**Return value**

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

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

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


## bluetooth.getState

getState(): BluetoothState

Obtains the Bluetooth state.

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

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

**Return value**

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

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

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


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

Obtains the profile connection state of this Bluetooth device.

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

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

**Return value**

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

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

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


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

setLocalName(name: string): boolean

Sets the name of the local Bluetooth device.

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

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

**Parameters**
Z
zengyawen 已提交
153

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

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

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

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

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


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

Initiates Bluetooth pairing.

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

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

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

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

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

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

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

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


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

getProfileConnState(profileId: ProfileId): ProfileConnectionState

Obtains the connection status of a profile.

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

cancelPairedDevice(deviceId: string): boolean

Cancels a paired remote device.

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

getRemoteDeviceName(deviceId: string): string

Obtains the name of the remote Bluetooth device.

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

getRemoteDeviceClass(deviceId: string): DeviceClass

Obtains the type of the remote Bluetooth device.

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

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

**Parameters**

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

**Return value**

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

**Example**

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


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

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

Obtains the Bluetooth pairing list.

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

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

**Return value**

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

getBluetoothScanMode(): ScanMode

Obtains the Bluetooth scan mode.

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

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

**Return value**

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

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

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


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

startBluetoothDiscovery(): boolean

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

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

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

**Return value**

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

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

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


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

stopBluetoothDiscovery(): boolean

Stops Bluetooth scan.

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

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

**Return value**

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

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

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


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

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

Sets the device pairing confirmation.

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

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

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

A
annie_wangli 已提交
457 458
| Name   | Type     | Mandatory  | Description                              |
| ------ | ------- | ---- | -------------------------------- |
A
Annie_wang 已提交
459
| device | string  | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
460
| accept | boolean | Yes   | Whether to accept the pairing request. The value **true** means to accept the pairing request, and the value **false** means the opposite.       |
Z
zengyawen 已提交
461

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

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

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

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


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

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

Subscribes to the Bluetooth device discovery events.

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

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

**Parameters**

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

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

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

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

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


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

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

Unsubscribes from the Bluetooth device discovery events.

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

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

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

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

**Parameters**

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

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

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

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

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


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

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

Subscribes to the Bluetooth pairing state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Unsubscribes from the Bluetooth pairing state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Subscribes to the Bluetooth connection state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Unsubscribes from the Bluetooth connection state change events.

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

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

**Parameters**

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

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

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

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

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


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

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

Creates a server listening socket.

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

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

**Parameters**

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

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

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

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


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

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

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

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

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

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

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

A
annie_wangli 已提交
784
```js
A
Annie_wang 已提交
785 786 787 788 789 790 791 792
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 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805
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 已提交
806
## bluetooth.sppConnect<sup>8+</sup><a name="sppConnect"></a>
Z
zengyawen 已提交
807 808 809

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

A
annie_wangli 已提交
810 811 812
Initiates an SPP connection to a remote device from the client.

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

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

A
annie_wangli 已提交
816 817
**Parameters**

A
annie_wangli 已提交
818 819 820 821 822
| 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 已提交
823

A
annie_wangli 已提交
824
**Example**
Z
zengyawen 已提交
825

A
annie_wangli 已提交
826
```js
Z
zengyawen 已提交
827 828 829 830 831 832 833 834 835 836
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 已提交
837
bluetooth.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
Z
zengyawen 已提交
838 839 840
```


A
annie_wangli 已提交
841
## bluetooth.sppCloseServerSocket<sup>8+</sup><a name="sppCloseServerSocket"></a>
Z
zengyawen 已提交
842 843 844 845 846

sppCloseServerSocket(socket: number): void

Closes the listening socket of the server.

A
annie_wangli 已提交
847 848 849
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
850

A
annie_wangli 已提交
851 852 853
| Name   | Type    | Mandatory  | Description             |
| ------ | ------ | ---- | --------------- |
| socket | number | Yes   | ID of the listening socket on the server. The ID is obtained by **sppListen**.|
Z
zengyawen 已提交
854

A
annie_wangli 已提交
855
**Example**
Z
zengyawen 已提交
856

A
annie_wangli 已提交
857
```js
A
Annie_wang 已提交
858 859 860 861 862 863 864 865
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 已提交
866 867 868 869
bluetooth.sppCloseServerSocket(serverNumber);
```


A
annie_wangli 已提交
870
## bluetooth.sppCloseClientSocket<sup>8+</sup><a name="sppCloseClientSocket"></a>
Z
zengyawen 已提交
871 872 873 874 875

sppCloseClientSocket(socket: number): void

Closes the client socket.

A
annie_wangli 已提交
876 877 878
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
879

A
annie_wangli 已提交
880 881 882 883
| Name   | Type    | Mandatory  | Description           |
| ------ | ------ | ---- | ------------- |
| Name   | Type    | Mandatory  | Description           |
| socket | number | Yes   | Client socket ID, which is obtained by **sppAccept** or **sppConnect**.|
Z
zengyawen 已提交
884

A
annie_wangli 已提交
885
**Example**
Z
zengyawen 已提交
886

A
annie_wangli 已提交
887
```js
A
Annie_wang 已提交
888 889 890 891 892 893 894 895 896
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 已提交
897 898 899 900
bluetooth.sppCloseClientSocket(clientNumber);
```


A
annie_wangli 已提交
901
## bluetooth.sppWrite<sup>8+</sup><a name="sppWrite"></a>
Z
zengyawen 已提交
902 903 904

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

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

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

A
annie_wangli 已提交
909 910
**Parameters**

A
annie_wangli 已提交
911 912 913 914
| 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 已提交
915

A
annie_wangli 已提交
916
**Return value**
Z
zengyawen 已提交
917

A
annie_wangli 已提交
918 919 920
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
921

A
annie_wangli 已提交
922
**Example**
Z
zengyawen 已提交
923

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

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

Subscribes to the SPP read request events.

A
annie_wangli 已提交
952 953 954
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**
Z
zengyawen 已提交
955

A
annie_wangli 已提交
956 957 958 959 960
| 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 已提交
961

A
annie_wangli 已提交
962
**Return value**
Z
zengyawen 已提交
963

A
annie_wangli 已提交
964
No value is returned.
Z
zengyawen 已提交
965

A
annie_wangli 已提交
966
**Example**
Z
zengyawen 已提交
967

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


A
annie_wangli 已提交
986
## bluetooth.off('sppRead')<sup>8+</sup>
Z
zengyawen 已提交
987 988 989 990 991

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

Unsubscribes from the SPP read request events.

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

A
annie_wangli 已提交
994 995
**Parameters**

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

A
annie_wangli 已提交
1002
**Return value**
Z
zengyawen 已提交
1003

A
annie_wangli 已提交
1004
No value is returned.
Z
zengyawen 已提交
1005

A
annie_wangli 已提交
1006
**Example**
Z
zengyawen 已提交
1007

A
annie_wangli 已提交
1008
```js
A
Annie_wang 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017
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 已提交
1018 1019 1020 1021
bluetooth.off('sppRead', clientNumber);
```


A
annie_wangli 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
## bluetooth.getProfile<sup>8+</sup><a name="getProfile"></a>

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

Obtains a profile object.

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

**Parameters**

A
annie_wangli 已提交
1032 1033
| Name      | Type       | Mandatory  | Description                                   |
| --------- | --------- | ---- | ------------------------------------- |
A
annie_wangli 已提交
1034
| ProfileId | profileId | Yes   | ID of the profile to obtain, for example, **PROFILE_A2DP_SOURCE**.|
A
annie_wangli 已提交
1035 1036 1037

**Return value**

A
annie_wangli 已提交
1038 1039 1040
| Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- |
| A2dpSourceProfile or HandsFreeAudioGatewayProfile| Profile object obtained. Only **A2dpSourceProfile** and **HandsFreeAudioGatewayProfile** are supported.|
A
annie_wangli 已提交
1041 1042 1043 1044

**Example**

```js
A
annie_wangli 已提交
1045
let a2dpSrc = bluetooth.getProfile(PROFILE_A2DP_SOURCE);
A
annie_wangli 已提交
1046 1047 1048
```


Z
zengyawen 已提交
1049 1050 1051 1052 1053 1054 1055 1056
## bluetooth.BLE

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

Creates a **GattServer** instance.

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

A
annie_wangli 已提交
1059 1060
**Return value**

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

A
annie_wangli 已提交
1065
**Example**
Z
zengyawen 已提交
1066

A
annie_wangli 已提交
1067
```js
Z
zengyawen 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
let gattServer = bluetooth.BLE.createGattServer();
```


### bluetooth.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

Creates a **GattClientDevice** instance.

A
annie_wangli 已提交
1078 1079 1080 1081
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1082 1083 1084
| Name     | Type    | Mandatory  | Description                                  |
| -------- | ------ | ---- | ------------------------------------ |
| deviceId | string | Yes   | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.|
A
annie_wangli 已提交
1085 1086 1087

**Return value**

A
annie_wangli 已提交
1088 1089
| Type                                   | Description                                  |
| ------------------------------------- | ------------------------------------ |
A
annie_wangli 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
| [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 已提交
1110

A
annie_wangli 已提交
1111 1112
| Type                 | Description                 |
| ------------------- | ------------------- |
A
annie_wangli 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
| 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 已提交
1134 1135 1136 1137
| 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 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

**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 已提交
1198 1199 1200 1201
| 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 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

**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 已提交
1229 1230 1231 1232
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **BLEDeviceFind** indicates an event reported when a BLE device is discovered.       |
| callback | Callback&lt;Array&lt;[ScanResult](#scanresult)&gt;&gt; | No   | Callback used to report the discovered devices. If this parameter is not set, this method unsubscribes from all callbacks corresponding to **type**.|
A
annie_wangli 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

**Return value**

No value is returned.

**Example**

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


## BaseProfile

Provides the profile base class.


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

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

Obtains the connected devices.

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

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

**Parameters**

No value is returned.

**Return value**

A
annie_wangli 已提交
1270 1271 1272
|                     |               |
| ------------------- | ------------- |
| Type                  | Description            |
A
Annie_wang 已提交
1273
| Array&lt;string&gt; | Addresses of the connected devices. |
A
annie_wangli 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287


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

getDeviceState(device: string): ProfileConnectionState

Obtains the connection status of the profile.

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

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

**Parameters**

A
annie_wangli 已提交
1288 1289
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
Z
zengyawen 已提交
1290
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1291
**Return value**
Z
zengyawen 已提交
1292

Z
zengyawen 已提交
1293 1294 1295
|                                                   |                         |
| ------------------------------------------------- | ----------------------- |
| Type                                              | Description                    |
A
Annie_wang 已提交
1296
| [ProfileConnectionState](#profileconnectionState) | Profile connection state obtained. |
A
annie_wangli 已提交
1297 1298 1299 1300 1301 1302 1303


## A2dpSourceProfile

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


A
Annie_wang 已提交
1304
### connect<sup>8+</sup><a name="a2dp-connect"></a>
A
annie_wangli 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

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 已提交
1316 1317
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1318
| device | string | Yes   | Address of the remote device to connect.|
A
annie_wangli 已提交
1319 1320 1321 1322
|

**Return value**

A
annie_wangli 已提交
1323 1324
|         |                     |
| ------- | ------------------- |
A
annie_wangli 已提交
1325 1326 1327 1328 1329 1330
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |

**Example**

```js
A
Annie_wang 已提交
1331
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE)
A
annie_wangli 已提交
1332
let ret = a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1333 1334 1335
```


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

disconnect(device: string): boolean

Disconnects an A2DP connection.

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

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

**Parameters**

A
annie_wangli 已提交
1348 1349
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1350
| device | string | Yes   | Address of the remote device to disconnect.|
A
annie_wangli 已提交
1351 1352 1353 1354
|

**Return value**

A
annie_wangli 已提交
1355 1356
|         |                     |
| ------- | ------------------- |
A
annie_wangli 已提交
1357 1358 1359 1360 1361 1362
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |

**Example**

```js
A
Annie_wang 已提交
1363
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Z
zengyawen 已提交
1364
let ret = a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
```


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

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

Subscribes to the A2DP connection status change events.

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

**Parameters**

A
annie_wangli 已提交
1378 1379 1380 1381
| 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 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406

**Return value**

No value is returned.

**Example**

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


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

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

Unsubscribes from the A2DP connection status change events.

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

**Parameters**

A
annie_wangli 已提交
1407 1408 1409
| Name     | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **connectionStateChange** indicates an A2DP connection state change event.|
A
annie_wangli 已提交
1410
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | Yes   | Callback used to return the A2DP connection state change event.                              |
A
annie_wangli 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

**Return value**

No value is returned.

**Example**

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


### getPlayingState

getPlayingState(device: string): PlayingState

Obtains the playing status of a device.

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

**Parameters**

A
annie_wangli 已提交
1436 1437
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1438
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1439 1440 1441

**Return value**

A
annie_wangli 已提交
1442 1443
|                               |            |
| ----------------------------- | ---------- |
A
annie_wangli 已提交
1444 1445
| Type | Description |
| [PlayingState](#PlayingState) | Playing status obtained. |
Z
zengyawen 已提交
1446

A
annie_wangli 已提交
1447
**Example**
Z
zengyawen 已提交
1448

A
annie_wangli 已提交
1449
```js
A
Annie_wang 已提交
1450
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
A
annie_wangli 已提交
1451
let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1452 1453 1454
```


A
annie_wangli 已提交
1455
## HandsFreeAudioGatewayProfile
Z
zengyawen 已提交
1456

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


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

A
annie_wangli 已提交
1462
connect(device: string): boolean
Z
zengyawen 已提交
1463

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

A
annie_wangli 已提交
1466
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1467

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

A
annie_wangli 已提交
1470
**Parameters**
Z
zengyawen 已提交
1471

A
annie_wangli 已提交
1472 1473
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1474
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1475
|
Z
zengyawen 已提交
1476

A
annie_wangli 已提交
1477
**Return value**
Z
zengyawen 已提交
1478

A
annie_wangli 已提交
1479 1480
|         |                     |
| ------- | ------------------- |
A
annie_wangli 已提交
1481 1482
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1483

A
annie_wangli 已提交
1484
**Example**
Z
zengyawen 已提交
1485

A
annie_wangli 已提交
1486
```js
A
Annie_wang 已提交
1487
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
annie_wangli 已提交
1488
let ret = hfpAg.connect('XX:XX:XX:XX:XX:XX');
A
annie_wangli 已提交
1489
```
Z
zengyawen 已提交
1490 1491


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

A
annie_wangli 已提交
1494
disconnect(device: string): boolean
Z
zengyawen 已提交
1495

A
annie_wangli 已提交
1496
Disconnects the HFP connection of a device.
Z
zengyawen 已提交
1497

A
annie_wangli 已提交
1498
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1499

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

A
annie_wangli 已提交
1502 1503
**Parameters**

A
annie_wangli 已提交
1504 1505
| Name   | Type    | Mandatory  | Description     |
| ------ | ------ | ---- | ------- |
A
annie_wangli 已提交
1506
| device | string | Yes   | Address of the target device.|
A
annie_wangli 已提交
1507
|
Z
zengyawen 已提交
1508

A
annie_wangli 已提交
1509
**Return value**
Z
zengyawen 已提交
1510

A
annie_wangli 已提交
1511 1512 1513
| Type     | Description                 |
| ------- | ------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
1514

A
annie_wangli 已提交
1515
**Example**
Z
zengyawen 已提交
1516

A
annie_wangli 已提交
1517
```js
A
Annie_wang 已提交
1518
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
A
annie_wangli 已提交
1519
let ret = hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1520 1521 1522
```


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

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

A
annie_wangli 已提交
1527 1528 1529
Subscribes to the HFP connection status change events.

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

A
annie_wangli 已提交
1531
**Parameters**
Z
zengyawen 已提交
1532

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

A
annie_wangli 已提交
1538
**Return value**
Z
zengyawen 已提交
1539

A
annie_wangli 已提交
1540
No value is returned.
Z
zengyawen 已提交
1541

A
annie_wangli 已提交
1542
**Example**
Z
zengyawen 已提交
1543

A
annie_wangli 已提交
1544
```js
Z
zengyawen 已提交
1545
function onReceiveEvent(data) {
A
annie_wangli 已提交
1546
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1547
}
A
annie_wangli 已提交
1548
HandsFreeAudioGatewayProfile.on('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1549 1550 1551
```


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

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

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

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

A
annie_wangli 已提交
1560 1561
**Parameters**

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

A
annie_wangli 已提交
1567
**Return value**
Z
zengyawen 已提交
1568

A
annie_wangli 已提交
1569
No value is returned.
Z
zengyawen 已提交
1570

A
annie_wangli 已提交
1571
**Example**
Z
zengyawen 已提交
1572

A
annie_wangli 已提交
1573
```js
Z
zengyawen 已提交
1574
function onReceiveEvent(data) {
A
annie_wangli 已提交
1575
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1576
}
A
annie_wangli 已提交
1577
HandsFreeAudioGatewayProfile.off('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
```


## 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 已提交
1592 1593 1594
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH

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

A
annie_wangli 已提交
1596 1597
**Parameters**

A
annie_wangli 已提交
1598 1599 1600 1601 1602
| 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 已提交
1603

A
annie_wangli 已提交
1604
**Return value**
Z
zengyawen 已提交
1605

A
annie_wangli 已提交
1606
No value is returned.
Z
zengyawen 已提交
1607

A
annie_wangli 已提交
1608
**Example**
Z
zengyawen 已提交
1609

A
annie_wangli 已提交
1610
```js
Z
zengyawen 已提交
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
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 已提交
1659
**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1660

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

A
annie_wangli 已提交
1663
**Return value**
Z
zengyawen 已提交
1664

A
annie_wangli 已提交
1665 1666 1667 1668 1669
No value is returned.

**Example**

```js
Z
zengyawen 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
let server = bluetooth.BLE.createGattServer();
server.stopAdvertising();
```


### addService

addService(service: GattService): boolean

Adds a service to this GATT server.

A
annie_wangli 已提交
1681 1682 1683
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
1685 1686
**Parameters**

A
annie_wangli 已提交
1687 1688 1689
| Name    | Type                         | Mandatory  | Description                      |
| ------- | --------------------------- | ---- | ------------------------ |
| service | [GattService](#gattservice) | Yes   | Service to add. Settings related to BLE advertising.|
Z
zengyawen 已提交
1690

A
annie_wangli 已提交
1691
**Return value**
Z
zengyawen 已提交
1692

A
annie_wangli 已提交
1693 1694
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
1695
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
1696

A
annie_wangli 已提交
1697
**Example**
Z
zengyawen 已提交
1698

A
annie_wangli 已提交
1699
```js
Z
zengyawen 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
// Create descriptors.
let descriptors = [];
let arrayBuffer = new ArrayBuffer(8);
let descV = new Uint8Array(arrayBuffer);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
descriptors[0] = descriptor;

// Create characteristics.
let characteristics = [];
let arrayBufferC = new ArrayBuffer(8);
let cccV = new Uint8Array(arrayBufferC);
cccV[0] = 1;
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
let characteristicN = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
A
Annie_wang 已提交
1718
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
Z
zengyawen 已提交
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
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 已提交
1740 1741 1742
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
1744 1745
**Parameters**

A
annie_wangli 已提交
1746 1747 1748
| 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 已提交
1749

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

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

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

A
annie_wangli 已提交
1759
```js
Z
zengyawen 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768
let server = bluetooth.BLE.createGattServer();
server.removeService('00001810-0000-1000-8000-00805F9B34FB');
```


### close

close(): void

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

A
annie_wangli 已提交
1771
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
1772

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

A
annie_wangli 已提交
1775 1776 1777
**Example**

```js
Z
zengyawen 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
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 已提交
1789
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
1790

A
annie_wangli 已提交
1791 1792 1793 1794
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1795 1796 1797 1798
| 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 已提交
1799

A
annie_wangli 已提交
1800
**Return value**
Z
zengyawen 已提交
1801

A
annie_wangli 已提交
1802 1803
|         |                          |
| ------- | ------------------------ |
A
annie_wangli 已提交
1804 1805
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1806

A
annie_wangli 已提交
1807
**Example**
Z
zengyawen 已提交
1808

A
annie_wangli 已提交
1809
```js
A
Annie_wang 已提交
1810 1811 1812
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 已提交
1813
let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
A
Annie_wang 已提交
1814
characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false};
Z
zengyawen 已提交
1815
let server = bluetooth.BLE.createGattServer();
A
annie_wangli 已提交
1816
server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic);
Z
zengyawen 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825
```


### sendResponse

sendResponse(serverResponse: ServerResponse): boolean

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

A
annie_wangli 已提交
1826 1827 1828
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
1830 1831
**Parameters**

A
annie_wangli 已提交
1832 1833 1834
| Name           | Type                               | Mandatory  | Description             |
| -------------- | --------------------------------- | ---- | --------------- |
| serverResponse | [ServerResponse](#serverresponse) | Yes   | Response returned by the GATT server.|
Z
zengyawen 已提交
1835

A
annie_wangli 已提交
1836
**Return value**
Z
zengyawen 已提交
1837

A
annie_wangli 已提交
1838 1839
|         |                            |
| ------- | -------------------------- |
A
annie_wangli 已提交
1840 1841
| Type | Description |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
Z
zengyawen 已提交
1842

A
annie_wangli 已提交
1843
**Example**
Z
zengyawen 已提交
1844

A
annie_wangli 已提交
1845 1846
```js
/* send response */
Z
zengyawen 已提交
1847 1848 1849 1850
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
cccValue[0] = 1123;
let serverResponse = {
A
annie_wangli 已提交
1851
    "deviceId": "XX:XX:XX:XX:XX:XX",
Z
zengyawen 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
    "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 已提交
1874 1875 1876 1877 1878
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
1879

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

A
annie_wangli 已提交
1885
**Return value**
Z
zengyawen 已提交
1886

A
annie_wangli 已提交
1887
No value is returned.
Z
zengyawen 已提交
1888

A
annie_wangli 已提交
1889
**Example**
Z
zengyawen 已提交
1890

A
annie_wangli 已提交
1891
```js
Z
zengyawen 已提交
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
cccValue[0] = 1123;
function ReadCharacteristicReq(CharacteristicReadReq) {
  let deviceId = CharacteristicReadReq.deviceId;
  let transId = CharacteristicReadReq.transId;
  let offset = CharacteristicReadReq.offset;
  let characteristicUuid = CharacteristicReadReq.characteristicUuid;

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

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


### off('characteristicRead')

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

Unsubscribes from the characteristic read request events.

A
annie_wangli 已提交
1922 1923 1924
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
1926 1927
**Parameters**

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

A
annie_wangli 已提交
1933
**Return value**
Z
zengyawen 已提交
1934

A
annie_wangli 已提交
1935
No value is returned.
Z
zengyawen 已提交
1936

A
annie_wangli 已提交
1937
**Example**
Z
zengyawen 已提交
1938

A
annie_wangli 已提交
1939
```js
Z
zengyawen 已提交
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
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 已提交
1951
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
1952

A
annie_wangli 已提交
1953 1954 1955 1956
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
1957 1958 1959
| Name     | Type                                      | Mandatory  | Description                                    |
| -------- | ---------------------------------------- | ---- | -------------------------------------- |
| type     | string                                   | Yes   | Event type. The value **characteristicWrite** indicates a characteristic write request event.|
Z
zengyawen 已提交
1960
| callback | Callback&lt;[CharacteristicWriteReq](#descriptorwritereq)&gt; | Yes   | Callback invoked to return a characteristic write request from the GATT client.            |
Z
zengyawen 已提交
1961

A
annie_wangli 已提交
1962
**Return value**
Z
zengyawen 已提交
1963

A
annie_wangli 已提交
1964
No value is returned.
Z
zengyawen 已提交
1965

A
annie_wangli 已提交
1966
**Example**
Z
zengyawen 已提交
1967

A
annie_wangli 已提交
1968
```js
Z
zengyawen 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
let arrayBufferCCC = new ArrayBuffer(8);
let cccValue = new Uint8Array(arrayBufferCCC);
function WriteCharacteristicReq(CharacteristicWriteReq) {
  let deviceId = CharacteristicWriteReq.deviceId;
  let transId = CharacteristicWriteReq.transId;
  let offset = CharacteristicWriteReq.offset;
  let isPrep = CharacteristicWriteReq.isPrep;
  let needRsp = CharacteristicWriteReq.needRsp;
  let value =  new Uint8Array(CharacteristicWriteReq.value);
  let characteristicUuid = CharacteristicWriteReq.characteristicUuid;
  
  cccValue[0] = value[0];
  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
  
  let ret = gattServer.sendResponse(serverResponse);
  if (ret) {
    console.log('bluetooth sendResponse successfully');
  } else {
    console.log('bluetooth sendResponse failed');
  }
}

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


### off('characteristicWrite')

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

Unsubscribes from the characteristic write request events.

A
annie_wangli 已提交
2002 2003 2004
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2006 2007
**Parameters**

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

A
annie_wangli 已提交
2013
**Return value**
Z
zengyawen 已提交
2014

A
annie_wangli 已提交
2015
No value is returned.
Z
zengyawen 已提交
2016

A
annie_wangli 已提交
2017
**Example**
Z
zengyawen 已提交
2018

A
annie_wangli 已提交
2019
```js
Z
zengyawen 已提交
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
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 已提交
2031 2032 2033 2034 2035
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
2036

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

A
annie_wangli 已提交
2042
**Return value**
Z
zengyawen 已提交
2043

A
annie_wangli 已提交
2044
No value is returned.
Z
zengyawen 已提交
2045

A
annie_wangli 已提交
2046
**Example**
Z
zengyawen 已提交
2047

A
annie_wangli 已提交
2048
```js
Z
zengyawen 已提交
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
let arrayBufferDesc = new ArrayBuffer(8);
let descValue = new Uint8Array(arrayBufferDesc);
descValue[0] = 1101;
function ReadDescriptorReq(DescriptorReadReq) {
  let deviceId = DescriptorReadReq.deviceId;
  let transId = DescriptorReadReq.transId;
  let offset = DescriptorReadReq.offset;
  let descriptorUuid = DescriptorReadReq.descriptorUuid;

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

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


### off('descriptorRead')

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

Unsubscribes from the descriptor read request events.

A
annie_wangli 已提交
2079 2080 2081
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2083 2084
**Parameters**

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

A
annie_wangli 已提交
2090
**Return value**
Z
zengyawen 已提交
2091

A
annie_wangli 已提交
2092
No value is returned.
Z
zengyawen 已提交
2093

A
annie_wangli 已提交
2094
**Example**
Z
zengyawen 已提交
2095

A
annie_wangli 已提交
2096
```js
Z
zengyawen 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
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 已提交
2108
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2109

A
annie_wangli 已提交
2110 2111 2112 2113
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

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

A
annie_wangli 已提交
2119
**Return value**
Z
zengyawen 已提交
2120

A
annie_wangli 已提交
2121
No value is returned.
Z
zengyawen 已提交
2122

A
annie_wangli 已提交
2123
**Example**
Z
zengyawen 已提交
2124

A
annie_wangli 已提交
2125
```js
Z
zengyawen 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
let arrayBufferDesc = new ArrayBuffer(8);
let descValue = new Uint8Array(arrayBufferDesc);
function WriteDescriptorReq(DescriptorWriteReq) {
  let deviceId = DescriptorWriteReq.deviceId;
  let transId = DescriptorWriteReq.transId;
  let offset = DescriptorWriteReq.offset;
  let isPrep = DescriptorWriteReq.isPrep;
  let needRsp = DescriptorWriteReq.needRsp;
  let value = new Uint8Array(DescriptorWriteReq.value);
  let descriptorUuid = DescriptorWriteReq.descriptorUuid;

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

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


### off('descriptorWrite')

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

Unsubscribes from the descriptor write request events.

A
annie_wangli 已提交
2159 2160 2161
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2163 2164
**Parameters**

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

A
annie_wangli 已提交
2170
**Return value**
Z
zengyawen 已提交
2171

A
annie_wangli 已提交
2172
No value is returned.
Z
zengyawen 已提交
2173

A
annie_wangli 已提交
2174
**Example**
Z
zengyawen 已提交
2175

A
annie_wangli 已提交
2176
```js
Z
zengyawen 已提交
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("descriptorWrite");
```


### on('connectStateChange')

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

Subscribes to the BLE connection state change events.

A
annie_wangli 已提交
2188 2189 2190 2191 2192
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
2193

A
annie_wangli 已提交
2194 2195 2196 2197
| 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 已提交
2198

A
annie_wangli 已提交
2199
**Return value**
Z
zengyawen 已提交
2200

A
annie_wangli 已提交
2201
No value is returned.
Z
zengyawen 已提交
2202

A
annie_wangli 已提交
2203
**Example**
Z
zengyawen 已提交
2204

A
annie_wangli 已提交
2205
```js
Z
zengyawen 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
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 已提交
2222 2223 2224
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2226 2227
**Parameters**

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

A
annie_wangli 已提交
2233
**Return value**
Z
zengyawen 已提交
2234

A
annie_wangli 已提交
2235
No value is returned.
Z
zengyawen 已提交
2236

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

A
annie_wangli 已提交
2239
```js
Z
zengyawen 已提交
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
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 已提交
2254
Initiates a connection to the remote BLE device.
Z
zengyawen 已提交
2255

A
annie_wangli 已提交
2256
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2257

A
annie_wangli 已提交
2258 2259 2260 2261
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2262 2263
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
2264
| boolean | Returns **true** if the connection is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2265

A
annie_wangli 已提交
2266
**Example**
Z
zengyawen 已提交
2267

A
annie_wangli 已提交
2268 2269
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2270 2271 2272 2273 2274 2275 2276 2277
let ret = device.connect();
```


### disconnect

disconnect(): boolean

A
annie_wangli 已提交
2278
Disconnects from the remote BLE device.
Z
zengyawen 已提交
2279

A
annie_wangli 已提交
2280
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2281

A
annie_wangli 已提交
2282 2283 2284 2285
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2286 2287
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
2288
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2289

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

A
annie_wangli 已提交
2292 2293
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
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 已提交
2304
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2305

A
annie_wangli 已提交
2306 2307 2308 2309
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2310 2311
| Type     | Description                        |
| ------- | -------------------------- |
A
annie_wangli 已提交
2312
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2313

A
annie_wangli 已提交
2314
**Example**
Z
zengyawen 已提交
2315

A
annie_wangli 已提交
2316 2317
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
let ret = device.close();
```




### getServices

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

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

A
annie_wangli 已提交
2330
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2331

A
annie_wangli 已提交
2332 2333 2334 2335
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

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

A
annie_wangli 已提交
2340
**Return value**
Z
zengyawen 已提交
2341

A
annie_wangli 已提交
2342
No value is returned.
Z
zengyawen 已提交
2343

A
annie_wangli 已提交
2344
**Example**
Z
zengyawen 已提交
2345

A
annie_wangli 已提交
2346
```js
Z
zengyawen 已提交
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
// 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 已提交
2360
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369
device.connect();
device.getServices(getServices);
```


### getServices

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

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

A
annie_wangli 已提交
2372
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2373

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

A
annie_wangli 已提交
2376 2377 2378 2379
**Parameters**

**Return value**

A
annie_wangli 已提交
2380 2381
| Type                                      | Description                         |
| ---------------------------------------- | --------------------------- |
A
annie_wangli 已提交
2382
| Promise&lt;Array&lt;[GattService](#gattservice)&gt;&gt; | Promise used to return the services obtained.|
Z
zengyawen 已提交
2383

A
annie_wangli 已提交
2384
**Example**
Z
zengyawen 已提交
2385

A
annie_wangli 已提交
2386
```js
Z
zengyawen 已提交
2387
// Promise
A
annie_wangli 已提交
2388
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2389
device.connect();
A
Annie_wang 已提交
2390
var services = device.getServices();
Z
zengyawen 已提交
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
console.log("bluetooth services size is ", services.length);

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


### readCharacteristicValue

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

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

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

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

A
annie_wangli 已提交
2409 2410
**Parameters**

A
annie_wangli 已提交
2411 2412 2413 2414
| 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 已提交
2415

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

A
annie_wangli 已提交
2418
No value is returned.
Z
zengyawen 已提交
2419

A
annie_wangli 已提交
2420
**Example**
Z
zengyawen 已提交
2421

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

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

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


### readCharacteristicValue

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

A
annie_wangli 已提交
2457 2458 2459 2460 2461
Reads the characteristic value of the specific service of the remote BLE device. This method uses a promise to return the result.

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

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

A
annie_wangli 已提交
2463
**Parameters**
Z
zengyawen 已提交
2464

A
annie_wangli 已提交
2465 2466 2467
| Name           | Type                                     | Mandatory  | Description      |
| -------------- | --------------------------------------- | ---- | -------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | Yes   | Characteristic value to read.|
Z
zengyawen 已提交
2468

A
annie_wangli 已提交
2469
**Return value**
Z
zengyawen 已提交
2470

A
annie_wangli 已提交
2471 2472
|                                          |                            |
| ---------------------------------------- | -------------------------- |
A
annie_wangli 已提交
2473 2474
| Type | Description |
| Promise&lt;[BLECharacteristic](#blecharacteristic)&gt; | Promise used to return the characteristic value read. |
Z
zengyawen 已提交
2475

A
annie_wangli 已提交
2476
**Example**
Z
zengyawen 已提交
2477

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

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

device.readCharacteristicValue(characteristic);
```


### readDescriptorValue

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

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

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

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

A
annie_wangli 已提交
2510 2511
**Parameters**

A
annie_wangli 已提交
2512 2513 2514 2515
| Name       | Type                                      | Mandatory  | Description                     |
| ---------- | ---------------------------------------- | ---- | ----------------------- |
| descriptor | [BLEDescriptor](#bledescriptor)          | Yes   | Descriptor to read.               |
| callback   | AsyncCallback&lt;[BLECharacteristic](#blecharacteristic)&gt; | Yes   | Callback invoked to return the descriptor read.|
Z
zengyawen 已提交
2516

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

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

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

A
annie_wangli 已提交
2523
```js
Z
zengyawen 已提交
2524 2525 2526 2527 2528 2529 2530 2531 2532
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 已提交
2533
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 11;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
device.readDescriptorValue(descriptor, readDesc);
```


### readDescriptorValue

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

A
annie_wangli 已提交
2548 2549 2550 2551 2552
Reads the descriptor contained in the specific characteristic of the remote BLE device. This method uses a promise to return the result.

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

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

A
annie_wangli 已提交
2554
**Parameters**
Z
zengyawen 已提交
2555

A
annie_wangli 已提交
2556 2557 2558
| Name       | Type                             | Mandatory  | Description      |
| ---------- | ------------------------------- | ---- | -------- |
| descriptor | [BLEDescriptor](#bledescriptor) | Yes   | Descriptor to read.|
Z
zengyawen 已提交
2559

A
annie_wangli 已提交
2560
**Return value**
Z
zengyawen 已提交
2561

A
annie_wangli 已提交
2562 2563
|                                          |                            |
| ---------------------------------------- | -------------------------- |
A
annie_wangli 已提交
2564 2565
| Type | Description |
| Promise&lt;[BLEDescriptor](#bledescriptor)&gt; | Promise used to return the descriptor read. |
Z
zengyawen 已提交
2566

A
annie_wangli 已提交
2567
**Example**
Z
zengyawen 已提交
2568

A
annie_wangli 已提交
2569 2570
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
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 已提交
2585 2586 2587
Writes a characteristic value to the remote BLE device.

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

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

A
annie_wangli 已提交
2591 2592
**Parameters**

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

A
annie_wangli 已提交
2597
**Return value**
Z
zengyawen 已提交
2598

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

A
annie_wangli 已提交
2603
**Example**
Z
zengyawen 已提交
2604

A
annie_wangli 已提交
2605 2606
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
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 已提交
2635 2636 2637 2638 2639
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 已提交
2640

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

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

A
annie_wangli 已提交
2647
**Return value**
Z
zengyawen 已提交
2648

A
annie_wangli 已提交
2649 2650
| Type     | Description                         |
| ------- | --------------------------- |
A
annie_wangli 已提交
2651
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2652

A
annie_wangli 已提交
2653
**Example**
Z
zengyawen 已提交
2654

A
annie_wangli 已提交
2655 2656
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 22;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
let retWriteDesc = device.writeDescriptorValue(descriptor);
if (retWriteDesc) {
  console.log('bluetooth write descriptor successfully');
} else {
  console.log('bluetooth write descriptor failed');
}
```


### setBLEMtuSize

setBLEMtuSize(mtu: number): boolean

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

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

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

A
annie_wangli 已提交
2682
**Parameters**
Z
zengyawen 已提交
2683

A
annie_wangli 已提交
2684 2685 2686
| Name | Type    | Mandatory  | Description            |
| ---- | ------ | ---- | -------------- |
| mtu  | number | Yes   | MTU to set, which ranges from 22 to 512 bytes.|
Z
zengyawen 已提交
2687

A
annie_wangli 已提交
2688
**Return value**
Z
zengyawen 已提交
2689

A
annie_wangli 已提交
2690 2691
| Type     | Description                          |
| ------- | ---------------------------- |
A
annie_wangli 已提交
2692
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2693

A
annie_wangli 已提交
2694
**Example**
Z
zengyawen 已提交
2695

A
annie_wangli 已提交
2696 2697
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2698 2699 2700 2701 2702 2703 2704 2705
device.setBLEMtuSize(128);
```


### setNotifyCharacteristicChanged

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

A
annie_wangli 已提交
2706 2707 2708 2709 2710
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 已提交
2711

A
annie_wangli 已提交
2712
**Parameters**
Z
zengyawen 已提交
2713

A
annie_wangli 已提交
2714 2715 2716 2717
| 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 已提交
2718

A
annie_wangli 已提交
2719
**Return value**
Z
zengyawen 已提交
2720

A
annie_wangli 已提交
2721 2722
| Type     | Description                       |
| ------- | ------------------------- |
A
annie_wangli 已提交
2723
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
2724

A
annie_wangli 已提交
2725
**Example**
Z
zengyawen 已提交
2726

A
annie_wangli 已提交
2727
```js
A
Annie_wang 已提交
2728 2729 2730
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 已提交
2731
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
A
Annie_wang 已提交
2732
device.setNotifyCharacteristicChanged(characteristic, false);
Z
zengyawen 已提交
2733 2734 2735 2736 2737 2738 2739 2740 2741
```


### 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 已提交
2742 2743 2744 2745 2746
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

**Parameters**
Z
zengyawen 已提交
2747

A
annie_wangli 已提交
2748 2749 2750 2751
| 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 已提交
2752

A
annie_wangli 已提交
2753
**Return value**
Z
zengyawen 已提交
2754

A
annie_wangli 已提交
2755
No value is returned.
Z
zengyawen 已提交
2756

A
annie_wangli 已提交
2757
**Example**
Z
zengyawen 已提交
2758

A
annie_wangli 已提交
2759
```js
Z
zengyawen 已提交
2760 2761 2762 2763 2764
function CharacteristicChange(CharacteristicChangeReq) {
  let serviceUuid = CharacteristicChangeReq.serviceUuid;
  let characteristicUuid = CharacteristicChangeReq.characteristicUuid;
  let value = new Uint8Array(CharacteristicChangeReq.characteristicValue);
}
A
annie_wangli 已提交
2765
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
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 已提交
2776 2777 2778
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

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

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

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

A
annie_wangli 已提交
2789
No value is returned.
Z
zengyawen 已提交
2790

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

A
annie_wangli 已提交
2793 2794
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
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 已提交
2805
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2806

A
annie_wangli 已提交
2807 2808 2809 2810
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Parameters**

A
annie_wangli 已提交
2811 2812 2813 2814
| 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 已提交
2815

A
annie_wangli 已提交
2816
**Return value**
Z
zengyawen 已提交
2817

A
annie_wangli 已提交
2818
No value is returned.
Z
zengyawen 已提交
2819

A
annie_wangli 已提交
2820
**Example**
Z
zengyawen 已提交
2821

A
annie_wangli 已提交
2822
```js
Z
zengyawen 已提交
2823 2824 2825 2826
function ConnectStateChanged(state) {
  console.log('bluetooth connect state changed');
  let connectState = state.state;
}
A
annie_wangli 已提交
2827
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
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 已提交
2838 2839 2840
**Required permissions**: ohos.permission.USE_BLUETOOTH

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

A
annie_wangli 已提交
2842 2843
**Parameters**

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

A
annie_wangli 已提交
2849
**Return value**
Z
zengyawen 已提交
2850

A
annie_wangli 已提交
2851
No value is returned.
Z
zengyawen 已提交
2852

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

A
annie_wangli 已提交
2855 2856
```js
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2857 2858 2859 2860 2861 2862 2863 2864
device.off('BLEConnectionStateChange');
```


### getDeviceName

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

A
annie_wangli 已提交
2865 2866 2867 2868 2869
Obtains the name of the remote BLE device. This method uses an asynchronous callback to return the result.

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

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

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

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

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

A
annie_wangli 已提交
2879
No value is returned.
Z
zengyawen 已提交
2880

A
annie_wangli 已提交
2881
**Example**
Z
zengyawen 已提交
2882

A
annie_wangli 已提交
2883 2884 2885
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
let deviceName = gattClient.getDeviceName((err, data)=> {
    console.info('device name err ' + JSON.stringify(err));
    console.info('device name' + JSON.stringify(data));
})
```


### getDeviceName

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

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

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

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

**Return value**

A
annie_wangli 已提交
2905 2906
| Type                   | Description                                |
| --------------------- | ---------------------------------- |
A
annie_wangli 已提交
2907
| Promise&lt;string&gt; | Promise used to return the remote BLE device name.|
Z
zengyawen 已提交
2908

A
annie_wangli 已提交
2909
**Example**
Z
zengyawen 已提交
2910

A
annie_wangli 已提交
2911 2912 2913
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
let ret = device.connect();
let deviceName = gattClient.getDeviceName().then((data) => {
    console.info('device name' + JSON.stringify(data));
})
```


### getRssiValue

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

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

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

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

A
annie_wangli 已提交
2931 2932
**Parameters**

A
annie_wangli 已提交
2933 2934 2935
| Name     | Type                         | Mandatory  | Description                            |
| -------- | --------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback invoked to return the RSSI, in dBm.|
Z
zengyawen 已提交
2936

A
annie_wangli 已提交
2937
**Return value**
Z
zengyawen 已提交
2938

A
annie_wangli 已提交
2939
No value is returned.
Z
zengyawen 已提交
2940

A
annie_wangli 已提交
2941
**Example**
Z
zengyawen 已提交
2942

A
annie_wangli 已提交
2943 2944 2945
```js
// callback
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957
let ret = device.connect();
let rssi = gattClient.getRssiValue((err, data)=> {
    console.info('rssi err ' + JSON.stringify(err));
    console.info('rssi value' + JSON.stringify(data));
})
```


### getRssiValue

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

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

A
annie_wangli 已提交
2960
**Required permissions**: ohos.permission.USE_BLUETOOTH
Z
zengyawen 已提交
2961

A
annie_wangli 已提交
2962 2963 2964 2965
**System capability**: SystemCapability.Communication.Bluetooth.Core

**Return value**

A
annie_wangli 已提交
2966 2967
| Type                   | Description                               |
| --------------------- | --------------------------------- |
A
annie_wangli 已提交
2968
| Promise&lt;number&gt; | Promise used to return the RSSI, in dBm.|
Z
zengyawen 已提交
2969

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

A
annie_wangli 已提交
2972 2973 2974
```js
// promise
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
2975 2976 2977 2978 2979
let rssi = gattClient.getRssiValue().then((data) => {
    console.info('rssi' + JSON.stringify(data));
})
```

A
annie_wangli 已提交
2980
## ScanMode<sup>8+</sup><a name="ScanMode"></a>
Z
zengyawen 已提交
2981 2982 2983

Enumerates the scan modes.

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

A
annie_wangli 已提交
2986 2987 2988 2989 2990 2991 2992 2993
| Name                                      | Default Value | Description             |
| ---------------------------------------- | ---- | --------------- |
| SCAN_MODE_NONE                           | 0    | No scan mode.        |
| SCAN_MODE_CONNECTABLE                    | 1    | Connectable mode.       |
| SCAN_MODE_GENERAL_DISCOVERABLE           | 2    | General discoverable mode.   |
| SCAN_MODE_LIMITED_DISCOVERABLE           | 3    | Limited discoverable mode.   |
| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4    | General connectable and discoverable mode.|
| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5    | Limited connectable and discoverable mode.|
Z
zengyawen 已提交
2994

A
annie_wangli 已提交
2995
## BondState<sup>8+</sup><a name="BondState"></a>
Z
zengyawen 已提交
2996 2997 2998

Enumerates the pairing states.

A
annie_wangli 已提交
2999 3000
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3001 3002 3003 3004 3005
| Name                | Default Value | Description    |
| ------------------ | ---- | ------ |
| BOND_STATE_INVALID | 0    | Invalid pairing.|
| BOND_STATE_BONDING | 1    | Pairing. |
| BOND_STATE_BONDED  | 2    | Paired.  |
Z
zengyawen 已提交
3006 3007


A
annie_wangli 已提交
3008
## SppOption<sup>8+</sup><a name="SppOption"></a>
Z
zengyawen 已提交
3009 3010 3011

Defines the SPP configuration parameters.

A
annie_wangli 已提交
3012 3013
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3014 3015 3016 3017 3018
| 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 已提交
3019 3020


A
annie_wangli 已提交
3021
## SppType<sup>8+</sup><a name="SppType"></a>
Z
zengyawen 已提交
3022 3023 3024

Enumerates the SPP link types.

A
annie_wangli 已提交
3025 3026
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3027 3028 3029
| Name        | Default Value | Description           |
| ---------- | ---- | ------------- |
| SPP_RFCOMM | 0    | Radio frequency communication (RFCOMM) link type.|
Z
zengyawen 已提交
3030 3031 3032 3033 3034 3035


## GattService

Defines the GATT service API parameters.

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

A
annie_wangli 已提交
3038 3039 3040 3041 3042 3043
| 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 已提交
3044 3045 3046 3047 3048 3049


## BLECharacteristic

Defines the characteristic API parameters.

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

A
annie_wangli 已提交
3052 3053 3054 3055 3056 3057
| 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 已提交
3058 3059 3060 3061 3062 3063


## BLEDescriptor

Defines the descriptor API parameters.

A
annie_wangli 已提交
3064 3065
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3066 3067 3068 3069 3070 3071
| 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 已提交
3072 3073 3074 3075 3076 3077


## NotifyCharacteristic

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

A
annie_wangli 已提交
3078 3079
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3080 3081 3082 3083 3084 3085
| 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 已提交
3086 3087 3088 3089 3090 3091


## CharacteristicReadReq

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

A
annie_wangli 已提交
3092 3093
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3094 3095 3096 3097 3098 3099 3100
| 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 已提交
3101 3102 3103 3104 3105 3106


## CharacteristicWriteReq

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

A
annie_wangli 已提交
3107 3108
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3109 3110 3111 3112 3113 3114 3115 3116
| 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 已提交
3117

A
annie_wangli 已提交
3118

Z
zengyawen 已提交
3119 3120 3121 3122
## DescriptorReadReq

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

A
annie_wangli 已提交
3123 3124
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3125 3126 3127 3128 3129 3130 3131 3132
| 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 已提交
3133 3134 3135 3136 3137 3138


## DescriptorWriteReq

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

A
annie_wangli 已提交
3139 3140
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
| 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 已提交
3152 3153 3154 3155 3156 3157


## ServerResponse

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

A
annie_wangli 已提交
3158 3159
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3160 3161 3162 3163 3164 3165 3166
| 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 已提交
3167 3168 3169 3170 3171 3172


## BLEConnectChangedState

Defines the parameters of **BLEConnectChangedState**.

A
annie_wangli 已提交
3173 3174
**System capability**: SystemCapability.Communication.Bluetooth.Core

Z
zengyawen 已提交
3175 3176 3177 3178
| 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 已提交
3179 3180 3181 3182 3183 3184


## ProfileConnectionState

Enumerates the profile connection states.

A
annie_wangli 已提交
3185 3186
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3187 3188 3189 3190 3191 3192
| Name                 | Default Value | Description            |
| ------------------- | ---- | -------------- |
| STATE_DISCONNECTED  | 0    | Disconnected. |
| STATE_CONNECTING    | 1    | Connecting.|
| STATE_CONNECTED     | 2    | Connected. |
| STATE_DISCONNECTING | 3    | Disconnecting.|
Z
zengyawen 已提交
3193 3194 3195 3196 3197 3198


## ScanFilter

Defines the scan filter parameters.

A
annie_wangli 已提交
3199 3200
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3201 3202 3203 3204 3205
| Name         | Type  | Readable  | Writable  | Description                                      |
| ----------- | ------ | ---- | ---- | ---------------------------------------- |
| deviceId    | string | Yes   | Yes   | Address of the BLE device to filter, for example, XX:XX:XX:XX:XX:XX.    |
| name        | string | Yes   | Yes   | Name of the BLE device to filter.                            |
| serviceUuid | string | Yes   | Yes   | UUID of the service, for example, **00001888-0000-1000-8000-00805f9b34fb**.|
Z
zengyawen 已提交
3206 3207 3208 3209 3210 3211


## ScanOptions

Defines the scan configuration parameters.

A
annie_wangli 已提交
3212 3213
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3214 3215 3216 3217 3218
| 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 已提交
3219 3220 3221 3222 3223 3224


## ScanDuty

Enumerates the scan duty options.

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

A
annie_wangli 已提交
3227 3228 3229 3230 3231
| Name                   | Default Value | Description          |
| --------------------- | ---- | ------------ |
| SCAN_MODE_LOW_POWER   | 0    | Low-power mode, which is the default value.|
| SCAN_MODE_BALANCED    | 1    | Balanced mode.     |
| SCAN_MODE_LOW_LATENCY | 2    | Low-latency mode.    |
Z
zengyawen 已提交
3232 3233 3234 3235 3236 3237


## MatchMode

Enumerates the hardware match modes of BLE scan filters.

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

A
annie_wangli 已提交
3240 3241 3242 3243
| Name                   | Default Value | Description                                      |
| --------------------- | ---- | ---------------------------------------- |
| MATCH_MODE_AGGRESSIVE | 1    | Hardware reports the scan result with a lower threshold of signal strength and few number of matches in a duration. This is the default value.|
| MATCH_MODE_STICKY     | 2    | Hardware reports the scan result with a higher threshold of signal strength and sightings.      |
Z
zengyawen 已提交
3244 3245 3246 3247 3248 3249


## ScanResult

Defines the scan result.

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

A
annie_wangli 已提交
3252 3253 3254 3255 3256
| 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 已提交
3257 3258 3259 3260 3261 3262


## BluetoothState

Enumerates the Bluetooth states.

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

A
annie_wangli 已提交
3265 3266 3267 3268 3269 3270 3271 3272 3273
| Name                   | Default Value | Description                |
| --------------------- | ---- | ------------------ |
| STATE_OFF             | 0    | Bluetooth is turned off.          |
| STATE_TURNING_ON      | 1    | Bluetooth is being turned on.         |
| STATE_ON              | 2    | Bluetooth is turned on.          |
| STATE_TURNING_OFF     | 3    | Bluetooth is being turned off.         |
| STATE_BLE_TURNING_ON  | 4    | The LE-only mode is being turned on for Bluetooth.|
| STATE_BLE_ON          | 5    | Bluetooth is in LE-only mode. |
| STATE_BLE_TURNING_OFF | 6    | The LE-only mode is being turned off for Bluetooth.|
Z
zengyawen 已提交
3274 3275 3276 3277 3278 3279


## AdvertiseSetting

Defines the BLE advertising parameters.

A
annie_wangli 已提交
3280 3281
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3282 3283 3284 3285 3286
| Name         | Type   | Readable  | Writable  | Description                                      |
| ----------- | ------- | ---- | ---- | ---------------------------------------- |
| interval    | number  | Yes   | Yes   | Interval for BLE advertising. The minimum value is **32** slots (20 ms). The maximum value is **16777215** slots. The default value is **1600** slots (1s).|
| txPower     | number  | Yes   | Yes   | Transmit power, in dBm. The value range is -127 to 1. The default value is **-7**.  |
| connectable | boolean | Yes   | Yes   | Whether the advertisement is connectable. The default value is **true**.                  |
Z
zengyawen 已提交
3287 3288 3289 3290 3291 3292


## AdvertiseData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3293 3294
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3295 3296 3297 3298 3299
| 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 已提交
3300 3301 3302 3303 3304 3305


## ManufactureData

Defines the content of a BLE advertisement packet.

A
annie_wangli 已提交
3306 3307
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3308 3309 3310 3311
| Name              | Type               | Readable  | Writable  | Description                |
| ---------------- | ------------------- | ---- | ---- | ------------------ |
| manufactureId    | Array&lt;string&gt; | Yes   | Yes   | Manufacturer ID allocated by the Bluetooth SIG.|
| manufactureValue | ArrayBuffer         | Yes   | Yes   | Manufacturer data.    |
Z
zengyawen 已提交
3312 3313 3314 3315 3316 3317


## ServiceData

Defines the service data contained in an advertisement packet.

A
annie_wangli 已提交
3318 3319
**System capability**: SystemCapability.Communication.Bluetooth.Core

A
annie_wangli 已提交
3320 3321 3322 3323
| Name          | Type       | Readable  | Writable  | Description        |
| ------------ | ----------- | ---- | ---- | ---------- |
| serviceUuid  | string      | Yes   | Yes   | Service UUID.|
| serviceValue | ArrayBuffer | Yes   | Yes   | Service data.   |
Z
zengyawen 已提交
3324 3325


A
annie_wangli 已提交
3326
## PinRequiredParam<sup>8+</sup><a name="PinRequiredParam"></a>
Z
zengyawen 已提交
3327 3328 3329

Defines the pairing request parameters.

A
annie_wangli 已提交
3330 3331
**System capability**: SystemCapability.Communication.Bluetooth.Core

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


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

Defines the profile state change parameters.

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

Z
zengyawen 已提交
3344 3345 3346 3347
| 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 已提交
3348 3349 3350 3351 3352 3353 3354 3355


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

Defines the class of a Bluetooth device.

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

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



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

Enumerates the major classes of Bluetooth devices.

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

A
annie_wangli 已提交
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
| Name                 | Default Value   | Description        |
| ------------------- | ------ | ---------- |
| MAJOR_MISC          | 0x0000 | Miscellaneous device.   |
| MAJOR_COMPUTER      | 0x0100 | Computer.  |
| MAJOR_PHONE         | 0x0200 | Mobile phone.   |
| MAJOR_NETWORKING    | 0x0300 | Network device.   |
| MAJOR_AUDIO_VIDEO   | 0x0400 | Audio or video device.|
| MAJOR_PERIPHERAL    | 0x0500 | Peripheral device.   |
| MAJOR_IMAGING       | 0x0600 | Imaging device.   |
| MAJOR_WEARABLE      | 0x0700 | Wearable device.  |
| MAJOR_TOY           | 0x0800 | Toy.   |
| MAJOR_HEALTH        | 0x0900 | Health device.   |
| MAJOR_UNCATEGORIZED | 0x1F00 | Unclassified device.  |
A
annie_wangli 已提交
3383 3384 3385 3386 3387 3388 3389 3390


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

Enumerates the major and minor classes of Bluetooth devices.

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

A
annie_wangli 已提交
3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478
| Name                                      | Default Value   | Description             |
| ---------------------------------------- | ------ | --------------- |
| COMPUTER_UNCATEGORIZED                   | 0x0100 | Unclassified computer.    |
| COMPUTER_DESKTOP                         | 0x0104 | Desktop computer.     |
| COMPUTER_SERVER                          | 0x0108 | Server.       |
| COMPUTER_LAPTOP                          | 0x010C | Laptop.    |
| COMPUTER_HANDHELD_PC_PDA                 | 0x0110 | Hand-held computer.    |
| COMPUTER_PALM_SIZE_PC_PDA                | 0x0114 | Palmtop computer.      |
| COMPUTER_WEARABLE                        | 0x0118 | Wearable computer.    |
| COMPUTER_TABLET                          | 0x011C | Tablet.      |
| PHONE_UNCATEGORIZED                      | 0x0200 | Unclassified mobile phone.     |
| PHONE_CELLULAR                           | 0x0204 | Portable phone.     |
| PHONE_CORDLESS                           | 0x0208 | Cordless phone.      |
| PHONE_SMART                              | 0x020C | Smartphone.      |
| PHONE_MODEM_OR_GATEWAY                   | 0x0210 | Modem or gateway phone.|
| PHONE_ISDN                               | 0x0214 | ISDN phone.    |
| NETWORK_FULLY_AVAILABLE                  | 0x0300 | Device with network fully available.    |
| NETWORK_1_TO_17_UTILIZED                 | 0x0320 | Device used on network 1 to 17.  |
| NETWORK_17_TO_33_UTILIZED                | 0x0340 | Device used on network 17 to 33. |
| NETWORK_33_TO_50_UTILIZED                | 0x0360 | Device used on network 33 to 50. |
| NETWORK_60_TO_67_UTILIZED                | 0x0380 | Device used on network 60 to 67. |
| NETWORK_67_TO_83_UTILIZED                | 0x03A0 | Device used on network 67 to 83. |
| NETWORK_83_TO_99_UTILIZED                | 0x03C0 | Device used on network 83 to 99. |
| NETWORK_NO_SERVICE                       | 0x03E0 | Device without network service     |
| AUDIO_VIDEO_UNCATEGORIZED                | 0x0400 | Unclassified audio or video device.   |
| AUDIO_VIDEO_WEARABLE_HEADSET             | 0x0404 | Wearable audio or video headset.  |
| AUDIO_VIDEO_HANDSFREE                    | 0x0408 | Hands-free audio or video device.    |
| AUDIO_VIDEO_MICROPHONE                   | 0x0410 | Audio or video microphone.   |
| AUDIO_VIDEO_LOUDSPEAKER                  | 0x0414 | Audio or video loudspeaker.   |
| AUDIO_VIDEO_HEADPHONES                   | 0x0418 | Audio or video headphones.   |
| AUDIO_VIDEO_PORTABLE_AUDIO               | 0x041C | Portable audio or video device.   |
| AUDIO_VIDEO_CAR_AUDIO                    | 0x0420 | In-vehicle audio or video device.    |
| AUDIO_VIDEO_SET_TOP_BOX                  | 0x0424 | Audio or video STB device.   |
| AUDIO_VIDEO_HIFI_AUDIO                   | 0x0428 | High-fidelity speaker device.     |
| AUDIO_VIDEO_VCR                          | 0x042C | Video cassette recording (VCR) device.   |
| AUDIO_VIDEO_VIDEO_CAMERA                 | 0x0430 | Camera.   |
| AUDIO_VIDEO_CAMCORDER                    | 0x0434 | Camcorder   |
| AUDIO_VIDEO_VIDEO_MONITOR                | 0x0438 | Audio or video monitor.   |
| AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER | 0x043C | Video display or loudspeaker. |
| AUDIO_VIDEO_VIDEO_CONFERENCING           | 0x0440 | Video conferencing device.    |
| AUDIO_VIDEO_VIDEO_GAMING_TOY             | 0x0448 | Audio or video gaming toy.  |
| PERIPHERAL_NON_KEYBOARD_NON_POINTING     | 0x0500 | Non-keyboard or non-pointing peripheral device.  |
| PERIPHERAL_KEYBOARD                      | 0x0540 | Keyboard device.      |
| PERIPHERAL_POINTING_DEVICE               | 0x0580 | Pointing peripheral device.    |
| PERIPHERAL_KEYBOARD_POINTING             | 0x05C0 | Keyboard pointing device.    |
| PERIPHERAL_UNCATEGORIZED                 | 0x0500 | Unclassified peripheral device.     |
| PERIPHERAL_JOYSTICK                      | 0x0504 | Peripheral joystick.     |
| PERIPHERAL_GAMEPAD                       | 0x0508 | Peripheral game pad     |
| PERIPHERAL_REMOTE_CONTROL                | 0x05C0 | Peripheral remote control device    |
| PERIPHERAL_SENSING_DEVICE                | 0x0510 | Peripheral sensing device.    |
| PERIPHERAL_DIGITIZER_TABLET              | 0x0514 | Peripheral digitizer tablet.|
| PERIPHERAL_CARD_READER                   | 0x0518 | Peripheral card reader.     |
| PERIPHERAL_DIGITAL_PEN                   | 0x051C | Peripheral digital pen.     |
| PERIPHERAL_SCANNER_RFID                  | 0x0520 | Peripheral RFID scanner. |
| PERIPHERAL_GESTURAL_INPUT                | 0x0522 | Gesture input device.    |
| IMAGING_UNCATEGORIZED                    | 0x0600 | Unclassified imaging device.    |
| IMAGING_DISPLAY                          | 0x0610 | Imaging display device.      |
| IMAGING_CAMERA                           | 0x0620 | Imaging camera device.     |
| IMAGING_SCANNER                          | 0x0640 | Imaging scanner.     |
| IMAGING_PRINTER                          | 0x0680 | Imaging printer.     |
| WEARABLE_UNCATEGORIZED                   | 0x0700 | Unclassified wearable device.   |
| WEARABLE_WRIST_WATCH                     | 0x0704 | Smart watch.     |
| WEARABLE_PAGER                           | 0x0708 | Wearable pager.    |
| WEARABLE_JACKET                          | 0x070C | Smart jacket.     |
| WEARABLE_HELMET                          | 0x0710 | Wearable helmet.     |
| WEARABLE_GLASSES                         | 0x0714 | Wearable glasses.     |
| TOY_UNCATEGORIZED                        | 0x0800 | Unclassified toy.    |
| TOY_ROBOT                                | 0x0804 | Toy robot.     |
| TOY_VEHICLE                              | 0x0808 | Toy vehicle.       |
| TOY_DOLL_ACTION_FIGURE                   | 0x080C | Humanoid toy doll.    |
| TOY_CONTROLLER                           | 0x0810 | Toy controller.     |
| TOY_GAME                                 | 0x0814 | Toy gaming device.      |
| HEALTH_UNCATEGORIZED                     | 0x0900 | Unclassified health devices.     |
| HEALTH_BLOOD_PRESSURE                    | 0x0904 | Blood pressure device.      |
| HEALTH_THERMOMETER                       | 0x0908 | Thermometer     |
| HEALTH_WEIGHING                          | 0x090C | Body scale.      |
| HEALTH_GLUCOSE                           | 0x0910 | Blood glucose monitor.     |
| HEALTH_PULSE_OXIMETER                    | 0x0914 | Pulse oximeter.   |
| HEALTH_PULSE_RATE                        | 0x0918 | Heart rate monitor.     |
| HEALTH_DATA_DISPLAY                      | 0x091C | Health data display.    |
| HEALTH_STEP_COUNTER                      | 0x0920 | Step counter.   |
| HEALTH_BODY_COMPOSITION_ANALYZER         | 0x0924 | Body composition analyzer. |
| HEALTH_PEAK_FLOW_MOITOR                  | 0x0928 | Hygrometer.     |
| HEALTH_MEDICATION_MONITOR                | 0x092C | Medication monitor.   |
| HEALTH_KNEE_PROSTHESIS                   | 0x0930 | Prosthetic knee.    |
| HEALTH_ANKLE_PROSTHESIS                  | 0x0934 | Prosthetic ankle.    |
| HEALTH_GENERIC_HEALTH_MANAGER            | 0x0938 | Generic health management device.    |
| HEALTH_PERSONAL_MOBILITY_DEVICE          | 0x093C | Personal mobility device.    |
A
annie_wangli 已提交
3479 3480 3481 3482 3483 3484 3485 3486


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

Enumerates the A2DP playing states.

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

A
annie_wangli 已提交
3487 3488 3489 3490
| Name               | Default Value   | Description     |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | Not playing. |
| STATE_PLAYING     | 0x0001 | Playing.|
A
annie_wangli 已提交
3491 3492 3493 3494 3495 3496 3497 3498


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

Enumerates the Bluetooth profile IDs.

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

A
annie_wangli 已提交
3499 3500 3501 3502
| Name                              | Default Value   | Description             |
| -------------------------------- | ------ | --------------- |
| PROFILE_A2DP_SOURCE              | 0x0001 | A2DP profile.|
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | HFP profile. |