js-apis-bluetooth.md 133.9 KB
Newer Older
W
wangyanhandsome 已提交
1 2
# 蓝牙

Z
zengyawen 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
S
smartbao 已提交
4
> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
5 6

蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。
Z
zengyawen 已提交
7 8 9 10


## 导入模块

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


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

enableBluetooth(): boolean

开启蓝牙。

S
smartbao 已提交
22 23 24 25
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
26 27
**返回值:**

H
HelloCrease 已提交
28 29
| 类型      | 说明                       |
| ------- | ------------------------ |
Z
zengyawen 已提交
30 31 32 33
| boolean | 打开蓝牙,成功返回true,否则返回false。 |

**示例:**

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


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

disableBluetooth(): boolean

关闭蓝牙。

S
smartbao 已提交
45 46 47 48
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
49 50
**返回值:**

H
HelloCrease 已提交
51 52
| 类型      | 说明                       |
| ------- | ------------------------ |
Z
zengyawen 已提交
53 54 55 56
| boolean | 关闭蓝牙,成功返回true,否则返回false。 |

**示例:**

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


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

getLocalName(): string

获取蓝牙本地设备名称。

S
smartbao 已提交
68 69 70 71
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
72 73
**返回值:**

H
HelloCrease 已提交
74 75
| 类型     | 说明        |
| ------ | --------- |
Z
zengyawen 已提交
76 77 78 79
| string | 蓝牙本地设备名称。 |

**示例:**

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


## bluetooth.getState

getState(): BluetoothState

获取蓝牙开关状态。

S
smartbao 已提交
91 92 93 94
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
95 96
**返回值:**

H
HelloCrease 已提交
97 98
| 类型                                | 说明        |
| --------------------------------- | --------- |
Z
zengyawen 已提交
99 100 101 102
| [BluetoothState](#bluetoothstate) | 表示蓝牙开关状态。 |

**示例:**

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


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

获取蓝牙设备的Profile连接状态。

S
smartbao 已提交
114 115 116 117
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
118 119
**返回值:**

H
HelloCrease 已提交
120 121
| 类型                                       | 说明                  |
| ---------------------------------------- | ------------------- |
Z
zengyawen 已提交
122 123 124 125
| [ProfileConnectionState](#profileconnectionstate) | 表示蓝牙设备的Profile连接状态。 |

**示例:**

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


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

setLocalName(name: string): boolean

设置蓝牙本地设备名称。

S
smartbao 已提交
137 138 139 140
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
141 142
**参数:**

H
HelloCrease 已提交
143 144 145
| 参数名  | 类型     | 必填   | 说明                    |
| ---- | ------ | ---- | --------------------- |
| name | string | 是    | 要设置的蓝牙名称,最大长度为248字节数。 |
Z
zengyawen 已提交
146 147 148

**返回值:**

H
HelloCrease 已提交
149 150
| 类型      | 说明                             |
| ------- | ------------------------------ |
Z
zengyawen 已提交
151 152 153 154
| boolean | 设置蓝牙本地设备名称,成功返回true,否则返回false。 |

**示例:**

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


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

发起蓝牙配对。

S
smartbao 已提交
166 167 168 169
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
170 171
**参数:**

H
HelloCrease 已提交
172 173 174
| 参数名      | 类型     | 必填   | 说明                                  |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
Z
zengyawen 已提交
175 176 177

**返回值:**

H
HelloCrease 已提交
178 179
| 类型      | 说明                         |
| ------- | -------------------------- |
Z
zengyawen 已提交
180 181 182 183
| boolean | 发起蓝牙配对,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
184
```js
Z
zengyawen 已提交
185
// 实际的地址可由扫描流程获取
W
wangyanhandsome 已提交
186
let result = bluetooth.pairDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
187 188 189
```


S
smartbao 已提交
190 191 192 193 194 195 196 197 198 199 200 201
## bluetooth.getProfileConnState<sup>8+</sup><a name="getProfileConnState"></a>

getProfileConnState(profileId: ProfileId): ProfileConnectionState

获取profile的连接状态。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
202 203 204
| 参数名       | 类型        | 必填   | 说明                                    |
| --------- | --------- | ---- | ------------------------------------- |
| ProfileId | profileId | 是    | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 |
S
smartbao 已提交
205 206 207

**返回值:**

Z
zengyawen 已提交
208 209 210
| 类型                                              | 说明                |
| ------------------------------------------------- | ------------------- |
| [ProfileConnectionState](#profileconnectionstate) | profile的连接状态。 |
S
smartbao 已提交
211 212 213 214

**示例:**

```js
S
smartbao 已提交
215
let result = bluetooth.getProfileConnState(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
S
smartbao 已提交
216 217 218 219
```


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

cancelPairedDevice(deviceId: string): boolean

删除配对的远程设备。

225 226
此接口为系统接口。

S
smartbao 已提交
227 228 229 230
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

W
wangyanhandsome 已提交
231 232
**参数:**

H
HelloCrease 已提交
233 234 235
| 参数名      | 类型     | 必填   | 说明                                    |
| -------- | ------ | ---- | ------------------------------------- |
| deviceId | string | 是    | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
W
wangyanhandsome 已提交
236 237 238

**返回值:**

H
HelloCrease 已提交
239 240
| 类型      | 说明                         |
| ------- | -------------------------- |
W
wangyanhandsome 已提交
241 242 243 244
| boolean | 启动取消配对,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
245
```js
W
wangyanhandsome 已提交
246
let result = bluetooth.cancelPairedDevice("XX:XX:XX:XX:XX:XX");
W
wangyanhandsome 已提交
247 248 249
```


S
smartbao 已提交
250
## bluetooth.getRemoteDeviceName<sup>8+</sup><a name="getRemoteDeviceName"></a>
W
wangyanhandsome 已提交
251 252 253 254 255

getRemoteDeviceName(deviceId: string): string

获取对端蓝牙设备的名称。

S
smartbao 已提交
256 257 258 259
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

W
wangyanhandsome 已提交
260 261
**参数:**

H
HelloCrease 已提交
262 263 264
| 参数名      | 类型     | 必填   | 说明                                |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
W
wangyanhandsome 已提交
265 266 267

**返回值:**

H
HelloCrease 已提交
268 269
| 类型     | 说明            |
| ------ | ------------- |
W
wangyanhandsome 已提交
270 271 272 273
| string | 以字符串格式返回设备名称。 |

**示例:**

S
smartbao 已提交
274
```js
W
wangyanhandsome 已提交
275
let remoteDeviceName = bluetooth.getRemoteDeviceName("XX:XX:XX:XX:XX:XX");
W
wangyanhandsome 已提交
276 277 278
```


S
smartbao 已提交
279
## bluetooth.getRemoteDeviceClass<sup>8+</sup><a name="getRemoteDeviceClass"></a>
W
wangyanhandsome 已提交
280 281 282 283 284

getRemoteDeviceClass(deviceId: string): DeviceClass

获取对端蓝牙设备的类别。

S
smartbao 已提交
285 286 287 288
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

W
wangyanhandsome 已提交
289 290
**参数:**

H
HelloCrease 已提交
291 292 293
| 参数名      | 类型     | 必填   | 说明                                |
| -------- | ------ | ---- | --------------------------------- |
| deviceId | string | 是    | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
W
wangyanhandsome 已提交
294 295 296

**返回值:**

H
HelloCrease 已提交
297 298
| 类型                          | 说明       |
| --------------------------- | -------- |
W
wangyanhandsome 已提交
299
| [DeviceClass](#deviceclass) | 远程设备的类别。 |
W
wangyanhandsome 已提交
300 301 302

**示例:**

S
smartbao 已提交
303
```js
W
wangyanhandsome 已提交
304
let remoteDeviceClass = bluetooth.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX");
W
wangyanhandsome 已提交
305 306 307
```


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

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

获取蓝牙配对列表。

S
smartbao 已提交
314 315 316 317
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
318 319
**返回值:**

H
HelloCrease 已提交
320 321
| 类型                  | 说明            |
| ------------------- | ------------- |
Z
zengyawen 已提交
322 323 324 325
| Array&lt;string&gt; | 已配对蓝牙设备的地址列表。 |

**示例:**

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


S
smartbao 已提交
331
## bluetooth.setBluetoothScanMode<sup>8+</sup><a name="setBluetoothScanMode"></a>
Z
zengyawen 已提交
332 333 334 335 336

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

设置蓝牙扫描模式,可以被远端设备发现。

S
smartbao 已提交
337 338 339 340
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
341 342
**参数:**

H
HelloCrease 已提交
343 344 345 346
| 参数名      | 类型                    | 必填   | 说明                           |
| -------- | --------------------- | ---- | ---------------------------- |
| mode     | [ScanMode](#scanmode) | 是    | 蓝牙扫描模式。                      |
| duration | number                | 是    | 设备可被发现的持续时间,单位为秒;设置为0则持续可发现。 |
Z
zengyawen 已提交
347 348 349

**返回值:**

H
HelloCrease 已提交
350 351
| 类型      | 说明                         |
| ------- | -------------------------- |
Z
zengyawen 已提交
352 353 354 355
| boolean | 设置蓝牙扫描,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
356
```js
Z
zengyawen 已提交
357
// 设置为可连接可发现才可被远端设备扫描到,可以连接。
S
smartbao 已提交
358
let result = bluetooth.setBluetoothScanMode(bluetooth.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
Z
zengyawen 已提交
359 360 361
```


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

getBluetoothScanMode(): ScanMode

获取蓝牙扫描模式。

S
smartbao 已提交
368 369 370 371
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
372 373
**返回值:**

H
HelloCrease 已提交
374 375
| 类型                    | 说明      |
| --------------------- | ------- |
Z
zengyawen 已提交
376 377 378 379
| [ScanMode](#scanmode) | 蓝牙扫描模式。 |

**示例:**

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


S
smartbao 已提交
385
## bluetooth.startBluetoothDiscovery<sup>8+</sup><a name="startBluetoothDiscovery"></a>
Z
zengyawen 已提交
386 387 388 389 390

startBluetoothDiscovery(): boolean

开启蓝牙扫描,可以发现远端设备。

391
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH;ohos.permission.LOCATION
S
smartbao 已提交
392 393 394

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
395 396
**返回值:**

H
HelloCrease 已提交
397 398
| 类型      | 说明                         |
| ------- | -------------------------- |
Z
zengyawen 已提交
399 400 401 402
| boolean | 开启蓝牙扫描,成功返回true,否则返回false。 |

**示例:**

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


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

stopBluetoothDiscovery(): boolean

关闭蓝牙扫描。

S
smartbao 已提交
419 420 421 422
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
423 424
**返回值:**

H
HelloCrease 已提交
425 426
| 类型      | 说明                         |
| ------- | -------------------------- |
Z
zengyawen 已提交
427 428 429 430
| boolean | 关闭蓝牙扫描,成功返回true,否则返回false。 |

**示例:**

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


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

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

设置设备配对请求确认。

S
smartbao 已提交
442 443 444 445
**需要权限**:ohos.permission.MANAGE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
446 447
**参数:**

H
HelloCrease 已提交
448 449 450 451
| 参数名    | 类型      | 必填   | 说明                               |
| ------ | ------- | ---- | -------------------------------- |
| device | string  | 是    | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| accept | boolean | 是    | 接受配对请求设置为true,否则设置为false。        |
Z
zengyawen 已提交
452 453 454

**返回值:**

H
HelloCrease 已提交
455 456
| 类型      | 说明                           |
| ------- | ---------------------------- |
Z
zengyawen 已提交
457 458 459 460
| boolean | 设置设备配对确认,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
461
```js
Z
zengyawen 已提交
462 463 464 465 466 467 468 469 470
// 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认
function onReceivePinRequiredEvent(data) { // data为配对请求的入参,配对请求参数
    console.info('pin required  = '+ JSON.stringify(data));
    bluetooth.setDevicePairingConfirmation(data.deviceId, true);
}
bluetooth.on("pinRequired", onReceivePinRequiredEvent);
```


S
smartbao 已提交
471
## bluetooth.on('bluetoothDeviceFind')<sup>8+</sup>
Z
zengyawen 已提交
472 473 474 475 476

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

订阅蓝牙设备发现上报事件。

S
smartbao 已提交
477 478 479 480
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
481 482
**参数:**

H
HelloCrease 已提交
483 484 485 486
| 参数名      | 类型                                  | 必填   | 说明                                     |
| -------- | ----------------------------------- | ---- | -------------------------------------- |
| type     | string                              | 是    | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 |
| callback | Callback&lt;Array&lt;string&gt;&gt; | 是    | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。    |
Z
zengyawen 已提交
487 488 489 490 491 492 493

**返回值:**



**示例:**

S
smartbao 已提交
494
```js
Z
zengyawen 已提交
495 496 497 498 499 500 501
function onReceiveEvent(data) { // data为蓝牙设备地址集合
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
```


S
smartbao 已提交
502
## bluetooth.off('bluetoothDeviceFind')<sup>8+</sup>
Z
zengyawen 已提交
503 504 505 506 507

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

取消订阅蓝牙设备发现上报事件。

S
smartbao 已提交
508 509 510 511
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
512 513
**参数:**

H
HelloCrease 已提交
514 515 516 517
| 参数名      | 类型                                  | 必填   | 说明                                       |
| -------- | ----------------------------------- | ---- | ---------------------------------------- |
| type     | string                              | 是    | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。   |
| callback | Callback&lt;Array&lt;string&gt;&gt; | 否    | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
518 519 520 521 522 523 524

**返回值:**



**示例:**

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


S
smartbao 已提交
534
## bluetooth.on('pinRequired')<sup>8+</sup>
Z
zengyawen 已提交
535 536 537 538 539

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

订阅远端蓝牙设备的配对请求事件。

S
smartbao 已提交
540 541 542 543
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
544 545
**参数:**

H
HelloCrease 已提交
546 547 548 549
| 参数名      | 类型                                       | 必填   | 说明                               |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| type     | string                                   | 是    | 填写"pinRequired"字符串,表示配对请求事件。     |
| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | 是    | 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。 |
Z
zengyawen 已提交
550 551 552 553 554 555 556

**返回值:**



**示例:**

S
smartbao 已提交
557
```js
Z
zengyawen 已提交
558 559 560 561 562 563 564
function onReceiveEvent(data) { // data为配对请求参数
    console.info('pin required = '+ JSON.stringify(data));
}
bluetooth.on('pinRequired', onReceiveEvent);
```


S
smartbao 已提交
565
## bluetooth.off('pinRequired')<sup>8+</sup>
Z
zengyawen 已提交
566 567 568 569 570

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

取消订阅远端蓝牙设备的配对请求事件。

S
smartbao 已提交
571 572 573 574
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
575 576
**参数:**

H
HelloCrease 已提交
577 578 579 580
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"pinRequired"字符串,表示配对请求事件。             |
| callback | Callback&lt;[PinRequiredParam](#pinrequiredparam)&gt; | 否    | 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
581 582 583 584 585 586 587

**返回值:**



**示例:**

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


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

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

订阅蓝牙配对状态改变事件。

S
smartbao 已提交
603 604 605 606
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
607 608
**参数:**

H
HelloCrease 已提交
609 610 611 612
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
| type     | string                                   | 是    | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 |
| callback | Callback&lt;[BondStateParam](#bondstate)&gt; | 是    | 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。    |
Z
zengyawen 已提交
613 614 615 616 617 618 619

**返回值:**



**示例:**

S
smartbao 已提交
620
```js
Z
zengyawen 已提交
621 622 623 624 625 626 627
function onReceiveEvent(data) { // data为回调函数入参,表示配对的状态
    console.info('pair state = '+ JSON.stringify(data));
}
bluetooth.on('bondStateChange', onReceiveEvent);
```


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

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

取消订阅蓝牙配对状态改变事件。

S
smartbao 已提交
634 635 636 637
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
638 639
**参数:**

H
HelloCrease 已提交
640 641 642 643
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。     |
| callback | Callback&lt;[BondStateParam](#bondstate)&gt; | 否    | 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
644 645 646 647 648 649 650

**返回值:**



**示例:**

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


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

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

订阅蓝牙连接状态改变事件。

S
smartbao 已提交
666 667 668 669
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
670 671
**参数:**

H
HelloCrease 已提交
672 673 674 675
| 参数名      | 类型                                       | 必填   | 说明                               |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| type     | string                                   | 是    | 填写"stateChange"字符串,表示蓝牙状态改变事件。   |
| callback | Callback&lt;[BluetoothState](#bluetoothstate)&gt; | 是    | 表示回调函数的入参,蓝牙状态。回调函数由用户创建通过该接口注册。 |
Z
zengyawen 已提交
676 677 678 679 680 681 682

**返回值:**



**示例:**

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


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

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

取消订阅蓝牙连接状态改变事件。

S
smartbao 已提交
697 698 699 700
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
701 702
**参数:**

H
HelloCrease 已提交
703 704 705 706
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"stateChange"字符串,表示蓝牙状态改变事件。           |
| callback | Callback&lt;[BluetoothState](#bluetoothstate)&gt; | 否    | 表示取消订阅蓝牙状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
707 708 709 710 711 712 713

**返回值:**



**示例:**

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


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

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

创建一个服务端监听Socket。

S
smartbao 已提交
729 730 731 732
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
733 734
**参数:**

H
HelloCrease 已提交
735 736 737 738 739
| 参数名      | 类型                          | 必填   | 说明                      |
| -------- | --------------------------- | ---- | ----------------------- |
| name     | string                      | 是    | 服务的名称。                  |
| option   | [SppOption](#sppoption)     | 是    | spp监听配置参数。              |
| callback | AsyncCallback&lt;number&gt; | 是    | 表示回调函数的入参,服务端Socket的id。 |
Z
zengyawen 已提交
740 741 742

**示例:**

S
smartbao 已提交
743
```js
Z
zengyawen 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757
let serverNumber = -1;
function serverSocket(code, number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}

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


S
smartbao 已提交
758
## bluetooth.sppAccept<sup>8+</sup><a name="sppAccept"></a>
Z
zengyawen 已提交
759 760 761 762 763

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

服务端监听socket等待客户端连接。

S
smartbao 已提交
764 765
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
766 767
**参数:**

H
HelloCrease 已提交
768 769 770 771
| 参数名          | 类型                          | 必填   | 说明                      |
| ------------ | --------------------------- | ---- | ----------------------- |
| serverSocket | number                      | 是    | 服务端socket的id。           |
| callback     | AsyncCallback&lt;number&gt; | 是    | 表示回调函数的入参,客户端socket的id。 |
Z
zengyawen 已提交
772 773 774

**示例:**

S
smartbao 已提交
775
```js
S
smartbao 已提交
776 777 778 779 780 781 782 783
let serverNumber = -1;
function serverSocket(code, number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}
Z
zengyawen 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796
let clientNumber = -1;
function acceptClientSocket(code, number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth clientSocket Number: ' + number);
    // 获取的clientNumber用作服务端后续读/写操作socket的id。
    clientNumber = number;
  }
}
bluetooth.sppAccept(serverNumber, acceptClientSocket);
```


S
smartbao 已提交
797
## bluetooth.sppConnect<sup>8+</sup><a name="sppConnect"></a>
Z
zengyawen 已提交
798 799 800 801 802

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

客户端向远端设备发起spp连接。

S
smartbao 已提交
803 804 805 806
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
807 808
**参数:**

H
HelloCrease 已提交
809 810 811 812 813
| 参数名      | 类型                          | 必填   | 说明                             |
| -------- | --------------------------- | ---- | ------------------------------ |
| device   | string                      | 是    | 对端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| option   | [SppOption](#sppoption)     | 是    | spp客户端连接配置参数。                  |
| callback | AsyncCallback&lt;number&gt; | 是    | 表示回调函数的入参,客户端socket的id。        |
Z
zengyawen 已提交
814 815 816

**示例:**

S
smartbao 已提交
817
```js
S
smartbao 已提交
818

Z
zengyawen 已提交
819 820 821 822 823 824 825 826 827 828
let clientNumber = -1;
function clientSocket(code, number) {
  if (code.code != 0) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // 获取的clientNumber用作客户端后续读/写操作socket的id。
  clientNumber = number;
}
let sppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
W
wangyanhandsome 已提交
829
bluetooth.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);
Z
zengyawen 已提交
830 831 832
```


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

sppCloseServerSocket(socket: number): void

关闭服务端监听Socket,入参socket由sppListen接口返回。

S
smartbao 已提交
839 840
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
841 842
**参数:**

H
HelloCrease 已提交
843 844 845
| 参数名    | 类型     | 必填   | 说明              |
| ------ | ------ | ---- | --------------- |
| socket | number | 是    | 服务端监听socket的id。 |
Z
zengyawen 已提交
846 847 848

**示例:**

S
smartbao 已提交
849
```js
S
smartbao 已提交
850 851 852 853 854 855 856 857
let serverNumber = -1;
function serverSocket(code, number) {
  console.log('bluetooth error code: ' + code.code);
  if (code.code == 0) {
    console.log('bluetooth serverSocket Number: ' + number);
    serverNumber = number;
  }
}
Z
zengyawen 已提交
858 859 860 861
bluetooth.sppCloseServerSocket(serverNumber);
```


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

sppCloseClientSocket(socket: number): void

关闭客户端socket,入参socket由sppAccept或sppConnect接口获取。

S
smartbao 已提交
868 869
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
870 871
**参数:**

H
HelloCrease 已提交
872 873 874 875
| 参数名    | 类型     | 必填   | 说明            |
| ------ | ------ | ---- | ------------- |
| 参数名    | 类型     | 必填   | 说明            |
| socket | number | 是    | 客户端socket的id。 |
Z
zengyawen 已提交
876 877 878

**示例:**

S
smartbao 已提交
879
```js
S
smartbao 已提交
880 881 882 883 884 885 886 887 888
let clientNumber = -1;
function clientSocket(code, number) {
  if (code.code != 0) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // 获取的clientNumber用作客户端后续读/写操作socket的id。
  clientNumber = number;
}
Z
zengyawen 已提交
889 890 891 892
bluetooth.sppCloseClientSocket(clientNumber);
```


S
smartbao 已提交
893
## bluetooth.sppWrite<sup>8+</sup><a name="sppWrite"></a>
Z
zengyawen 已提交
894 895 896 897 898

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

通过socket向远端发送数据,入参clientSocket由sppAccept或sppConnect接口获取 。

S
smartbao 已提交
899 900
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
901 902
**参数:**

H
HelloCrease 已提交
903 904 905 906
| 参数名          | 类型          | 必填   | 说明            |
| ------------ | ----------- | ---- | ------------- |
| clientSocket | number      | 是    | 客户端socket的id。 |
| data         | ArrayBuffer | 是    | 写入的数据。        |
Z
zengyawen 已提交
907 908 909

**返回值:**

H
HelloCrease 已提交
910 911
| 类型      | 说明                        |
| ------- | ------------------------- |
Z
zengyawen 已提交
912 913 914 915
| boolean | 写数据操作,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
916
```js
S
smartbao 已提交
917 918 919 920 921 922 923 924 925
let clientNumber = -1;
function clientSocket(code, number) {
  if (code.code != 0) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // 获取的clientNumber用作客户端后续读/写操作socket的id。
  clientNumber = number;
}
Z
zengyawen 已提交
926 927 928 929 930 931 932 933 934 935 936 937
let arrayBuffer = new ArrayBuffer(8);
let data = new Uint8Array(arrayBuffer);
data[0] = 123;
let ret = bluetooth.sppWrite(clientNumber, arrayBuffer);
if (ret) {
  console.log('spp write successfully');
} else {
  console.log('spp write failed');
}
```


S
smartbao 已提交
938
## bluetooth.on('sppRead')<sup>8+</sup>
Z
zengyawen 已提交
939 940 941 942 943

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

订阅spp读请求事件,入参clientSocket由sppAccept或sppConnect接口获取。

S
smartbao 已提交
944 945
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
946 947
**参数:**

H
HelloCrease 已提交
948 949 950 951 952
| 参数名          | 类型                          | 必填   | 说明                         |
| ------------ | --------------------------- | ---- | -------------------------- |
| type         | string                      | 是    | 填写"sppRead"字符串,表示spp读请求事件。 |
| clientSocket | number                      | 是    | 客户端socket的id。              |
| callback     | Callback&lt;ArrayBuffer&gt; | 是    | 表示回调函数的入参,读取到的数据。          |
Z
zengyawen 已提交
953 954 955 956 957 958 959

**返回值:**



**示例:**

S
smartbao 已提交
960
```js
S
smartbao 已提交
961 962 963 964 965 966 967 968 969
let clientNumber = -1;
function clientSocket(code, number) {
  if (code.code != 0) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // 获取的clientNumber用作客户端后续读/写操作socket的id。
  clientNumber = number;
}
Z
zengyawen 已提交
970 971 972 973 974 975 976 977
function dataRead(dataBuffer) {
  let data = new Uint8Array(dataBuffer);
  console.log('bluetooth data is: ' + data[0]);
}
bluetooth.on('sppRead', clientNumber, dataRead);
```


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

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

取消订阅spp读请求事件,入参clientSocket由sppAccept或sppConnect接口获取。

S
smartbao 已提交
984 985
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
986 987
**参数:**

H
HelloCrease 已提交
988 989 990 991 992
| 参数名          | 类型                          | 必填   | 说明                                       |
| ------------ | --------------------------- | ---- | ---------------------------------------- |
| type         | string                      | 是    | 填写"sppRead"字符串,表示spp读请求事件。               |
| clientSocket | number                      | 是    | 客户端Socket的id。                            |
| callback     | Callback&lt;ArrayBuffer&gt; | 否    | 表示取消订阅spp读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
993 994 995 996 997 998 999

**返回值:**



**示例:**

S
smartbao 已提交
1000
```js
S
smartbao 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009
let clientNumber = -1;
function clientSocket(code, number) {
  if (code.code != 0) {
    return;
  }
  console.log('bluetooth serverSocket Number: ' + number);
  // 获取的clientNumber用作客户端后续读/写操作socket的id。
  clientNumber = number;
}
Z
zengyawen 已提交
1010 1011 1012 1013
bluetooth.off('sppRead', clientNumber);
```


S
smartbao 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
## bluetooth.getProfile<sup>8+</sup><a name="getProfile"></a>

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

通过ProfileId,获取profile的对象实例。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1024 1025
| 参数名       | 类型        | 必填   | 说明                                    |
| --------- | --------- | ---- | ------------------------------------- |
L
lee 已提交
1026
| profileId | [ProfileId](#ProfileId) | 是    | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 |
S
smartbao 已提交
1027 1028 1029

**返回值:**

L
lee 已提交
1030 1031 1032
| 类型                                                         | 说明                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [A2dpSourceProfile](#A2dpSourceProfile)[HandsFreeAudioGatewayProfile](#HandsFreeAudioGatewayProfile) | 对应的profile的对象实例,当前支持A2dpSourceProfile, HandsFreeAudioGatewayProfile。 |
S
smartbao 已提交
1033 1034 1035 1036

**示例:**

```js
S
smartbao 已提交
1037
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
S
smartbao 已提交
1038 1039
```

L
lee 已提交
1040 1041
## bluetooth.getProfile<sup>9+</sup><a name="getProfile"></a>

L
lverpeng 已提交
1042
getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile | HidHostProfile | PanProfile
L
lee 已提交
1043

L
lverpeng 已提交
1044
通过ProfileId,获取profile的对象实例,API9新增了HidHostProfile,PanProfile。
L
lee 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名       | 类型        | 必填   | 说明                                    |
| --------- | --------- | ---- | ------------------------------------- |
| profileId | [ProfileId](#ProfileId) | 是    | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 |

**返回值:**

| 类型                                                         | 说明                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
L
lverpeng 已提交
1058
| [A2dpSourceProfile](#A2dpSourceProfile)[HandsFreeAudioGatewayProfile](#HandsFreeAudioGatewayProfile)[HidHostProfile](#HidHostProfile)[PanProfile](#PanProfile) | 对应的profile的对象实例,当前支持A2dpSourceProfile/HandsFreeAudioGatewayProfile/HidHostProfile/PanProfile。 |
L
lee 已提交
1059 1060 1061 1062

**示例:**

```js
S
smartbao 已提交
1063
let hidHost = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
L
lee 已提交
1064 1065
```

S
smartbao 已提交
1066

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

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

创建一个可使用的GattServer实例。

S
smartbao 已提交
1075 1076
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
1077 1078
**返回值:**

H
HelloCrease 已提交
1079 1080
| 类型                        | 说明                                   |
| ------------------------- | ------------------------------------ |
Z
zengyawen 已提交
1081 1082 1083 1084
| [GattServer](#gattserver) | server端类,使用server端方法之前需要创建该类的实例进行操作。 |

**示例:**

S
smartbao 已提交
1085
```js
Z
zengyawen 已提交
1086 1087 1088 1089
let gattServer = bluetooth.BLE.createGattServer();
```


S
smartbao 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
### bluetooth.BLE.createGattClientDevice

createGattClientDevice(deviceId: string): GattClientDevice

创建一个可使用的GattClientDevice实例。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1100 1101 1102
| 参数名      | 类型     | 必填   | 说明                                   |
| -------- | ------ | ---- | ------------------------------------ |
| deviceId | string | 是    | 对端设备地址,&nbsp;例如:"XX:XX:XX:XX:XX:XX"。 |
S
smartbao 已提交
1103 1104 1105

**返回值:**

H
HelloCrease 已提交
1106 1107
| 类型                                    | 说明                                   |
| ------------------------------------- | ------------------------------------ |
S
smartbao 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
| [GattClientDevice](#gattclientdevice) | client端类,使用client端方法之前需要创建该类的实例进行操作。 |

**示例:**

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


### bluetooth.BLE.getConnectedBLEDevices

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

获取和当前设备连接的BLE设备。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**返回值:**

H
HelloCrease 已提交
1129 1130
| 类型                  | 说明                  |
| ------------------- | ------------------- |
L
napi  
lverpeng 已提交
1131
| Array&lt;string&gt; | 返回当前设备作为Server端时连接BLE设备地址集合。 |
S
smartbao 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

**示例:**

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


### bluetooth.BLE.startBLEScan

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

发起BLE扫描流程。

1146
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH;ohos.permission.MANAGE_BLUETOOTH;ohos.permission.LOCATION
S
smartbao 已提交
1147 1148 1149 1150 1151

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1152 1153 1154 1155
| 参数名     | 类型                                     | 必填   | 说明                                  |
| ------- | -------------------------------------- | ---- | ----------------------------------- |
| filters | Array&lt;[ScanFilter](#scanfilter)&gt; | 是    | 表示扫描结果过滤策略集合,如果不使用过滤的方式,该参数设置为null。 |
| options | [ScanOptions](#scanoptions)            | 否    | 表示扫描的参数配置,可选参数。                     |
S
smartbao 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

**返回值:**



**示例:**

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

停止BLE扫描流程。

**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**返回值:**



**示例:**

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


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

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

订阅BLE设备发现上报事件。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1216 1217 1218 1219
| 参数名      | 类型                                       | 必填   | 说明                                  |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| type     | string                                   | 是    | 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。   |
| callback | Callback&lt;Array&lt;[ScanResult](#scanresult)&gt;&gt; | 是    | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 |
S
smartbao 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

**返回值:**



**示例:**

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

取消订阅BLE设备发现上报事件。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1247 1248 1249 1250
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。        |
| callback | Callback&lt;Array&lt;[ScanResult](#scanresult)&gt;&gt; | 否    | 表示取消订阅BLE设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 |
S
smartbao 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

**返回值:**



**示例:**

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


## BaseProfile

profile基类。


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

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

获取已连接设备列表。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**



**返回值:**

H
HelloCrease 已提交
1288
| 类型                  | 说明            |
L
lee 已提交
1289
| ------------------- | ------------- |
S
smartbao 已提交
1290 1291
| Array&lt;string&gt; | 返回已连接设备的地址列表。 |

L
lee 已提交
1292 1293 1294
**示例:**

```js
S
smartbao 已提交
1295
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE)
L
lee 已提交
1296 1297
let retArray = a2dpSrc.getConnectionDevices();
```
S
smartbao 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

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

getDeviceState(device: string): ProfileConnectionState

获取设备profile的连接状态。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1311 1312 1313
| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |
S
smartbao 已提交
1314 1315 1316

**返回值:**

Z
zengyawen 已提交
1317
| 类型                                              | 说明                    |
L
lee 已提交
1318
| ------------------------------------------------- | ----------------------- |
Z
zengyawen 已提交
1319
| [ProfileConnectionState](#profileconnectionstate) | 返回profile的连接状态。 |
S
smartbao 已提交
1320

L
lee 已提交
1321 1322 1323
**示例:**

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

## A2dpSourceProfile

使用A2dpSourceProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。


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

connect(device: string): boolean

发起设备的A2dp服务连接请求。

**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1345 1346 1347
| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |
S
smartbao 已提交
1348 1349 1350

**返回值:**

H
HelloCrease 已提交
1351
| 类型      | 说明                  |
L
lee 已提交
1352
| ------- | ------------------- |
S
smartbao 已提交
1353 1354 1355 1356 1357
| boolean | 成功返回true,失败返回false。 |

**示例:**

```js
S
smartbao 已提交
1358
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE)
H
HelloCrease 已提交
1359
let ret = a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
S
smartbao 已提交
1360 1361 1362
```


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

disconnect(device: string): boolean

断开设备的a2dp服务连接。

**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1375 1376 1377
| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |
S
smartbao 已提交
1378 1379 1380

**返回值:**

H
HelloCrease 已提交
1381
| 类型      | 说明                  |
L
lee 已提交
1382
| ------- | ------------------- |
S
smartbao 已提交
1383 1384 1385 1386 1387
| boolean | 成功返回true,失败返回false。 |

**示例:**

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


L
lee 已提交
1393
### on('connectionStateChange')<sup>8+</sup>
S
smartbao 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402

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

订阅a2dp连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1403 1404 1405 1406
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 是    | 表示回调函数的入参。                               |
S
smartbao 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

**返回值:**



**示例:**

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


L
lee 已提交
1423
### off('connectionStateChange')<sup>8+</sup>
S
smartbao 已提交
1424

1425
off(type: "connectionStateChange", callback?: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
S
smartbao 已提交
1426 1427 1428 1429 1430 1431 1432

取消订阅a2dp连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1433 1434 1435
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
L
lee 已提交
1436
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 否    | 表示回调函数的入参。                               |
S
smartbao 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

**返回值:**



**示例:**

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


1454
### getPlayingState<sup>9+</sup>
Z
zengyawen 已提交
1455

S
smartbao 已提交
1456
getPlayingState(device: string): PlayingState
Z
zengyawen 已提交
1457

S
smartbao 已提交
1458 1459
获取设备的播放状态。

1460 1461
**需要权限**:ohos.permission.USE_BLUETOOTH

S
smartbao 已提交
1462
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
Z
zengyawen 已提交
1463 1464 1465

**参数:**

H
HelloCrease 已提交
1466 1467 1468
| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |
Z
zengyawen 已提交
1469 1470 1471

**返回值:**

H
HelloCrease 已提交
1472
| 类型                            | 说明         |
L
lee 已提交
1473
| ----------------------------- | ---------- |
S
smartbao 已提交
1474
| [PlayingState](#PlayingState) | 远端设备的播放状态。 |
Z
zengyawen 已提交
1475 1476 1477

**示例:**

S
smartbao 已提交
1478
```js
S
smartbao 已提交
1479
let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
H
HelloCrease 已提交
1480
let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1481 1482 1483
```


S
smartbao 已提交
1484
## HandsFreeAudioGatewayProfile
Z
zengyawen 已提交
1485

S
smartbao 已提交
1486
使用HandsFreeAudioGatewayProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。
Z
zengyawen 已提交
1487 1488


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

S
smartbao 已提交
1491
connect(device: string): boolean
Z
zengyawen 已提交
1492

S
smartbao 已提交
1493
连接设备的HFP服务。
Z
zengyawen 已提交
1494

S
smartbao 已提交
1495
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
Z
zengyawen 已提交
1496

S
smartbao 已提交
1497
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
Z
zengyawen 已提交
1498 1499 1500

**参数:**

H
HelloCrease 已提交
1501 1502 1503
| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |
Z
zengyawen 已提交
1504 1505 1506

**返回值:**

H
HelloCrease 已提交
1507
| 类型      | 说明                  |
L
lee 已提交
1508
| ------- | ------------------- |
S
smartbao 已提交
1509
| boolean | 成功返回true,失败返回false。 |
Z
zengyawen 已提交
1510 1511 1512

**示例:**

S
smartbao 已提交
1513
```js
S
smartbao 已提交
1514
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
H
HelloCrease 已提交
1515
let ret = hfpAg.connect('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1516 1517 1518
```


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

S
smartbao 已提交
1521
disconnect(device: string): boolean
Z
zengyawen 已提交
1522

Z
zengyawen 已提交
1523
断开连接设备的HFP服务。
S
smartbao 已提交
1524 1525 1526 1527 1528 1529 1530

**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

H
HelloCrease 已提交
1531 1532 1533
| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |
Z
zengyawen 已提交
1534 1535 1536

**返回值:**

H
HelloCrease 已提交
1537 1538
| 类型      | 说明                  |
| ------- | ------------------- |
S
smartbao 已提交
1539
| boolean | 成功返回true,失败返回false。 |
Z
zengyawen 已提交
1540 1541 1542

**示例:**

S
smartbao 已提交
1543
```js
S
smartbao 已提交
1544
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
H
HelloCrease 已提交
1545
let ret = hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1546 1547 1548
```


L
lee 已提交
1549
### on('connectionStateChange')<sup>8+</sup>
Z
zengyawen 已提交
1550

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

S
smartbao 已提交
1553 1554 1555
订阅HFP连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。
Z
zengyawen 已提交
1556 1557 1558

**参数:**

H
HelloCrease 已提交
1559 1560 1561 1562
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 是    | 表示回调函数的入参。                               |
Z
zengyawen 已提交
1563 1564 1565 1566 1567 1568 1569

**返回值:**



**示例:**

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


L
lee 已提交
1579
### off('connectionStateChange')<sup>8+</sup>
Z
zengyawen 已提交
1580

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

S
smartbao 已提交
1583 1584 1585
取消订阅HFP连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。
Z
zengyawen 已提交
1586 1587 1588

**参数:**

H
HelloCrease 已提交
1589 1590 1591
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
L
lee 已提交
1592
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 否    | 表示回调函数的入参。                               |
Z
zengyawen 已提交
1593 1594 1595 1596 1597 1598 1599

**返回值:**



**示例:**

S
smartbao 已提交
1600
```js
Z
zengyawen 已提交
1601
function onReceiveEvent(data) {
S
smartbao 已提交
1602
    console.info('hfp state = '+ JSON.stringify(data));
Z
zengyawen 已提交
1603
}
S
smartbao 已提交
1604
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
L
lee 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
hfpAg.on('connectionStateChange', onReceiveEvent);
hfpAg.off('connectionStateChange', onReceiveEvent);
```


## HidHostProfile

使用HidHostProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。


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

connect(device: string): boolean

连接设备的HidHost服务。

1621 1622
此接口为系统接口。

L
lee 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |

**返回值:**

| 类型      | 说明                  |
| --------------------- | --------------------------------- |
| boolean | 成功返回true,失败返回false。 |

**示例:**

```js
S
smartbao 已提交
1642
let hidHostProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HID_HOST);
L
lee 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
let ret = hidHostProfile.connect('XX:XX:XX:XX:XX:XX');
```


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

disconnect(device: string): boolean

断开连接设备的HidHost服务。

1653 1654
此接口为系统接口。

L
lee 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |

**返回值:**

| 类型      | 说明                  |
| --------------------- | --------------------------------- |
| boolean | 成功返回true,失败返回false。 |

**示例:**

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


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

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

订阅HidHost连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 是    | 表示回调函数的入参。                               |

**返回值:**



**示例:**

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


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

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

取消订阅HidHost连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名   | 类型                                                  | 必填 | 说明                                                      |
| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- |
| type     | string                                                | 是   | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 否   | 表示回调函数的入参。                                      |

**返回值:**



**示例:**

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


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

使用PanProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。


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

disconnect(device: string): boolean

断开连接设备的Pan服务。

此接口为系统接口。

**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| device | string | 是    | 远端设备地址。 |

**返回值:**

| 类型      | 说明                  |
| --------------------- | --------------------------------- |
| boolean | 成功返回true,失败返回false。 |

**示例:**

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


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

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

订阅Pan连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 是    | 表示回调函数的入参。                               |

**返回值:**



**示例:**

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


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

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

取消订阅Pan连接状态变化事件。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名   | 类型                                                  | 必填 | 说明                                                      |
| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- |
| type     | string                                                | 是   | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 否   | 表示回调函数的入参。                                      |

**返回值:**



**示例:**

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


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

1840
setTethering(enable: boolean): void
L
lverpeng 已提交
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893

设置网络共享状态。

此接口为系统接口。

**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**参数:**

| 参数名    | 类型     | 必填   | 说明      |
| ------ | ------ | ---- | ------- |
| value | boolean | 是    | 是否设置蓝牙共享。 |

**返回值:**

| 类型      | 说明                  |
| --------------------- | --------------------------------- |
| boolean | 成功返回true,失败返回false。 |

**示例:**

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


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

isTetheringOn(): boolean

获取网络共享状态。

此接口为系统接口。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

**返回值:**

| 类型      | 说明                  |
| --------------------- | --------------------------------- |
| boolean | 网络共享开启返回true,网络共享关闭返回false。 |

**示例:**

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


Z
zengyawen 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
## GattServer

server端类,使用server端方法之前需要创建该类的实例进行操作,通过createGattServer()方法构造此实例。


### startAdvertising

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

开始发送BLE广播。

S
smartbao 已提交
1905 1906 1907 1908
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
1909 1910
**参数:**

H
HelloCrease 已提交
1911 1912 1913 1914 1915
| 参数名         | 类型                                    | 必填   | 说明             |
| ----------- | ------------------------------------- | ---- | -------------- |
| setting     | [AdvertiseSetting](#advertisesetting) | 是    | BLE广播的相关参数。    |
| advData     | [AdvertiseData](#advertisedata)       | 是    | BLE广播包内容。      |
| advResponse | [AdvertiseData](#advertisedata)       | 否    | BLE回复扫描请求回复响应。 |
Z
zengyawen 已提交
1916 1917 1918 1919 1920 1921 1922

**返回值:**



**示例:**

S
smartbao 已提交
1923
```js
Z
zengyawen 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
let manufactureValueBuffer = new Uint8Array(4);
manufactureValueBuffer[0] = 1;
manufactureValueBuffer[1] = 2;
manufactureValueBuffer[2] = 3;
manufactureValueBuffer[3] = 4;

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


### stopAdvertising

stopAdvertising(): void

停止发送BLE广播。

S
smartbao 已提交
1972 1973 1974 1975
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
1976 1977 1978 1979 1980 1981
**返回值:**



**示例:**

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


### addService

addService(service: GattService): boolean

server端添加服务。

S
smartbao 已提交
1994 1995 1996 1997
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
1998 1999
**参数:**

H
HelloCrease 已提交
2000 2001 2002
| 参数名     | 类型                          | 必填   | 说明                       |
| ------- | --------------------------- | ---- | ------------------------ |
| service | [GattService](#gattservice) | 是    | 服务端的service数据。BLE广播的相关参数 |
Z
zengyawen 已提交
2003 2004 2005

**返回值:**

H
HelloCrease 已提交
2006 2007
| 类型      | 说明                         |
| ------- | -------------------------- |
Z
zengyawen 已提交
2008 2009 2010 2011
| boolean | 添加服务操作,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
2012
```js
Z
zengyawen 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
// 创建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;

// 创建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',
S
smartbao 已提交
2031
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
Z
zengyawen 已提交
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
characteristics[0] = characteristic;

// 创建gattService
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

删除已添加的服务。

S
smartbao 已提交
2053 2054 2055 2056
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2057 2058
**参数:**

H
HelloCrease 已提交
2059 2060 2061
| 参数名         | 类型     | 必填   | 说明                                       |
| ----------- | ------ | ---- | ---------------------------------------- |
| serviceUuid | string | 是    | service的UUID,例如“00001810-0000-1000-8000-00805F9B34FB”。 |
Z
zengyawen 已提交
2062 2063 2064

**返回值:**

H
HelloCrease 已提交
2065 2066 2067
|         |                            |
| ------- | -------------------------- |
| 类型      | 说明                         |
Z
zengyawen 已提交
2068 2069 2070 2071
| boolean | 删除服务操作,成功返回true,否则返回false。 |

**示例:**

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


### close

close(): void

关闭服务端功能,去注册server在协议栈的注册,调用该接口后[GattServer](#gattserver)实例将不能再使用。

S
smartbao 已提交
2084 2085 2086 2087
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2088 2089
**示例:**

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


### notifyCharacteristicChanged

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

server端特征值发生变化时,主动通知已连接的client设备。

S
smartbao 已提交
2102 2103 2104 2105
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2106 2107
**参数:**

H
HelloCrease 已提交
2108 2109 2110 2111
| 参数名                  | 类型                                       | 必填   | 说明                                      |
| -------------------- | ---------------------------------------- | ---- | --------------------------------------- |
| deviceId             | string                                   | 是    | 接收通知的client端设备地址,例如“XX:XX:XX:XX:XX:XX”。 |
| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristic) | 是    | 通知的特征值数据。                               |
Z
zengyawen 已提交
2112 2113 2114

**返回值:**

H
HelloCrease 已提交
2115 2116 2117
|         |                          |
| ------- | ------------------------ |
| 类型      | 说明                       |
Z
zengyawen 已提交
2118 2119 2120 2121
| boolean | 通知操作,成功返回true,否则返回false。 |

**示例:**

S
smartbao 已提交
2122
```js
2123 2124 2125 2126 2127 2128 2129 2130 2131
// 创建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;
S
smartbao 已提交
2132 2133 2134
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 已提交
2135
let notifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
S
smartbao 已提交
2136
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false};
Z
zengyawen 已提交
2137
let server = bluetooth.BLE.createGattServer();
W
wangyanhandsome 已提交
2138
server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic);
Z
zengyawen 已提交
2139 2140 2141 2142 2143 2144 2145 2146 2147
```


### sendResponse

sendResponse(serverResponse: ServerResponse): boolean

server端回复client端的读写请求。

S
smartbao 已提交
2148 2149 2150 2151
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2152 2153
**参数:**

H
HelloCrease 已提交
2154 2155 2156
| 参数名            | 类型                                | 必填   | 说明              |
| -------------- | --------------------------------- | ---- | --------------- |
| serverResponse | [ServerResponse](#serverresponse) | 是    | server端回复的响应数据。 |
Z
zengyawen 已提交
2157 2158 2159

**返回值:**

H
HelloCrease 已提交
2160 2161 2162
|         |                            |
| ------- | -------------------------- |
| 类型      | 说明                         |
Z
zengyawen 已提交
2163 2164 2165 2166
| boolean | 回复响应操作,成功返回true,否则返回false。 |

**示例:**

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

server端订阅特征值读请求事件。

S
smartbao 已提交
2196 2197 2198 2199
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2200 2201
**参数:**

H
HelloCrease 已提交
2202 2203 2204 2205
| 参数名      | 类型                                       | 必填   | 说明                                    |
| -------- | ---------------------------------------- | ---- | ------------------------------------- |
| type     | string                                   | 是    | 填写"characteristicRead"字符串,表示特征值读请求事件。 |
| callback | Callback&lt;[CharacteristicReadReq](#characteristicreadreq)&gt; | 是    | 表示回调函数的入参,client端发送的读请求数据。            |
Z
zengyawen 已提交
2206 2207 2208 2209 2210 2211 2212

**返回值:**



**示例:**

S
smartbao 已提交
2213
```js
Z
zengyawen 已提交
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
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

server端取消订阅特征值读请求事件。

S
smartbao 已提交
2244 2245 2246 2247
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2248 2249
**参数:**

H
HelloCrease 已提交
2250 2251 2252 2253
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"characteristicRead"字符串,表示特征值读请求事件。    |
| callback | Callback&lt;[CharacteristicReadReq](#characteristicreadreq)&gt; | 否    | 表示取消订阅特征值读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
2254 2255 2256 2257 2258 2259 2260

**返回值:**



**示例:**

S
smartbao 已提交
2261
```js
Z
zengyawen 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("characteristicRead");
```


### on('characteristicWrite')

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

server端订阅特征值写请求事件。

S
smartbao 已提交
2273 2274 2275 2276
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2277 2278
**参数:**

H
HelloCrease 已提交
2279 2280 2281
| 参数名      | 类型                                       | 必填   | 说明                                     |
| -------- | ---------------------------------------- | ---- | -------------------------------------- |
| type     | string                                   | 是    | 填写"characteristicWrite"字符串,表示特征值写请求事件。 |
A
annie_wangli 已提交
2282
| callback | Callback&lt;[CharacteristicWriteReq](#characteristicwritereq)&gt; | 是    | 表示回调函数的入参,client端发送的写请求数据。             |
Z
zengyawen 已提交
2283 2284 2285 2286 2287 2288 2289

**返回值:**



**示例:**

S
smartbao 已提交
2290
```js
Z
zengyawen 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
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

server端取消订阅特征值写请求事件。

S
smartbao 已提交
2324 2325 2326 2327
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2328 2329
**参数:**

H
HelloCrease 已提交
2330 2331 2332 2333
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"characteristicWrite"字符串,表示特征值写请求事件。   |
| callback | Callback&lt;[CharacteristicWriteReq](#characteristicwritereq)&gt; | 否    | 表示取消订阅特征值写请求事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
2334 2335 2336 2337 2338 2339 2340

**返回值:**



**示例:**

S
smartbao 已提交
2341
```js
Z
zengyawen 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("characteristicWrite");
```


### on('descriptorRead')

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

server端订阅描述符读请求事件。

S
smartbao 已提交
2353 2354 2355 2356
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2357 2358
**参数:**

H
HelloCrease 已提交
2359 2360 2361 2362
| 参数名      | 类型                                       | 必填   | 说明                                |
| -------- | ---------------------------------------- | ---- | --------------------------------- |
| type     | string                                   | 是    | 填写"descriptorRead"字符串,表示描述符读请求事件。 |
| callback | Callback&lt;[DescriptorReadReq](#descriptorreadreq)&gt; | 是    | 表示回调函数的入参,client端发送的读请求数据。        |
Z
zengyawen 已提交
2363 2364 2365 2366 2367 2368 2369

**返回值:**



**示例:**

S
smartbao 已提交
2370
```js
Z
zengyawen 已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
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

server端取消订阅描述符读请求事件。

S
smartbao 已提交
2401 2402 2403 2404
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2405 2406
**参数:**

H
HelloCrease 已提交
2407 2408 2409 2410
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"descriptorRead"字符串,表示描述符读请求事件。        |
| callback | Callback&lt;[DescriptorReadReq](#descriptorreadreq)&gt; | 否    | 表示取消订阅描述符读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
2411 2412 2413 2414 2415 2416 2417

**返回值:**



**示例:**

S
smartbao 已提交
2418
```js
Z
zengyawen 已提交
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("descriptorRead");
```


### on('descriptorWrite')

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

server端订阅描述符写请求事件。

S
smartbao 已提交
2430 2431 2432 2433
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2434 2435
**参数:**

H
HelloCrease 已提交
2436 2437 2438 2439
| 参数名      | 类型                                       | 必填   | 说明                                 |
| -------- | ---------------------------------------- | ---- | ---------------------------------- |
| type     | string                                   | 是    | 填写"descriptorWrite"字符串,表示描述符写请求事件。 |
| callback | Callback&lt;[DescriptorWriteReq](#descriptorwritereq)&gt; | 是    | 表示回调函数的入参,client端发送的写请求数据。         |
Z
zengyawen 已提交
2440 2441 2442 2443 2444 2445 2446

**返回值:**



**示例:**

S
smartbao 已提交
2447
```js
Z
zengyawen 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
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

server端取消订阅描述符写请求事件。

S
smartbao 已提交
2481 2482 2483 2484
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2485 2486
**参数:**

H
HelloCrease 已提交
2487 2488 2489 2490
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"descriptorWrite"字符串,表示描述符写请求事件。       |
| callback | Callback&lt;[DescriptorWriteReq](#descriptorwritereq)&gt; | 否    | 表示取消订阅描述符写请求事件上报。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
2491 2492 2493 2494 2495 2496 2497

**返回值:**



**示例:**

S
smartbao 已提交
2498
```js
Z
zengyawen 已提交
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("descriptorWrite");
```


### on('connectStateChange')

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

server端订阅BLE连接状态变化事件。

S
smartbao 已提交
2510 2511 2512 2513
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2514 2515
**参数:**

H
HelloCrease 已提交
2516 2517 2518 2519
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectStateChange"字符串,表示BLE连接状态变化事件。 |
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | 是    | 表示回调函数的入参,连接状态。                          |
Z
zengyawen 已提交
2520 2521 2522 2523 2524 2525 2526

**返回值:**



**示例:**

S
smartbao 已提交
2527
```js
Z
zengyawen 已提交
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
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

server端取消订阅BLE连接状态变化事件。

S
smartbao 已提交
2544 2545 2546 2547
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2548 2549
**参数:**

H
HelloCrease 已提交
2550 2551 2552 2553
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"connectStateChange"字符串,表示BLE连接状态变化事件。 |
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | 否    | 表示取消订阅BLE连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
2554 2555 2556 2557 2558 2559 2560

**返回值:**



**示例:**

S
smartbao 已提交
2561
```js
Z
zengyawen 已提交
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
let gattServer = bluetooth.BLE.createGattServer();
gattServer.off("connectStateChange");
```


## GattClientDevice

client端类,使用client端方法之前需要创建该类的实例进行操作,通过createGattClientDevice(deviceId: string)方法构造此实例。


### connect

connect(): boolean

client端发起连接远端蓝牙低功耗设备。

S
smartbao 已提交
2578 2579 2580 2581
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2582 2583
**返回值:**

H
HelloCrease 已提交
2584 2585
| 类型      | 说明                        |
| ------- | ------------------------- |
Z
zengyawen 已提交
2586 2587 2588 2589
| boolean | 连接操作成功返回true,操作失败返回false。 |

**示例:**

S
smartbao 已提交
2590
```js
W
wangyanhandsome 已提交
2591
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
let ret = device.connect();
```


### disconnect

disconnect(): boolean

client端断开与远端蓝牙低功耗设备的连接。

S
smartbao 已提交
2602 2603 2604 2605
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2606 2607
**返回值:**

H
HelloCrease 已提交
2608 2609
| 类型      | 说明                           |
| ------- | ---------------------------- |
Z
zengyawen 已提交
2610
| boolean | 断开连接操作,成功返回true,操作失败返回false。 |
Z
zengyawen 已提交
2611 2612 2613

**示例:**

S
smartbao 已提交
2614
```js
W
wangyanhandsome 已提交
2615
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
let ret = device.disconnect();
```


### close

close(): boolean

关闭客户端功能,注销client在协议栈的注册,调用该接口后[GattClientDevice](#gattclientdevice)实例将不能再使用。

S
smartbao 已提交
2626 2627 2628 2629
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2630 2631
**返回值:**

H
HelloCrease 已提交
2632 2633
| 类型      | 说明                         |
| ------- | -------------------------- |
Z
zengyawen 已提交
2634 2635 2636 2637
| boolean | 关闭操作,成功返回true,操作失败返回false。 |

**示例:**

S
smartbao 已提交
2638
```js
W
wangyanhandsome 已提交
2639
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
let ret = device.close();
```




### getServices

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

client端获取蓝牙低功耗设备的所有服务,即服务发现 。

S
smartbao 已提交
2652 2653 2654 2655
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2656 2657
**参数:**

H
HelloCrease 已提交
2658 2659 2660
| 参数名      | 类型                                       | 必填   | 说明                       |
| -------- | ---------------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;Array&lt;[GattService](#gattservice)&gt;&gt; | 是    | client进行服务发现,通过注册回调函数获取。 |
Z
zengyawen 已提交
2661 2662 2663 2664 2665 2666 2667

**返回值:**



**示例:**

S
smartbao 已提交
2668
```js
Z
zengyawen 已提交
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
// callkback 模式
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);
      }
  }
}

W
wangyanhandsome 已提交
2682
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
device.connect();
device.getServices(getServices);
```


### getServices

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

client端获取蓝牙低功耗设备的所有服务,即服务发现。

S
smartbao 已提交
2694 2695 2696 2697
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2698 2699 2700 2701
**参数:**

**返回值:**

H
HelloCrease 已提交
2702 2703
| 类型                                       | 说明                          |
| ---------------------------------------- | --------------------------- |
Z
zengyawen 已提交
2704 2705 2706 2707
| Promise&lt;Array&lt;[GattService](#gattservice)&gt;&gt; | client进行服务发现,通过promise形式获取。 |

**示例:**

S
smartbao 已提交
2708
```js
Z
zengyawen 已提交
2709
// Promise 模式
2710 2711 2712
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
device.connect();
device.getServices().then(result => {
2713 2714
    console.info("getServices successfully:" + JSON.stringify(result));
});
Z
zengyawen 已提交
2715 2716 2717 2718 2719 2720 2721 2722 2723
```


### readCharacteristicValue

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

client端读取蓝牙低功耗设备特定服务的特征值。

S
smartbao 已提交
2724 2725 2726 2727
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2728 2729
**参数:**

H
HelloCrease 已提交
2730 2731 2732 2733
| 参数名            | 类型                                       | 必填   | 说明                      |
| -------------- | ---------------------------------------- | ---- | ----------------------- |
| characteristic | [BLECharacteristic](#blecharacteristic)  | 是    | 待读取的特征值。                |
| callback       | AsyncCallback&lt;[BLECharacteristic](#blecharacteristic)&gt; | 是    | client读取特征值,通过注册回调函数获取。 |
Z
zengyawen 已提交
2734 2735 2736 2737 2738 2739 2740

**返回值:**



**示例:**

S
smartbao 已提交
2741
```js
Z
zengyawen 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750
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]);
}

W
wangyanhandsome 已提交
2751
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
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;

client端读取蓝牙低功耗设备特定服务的特征值。

S
smartbao 已提交
2778 2779 2780 2781
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2782 2783
**参数:**

H
HelloCrease 已提交
2784 2785 2786
| 参数名            | 类型                                      | 必填   | 说明       |
| -------------- | --------------------------------------- | ---- | -------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | 是    | 待读取的特征值。 |
Z
zengyawen 已提交
2787 2788 2789

**返回值:**

H
HelloCrease 已提交
2790 2791 2792
|                                          |                            |
| ---------------------------------------- | -------------------------- |
| 类型                                       | 说明                         |
Z
zengyawen 已提交
2793 2794 2795 2796
| Promise&lt;[BLECharacteristic](#blecharacteristic)&gt; | client读取特征值,通过promise形式获取。 |

**示例:**

S
smartbao 已提交
2797
```js
W
wangyanhandsome 已提交
2798
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824
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

client端读取蓝牙低功耗设备特定的特征包含的描述符。

S
smartbao 已提交
2825 2826 2827 2828
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2829 2830
**参数:**

H
HelloCrease 已提交
2831 2832 2833 2834
| 参数名        | 类型                                       | 必填   | 说明                      |
| ---------- | ---------------------------------------- | ---- | ----------------------- |
| descriptor | [BLEDescriptor](#bledescriptor)          | 是    | 待读取的描述符。                |
| callback   | AsyncCallback&lt;[BLECharacteristic](#blecharacteristic)&gt; | 是    | client读取描述符,通过注册回调函数获取。 |
Z
zengyawen 已提交
2835 2836 2837 2838 2839 2840 2841

**返回值:**



**示例:**

S
smartbao 已提交
2842
```js
Z
zengyawen 已提交
2843 2844 2845 2846 2847 2848 2849 2850 2851
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]);
}

W
wangyanhandsome 已提交
2852
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
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;

client端读取蓝牙低功耗设备特定的特征包含的描述符。

S
smartbao 已提交
2869 2870 2871 2872
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2873 2874
**参数:**

H
HelloCrease 已提交
2875 2876 2877
| 参数名        | 类型                              | 必填   | 说明       |
| ---------- | ------------------------------- | ---- | -------- |
| descriptor | [BLEDescriptor](#bledescriptor) | 是    | 待读取的描述符。 |
Z
zengyawen 已提交
2878 2879 2880

**返回值:**

H
HelloCrease 已提交
2881 2882 2883
|                                          |                            |
| ---------------------------------------- | -------------------------- |
| 类型                                       | 说明                         |
Z
zengyawen 已提交
2884 2885 2886 2887
| Promise&lt;[BLEDescriptor](#bledescriptor)&gt; | client读取描述符,通过promise形式获取。 |

**示例:**

S
smartbao 已提交
2888
```js
W
wangyanhandsome 已提交
2889
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
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

client端向低功耗蓝牙设备写入特定的特征值。

S
smartbao 已提交
2906 2907 2908 2909
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2910 2911
**参数:**

H
HelloCrease 已提交
2912 2913 2914
| 参数名            | 类型                                      | 必填   | 说明                  |
| -------------- | --------------------------------------- | ---- | ------------------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | 是    | 蓝牙设备特征对应的二进制值及其它参数。 |
Z
zengyawen 已提交
2915 2916 2917

**返回值:**

H
HelloCrease 已提交
2918 2919
| 类型      | 说明                          |
| ------- | --------------------------- |
Z
zengyawen 已提交
2920 2921 2922 2923
| boolean | 写特征值操作成功返回true,操作失败返回false。 |

**示例:**

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

client端向低功耗蓝牙设备特定的描述符写入二进制数据。

S
smartbao 已提交
2956 2957 2958 2959
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
2960 2961
**参数:**

H
HelloCrease 已提交
2962 2963 2964
| 参数名        | 类型                              | 必填   | 说明                 |
| ---------- | ------------------------------- | ---- | ------------------ |
| descriptor | [BLEDescriptor](#bledescriptor) | 是    | 蓝牙设备描述符的二进制值及其它参数。 |
Z
zengyawen 已提交
2965 2966 2967

**返回值:**

H
HelloCrease 已提交
2968 2969
| 类型      | 说明                          |
| ------- | --------------------------- |
Z
zengyawen 已提交
2970 2971 2972 2973
| boolean | 写描述符操作成功返回true,操作失败返回false。 |

**示例:**

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

W
wangyanhandsome 已提交
2995
client协商远端蓝牙低功耗设备的最大传输单元(Maximum Transmission Unit, MTU),调用[connect](#connect)接口连接成功后才能使用。
Z
zengyawen 已提交
2996

S
smartbao 已提交
2997 2998 2999 3000
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3001 3002
**参数:**

H
HelloCrease 已提交
3003 3004 3005
| 参数名  | 类型     | 必填   | 说明             |
| ---- | ------ | ---- | -------------- |
| mtu  | number | 是    | 设置范围为22~512字节。 |
Z
zengyawen 已提交
3006 3007 3008

**返回值:**

H
HelloCrease 已提交
3009 3010
| 类型      | 说明                           |
| ------- | ---------------------------- |
Z
zengyawen 已提交
3011 3012 3013 3014
| boolean | MTU协商操作成功返回true,操作失败返回false。 |

**示例:**

S
smartbao 已提交
3015
```js
W
wangyanhandsome 已提交
3016
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
device.setBLEMtuSize(128);
```


### setNotifyCharacteristicChanged

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

向服务端发送设置通知此特征值请求。

S
smartbao 已提交
3027 3028 3029 3030
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3031 3032
**参数:**

H
HelloCrease 已提交
3033 3034 3035 3036
| 参数名            | 类型                                      | 必填   | 说明                            |
| -------------- | --------------------------------------- | ---- | ----------------------------- |
| characteristic | [BLECharacteristic](#blecharacteristic) | 是    | 蓝牙低功耗特征。                      |
| enable         | boolean                                 | 是    | 启用接收notify设置为true,否则设置为false。 |
Z
zengyawen 已提交
3037 3038 3039

**返回值:**

H
HelloCrease 已提交
3040 3041
| 类型      | 说明                        |
| ------- | ------------------------- |
Z
zengyawen 已提交
3042 3043 3044 3045
| boolean | 设置操作成功返回true,操作失败返回false。 |

**示例:**

S
smartbao 已提交
3046
```js
3047 3048 3049 3050 3051 3052 3053 3054 3055
// 创建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;
S
smartbao 已提交
3056 3057 3058
let arrayBufferC = new ArrayBuffer(8);
let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
W
wangyanhandsome 已提交
3059
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
S
smartbao 已提交
3060
device.setNotifyCharacteristicChanged(characteristic, false);
Z
zengyawen 已提交
3061 3062 3063 3064 3065 3066 3067 3068 3069
```


### on('BLECharacteristicChange')

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

订阅蓝牙低功耗设备的特征值变化事件。需要先调用setNotifyCharacteristicChanged接口才能接收server端的通知。

S
smartbao 已提交
3070 3071 3072 3073
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3074 3075
**参数:**

H
HelloCrease 已提交
3076 3077 3078 3079
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"BLECharacteristicChange"字符串,表示特征值变化事件。 |
| callback | Callback&lt;[BLECharacteristic](#blecharacteristic)&gt; | 是    | 表示蓝牙低功耗设备的特征值变化事件的回调函数。                  |
Z
zengyawen 已提交
3080 3081 3082 3083 3084 3085 3086

**返回值:**



**示例:**

S
smartbao 已提交
3087
```js
Z
zengyawen 已提交
3088 3089 3090 3091 3092
function CharacteristicChange(CharacteristicChangeReq) {
  let serviceUuid = CharacteristicChangeReq.serviceUuid;
  let characteristicUuid = CharacteristicChangeReq.characteristicUuid;
  let value = new Uint8Array(CharacteristicChangeReq.characteristicValue);
}
W
wangyanhandsome 已提交
3093
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
device.on('BLECharacteristicChange', CharacteristicChange);
```


### off('BLECharacteristicChange')

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

取消订阅蓝牙低功耗设备的特征值变化事件。

S
smartbao 已提交
3104 3105 3106 3107
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3108 3109
**参数:**

H
HelloCrease 已提交
3110 3111 3112 3113
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"BLECharacteristicChange"字符串,表示特征值变化事件。 |
| callback | Callback&lt;[BLECharacteristic](#blecharacteristic)&gt; | 否    | 表示取消订阅蓝牙低功耗设备的特征值变化事件。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
3114 3115 3116 3117 3118 3119 3120

**返回值:**



**示例:**

S
smartbao 已提交
3121
```js
W
wangyanhandsome 已提交
3122
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
device.off('BLECharacteristicChange');
```


### on('BLEConnectionStateChange')

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

client端订阅蓝牙低功耗设备的连接状态变化事件。

S
smartbao 已提交
3133 3134 3135 3136
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3137 3138
**参数:**

H
HelloCrease 已提交
3139 3140 3141 3142
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | 是    | 表示连接状态,已连接或断开。                           |
Z
zengyawen 已提交
3143 3144 3145 3146 3147 3148 3149

**返回值:**



**示例:**

S
smartbao 已提交
3150
```js
Z
zengyawen 已提交
3151 3152 3153 3154
function ConnectStateChanged(state) {
  console.log('bluetooth connect state changed');
  let connectState = state.state;
}
W
wangyanhandsome 已提交
3155
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
device.on('BLEConnectionStateChange', ConnectStateChanged);
```


### off('BLEConnectionStateChange')

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

取消订阅蓝牙低功耗设备的连接状态变化事件。

S
smartbao 已提交
3166 3167 3168 3169
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3170 3171
**参数:**

H
HelloCrease 已提交
3172 3173 3174 3175
| 参数名      | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type     | string                                   | 是    | 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback&lt;[BLEConnectChangedState](#bleconnectchangedstate)&gt; | 否    | 表示取消订阅蓝牙低功耗设备的连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。 |
Z
zengyawen 已提交
3176 3177 3178 3179 3180 3181 3182

**返回值:**



**示例:**

S
smartbao 已提交
3183
```js
W
wangyanhandsome 已提交
3184
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
device.off('BLEConnectionStateChange');
```


### getDeviceName

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

client获取远端蓝牙低功耗设备名。

S
smartbao 已提交
3195 3196 3197 3198
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3199 3200
**参数:**

H
HelloCrease 已提交
3201 3202 3203
| 参数名      | 类型                          | 必填   | 说明                              |
| -------- | --------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback&lt;string&gt; | 是    | client获取对端server设备名,通过注册回调函数获取。 |
Z
zengyawen 已提交
3204 3205 3206 3207 3208 3209 3210

**返回值:**



**示例:**

S
smartbao 已提交
3211
```js
Z
zengyawen 已提交
3212
// callback
W
wangyanhandsome 已提交
3213
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
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;

client获取远端蓝牙低功耗设备名。

S
smartbao 已提交
3227 3228 3229 3230
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3231 3232
**返回值:**

H
HelloCrease 已提交
3233 3234
| 类型                    | 说明                                 |
| --------------------- | ---------------------------------- |
Z
zengyawen 已提交
3235 3236 3237 3238
| Promise&lt;string&gt; | client获取对端server设备名,通过promise形式获取。 |

**示例:**

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

W
wangyanhandsome 已提交
3253
client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用[connect](#connect)接口连接成功后才能使用。
Z
zengyawen 已提交
3254

S
smartbao 已提交
3255 3256 3257 3258
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3259 3260
**参数:**

H
HelloCrease 已提交
3261 3262 3263
| 参数名      | 类型                          | 必填   | 说明                             |
| -------- | --------------------------- | ---- | ------------------------------ |
| callback | AsyncCallback&lt;number&gt; | 是    | 返回信号强度,单位&nbsp;dBm,通过注册回调函数获取。 |
Z
zengyawen 已提交
3264 3265 3266 3267 3268 3269 3270

**返回值:**



**示例:**

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

W
wangyanhandsome 已提交
3286
client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用[connect](#connect)接口连接成功后才能使用。
Z
zengyawen 已提交
3287

S
smartbao 已提交
3288 3289 3290 3291
**需要权限**:ohos.permission.USE_BLUETOOTH

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3292 3293
**返回值:**

H
HelloCrease 已提交
3294 3295
| 类型                    | 说明                                |
| --------------------- | --------------------------------- |
Z
zengyawen 已提交
3296 3297 3298 3299
| Promise&lt;number&gt; | 返回信号强度,单位&nbsp;dBm,通过promise形式获取。 |

**示例:**

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

S
smartbao 已提交
3308
## ScanMode<sup>8+</sup><a name="ScanMode"></a>
Z
zengyawen 已提交
3309 3310 3311

枚举,扫描模式。

S
smartbao 已提交
3312 3313
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3314 3315 3316 3317 3318 3319 3320 3321
| 名称                                       | 默认值  | 说明              |
| ---------------------------------------- | ---- | --------------- |
| SCAN_MODE_NONE                           | 0    | 没有扫描模式。         |
| SCAN_MODE_CONNECTABLE                    | 1    | 可连接扫描模式。        |
| SCAN_MODE_GENERAL_DISCOVERABLE           | 2    | general发现模式。    |
| SCAN_MODE_LIMITED_DISCOVERABLE           | 3    | limited发现模式。    |
| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4    | 可连接general发现模式。 |
| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5    | 可连接limited发现模式。 |
Z
zengyawen 已提交
3322

S
smartbao 已提交
3323
## BondState<sup>8+</sup><a name="BondState"></a>
Z
zengyawen 已提交
3324 3325 3326

枚举,配对状态。

S
smartbao 已提交
3327 3328
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3329 3330 3331 3332 3333
| 名称                 | 默认值  | 说明     |
| ------------------ | ---- | ------ |
| BOND_STATE_INVALID | 0    | 无效的配对。 |
| BOND_STATE_BONDING | 1    | 正在配对。  |
| BOND_STATE_BONDED  | 2    | 已配对。   |
Z
zengyawen 已提交
3334 3335


S
smartbao 已提交
3336
## SppOption<sup>8+</sup><a name="SppOption"></a>
Z
zengyawen 已提交
3337 3338 3339

描述spp的配置参数。

S
smartbao 已提交
3340 3341
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3342 3343 3344 3345 3346
| 名称     | 参数类型                | 可读   | 可写   | 说明          |
| ------ | ------------------- | ---- | ---- | ----------- |
| uuid   | string              | 是    | 是    | spp单据的uuid。 |
| secure | boolean             | 是    | 是    | 是否是安全通道。    |
| type   | [SppType](#spptype) | 是    | 是    | Spp链路类型。    |
Z
zengyawen 已提交
3347 3348


S
smartbao 已提交
3349
## SppType<sup>8+</sup><a name="SppType"></a>
Z
zengyawen 已提交
3350 3351 3352

枚举,Spp链路类型。

S
smartbao 已提交
3353 3354
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3355 3356 3357
| 名称         | 默认值  | 说明            |
| ---------- | ---- | ------------- |
| SPP_RFCOMM | 0    | 表示rfcomm链路类型。 |
Z
zengyawen 已提交
3358 3359 3360 3361 3362 3363


## GattService

描述service的接口参数定义。

S
smartbao 已提交
3364 3365
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3366 3367 3368 3369 3370 3371
| 名称              | 参数类型                                     | 可读   | 可写   | 说明                                       |
| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| serviceUuid     | string                                   | 是    | 是    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
| isPrimary       | boolean                                  | 是    | 是    | 如果是主服务设置为true,否则设置为false。                |
| characteristics | Array&lt;[BLECharacteristic](#blecharacteristic)&gt; | 是    | 是    | 当前服务包含的特征列表。                             |
| includeServices | Array&lt;[GattService](#gattservice)&gt; | 是    | 是    | 当前服务依赖的其它服务。                             |
Z
zengyawen 已提交
3372 3373 3374 3375 3376 3377


## BLECharacteristic

描述characteristic的接口参数定义 。

S
smartbao 已提交
3378 3379
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3380 3381 3382 3383 3384 3385
| 名称                  | 参数类型                                     | 可读   | 可写   | 说明                                       |
| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| serviceUuid         | string                                   | 是    | 是    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
| characteristicUuid  | string                                   | 是    | 是    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| characteristicValue | ArrayBuffer                              | 是    | 是    | 特征对应的二进制值。                               |
| descriptors         | Array&lt;[BLEDescriptor](#bledescriptor)&gt; | 是    | 是    | 特定特征的描述符列表。                              |
Z
zengyawen 已提交
3386 3387 3388 3389 3390 3391


## BLEDescriptor

描述descriptor的接口参数定义 。

S
smartbao 已提交
3392 3393
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3394 3395 3396 3397 3398 3399
| 名称                 | 参数类型        | 可读   | 可写   | 说明                                       |
| ------------------ | ----------- | ---- | ---- | ---------------------------------------- |
| serviceUuid        | string      | 是    | 是    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
| characteristicUuid | string      | 是    | 是    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| descriptorUuid     | string      | 是    | 是    | 描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 |
| descriptorValue    | ArrayBuffer | 是    | 是    | 描述符对应的二进制值。                              |
Z
zengyawen 已提交
3400 3401 3402 3403 3404 3405


## NotifyCharacteristic

描述server端特征值变化时发送的特征通知参数定义。

S
smartbao 已提交
3406 3407
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3408 3409 3410 3411 3412 3413
| 名称                  | 参数类型        | 可读   | 可写   | 说明                                       |
| ------------------- | ----------- | ---- | ---- | ---------------------------------------- |
| serviceUuid         | string      | 是    | 是    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
| characteristicUuid  | string      | 是    | 是    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| characteristicValue | ArrayBuffer | 是    | 是    | 特征对应的二进制值。                               |
| confirm             | boolean     | 是    | 是    | 如果是notification则对端回复确认设置为true,如果是indication则对端不需要回复确认设置为false。 |
Z
zengyawen 已提交
3414 3415 3416 3417 3418 3419


## CharacteristicReadReq

描述server端订阅后收到的特征值读请求事件参数结构。

S
smartbao 已提交
3420 3421
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3422 3423 3424 3425 3426 3427 3428
| 名称                 | 参数类型   | 可读   | 可写   | 说明                                       |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| deviceId           | string | 是    | 否    | 表示发送特征值读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| transId            | number | 是    | 否    | 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。       |
| offset             | number | 是    | 否    | 表示读特征值数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。 |
| characteristicUuid | string | 是    | 否    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| serviceUuid        | string | 是    | 否    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
Z
zengyawen 已提交
3429 3430 3431 3432 3433 3434


## CharacteristicWriteReq

描述server端订阅后收到的特征值写请求事件参数结构。

S
smartbao 已提交
3435 3436
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3437 3438 3439 3440 3441 3442 3443 3444
| 名称                 | 参数类型   | 可读   | 可写   | 说明                                       |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| deviceId           | string | 是    | 否    | 表示发送特征值写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| transId            | number | 是    | 否    | 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。       |
| offset             | number | 是    | 否    | 表示写特征值数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。 |
| descriptorUuid     | string | 是    | 否    | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 |
| characteristicUuid | string | 是    | 否    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| serviceUuid        | string | 是    | 否    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
Z
zengyawen 已提交
3445 3446 3447 3448 3449 3450


## DescriptorReadReq

描述server端订阅后收到的描述符读请求事件参数结构。

S
smartbao 已提交
3451 3452
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3453 3454 3455 3456 3457 3458 3459 3460
| 名称                 | 参数类型   | 可读   | 可写   | 说明                                       |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| deviceId           | string | 是    | 否    | 表示发送描述符读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| transId            | number | 是    | 否    | 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。       |
| offset             | number | 是    | 否    | 表示读描述符数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。 |
| descriptorUuid     | string | 是    | 否    | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 |
| characteristicUuid | string | 是    | 否    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| serviceUuid        | string | 是    | 否    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
Z
zengyawen 已提交
3461 3462 3463 3464 3465 3466


## DescriptorWriteReq

描述server端订阅后收到的描述符写请求事件参数结构。

S
smartbao 已提交
3467 3468
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
| 名称                 | 参数类型        | 可读   | 可写   | 说明                                       |
| ------------------ | ----------- | ---- | ---- | ---------------------------------------- |
| deviceId           | string      | 是    | 否    | 表示发送描述符写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| transId            | number      | 是    | 否    | 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。       |
| offset             | number      | 是    | 否    | 表示写描述符数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。 |
| isPrep             | boolean     | 是    | 否    | 表示写请求是否立即执行。                             |
| needRsp            | boolean     | 是    | 否    | 表示是否要给client端回复响应。                       |
| value              | ArrayBuffer | 是    | 否    | 表示写入的描述符二进制数据。                           |
| descriptorUuid     | string      | 是    | 否    | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 |
| characteristicUuid | string      | 是    | 否    | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 |
| serviceUuid        | string      | 是    | 否    | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 |
Z
zengyawen 已提交
3480 3481 3482 3483 3484 3485


## ServerResponse

描述server端回复client端读/写请求的响应参数结构。

S
smartbao 已提交
3486 3487
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3488 3489 3490 3491 3492 3493 3494
| 名称       | 参数类型        | 可读   | 可写   | 说明                                     |
| -------- | ----------- | ---- | ---- | -------------------------------------- |
| deviceId | string      | 是    | 否    | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。       |
| transId  | number      | 是    | 否    | 表示请求的传输ID,与订阅的读/写请求事件携带的ID保持一致。        |
| status   | number      | 是    | 否    | 表示响应的状态,设置为0即可,表示正常。                   |
| offset   | number      | 是    | 否    | 表示请求的读/写起始位置,与订阅的读/写请求事件携带的offset保持一致。 |
| value    | ArrayBuffer | 是    | 否    | 表示回复响应的二进制数据。                          |
Z
zengyawen 已提交
3495 3496 3497 3498 3499 3500


## BLEConnectChangedState

描述Gatt profile连接状态 。

S
smartbao 已提交
3501 3502
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3503 3504 3505 3506
| 名称     | 参数类型                                          | 可读 | 可写 | 说明                                          |
| -------- | ------------------------------------------------- | ---- | ---- | --------------------------------------------- |
| deviceId | string                                            | 是   | 否   | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| state    | [ProfileConnectionState](#profileconnectionstate) | 是   | 是   | 表示BLE连接状态的枚举。                       |
Z
zengyawen 已提交
3507 3508 3509 3510 3511 3512


## ProfileConnectionState

枚举,蓝牙设备的profile连接状态。

S
smartbao 已提交
3513 3514
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3515 3516 3517 3518 3519 3520
| 名称                  | 默认值  | 说明             |
| ------------------- | ---- | -------------- |
| STATE_DISCONNECTED  | 0    | 表示profile已断连。  |
| STATE_CONNECTING    | 1    | 表示profile正在连接。 |
| STATE_CONNECTED     | 2    | 表示profile已连接。  |
| STATE_DISCONNECTING | 3    | 表示profile正在断连。 |
Z
zengyawen 已提交
3521 3522 3523 3524 3525 3526


## ScanFilter

扫描过滤参数。

S
smartbao 已提交
3527 3528
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

T
tangjia 已提交
3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
| 名称                                     | 参数类型    | 可读 | 可写 | 说明                                                         |
| ---------------------------------------- | ----------- | ---- | ---- | ------------------------------------------------------------ |
| deviceId                                 | string      | 是   | 是   | 表示过滤的BLE设备地址,例如:"XX:XX:XX:XX:XX:XX"。           |
| name                                     | string      | 是   | 是   | 表示过滤的BLE设备名。                                        |
| serviceUuid                              | string      | 是   | 是   | 表示过滤包含该UUID服务的设备,例如:00001888-0000-1000-8000-00805f9b34fb。 |
| serviceUuidMask<sup>9+</sup>             | string      | 是   | 是   | 表示过滤包含该UUID服务掩码的设备,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。 |
| serviceSolicitationUuid<sup>9+</sup>     | string      | 是   | 是   | 表示过滤包含该UUID服务请求的设备,例如:00001888-0000-1000-8000-00805F9B34FB。 |
| serviceSolicitationUuidMask<sup>9+</sup> | string      | 是   | 是   | 表示过滤包含该UUID服务请求掩码的设备,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。 |
| serviceData<sup>9+</sup>                 | ArrayBuffer | 是   | 是   | 表示过滤包含该服务相关数据的设备,例如:[0x90,0x00,0xF1,0xF2]。 |
| serviceDataMask<sup>9+</sup>             | ArrayBuffer | 是   | 是   | 表示过滤包含该服务相关数据掩码的设备,例如:[0xFF,0xFF,0xFF,0xFF]。 |
3539
| manufactureId<sup>9+</sup>               | number      | 是   | 是   | 表示过滤包含该制造商ID的设备,例如:0x0006。                 |
T
tangjia 已提交
3540 3541
| manufactureData<sup>9+</sup>             | ArrayBuffer | 是   | 是   | 表示过滤包含该制造商相关数据的设备,例如:[0x1F,0x2F,0x3F]。 |
| manufactureDataMask<sup>9+</sup>         | ArrayBuffer | 是   | 是   | 表示过滤包含该制造商相关数据掩码的设备,例如:[0xFF,0xFF,0xFF]。 |
Z
zengyawen 已提交
3542 3543 3544 3545 3546 3547


## ScanOptions

扫描的配置参数。

S
smartbao 已提交
3548 3549
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3550 3551 3552 3553 3554
| 名称        | 参数类型                    | 可读   | 可写   | 说明                                     |
| --------- | ----------------------- | ---- | ---- | -------------------------------------- |
| interval  | number                  | 是    | 是    | 表示扫描结果上报延迟时间,默认值为0。                    |
| dutyMode  | [ScanDuty](#scanduty)   | 是    | 是    | 表示扫描模式,默认值为SCAN_MODE_LOW_POWER。        |
| matchMode | [MatchMode](#matchmode) | 是    | 是    | 表示硬件的过滤匹配模式,默认值为MATCH_MODE_AGGRESSIVE。 |
Z
zengyawen 已提交
3555 3556 3557 3558 3559 3560


## ScanDuty

枚举,扫描模式。

S
smartbao 已提交
3561 3562
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3563 3564 3565 3566 3567
| 名称                    | 默认值  | 说明           |
| --------------------- | ---- | ------------ |
| SCAN_MODE_LOW_POWER   | 0    | 表示低功耗模式,默认值。 |
| SCAN_MODE_BALANCED    | 1    | 表示均衡模式。      |
| SCAN_MODE_LOW_LATENCY | 2    | 表示低延迟模式。     |
Z
zengyawen 已提交
3568 3569 3570 3571 3572 3573


## MatchMode

枚举,硬件过滤匹配模式。

S
smartbao 已提交
3574 3575
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3576 3577 3578 3579
| 名称                    | 默认值  | 说明                                       |
| --------------------- | ---- | ---------------------------------------- |
| MATCH_MODE_AGGRESSIVE | 1    | 表示硬件上报扫描结果门限较低,比如扫描到的功率较低或者一段时间扫描到的次数较少也触发上报,默认值。 |
| MATCH_MODE_STICKY     | 2    | 表示硬件上报扫描结果门限较高,更高的功率门限以及扫描到多次才会上报。       |
Z
zengyawen 已提交
3580 3581 3582 3583 3584 3585


## ScanResult

扫描结果上报数据。

S
smartbao 已提交
3586 3587
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3588 3589 3590 3591 3592
| 名称       | 参数类型        | 可读   | 可写   | 说明                                 |
| -------- | ----------- | ---- | ---- | ---------------------------------- |
| deviceId | string      | 是    | 否    | 表示扫描到的设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| rssi     | number      | 是    | 否    | 表示扫描到的设备的rssi值。                    |
| data     | ArrayBuffer | 是    | 否    | 表示扫描到的设备发送的广播包。                    |
Z
zengyawen 已提交
3593 3594 3595 3596 3597 3598


## BluetoothState

枚举,蓝牙开关状态。

S
smartbao 已提交
3599 3600
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3601 3602 3603 3604 3605 3606 3607 3608 3609
| 名称                    | 默认值  | 说明                 |
| --------------------- | ---- | ------------------ |
| STATE_OFF             | 0    | 表示蓝牙已关闭。           |
| STATE_TURNING_ON      | 1    | 表示蓝牙正在打开。          |
| STATE_ON              | 2    | 表示蓝牙已打开。           |
| STATE_TURNING_OFF     | 3    | 表示蓝牙正在关闭。          |
| STATE_BLE_TURNING_ON  | 4    | 表示蓝牙正在打开LE-only模式。 |
| STATE_BLE_ON          | 5    | 表示蓝牙正处于LE-only模式。  |
| STATE_BLE_TURNING_OFF | 6    | 表示蓝牙正在关闭LE-only模式。 |
Z
zengyawen 已提交
3610 3611 3612 3613 3614 3615


## AdvertiseSetting

描述蓝牙低功耗设备发送广播的参数。

S
smartbao 已提交
3616 3617
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3618 3619 3620 3621 3622
| 名称          | 参数类型    | 可读   | 可写   | 说明                                       |
| ----------- | ------- | ---- | ---- | ---------------------------------------- |
| interval    | number  | 是    | 是    | 表示广播间隔,最小值设置32个slot表示20ms,最大值设置16777215个slot,默认值设置为1600个slot表示1s。 |
| txPower     | number  | 是    | 是    | 表示发送功率,最小值设置-127,最大值设置1,默认值设置-7,单位dbm。   |
| connectable | boolean | 是    | 是    | 表示是否是可连接广播,默认值设置为true。                   |
Z
zengyawen 已提交
3623 3624 3625 3626 3627 3628


## AdvertiseData

描述BLE广播数据包的内容。

S
smartbao 已提交
3629 3630
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3631 3632 3633 3634 3635
| 名称              | 参数类型                                     | 可读   | 可写   | 说明                          |
| --------------- | ---------------------------------------- | ---- | ---- | --------------------------- |
| serviceUuids    | Array&lt;string&gt;                      | 是    | 是    | 表示要广播的服务&nbsp;UUID&nbsp;列表。 |
| manufactureData | Array&lt;[ManufactureData](#manufacturedata)&gt; | 是    | 是    | 表示要广播的广播的制造商信息列表。           |
| serviceData     | Array&lt;[ServiceData](#servicedata)&gt; | 是    | 是    | 表示要广播的服务数据列表。               |
Z
zengyawen 已提交
3636 3637 3638 3639 3640 3641


## ManufactureData

描述BLE广播数据包的内容。

S
smartbao 已提交
3642 3643
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3644 3645 3646 3647
| 名称               | 参数类型                | 可读   | 可写   | 说明                 |
| ---------------- | ------------------- | ---- | ---- | ------------------ |
| manufactureId    | Array&lt;string&gt; | 是    | 是    | 表示制造商的ID,由蓝牙SIG分配。 |
| manufactureValue | ArrayBuffer         | 是    | 是    | 表示制造商发送的制造商数据。     |
Z
zengyawen 已提交
3648 3649 3650 3651 3652 3653


## ServiceData

描述广播包中服务数据内容。

S
smartbao 已提交
3654 3655
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3656 3657 3658 3659
| 名称           | 参数类型        | 可读   | 可写   | 说明         |
| ------------ | ----------- | ---- | ---- | ---------- |
| serviceUuid  | string      | 是    | 是    | 表示服务的UUID。 |
| serviceValue | ArrayBuffer | 是    | 是    | 表示服务数据。    |
Z
zengyawen 已提交
3660 3661


S
smartbao 已提交
3662
## PinRequiredParam<sup>8+</sup><a name="PinRequiredParam"></a>
Z
zengyawen 已提交
3663 3664 3665

描述配对请求参数。

S
smartbao 已提交
3666 3667
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3668 3669 3670 3671
| 名称       | 参数类型   | 可读   | 可写   | 说明          |
| -------- | ------ | ---- | ---- | ----------- |
| deviceId | string | 是    | 否    | 表示要配对的设备ID。 |
| pinCode  | string | 是    | 否    | 表示要配对的密钥。   |
W
wangyanhandsome 已提交
3672 3673


S
smartbao 已提交
3674 3675 3676 3677 3678 3679
## StateChangeParam<sup>8+</sup><a name="StateChangeParam"></a>

描述profile状态改变参数。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

Z
zengyawen 已提交
3680 3681 3682 3683
| 名称     | 参数类型                                          | 可读 | 可写 | 说明                            |
| -------- | ------------------------------------------------- | ---- | ---- | ------------------------------- |
| deviceId | string                                            | 是   | 否   | 表示蓝牙设备地址。              |
| state    | [ProfileConnectionState](#profileconnectionstate) | 是   | 否   | 表示蓝牙设备的profile连接状态。 |
S
smartbao 已提交
3684 3685 3686


## DeviceClass<sup>8+</sup><a name="DeviceClass"></a>
W
wangyanhandsome 已提交
3687 3688 3689

描述蓝牙设备的类别。

S
smartbao 已提交
3690 3691
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3692 3693 3694 3695 3696
| 名称              | 参数类型                                | 可读   | 可写   | 说明               |
| --------------- | ----------------------------------- | ---- | ---- | ---------------- |
| majorClass      | [MajorClass](#majorclass)           | 是    | 否    | 表示蓝牙设备主要类别的枚举。   |
| majorMinorClass | [MajorMinorClass](#majorminorclass) | 是    | 否    | 表示主要次要蓝牙设备类别的枚举。 |
| classOfDevice   | number                              | 是    | 否    | 表示设备类别。          |
W
wangyanhandsome 已提交
3697 3698


S
smartbao 已提交
3699 3700

## MajorClass<sup>8+</sup><a name="MajorClass"></a>
W
wangyanhandsome 已提交
3701 3702 3703

枚举,蓝牙设备主要类别。

S
smartbao 已提交
3704 3705
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3706 3707 3708 3709 3710 3711
| 名称                  | 默认值    | 说明         |
| ------------------- | ------ | ---------- |
| MAJOR_MISC          | 0x0000 | 表示杂项设备。    |
| MAJOR_COMPUTER      | 0x0100 | 表示计算机设备。   |
| MAJOR_PHONE         | 0x0200 | 表示手机设备。    |
| MAJOR_NETWORKING    | 0x0300 | 表示网络设备。    |
Z
zengyawen 已提交
3712
| MAJOR_AUDIO_VIDEO   | 0x0400 | 表示音频和视频设备。 |
H
HelloCrease 已提交
3713 3714 3715 3716 3717 3718
| MAJOR_PERIPHERAL    | 0x0500 | 表示外围设备。    |
| MAJOR_IMAGING       | 0x0600 | 表示成像设备。    |
| MAJOR_WEARABLE      | 0x0700 | 表示可穿戴设备。   |
| MAJOR_TOY           | 0x0800 | 表示玩具设备。    |
| MAJOR_HEALTH        | 0x0900 | 表示健康设备。    |
| MAJOR_UNCATEGORIZED | 0x1F00 | 表示未分类设备。   |
W
wangyanhandsome 已提交
3719 3720


S
smartbao 已提交
3721
## MajorMinorClass<sup>8+</sup><a name="MajorMinorClass"></a>
W
wangyanhandsome 已提交
3722 3723 3724

枚举,主要次要蓝牙设备类别。

S
smartbao 已提交
3725 3726
**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
| 名称                                       | 默认值    | 说明              |
| ---------------------------------------- | ------ | --------------- |
| COMPUTER_UNCATEGORIZED                   | 0x0100 | 表示未分类计算机设备。     |
| COMPUTER_DESKTOP                         | 0x0104 | 表示台式计算机设备。      |
| COMPUTER_SERVER                          | 0x0108 | 表示服务器设备。        |
| COMPUTER_LAPTOP                          | 0x010C | 表示便携式计算机设备。     |
| COMPUTER_HANDHELD_PC_PDA                 | 0x0110 | 表示手持式计算机设备。     |
| COMPUTER_PALM_SIZE_PC_PDA                | 0x0114 | 表示掌上电脑设备。       |
| COMPUTER_WEARABLE                        | 0x0118 | 表示可穿戴计算机设备。     |
| COMPUTER_TABLET                          | 0x011C | 表示平板电脑设备。       |
| PHONE_UNCATEGORIZED                      | 0x0200 | 表示未分类手机设备。      |
| PHONE_CELLULAR                           | 0x0204 | 表示便携式手机设备。      |
| PHONE_CORDLESS                           | 0x0208 | 表示无线电话设备。       |
| PHONE_SMART                              | 0x020C | 表示智能手机设备。       |
| PHONE_MODEM_OR_GATEWAY                   | 0x0210 | 表示调制解调器或网关手机设备。 |
| PHONE_ISDN                               | 0x0214 | 表示ISDN手机设备。     |
| NETWORK_FULLY_AVAILABLE                  | 0x0300 | 表示网络完全可用设备。     |
| NETWORK_1_TO_17_UTILIZED                 | 0x0320 | 表示使用网络1到17设备。   |
| NETWORK_17_TO_33_UTILIZED                | 0x0340 | 表示使用网络17到33设备。  |
| NETWORK_33_TO_50_UTILIZED                | 0x0360 | 表示使用网络33到50设备。  |
| NETWORK_60_TO_67_UTILIZED                | 0x0380 | 表示使用网络60到67设备。  |
| NETWORK_67_TO_83_UTILIZED                | 0x03A0 | 表示使用网络67到83设备。  |
| NETWORK_83_TO_99_UTILIZED                | 0x03C0 | 表示使用网络83到99设备。  |
| NETWORK_NO_SERVICE                       | 0x03E0 | 表示网络无服务设备。      |
| AUDIO_VIDEO_UNCATEGORIZED                | 0x0400 | 表示未分类音频视频设备。    |
| AUDIO_VIDEO_WEARABLE_HEADSET             | 0x0404 | 表示可穿戴式音频视频设备。   |
| AUDIO_VIDEO_HANDSFREE                    | 0x0408 | 表示免提音频视频设备。     |
| AUDIO_VIDEO_MICROPHONE                   | 0x0410 | 表示麦克风音频视频设备。    |
| AUDIO_VIDEO_LOUDSPEAKER                  | 0x0414 | 表示扬声器音频视频设备。    |
| AUDIO_VIDEO_HEADPHONES                   | 0x0418 | 表示头戴式音频视频设备。    |
| AUDIO_VIDEO_PORTABLE_AUDIO               | 0x041C | 表示便携式音频视频设备。    |
| AUDIO_VIDEO_CAR_AUDIO                    | 0x0420 | 表示汽车音频视频设备。     |
| AUDIO_VIDEO_SET_TOP_BOX                  | 0x0424 | 表示机顶盒音频视频设备。    |
| AUDIO_VIDEO_HIFI_AUDIO                   | 0x0428 | 表示高保真音响设备。      |
| AUDIO_VIDEO_VCR                          | 0x042C | 表示录像机音频视频设备。    |
| AUDIO_VIDEO_VIDEO_CAMERA                 | 0x0430 | 表示照相机音频视频设备。    |
| AUDIO_VIDEO_CAMCORDER                    | 0x0434 | 表示摄像机音频视频设备。    |
| AUDIO_VIDEO_VIDEO_MONITOR                | 0x0438 | 表示监视器音频视频设备。    |
| AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER | 0x043C | 表示视频显示器和扬声器设备。  |
| AUDIO_VIDEO_VIDEO_CONFERENCING           | 0x0440 | 表示音频视频会议设备。     |
| AUDIO_VIDEO_VIDEO_GAMING_TOY             | 0x0448 | 表示游戏玩具音频视频设备。   |
| PERIPHERAL_NON_KEYBOARD_NON_POINTING     | 0x0500 | 表示非键盘非指向外围设备。   |
| PERIPHERAL_KEYBOARD                      | 0x0540 | 表示外设键盘设备。       |
| PERIPHERAL_POINTING_DEVICE               | 0x0580 | 表示定点装置外围设备。     |
| PERIPHERAL_KEYBOARD_POINTING             | 0x05C0 | 表示键盘指向外围设备。     |
| PERIPHERAL_UNCATEGORIZED                 | 0x0500 | 表示未分类外围设备。      |
| PERIPHERAL_JOYSTICK                      | 0x0504 | 表示周边操纵杆设备。      |
| PERIPHERAL_GAMEPAD                       | 0x0508 | 表示周边游戏板设备。      |
| PERIPHERAL_REMOTE_CONTROL                | 0x05C0 | 表示远程控制外围设备。     |
| PERIPHERAL_SENSING_DEVICE                | 0x0510 | 表示外围传感设备设备。     |
| PERIPHERAL_DIGITIZER_TABLET              | 0x0514 | 表示外围数字化仪平板电脑设备。 |
| PERIPHERAL_CARD_READER                   | 0x0518 | 表示外围读卡器设备。      |
| PERIPHERAL_DIGITAL_PEN                   | 0x051C | 表示外设数码笔设备。      |
| PERIPHERAL_SCANNER_RFID                  | 0x0520 | 表示射频识别扫描仪外围设备。  |
| PERIPHERAL_GESTURAL_INPUT                | 0x0522 | 表示手势输入外围设备。     |
| IMAGING_UNCATEGORIZED                    | 0x0600 | 表示未分类的图像设备。     |
| IMAGING_DISPLAY                          | 0x0610 | 表示图像显示设备。       |
| IMAGING_CAMERA                           | 0x0620 | 表示成像照相机设备。      |
| IMAGING_SCANNER                          | 0x0640 | 表示成像扫描仪设备。      |
| IMAGING_PRINTER                          | 0x0680 | 表示成像打印机设备。      |
| WEARABLE_UNCATEGORIZED                   | 0x0700 | 表示未分类的可穿戴设备。    |
| WEARABLE_WRIST_WATCH                     | 0x0704 | 表示可穿戴腕表设备。      |
| WEARABLE_PAGER                           | 0x0708 | 表示可穿戴寻呼机设备。     |
| WEARABLE_JACKET                          | 0x070C | 表示夹克可穿戴设备。      |
| WEARABLE_HELMET                          | 0x0710 | 表示可穿戴头盔设备。      |
| WEARABLE_GLASSES                         | 0x0714 | 表示可穿戴眼镜设备。      |
| TOY_UNCATEGORIZED                        | 0x0800 | 表示未分类的玩具设备。     |
| TOY_ROBOT                                | 0x0804 | 表示玩具机器人设备。      |
| TOY_VEHICLE                              | 0x0808 | 表示玩具车设备。        |
| TOY_DOLL_ACTION_FIGURE                   | 0x080C | 表示人形娃娃玩具设备。     |
| TOY_CONTROLLER                           | 0x0810 | 表示玩具控制器设备。      |
| TOY_GAME                                 | 0x0814 | 表示玩具游戏设备。       |
| HEALTH_UNCATEGORIZED                     | 0x0900 | 表示未分类健康设备。      |
| HEALTH_BLOOD_PRESSURE                    | 0x0904 | 表示血压健康设备。       |
| HEALTH_THERMOMETER                       | 0x0908 | 表示温度计健康设备。      |
| HEALTH_WEIGHING                          | 0x090C | 表示体重健康设备。       |
| HEALTH_GLUCOSE                           | 0x0910 | 表示葡萄糖健康设备。      |
| HEALTH_PULSE_OXIMETER                    | 0x0914 | 表示脉搏血氧仪健康设备。    |
| HEALTH_PULSE_RATE                        | 0x0918 | 表示脉搏率健康设备。      |
| HEALTH_DATA_DISPLAY                      | 0x091C | 表示数据显示健康设备。     |
| HEALTH_STEP_COUNTER                      | 0x0920 | 表示阶梯计数器健康设备。    |
| HEALTH_BODY_COMPOSITION_ANALYZER         | 0x0924 | 表示身体成分分析仪健康设备。  |
| HEALTH_PEAK_FLOW_MOITOR                  | 0x0928 | 表示湿度计健康设备。      |
| HEALTH_MEDICATION_MONITOR                | 0x092C | 表示药物监视仪健康设备。    |
| HEALTH_KNEE_PROSTHESIS                   | 0x0930 | 表示膝盖假肢健康设备。     |
| HEALTH_ANKLE_PROSTHESIS                  | 0x0934 | 表示脚踝假肢健康设备。     |
| HEALTH_GENERIC_HEALTH_MANAGER            | 0x0938 | 表示通用健康管理设备。     |
| HEALTH_PERSONAL_MOBILITY_DEVICE          | 0x093C | 表示个人移动健康设备。     |
S
smartbao 已提交
3815 3816 3817 3818 3819 3820 3821 3822


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

枚举,蓝牙A2DP 播放状态。

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3823 3824 3825
| 名称                | 默认值    | 说明      |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | 表示未播放。  |
Z
zengyawen 已提交
3826
| STATE_PLAYING     | 0x0001 | 表示正在播放。 |
S
smartbao 已提交
3827 3828 3829 3830


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

3831
蓝牙profile枚举,API9新增PROFILE_HID_HOST,PROFILE_PAN_NETWORK。
S
smartbao 已提交
3832 3833 3834

**系统能力**:SystemCapability.Communication.Bluetooth.Core。

H
HelloCrease 已提交
3835 3836
| 名称                               | 默认值    | 说明              |
| -------------------------------- | ------ | --------------- |
Z
zengyawen 已提交
3837
| PROFILE_A2DP_SOURCE              | 0x0001 | 表示A2DP profile。 |
L
lee 已提交
3838
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | 表示HFP profile。  |
3839 3840
| PROFILE_HID_HOST<sup>9+</sup> | 0x0006 | 表示HID profile。  |
| PROFILE_PAN_NETWORK<sup>9+</sup> | 0x0007 | 表示PAN profile。  |