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

3 4
蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。

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

Z
zengyawen 已提交
8 9 10 11


## 导入模块

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


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

enableBluetooth(): boolean

开启蓝牙。

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

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

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

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

**示例:**

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


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

disableBluetooth(): boolean

关闭蓝牙。

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

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

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

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

**示例:**

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


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

getLocalName(): string

获取蓝牙本地设备名称。

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

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

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

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

**示例:**

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


## bluetooth.getState

getState(): BluetoothState

获取蓝牙开关状态。

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

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

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

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

**示例:**

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


## bluetooth.getBtConnectionState

getBtConnectionState(): ProfileConnectionState

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

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

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

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

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

**示例:**

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


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

setLocalName(name: string): boolean

设置蓝牙本地设备名称。

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

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

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

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

**返回值:**

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

**示例:**

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


## bluetooth.pairDevice

pairDevice(deviceId: string): boolean

发起蓝牙配对。

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

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

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

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

**返回值:**

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

**示例:**

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


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

getProfileConnState(profileId: ProfileId): ProfileConnectionState

获取profile的连接状态。

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

cancelPairedDevice(deviceId: string): boolean

删除配对的远程设备。

T
tangjia 已提交
226
**系统接口**:此接口为系统接口。
227

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

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

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

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

**返回值:**

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

**示例:**

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


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

getRemoteDeviceName(deviceId: string): string

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

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

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

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

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

**返回值:**

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

**示例:**

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


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

getRemoteDeviceClass(deviceId: string): DeviceClass

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

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

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

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

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

**返回值:**

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

**示例:**

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


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

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

获取蓝牙配对列表。

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

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

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

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

**示例:**

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


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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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


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

getBluetoothScanMode(): ScanMode

获取蓝牙扫描模式。

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

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

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

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

**示例:**

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


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

startBluetoothDiscovery(): boolean

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

L
lverpeng 已提交
392
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.LOCATION
S
smartbao 已提交
393 394 395

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

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

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

**示例:**

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


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

stopBluetoothDiscovery(): boolean

关闭蓝牙扫描。

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

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

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

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

**示例:**

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


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

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

设置设备配对请求确认。

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

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

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

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

**返回值:**

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

**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

创建一个服务端监听Socket。

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

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

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

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

**示例:**

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

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


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

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

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

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

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

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

**示例:**

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


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

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

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

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

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

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

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

**示例:**

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

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


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

sppCloseServerSocket(socket: number): void

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

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

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

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

**示例:**

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


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

sppCloseClientSocket(socket: number): void

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

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

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

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

**示例:**

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


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

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

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

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

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

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

**返回值:**

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

**示例:**

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


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

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

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

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

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

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

**返回值:**



**示例:**

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


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

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

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

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

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

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

**返回值:**



**示例:**

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


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

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

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

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

**参数:**

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

**返回值:**

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

**示例:**

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

L
lverpeng 已提交
1041
## bluetooth.getProfileInst<sup>9+</sup><a name="getProfileInst"></a>
L
lee 已提交
1042

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
L
lverpeng 已提交
1064
let hidHost = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST);
L
lee 已提交
1065 1066
```

S
smartbao 已提交
1067

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

### bluetooth.BLE.createGattServer

createGattServer(): GattServer

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

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

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

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

**示例:**

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


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

createGattClientDevice(deviceId: string): GattClientDevice

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

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

**参数:**

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

**返回值:**

H
HelloCrease 已提交
1107 1108
| 类型                                    | 说明                                   |
| ------------------------------------- | ------------------------------------ |
S
smartbao 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
| [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 已提交
1130 1131
| 类型                  | 说明                  |
| ------------------- | ------------------- |
L
napi  
lverpeng 已提交
1132
| Array&lt;string&gt; | 返回当前设备作为Server端时连接BLE设备地址集合。 |
S
smartbao 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

**示例:**

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


### bluetooth.BLE.startBLEScan

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

发起BLE扫描流程。

L
lverpeng 已提交
1147
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 和 ohos.permission.LOCATION
S
smartbao 已提交
1148 1149 1150 1151 1152

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

**参数:**

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

**返回值:**



**示例:**

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

**返回值:**



**示例:**

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

**返回值:**



**示例:**

```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 已提交
1289
| 类型                  | 说明            |
L
lee 已提交
1290
| ------------------- | ------------- |
S
smartbao 已提交
1291 1292
| Array&lt;string&gt; | 返回已连接设备的地址列表。 |

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

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

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

getDeviceState(device: string): ProfileConnectionState

获取设备profile的连接状态。

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

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

**参数:**

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

**返回值:**

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

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

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

## A2dpSourceProfile

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


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

connect(device: string): boolean

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

disconnect(device: string): boolean

断开设备的a2dp服务连接。

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

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

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

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

**参数:**

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

**返回值:**



**示例:**

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


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

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

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

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

**参数:**

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

**返回值:**



**示例:**

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


L
lverpeng 已提交
1455
### getPlayingState<sup>8+</sup>
Z
zengyawen 已提交
1456

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

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


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

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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

S
smartbao 已提交
1512
```js
T
tangjia 已提交
1513 1514
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as
  bluetooth.HandsFreeAudioGatewayProfile;
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
T
tangjia 已提交
1544 1545
let hfpAg = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as
  bluetooth.HandsFreeAudioGatewayProfile;
H
HelloCrease 已提交
1546
let ret = hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
1547 1548 1549
```


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

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

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

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

**参数:**

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

**返回值:**



**示例:**

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


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

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

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

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

**参数:**

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

**返回值:**



**示例:**

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


## HidHostProfile

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


1618
### connect<sup>9+</sup><a name="HidHost-connect"></a>
L
lee 已提交
1619 1620 1621 1622 1623

connect(device: string): boolean

连接设备的HidHost服务。

T
tangjia 已提交
1624
**系统接口**:此接口为系统接口。
1625

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
T
tangjia 已提交
1645
let hidHostProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST) as bluetooth.HidHostProfile;
L
lee 已提交
1646 1647 1648 1649
let ret = hidHostProfile.connect('XX:XX:XX:XX:XX:XX');
```


1650
### disconnect<sup>9+</sup><a name="HidHost-disconnect"></a>
L
lee 已提交
1651 1652 1653 1654 1655

disconnect(device: string): boolean

断开连接设备的HidHost服务。

T
tangjia 已提交
1656
**系统接口**:此接口为系统接口。
1657

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
T
tangjia 已提交
1677
let hidHostProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST) as bluetooth.HidHostProfile;
L
lee 已提交
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 1704 1705 1706
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));
}
T
tangjia 已提交
1707
let hidHost = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST) as bluetooth.HidHostProfile;
L
lee 已提交
1708 1709 1710 1711 1712 1713
hidHost.on('connectionStateChange', onReceiveEvent);
```


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

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

取消订阅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));
}
T
tangjia 已提交
1737
let hidHost = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_HID_HOST) as bluetooth.HidHostProfile;
L
lee 已提交
1738 1739
hidHost.on('connectionStateChange', onReceiveEvent);
hidHost.off('connectionStateChange', onReceiveEvent);
Z
zengyawen 已提交
1740 1741 1742
```


L
lverpeng 已提交
1743 1744 1745 1746 1747
## PanProfile

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


1748
### disconnect<sup>9+</sup><a name="PanP-disconnect"></a>
L
lverpeng 已提交
1749 1750 1751 1752 1753

disconnect(device: string): boolean

断开连接设备的Pan服务。

T
tangjia 已提交
1754
**系统接口**:此接口为系统接口。
L
lverpeng 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
T
tangjia 已提交
1775
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
L
lverpeng 已提交
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
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));
}
T
tangjia 已提交
1805
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
L
lverpeng 已提交
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
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));
}
T
tangjia 已提交
1835
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
L
lverpeng 已提交
1836 1837 1838 1839 1840 1841 1842
panProfile.on('connectionStateChange', onReceiveEvent);
panProfile.off('connectionStateChange', onReceiveEvent);
```


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

1843
setTethering(enable: boolean): void
L
lverpeng 已提交
1844 1845 1846

设置网络共享状态。

T
tangjia 已提交
1847
**系统接口**:此接口为系统接口。
L
lverpeng 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867

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

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

**参数:**

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

**返回值:**

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

**示例:**

```js
T
tangjia 已提交
1868
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
L
lverpeng 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
let ret = panProfile.setTethering(true);
```


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

isTetheringOn(): boolean

获取网络共享状态。

T
tangjia 已提交
1879
**系统接口**:此接口为系统接口。
L
lverpeng 已提交
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891

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

**返回值:**

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

**示例:**

```js
T
tangjia 已提交
1892
let panProfile = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_PAN_NETWORK) as bluetooth.PanProfile;
L
lverpeng 已提交
1893 1894 1895 1896
let ret = panProfile.isTetheringOn();
```


Z
zengyawen 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
## GattServer

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


### startAdvertising

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

开始发送BLE广播。

S
smartbao 已提交
1908 1909 1910 1911
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

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

Z
zengyawen 已提交
1912 1913
**参数:**

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

**返回值:**



**示例:**

S
smartbao 已提交
1926
```js
Z
zengyawen 已提交
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 1972 1973 1974
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 已提交
1975 1976 1977 1978
**需要权限**:ohos.permission.DISCOVER_BLUETOOTH

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

Z
zengyawen 已提交
1979 1980 1981 1982 1983 1984
**返回值:**



**示例:**

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


### addService

addService(service: GattService): boolean

server端添加服务。

S
smartbao 已提交
1997 1998 1999 2000
**需要权限**:ohos.permission.USE_BLUETOOTH

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

Z
zengyawen 已提交
2001 2002
**参数:**

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

**返回值:**

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

**示例:**

S
smartbao 已提交
2015
```js
Z
zengyawen 已提交
2016 2017 2018 2019 2020 2021 2022
// 创建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',
L
lverpeng 已提交
2023
  descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
Z
zengyawen 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
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 已提交
2034
  characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
Z
zengyawen 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
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 已提交
2056 2057 2058 2059
**需要权限**:ohos.permission.USE_BLUETOOTH

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

Z
zengyawen 已提交
2060 2061
**参数:**

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

**返回值:**

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

**示例:**

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


### close

close(): void

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

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

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

Z
zengyawen 已提交
2090 2091
**示例:**

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


### notifyCharacteristicChanged

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

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

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

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

Z
zengyawen 已提交
2108 2109
**参数:**

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

**返回值:**

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

**示例:**

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


### sendResponse

sendResponse(serverResponse: ServerResponse): boolean

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

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

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

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

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

**返回值:**

H
HelloCrease 已提交
2161
| 类型      | 说明                         |
B
bmeangel 已提交
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
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};
T
tangjia 已提交
2224

Z
zengyawen 已提交
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
  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
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;
T
tangjia 已提交
2301

Z
zengyawen 已提交
2302 2303
  cccValue[0] = value[0];
  let serverResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC};
T
tangjia 已提交
2304

Z
zengyawen 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
  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
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};
T
tangjia 已提交
2381

Z
zengyawen 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
  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
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};
T
tangjia 已提交
2461

Z
zengyawen 已提交
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
  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
| 类型                                       | 说明                         |
B
bmeangel 已提交
2791
| ---------------------------------------- | -------------------------- |
Z
zengyawen 已提交
2792 2793 2794 2795
| Promise&lt;[BLECharacteristic](#blecharacteristic)&gt; | client读取特征值,通过promise形式获取。 |

**示例:**

S
smartbao 已提交
2796
```js
W
wangyanhandsome 已提交
2797
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2798 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
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 已提交
2824 2825 2826 2827
**需要权限**:ohos.permission.USE_BLUETOOTH

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

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

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

**返回值:**



**示例:**

S
smartbao 已提交
2841
```js
Z
zengyawen 已提交
2842 2843 2844 2845 2846 2847 2848 2849 2850
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 已提交
2851
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
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 已提交
2868 2869 2870 2871
**需要权限**:ohos.permission.USE_BLUETOOTH

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

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

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

**返回值:**

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

**示例:**

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

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

Z
zengyawen 已提交
2908 2909
**参数:**

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

**返回值:**

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

**示例:**

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

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

Z
zengyawen 已提交
2958 2959
**参数:**

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

**返回值:**

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

**示例:**

S
smartbao 已提交
2972
```js
W
wangyanhandsome 已提交
2973
let device = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
Z
zengyawen 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992
let bufferDesc = new ArrayBuffer(8);
let descV = new Uint8Array(bufferDesc);
descV[0] = 22;
let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
let retWriteDesc = device.writeDescriptorValue(descriptor);
if (retWriteDesc) {
  console.log('bluetooth write descriptor successfully');
} else {
  console.log('bluetooth write descriptor failed');
}
```


### setBLEMtuSize

setBLEMtuSize(mtu: number): boolean

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

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

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

Z
zengyawen 已提交
2999 3000
**参数:**

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

**返回值:**

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

**示例:**

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


### setNotifyCharacteristicChanged

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

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

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

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

Z
zengyawen 已提交
3029 3030
**参数:**

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

**返回值:**

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

**示例:**

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


### on('BLECharacteristicChange')

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

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

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

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

Z
zengyawen 已提交
3072 3073
**参数:**

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

**返回值:**



**示例:**

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


### off('BLECharacteristicChange')

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

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

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

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

Z
zengyawen 已提交
3106 3107
**参数:**

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

**返回值:**



**示例:**

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


### on('BLEConnectionStateChange')

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

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

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

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

Z
zengyawen 已提交
3135 3136
**参数:**

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

**返回值:**



**示例:**

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


### off('BLEConnectionStateChange')

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

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

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

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

Z
zengyawen 已提交
3168 3169
**参数:**

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

**返回值:**



**示例:**

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


### getDeviceName

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

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

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

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

Z
zengyawen 已提交
3197 3198
**参数:**

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

**返回值:**



**示例:**

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

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

Z
zengyawen 已提交
3229 3230
**返回值:**

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

**示例:**

S
smartbao 已提交
3237
```js
Z
zengyawen 已提交
3238
// promise
W
wangyanhandsome 已提交
3239
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
let ret = device.connect();
let deviceName = gattClient.getDeviceName().then((data) => {
    console.info('device name' + JSON.stringify(data));
})
```


### getRssiValue

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

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

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

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

Z
zengyawen 已提交
3257 3258
**参数:**

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

**返回值:**



**示例:**

S
smartbao 已提交
3269
```js
Z
zengyawen 已提交
3270
// callback
W
wangyanhandsome 已提交
3271
let gattClient = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
Z
zengyawen 已提交
3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
let ret = device.connect();
let rssi = gattClient.getRssiValue((err, data)=> {
    console.info('rssi err ' + JSON.stringify(err));
    console.info('rssi value' + JSON.stringify(data));
})
```


### getRssiValue

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

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

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

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

Z
zengyawen 已提交
3290 3291
**返回值:**

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

**示例:**

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

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

枚举,扫描模式。

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

H
HelloCrease 已提交
3312 3313 3314 3315 3316 3317 3318 3319
| 名称                                       | 默认值  | 说明              |
| ---------------------------------------- | ---- | --------------- |
| 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 已提交
3320

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

枚举,配对状态。

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

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


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

描述spp的配置参数。

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

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


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

枚举,Spp链路类型。

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

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


## GattService

描述service的接口参数定义。

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

H
HelloCrease 已提交
3364 3365 3366 3367 3368 3369
| 名称              | 参数类型                                     | 可读   | 可写   | 说明                                       |
| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| 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 已提交
3370 3371 3372 3373 3374 3375


## BLECharacteristic

描述characteristic的接口参数定义 。

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

H
HelloCrease 已提交
3378 3379 3380 3381 3382 3383
| 名称                  | 参数类型                                     | 可读   | 可写   | 说明                                       |
| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- |
| 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 已提交
3384 3385 3386 3387 3388 3389


## BLEDescriptor

描述descriptor的接口参数定义 。

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

H
HelloCrease 已提交
3392 3393 3394 3395 3396 3397
| 名称                 | 参数类型        | 可读   | 可写   | 说明                                       |
| ------------------ | ----------- | ---- | ---- | ---------------------------------------- |
| 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 已提交
3398 3399 3400 3401 3402 3403


## NotifyCharacteristic

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

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

H
HelloCrease 已提交
3406 3407 3408 3409 3410 3411
| 名称                  | 参数类型        | 可读   | 可写   | 说明                                       |
| ------------------- | ----------- | ---- | ---- | ---------------------------------------- |
| 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 已提交
3412 3413 3414 3415 3416 3417


## CharacteristicReadReq

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

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

H
HelloCrease 已提交
3420 3421 3422 3423 3424 3425 3426
| 名称                 | 参数类型   | 可读   | 可写   | 说明                                       |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
3427 3428 3429 3430 3431 3432


## CharacteristicWriteReq

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

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

H
HelloCrease 已提交
3435 3436 3437 3438 3439 3440 3441 3442
| 名称                 | 参数类型   | 可读   | 可写   | 说明                                       |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
3443 3444 3445 3446 3447 3448


## DescriptorReadReq

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

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

H
HelloCrease 已提交
3451 3452 3453 3454 3455 3456 3457 3458
| 名称                 | 参数类型   | 可读   | 可写   | 说明                                       |
| ------------------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
3459 3460 3461 3462 3463 3464


## DescriptorWriteReq

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

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

H
HelloCrease 已提交
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
| 名称                 | 参数类型        | 可读   | 可写   | 说明                                       |
| ------------------ | ----------- | ---- | ---- | ---------------------------------------- |
| 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 已提交
3478 3479 3480 3481 3482 3483


## ServerResponse

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

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

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


## BLEConnectChangedState

描述Gatt profile连接状态 。

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

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


## ProfileConnectionState

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

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

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


## ScanFilter

扫描过滤参数。

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

T
tangjia 已提交
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
| 名称                                     | 参数类型    | 可读 | 可写 | 说明                                                         |
| ---------------------------------------- | ----------- | ---- | ---- | ------------------------------------------------------------ |
| 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]。 |
3537
| manufactureId<sup>9+</sup>               | number      | 是   | 是   | 表示过滤包含该制造商ID的设备,例如:0x0006。                 |
T
tangjia 已提交
3538 3539
| manufactureData<sup>9+</sup>             | ArrayBuffer | 是   | 是   | 表示过滤包含该制造商相关数据的设备,例如:[0x1F,0x2F,0x3F]。 |
| manufactureDataMask<sup>9+</sup>         | ArrayBuffer | 是   | 是   | 表示过滤包含该制造商相关数据掩码的设备,例如:[0xFF,0xFF,0xFF]。 |
Z
zengyawen 已提交
3540 3541 3542 3543 3544 3545


## ScanOptions

扫描的配置参数。

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

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


## ScanDuty

枚举,扫描模式。

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

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


## MatchMode

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

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

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


## ScanResult

扫描结果上报数据。

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

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


## BluetoothState

枚举,蓝牙开关状态。

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

H
HelloCrease 已提交
3599 3600 3601 3602 3603 3604 3605 3606 3607
| 名称                    | 默认值  | 说明                 |
| --------------------- | ---- | ------------------ |
| 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 已提交
3608 3609 3610 3611 3612 3613


## AdvertiseSetting

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

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

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


## AdvertiseData

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

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

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


## ManufactureData

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

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

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


## ServiceData

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

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

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


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

描述配对请求参数。

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

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


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

描述配对状态参数。

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

| 名称       | 参数类型   | 可读   | 可写   | 说明          |
| -------- | ------ | ---- | ---- | ----------- |
| deviceId | string      | 是    | 否    | 表示要配对的设备ID。 |
| state    | BondState   | 是    | 否    | 表示配对设备的状态。 |


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

描述profile状态改变参数。

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

Z
zengyawen 已提交
3690 3691 3692 3693
| 名称     | 参数类型                                          | 可读 | 可写 | 说明                            |
| -------- | ------------------------------------------------- | ---- | ---- | ------------------------------- |
| deviceId | string                                            | 是   | 否   | 表示蓝牙设备地址。              |
| state    | [ProfileConnectionState](#profileconnectionstate) | 是   | 否   | 表示蓝牙设备的profile连接状态。 |
S
smartbao 已提交
3694 3695 3696


## DeviceClass<sup>8+</sup><a name="DeviceClass"></a>
W
wangyanhandsome 已提交
3697 3698 3699

描述蓝牙设备的类别。

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

H
HelloCrease 已提交
3702 3703 3704 3705 3706
| 名称              | 参数类型                                | 可读   | 可写   | 说明               |
| --------------- | ----------------------------------- | ---- | ---- | ---------------- |
| majorClass      | [MajorClass](#majorclass)           | 是    | 否    | 表示蓝牙设备主要类别的枚举。   |
| majorMinorClass | [MajorMinorClass](#majorminorclass) | 是    | 否    | 表示主要次要蓝牙设备类别的枚举。 |
| classOfDevice   | number                              | 是    | 否    | 表示设备类别。          |
W
wangyanhandsome 已提交
3707 3708


S
smartbao 已提交
3709 3710

## MajorClass<sup>8+</sup><a name="MajorClass"></a>
W
wangyanhandsome 已提交
3711 3712 3713

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

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

H
HelloCrease 已提交
3716 3717 3718 3719 3720 3721
| 名称                  | 默认值    | 说明         |
| ------------------- | ------ | ---------- |
| MAJOR_MISC          | 0x0000 | 表示杂项设备。    |
| MAJOR_COMPUTER      | 0x0100 | 表示计算机设备。   |
| MAJOR_PHONE         | 0x0200 | 表示手机设备。    |
| MAJOR_NETWORKING    | 0x0300 | 表示网络设备。    |
Z
zengyawen 已提交
3722
| MAJOR_AUDIO_VIDEO   | 0x0400 | 表示音频和视频设备。 |
H
HelloCrease 已提交
3723 3724 3725 3726 3727 3728
| MAJOR_PERIPHERAL    | 0x0500 | 表示外围设备。    |
| MAJOR_IMAGING       | 0x0600 | 表示成像设备。    |
| MAJOR_WEARABLE      | 0x0700 | 表示可穿戴设备。   |
| MAJOR_TOY           | 0x0800 | 表示玩具设备。    |
| MAJOR_HEALTH        | 0x0900 | 表示健康设备。    |
| MAJOR_UNCATEGORIZED | 0x1F00 | 表示未分类设备。   |
W
wangyanhandsome 已提交
3729 3730


S
smartbao 已提交
3731
## MajorMinorClass<sup>8+</sup><a name="MajorMinorClass"></a>
W
wangyanhandsome 已提交
3732 3733 3734

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

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

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


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

枚举,蓝牙A2DP 播放状态。

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

H
HelloCrease 已提交
3833 3834 3835
| 名称                | 默认值    | 说明      |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | 表示未播放。  |
Z
zengyawen 已提交
3836
| STATE_PLAYING     | 0x0001 | 表示正在播放。 |
S
smartbao 已提交
3837 3838 3839 3840


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

3841
蓝牙profile枚举,API9新增PROFILE_HID_HOST,PROFILE_PAN_NETWORK。
S
smartbao 已提交
3842 3843 3844

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

H
HelloCrease 已提交
3845 3846
| 名称                               | 默认值    | 说明              |
| -------------------------------- | ------ | --------------- |
Z
zengyawen 已提交
3847
| PROFILE_A2DP_SOURCE              | 0x0001 | 表示A2DP profile。 |
L
lee 已提交
3848
| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 0x0004 | 表示HFP profile。  |
3849 3850
| PROFILE_HID_HOST<sup>9+</sup> | 0x0006 | 表示HID profile。  |
| PROFILE_PAN_NETWORK<sup>9+</sup> | 0x0007 | 表示PAN profile。  |