1# @ohos.bluetoothManager (蓝牙) 2 3蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 从API Version 10 开始,该接口不再维护,推荐使用[`@ohos.bluetooth.ble`](js-apis-bluetooth-ble.md)等相关profile接口。 9 10 11 12## 导入模块 13 14```js 15import bluetoothManager from '@ohos.bluetoothManager'; 16``` 17 18 19## bluetoothManager.enableBluetooth<sup>(deprecated)</sup> 20 21enableBluetooth(): void 22 23开启蓝牙。 24 25> **说明:**<br/> 26> 从API version 9开始支持,从API version 10开始废弃。建议使用[access.enableBluetooth](js-apis-bluetooth-access.md#accessenablebluetooth)替代。 27 28**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 29 30**系统能力**:SystemCapability.Communication.Bluetooth.Core。 31 32**错误码**: 33 34以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 35 36| 错误码ID | 错误信息 | 37| -------- | ---------------------------- | 38|2900001 | Service stopped. | 39|2900099 | Operation failed. | 40 41**示例:** 42 43```js 44import { BusinessError } from '@ohos.base'; 45try { 46 bluetoothManager.enableBluetooth(); 47} catch (err) { 48 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 49} 50``` 51 52 53## bluetoothManager.disableBluetooth<sup>(deprecated)</sup> 54 55disableBluetooth(): void 56 57关闭蓝牙。 58 59> **说明:**<br/> 60> 从API version 9开始支持,从API version 10开始废弃。建议使用[access.disableBluetooth](js-apis-bluetooth-access.md#accessdisablebluetooth)替代。 61 62**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 63 64**系统能力**:SystemCapability.Communication.Bluetooth.Core。 65 66**错误码**: 67 68以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 69 70| 错误码ID | 错误信息 | 71| -------- | ---------------------------- | 72|2900001 | Service stopped. | 73|2900099 | Operation failed. | 74 75**示例:** 76 77```js 78import { BusinessError } from '@ohos.base'; 79try { 80 bluetoothManager.disableBluetooth(); 81} catch (err) { 82 console.error("errCode:" + (err as BusinessError).code + ", errMessage:" + (err as BusinessError).message); 83} 84``` 85 86 87## bluetoothManager.getLocalName<sup>(deprecated)</sup> 88 89getLocalName(): string 90 91获取蓝牙本地设备名称。 92 93> **说明:**<br/> 94> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getLocalName](js-apis-bluetooth-connection.md#connectiongetlocalname)替代。 95 96**需要权限**:ohos.permission.USE_BLUETOOTH 97 98**系统能力**:SystemCapability.Communication.Bluetooth.Core。 99 100**返回值:** 101 102| 类型 | 说明 | 103| ------ | --------- | 104| string | 蓝牙本地设备名称。 | 105 106**错误码**: 107 108以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 109 110| 错误码ID | 错误信息 | 111| -------- | ---------------------------- | 112|2900001 | Service stopped. | 113|2900099 | Operation failed. | 114 115**示例:** 116 117```js 118import { BusinessError } from '@ohos.base'; 119try { 120 let localName: string = bluetoothManager.getLocalName(); 121} catch (err) { 122 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 123} 124``` 125 126 127## bluetoothManager.getState<sup>(deprecated)</sup> 128 129getState(): BluetoothState 130 131获取蓝牙开关状态。 132 133> **说明:**<br/> 134> 从API version 9开始支持,从API version 10开始废弃。建议使用[access.getState](js-apis-bluetooth-access.md#accessgetstate)替代。 135 136**需要权限**:ohos.permission.USE_BLUETOOTH 137 138**系统能力**:SystemCapability.Communication.Bluetooth.Core。 139 140**返回值:** 141 142| 类型 | 说明 | 143| --------------------------------- | --------- | 144| [BluetoothState](#bluetoothstatedeprecated) | 表示蓝牙开关状态。 | 145 146**错误码**: 147 148以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 149 150| 错误码ID | 错误信息 | 151| -------- | ---------------------------- | 152|2900001 | Service stopped. | 153|2900099 | Operation failed. | 154 155**示例:** 156 157```js 158import { BusinessError } from '@ohos.base'; 159try { 160 let state: bluetoothManager.BluetoothState = bluetoothManager.getState(); 161} catch (err) { 162 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 163} 164``` 165 166 167## bluetoothManager.getBtConnectionState<sup>(deprecated)</sup> 168 169getBtConnectionState(): ProfileConnectionState 170 171获取蓝牙本端的Profile连接状态,例如:任意一个支持的Profile连接状态为已连接,则此接口返回状态为已连接。 172 173> **说明:**<br/> 174> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getProfileConnectionState](js-apis-bluetooth-connection.md#connectiongetprofileconnectionstate)替代。 175 176**需要权限**:ohos.permission.USE_BLUETOOTH 177 178**系统能力**:SystemCapability.Communication.Bluetooth.Core。 179 180**返回值:** 181 182| 类型 | 说明 | 183| ---------------------------------------- | ------------------- | 184| [ProfileConnectionState](#profileconnectionstatedeprecated) | 表示蓝牙设备的Profile连接状态。 | 185 186**错误码**: 187 188以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 189 190| 错误码ID | 错误信息 | 191| -------- | ---------------------------- | 192|2900001 | Service stopped. | 193|2900003 | Bluetooth switch is off. | 194|2900099 | Operation failed. | 195 196**示例:** 197 198```js 199import { BusinessError } from '@ohos.base'; 200try { 201 let connectionState: bluetoothManager.ProfileConnectionState = bluetoothManager.getBtConnectionState(); 202} catch (err) { 203 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 204} 205``` 206 207 208## bluetoothManager.setLocalName<sup>(deprecated)</sup> 209 210setLocalName(name: string): void 211 212设置蓝牙本地设备名称。 213 214> **说明:**<br/> 215> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.setLocalName](js-apis-bluetooth-connection.md#connectionsetlocalname)替代。 216 217**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 218 219**系统能力**:SystemCapability.Communication.Bluetooth.Core。 220 221**参数:** 222 223| 参数名 | 类型 | 必填 | 说明 | 224| ---- | ------ | ---- | --------------------- | 225| name | string | 是 | 要设置的蓝牙名称,最大长度为248字节数。 | 226 227**错误码**: 228 229以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 230 231| 错误码ID | 错误信息 | 232| -------- | ---------------------------- | 233|2900001 | Service stopped. | 234|2900003 | Bluetooth switch is off. | 235|2900099 | Operation failed. | 236 237**示例:** 238 239```js 240import { BusinessError } from '@ohos.base'; 241try { 242 bluetoothManager.setLocalName('device_name'); 243} catch (err) { 244 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 245} 246``` 247 248 249## bluetoothManager.pairDevice<sup>(deprecated)</sup> 250 251pairDevice(deviceId: string): void 252 253发起蓝牙配对。 254 255> **说明:**<br/> 256> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.pairDevice](js-apis-bluetooth-connection.md#connectionpairdevice)替代。 257 258**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 259 260**系统能力**:SystemCapability.Communication.Bluetooth.Core。 261 262**参数:** 263 264| 参数名 | 类型 | 必填 | 说明 | 265| -------- | ------ | ---- | ----------------------------------- | 266| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 267 268**错误码**: 269 270以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 271 272| 错误码ID | 错误信息 | 273| -------- | ---------------------------- | 274|2900001 | Service stopped. | 275|2900003 | Bluetooth switch is off. | 276|2900099 | Operation failed. | 277 278**示例:** 279 280```js 281import { BusinessError } from '@ohos.base'; 282try { 283 // 实际的地址可由扫描流程获取 284 bluetoothManager.pairDevice("XX:XX:XX:XX:XX:XX"); 285} catch (err) { 286 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 287} 288``` 289 290 291## bluetoothManager.getProfileConnectionState<sup>(deprecated)</sup> 292 293getProfileConnectionState(profileId: ProfileId): ProfileConnectionState 294 295依据ProfileId获取指定profile的连接状态。 296 297> **说明:**<br/> 298> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getProfileConnectionState](js-apis-bluetooth-connection.md#connectiongetprofileconnectionstate)替代。 299 300**需要权限**:ohos.permission.USE_BLUETOOTH 301 302**系统能力**:SystemCapability.Communication.Bluetooth.Core。 303 304**参数:** 305 306| 参数名 | 类型 | 必填 | 说明 | 307| --------- | --------- | ---- | ------------------------------------- | 308| ProfileId | profileId | 是 | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 | 309 310**返回值:** 311 312| 类型 | 说明 | 313| ------------------------------------------------- | ------------------- | 314| [ProfileConnectionState](#profileconnectionstatedeprecated) | profile的连接状态。 | 315 316**错误码**: 317 318以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 319 320| 错误码ID | 错误信息 | 321| -------- | ---------------------------- | 322|2900001 | Service stopped. | 323|2900003 | Bluetooth switch is off. | 324|2900004 | Profile is not supported. | 325|2900099 | Operation failed. | 326 327**示例:** 328 329```js 330import { BusinessError } from '@ohos.base'; 331try { 332 let result: bluetoothManager.ProfileConnectionState = bluetoothManager.getProfileConnectionState(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE); 333} catch (err) { 334 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 335} 336``` 337 338 339 340## bluetoothManager.getRemoteDeviceName<sup>(deprecated)</sup> 341 342getRemoteDeviceName(deviceId: string): string 343 344获取对端蓝牙设备的名称。 345 346> **说明:**<br/> 347> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getRemoteDeviceName](js-apis-bluetooth-connection.md#connectiongetremotedevicename)替代。 348 349**需要权限**:ohos.permission.USE_BLUETOOTH 350 351**系统能力**:SystemCapability.Communication.Bluetooth.Core。 352 353**参数:** 354 355| 参数名 | 类型 | 必填 | 说明 | 356| -------- | ------ | ---- | --------------------------------- | 357| deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 358 359**返回值:** 360 361| 类型 | 说明 | 362| ------ | ------------- | 363| string | 以字符串格式返回设备名称。 | 364 365**错误码**: 366 367以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 368 369| 错误码ID | 错误信息 | 370| -------- | ---------------------------- | 371|2900001 | Service stopped. | 372|2900003 | Bluetooth switch is off. | 373|2900099 | Operation failed. | 374 375**示例:** 376 377```js 378import { BusinessError } from '@ohos.base'; 379try { 380 let remoteDeviceName: string = bluetoothManager.getRemoteDeviceName("XX:XX:XX:XX:XX:XX"); 381} catch (err) { 382 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 383} 384``` 385 386 387## bluetoothManager.getRemoteDeviceClass<sup>(deprecated)</sup> 388 389getRemoteDeviceClass(deviceId: string): DeviceClass 390 391获取对端蓝牙设备的类别。 392 393> **说明:**<br/> 394> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getRemoteDeviceClass](js-apis-bluetooth-connection.md#connectiongetremotedeviceclass)替代。 395 396**需要权限**:ohos.permission.USE_BLUETOOTH 397 398**系统能力**:SystemCapability.Communication.Bluetooth.Core。 399 400**参数:** 401 402| 参数名 | 类型 | 必填 | 说明 | 403| -------- | ------ | ---- | --------------------------------- | 404| deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 405 406**返回值:** 407 408| 类型 | 说明 | 409| --------------------------- | -------- | 410| [DeviceClass](#deviceclassdeprecated) | 远程设备的类别。 | 411 412**错误码**: 413 414以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 415 416| 错误码ID | 错误信息 | 417| -------- | ---------------------------- | 418|2900001 | Service stopped. | 419|2900003 | Bluetooth switch is off. | 420|2900099 | Operation failed. | 421 422**示例:** 423 424```js 425import { BusinessError } from '@ohos.base'; 426try { 427 let remoteDeviceClass: bluetoothManager.DeviceClass = bluetoothManager.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX"); 428} catch (err) { 429 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 430} 431``` 432 433 434## bluetoothManager.getPairedDevices<sup>(deprecated)</sup> 435 436getPairedDevices(): Array<string> 437 438获取蓝牙配对列表。 439 440> **说明:**<br/> 441> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getPairedDevices](js-apis-bluetooth-connection.md#connectiongetpaireddevices)替代。 442 443**需要权限**:ohos.permission.USE_BLUETOOTH 444 445**系统能力**:SystemCapability.Communication.Bluetooth.Core。 446 447**返回值:** 448 449| 类型 | 说明 | 450| ------------------- | ------------- | 451| Array<string> | 已配对蓝牙设备的地址列表。 | 452 453**错误码**: 454 455以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 456 457| 错误码ID | 错误信息 | 458| -------- | ---------------------------- | 459|2900001 | Service stopped. | 460|2900003 | Bluetooth switch is off. | 461|2900099 | Operation failed. | 462 463**示例:** 464 465```js 466import { BusinessError } from '@ohos.base'; 467try { 468 let devices: Array<string> = bluetoothManager.getPairedDevices(); 469} catch (err) { 470 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 471} 472``` 473 474 475## bluetoothManager.setBluetoothScanMode<sup>(deprecated)</sup> 476 477setBluetoothScanMode(mode: ScanMode, duration: number): void 478 479设置蓝牙扫描模式,可以被远端设备发现。 480 481> **说明:**<br/> 482> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.setBluetoothScanMode](js-apis-bluetooth-connection.md#connectionsetbluetoothscanmode)替代。 483 484**需要权限**:ohos.permission.USE_BLUETOOTH 485 486**系统能力**:SystemCapability.Communication.Bluetooth.Core。 487 488**参数:** 489 490| 参数名 | 类型 | 必填 | 说明 | 491| -------- | --------------------- | ---- | ---------------------------- | 492| mode | [ScanMode](#scanmodedeprecated) | 是 | 蓝牙扫描模式。 | 493| duration | number | 是 | 设备可被发现的持续时间,单位为毫秒;设置为0则持续可发现。 | 494 495**错误码**: 496 497以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 498 499| 错误码ID | 错误信息 | 500| -------- | ---------------------------- | 501|2900001 | Service stopped. | 502|2900003 | Bluetooth switch is off. | 503|2900099 | Operation failed. | 504 505**示例:** 506 507```js 508import { BusinessError } from '@ohos.base'; 509try { 510 // 设置为可连接可发现才可被远端设备扫描到,可以连接。 511 bluetoothManager.setBluetoothScanMode(bluetoothManager.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 512} catch (err) { 513 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 514} 515``` 516 517 518## bluetoothManager.getBluetoothScanMode<sup>(deprecated)</sup> 519 520getBluetoothScanMode(): ScanMode 521 522获取蓝牙扫描模式。 523 524> **说明:**<br/> 525> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.getBluetoothScanMode](js-apis-bluetooth-connection.md#connectiongetbluetoothscanmode)替代。 526 527**需要权限**:ohos.permission.USE_BLUETOOTH 528 529**系统能力**:SystemCapability.Communication.Bluetooth.Core。 530 531**返回值:** 532 533| 类型 | 说明 | 534| --------------------- | ------- | 535| [ScanMode](#scanmodedeprecated) | 蓝牙扫描模式。 | 536 537**错误码**: 538 539以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 540 541| 错误码ID | 错误信息 | 542| -------- | ---------------------------- | 543|2900001 | Service stopped. | 544|2900003 | Bluetooth switch is off. | 545|2900099 | Operation failed. | 546 547**示例:** 548 549```js 550import { BusinessError } from '@ohos.base'; 551try { 552 let scanMode: bluetoothManager.ScanMode = bluetoothManager.getBluetoothScanMode(); 553} catch (err) { 554 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 555} 556``` 557 558 559## bluetoothManager.startBluetoothDiscovery<sup>(deprecated)</sup> 560 561startBluetoothDiscovery(): void 562 563开启蓝牙扫描,可以发现远端设备。 564 565> **说明:**<br/> 566> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.startBluetoothDiscovery](js-apis-bluetooth-connection.md#connectionstartbluetoothdiscovery)替代。 567 568**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 569 570**系统能力**:SystemCapability.Communication.Bluetooth.Core。 571 572**错误码**: 573 574以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 575 576| 错误码ID | 错误信息 | 577| -------- | ---------------------------- | 578|2900001 | Service stopped. | 579|2900003 | Bluetooth switch is off. | 580|2900099 | Operation failed. | 581 582**示例:** 583 584```js 585import { BusinessError } from '@ohos.base'; 586let deviceId: Array<string>; 587function onReceiveEvent(data: Array<string>) { 588 deviceId = data; 589} 590try { 591 bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent); 592 bluetoothManager.startBluetoothDiscovery(); 593} catch (err) { 594 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 595} 596``` 597 598 599## bluetoothManager.stopBluetoothDiscovery<sup>(deprecated)</sup> 600 601stopBluetoothDiscovery(): void 602 603关闭蓝牙扫描。 604 605> **说明:**<br/> 606> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.stopBluetoothDiscovery](js-apis-bluetooth-connection.md#connectionstopbluetoothdiscovery)替代。 607 608**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 609 610**系统能力**:SystemCapability.Communication.Bluetooth.Core。 611 612**错误码**: 613 614以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 615 616| 错误码ID | 错误信息 | 617| -------- | ---------------------------- | 618|2900001 | Service stopped. | 619|2900003 | Bluetooth switch is off. | 620|2900099 | Operation failed. | 621 622**示例:** 623 624```js 625import { BusinessError } from '@ohos.base'; 626try { 627 bluetoothManager.stopBluetoothDiscovery(); 628} catch (err) { 629 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 630} 631``` 632 633 634## bluetoothManager.setDevicePairingConfirmation<sup>(deprecated)</sup> 635 636setDevicePairingConfirmation(device: string, accept: boolean): void 637 638设置设备配对请求确认。 639 640> **说明:**<br/> 641> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.setDevicePairingConfirmation](js-apis-bluetooth-connection.md#connectionsetdevicepairingconfirmation)替代。 642 643**需要权限**:ohos.permission.MANAGE_BLUETOOTH 644 645**系统能力**:SystemCapability.Communication.Bluetooth.Core。 646 647**参数:** 648 649| 参数名 | 类型 | 必填 | 说明 | 650| ------ | ------- | ---- | -------------------------------- | 651| device | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 652| accept | boolean | 是 | 接受配对请求设置为true,否则设置为false。 | 653 654**错误码**: 655 656以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 657 658| 错误码ID | 错误信息 | 659| -------- | ---------------------------- | 660|2900001 | Service stopped. | 661|2900003 | Bluetooth switch is off. | 662|2900099 | Operation failed. | 663 664**示例:** 665 666```js 667import { BusinessError } from '@ohos.base'; 668// 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认 669function onReceivePinRequiredEvent(data: bluetoothManager.PinRequiredParam) { // data为配对请求的入参,配对请求参数 670 console.info('pin required = '+ JSON.stringify(data)); 671 bluetoothManager.setDevicePairingConfirmation(data.deviceId, true); 672} 673try { 674 bluetoothManager.on("pinRequired", onReceivePinRequiredEvent); 675} catch (err) { 676 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 677} 678``` 679 680 681## bluetoothManager.on('bluetoothDeviceFind')<sup>(deprecated)</sup> 682 683on(type: "bluetoothDeviceFind", callback: Callback<Array<string>>): void 684 685订阅蓝牙设备发现上报事件。 686 687> **说明:**<br/> 688> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.on('bluetoothDeviceFind')](js-apis-bluetooth-connection.md#connectiononbluetoothdevicefind)替代。 689 690**需要权限**:ohos.permission.USE_BLUETOOTH 691 692**系统能力**:SystemCapability.Communication.Bluetooth.Core。 693 694**参数:** 695 696| 参数名 | 类型 | 必填 | 说明 | 697| -------- | ----------------------------------- | ---- | -------------------------------------- | 698| type | string | 是 | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 | 699| callback | Callback<Array<string>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | 700 701**错误码**: 702 703以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 704 705| 错误码ID | 错误信息 | 706| -------- | ---------------------------- | 707|2900099 | Operation failed. | 708 709**示例:** 710 711```js 712import { BusinessError } from '@ohos.base'; 713function onReceiveEvent(data: Array<string>) { // data为蓝牙设备地址集合 714 console.info('bluetooth device find = '+ JSON.stringify(data)); 715} 716try { 717 bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent); 718} catch (err) { 719 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 720} 721``` 722 723 724## bluetoothManager.off('bluetoothDeviceFind')<sup>(deprecated)</sup> 725 726off(type: "bluetoothDeviceFind", callback?: Callback<Array<string>>): void 727 728取消订阅蓝牙设备发现上报事件。 729 730> **说明:**<br/> 731> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.off('bluetoothDeviceFind')](js-apis-bluetooth-connection.md#connectionoffbluetoothdevicefind)替代。 732 733**需要权限**:ohos.permission.USE_BLUETOOTH 734 735**系统能力**:SystemCapability.Communication.Bluetooth.Core。 736 737**参数:** 738 739| 参数名 | 类型 | 必填 | 说明 | 740| -------- | ----------------------------------- | ---- | ---------------------------------------- | 741| type | string | 是 | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 | 742| callback | Callback<Array<string>> | 否 | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | 743 744**错误码**: 745 746以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 747 748| 错误码ID | 错误信息 | 749| -------- | ---------------------------- | 750|2900099 | Operation failed. | 751 752**示例:** 753 754```js 755import { BusinessError } from '@ohos.base'; 756function onReceiveEvent(data: Array<string>) { 757 console.info('bluetooth device find = '+ JSON.stringify(data)); 758} 759try { 760 bluetoothManager.on('bluetoothDeviceFind', onReceiveEvent); 761 bluetoothManager.off('bluetoothDeviceFind', onReceiveEvent); 762} catch (err) { 763 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 764} 765``` 766 767 768## bluetoothManager.on('pinRequired')<sup>(deprecated)</sup> 769 770on(type: "pinRequired", callback: Callback<PinRequiredParam>): void 771 772订阅远端蓝牙设备的配对请求事件。 773 774> **说明:**<br/> 775> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.on('pinRequired')](js-apis-bluetooth-connection.md#connectiononpinrequired)替代。 776 777**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 778 779**系统能力**:SystemCapability.Communication.Bluetooth.Core。 780 781**参数:** 782 783| 参数名 | 类型 | 必填 | 说明 | 784| -------- | ---------------------------------------- | ---- | -------------------------------- | 785| type | string | 是 | 填写"pinRequired"字符串,表示配对请求事件。 | 786| callback | Callback<[PinRequiredParam](#pinrequiredparamdeprecated)> | 是 | 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。 | 787 788**错误码**: 789 790以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 791 792| 错误码ID | 错误信息 | 793| -------- | ---------------------------- | 794|2900099 | Operation failed. | 795 796**示例:** 797 798```js 799import { BusinessError } from '@ohos.base'; 800function onReceiveEvent(data: bluetoothManager.PinRequiredParam) { // data为配对请求参数 801 console.info('pin required = '+ JSON.stringify(data)); 802} 803try { 804 bluetoothManager.on('pinRequired', onReceiveEvent); 805} catch (err) { 806 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 807} 808``` 809 810 811## bluetoothManager.off('pinRequired')<sup>(deprecated)</sup> 812 813off(type: "pinRequired", callback?: Callback<PinRequiredParam>): void 814 815取消订阅远端蓝牙设备的配对请求事件。 816 817> **说明:**<br/> 818> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.off('pinRequired')](js-apis-bluetooth-connection.md#connectionoffpinrequired)替代。 819 820**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 821 822**系统能力**:SystemCapability.Communication.Bluetooth.Core。 823 824**参数:** 825 826| 参数名 | 类型 | 必填 | 说明 | 827| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 828| type | string | 是 | 填写"pinRequired"字符串,表示配对请求事件。 | 829| callback | Callback<[PinRequiredParam](#pinrequiredparamdeprecated)> | 否 | 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。 | 830 831**错误码**: 832 833以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 834 835| 错误码ID | 错误信息 | 836| -------- | ---------------------------- | 837|2900099 | Operation failed. | 838 839**示例:** 840 841```js 842import { BusinessError } from '@ohos.base'; 843function onReceiveEvent(data: bluetoothManager.PinRequiredParam) { 844 console.info('pin required = '+ JSON.stringify(data)); 845} 846try { 847 bluetoothManager.on('pinRequired', onReceiveEvent); 848 bluetoothManager.off('pinRequired', onReceiveEvent); 849} catch (err) { 850 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 851} 852``` 853 854 855## bluetoothManager.on('bondStateChange')<sup>(deprecated)</sup> 856 857on(type: "bondStateChange", callback: Callback<BondStateParam>): void 858 859订阅蓝牙配对状态改变事件。 860 861> **说明:**<br/> 862> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.on('bondStateChange')](js-apis-bluetooth-connection.md#connectiononbondstatechange)替代。 863 864**需要权限**:ohos.permission.USE_BLUETOOTH 865 866**系统能力**:SystemCapability.Communication.Bluetooth.Core。 867 868**参数:** 869 870| 参数名 | 类型 | 必填 | 说明 | 871| -------- | ---------------------------------------- | ---- | ------------------------------------ | 872| type | string | 是 | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 | 873| callback | Callback<[BondStateParam](#bondstateparamdeprecated)> | 是 | 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。 | 874 875**错误码**: 876 877以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 878 879| 错误码ID | 错误信息 | 880| -------- | ---------------------------- | 881|2900099 | Operation failed. | 882 883**示例:** 884 885```js 886import { BusinessError } from '@ohos.base'; 887function onReceiveEvent(data: bluetoothManager.BondStateParam) { // data为回调函数入参,表示配对的状态 888 console.info('pair state = '+ JSON.stringify(data)); 889} 890try { 891 bluetoothManager.on('bondStateChange', onReceiveEvent); 892} catch (err) { 893 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 894} 895``` 896 897 898## bluetoothManager.off('bondStateChange')<sup>(deprecated)</sup> 899 900off(type: "bondStateChange", callback?: Callback<BondStateParam>): void 901 902取消订阅蓝牙配对状态改变事件。 903 904> **说明:**<br/> 905> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.off('bondStateChange')](js-apis-bluetooth-connection.md#connectionoffbondstatechange)替代。 906 907**需要权限**:ohos.permission.USE_BLUETOOTH 908 909**系统能力**:SystemCapability.Communication.Bluetooth.Core。 910 911**参数:** 912 913| 参数名 | 类型 | 必填 | 说明 | 914| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 915| type | string | 是 | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 | 916| callback | Callback<[BondStateParam](#bondstateparamdeprecated)> | 否 | 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 | 917 918**错误码**: 919 920以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 921 922| 错误码ID | 错误信息 | 923| -------- | ---------------------------- | 924|2900099 | Operation failed. | 925 926**示例:** 927 928```js 929import { BusinessError } from '@ohos.base'; 930function onReceiveEvent(data: bluetoothManager.BondStateParam) { 931 console.info('bond state = '+ JSON.stringify(data)); 932} 933try { 934 bluetoothManager.on('bondStateChange', onReceiveEvent); 935 bluetoothManager.off('bondStateChange', onReceiveEvent); 936} catch (err) { 937 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 938} 939``` 940 941 942## bluetoothManager.on('stateChange')<sup>(deprecated)</sup> 943 944on(type: "stateChange", callback: Callback<BluetoothState>): void 945 946订阅蓝牙设备开关状态事件。 947 948> **说明:**<br/> 949> 从API version 9开始支持,从API version 10开始废弃。建议使用[access.on('stateChange')](js-apis-bluetooth-access.md#accessonstatechange)替代。 950 951**需要权限**:ohos.permission.USE_BLUETOOTH 952 953**系统能力**:SystemCapability.Communication.Bluetooth.Core。 954 955**参数:** 956 957| 参数名 | 类型 | 必填 | 说明 | 958| -------- | ---------------------------------------- | ---- | -------------------------------- | 959| type | string | 是 | 填写"stateChange"字符串,表示蓝牙状态改变事件。 | 960| callback | Callback<[BluetoothState](#bluetoothstatedeprecated)> | 是 | 表示回调函数的入参,蓝牙状态。回调函数由用户创建通过该接口注册。 | 961 962**错误码**: 963 964以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 965 966| 错误码ID | 错误信息 | 967| -------- | ---------------------------- | 968|2900099 | Operation failed. | 969 970**示例:** 971 972```js 973import { BusinessError } from '@ohos.base'; 974function onReceiveEvent(data: bluetoothManager.BluetoothState) { 975 console.info('bluetooth state = '+ JSON.stringify(data)); 976} 977try { 978 bluetoothManager.on('stateChange', onReceiveEvent); 979} catch (err) { 980 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 981} 982``` 983 984 985## bluetoothManager.off('stateChange')<sup>(deprecated)</sup> 986 987off(type: "stateChange", callback?: Callback<BluetoothState>): void 988 989取消订阅蓝牙设备开关状态事件。 990 991> **说明:**<br/> 992> 从API version 9开始支持,从API version 10开始废弃。建议使用[access.off('stateChange')](js-apis-bluetooth-access.md#accessoffstatechange)替代。 993 994**需要权限**:ohos.permission.USE_BLUETOOTH 995 996**系统能力**:SystemCapability.Communication.Bluetooth.Core。 997 998**参数:** 999 1000| 参数名 | 类型 | 必填 | 说明 | 1001| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1002| type | string | 是 | 填写"stateChange"字符串,表示蓝牙状态改变事件。 | 1003| callback | Callback<[BluetoothState](#bluetoothstatedeprecated)> | 否 | 表示取消订阅蓝牙状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1004 1005**错误码**: 1006 1007以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1008 1009| 错误码ID | 错误信息 | 1010| -------- | ---------------------------- | 1011|2900099 | Operation failed. | 1012 1013**示例:** 1014 1015```js 1016import { BusinessError } from '@ohos.base'; 1017function onReceiveEvent(data: bluetoothManager.BluetoothState) { 1018 console.info('bluetooth state = '+ JSON.stringify(data)); 1019} 1020try { 1021 bluetoothManager.on('stateChange', onReceiveEvent); 1022 bluetoothManager.off('stateChange', onReceiveEvent); 1023} catch (err) { 1024 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1025} 1026``` 1027 1028 1029## bluetoothManager.sppListen<sup>(deprecated)</sup> 1030 1031sppListen(name: string, option: SppOption, callback: AsyncCallback<number>): void 1032 1033创建一个服务端监听Socket。 1034 1035> **说明:**<br/> 1036> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.sppListen](js-apis-bluetooth-socket.md#socketspplisten)替代。 1037 1038**需要权限**:ohos.permission.USE_BLUETOOTH 1039 1040**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1041 1042**参数:** 1043 1044| 参数名 | 类型 | 必填 | 说明 | 1045| -------- | --------------------------- | ---- | ----------------------- | 1046| name | string | 是 | 服务的名称。 | 1047| option | [SppOption](#sppoptiondeprecated) | 是 | spp监听配置参数。 | 1048| callback | AsyncCallback<number> | 是 | 表示回调函数的入参,服务端Socket的id。 | 1049 1050**错误码**: 1051 1052以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1053 1054| 错误码ID | 错误信息 | 1055| -------- | ---------------------------- | 1056|2900001 | Service stopped. | 1057|2900003 | Bluetooth switch is off. | 1058|2900004 | Profile is not supported. | 1059|2900099 | Operation failed. | 1060 1061**示例:** 1062 1063```js 1064import { BusinessError } from '@ohos.base'; 1065let serverNumber = -1; 1066function serverSocket(code: BusinessError, number: number) { 1067 console.log('bluetooth error code: ' + code.code); 1068 if (code.code == 0) { 1069 console.log('bluetooth serverSocket Number: ' + number); 1070 serverNumber = number; 1071 } 1072} 1073 1074let sppOption: bluetoothManager.SppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0}; 1075try { 1076 bluetoothManager.sppListen('server1', sppOption, serverSocket); 1077} catch (err) { 1078 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1079} 1080``` 1081 1082 1083## bluetoothManager.sppAccept<sup>(deprecated)</sup> 1084 1085sppAccept(serverSocket: number, callback: AsyncCallback<number>): void 1086 1087服务端监听socket等待客户端连接。 1088 1089> **说明:**<br/> 1090> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.sppAccept](js-apis-bluetooth-socket.md#socketsppaccept)替代。 1091 1092**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1093 1094**参数:** 1095 1096| 参数名 | 类型 | 必填 | 说明 | 1097| ------------ | --------------------------- | ---- | ----------------------- | 1098| serverSocket | number | 是 | 服务端socket的id。 | 1099| callback | AsyncCallback<number> | 是 | 表示回调函数的入参,客户端socket的id。 | 1100 1101**错误码**: 1102 1103以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1104 1105| 错误码ID | 错误信息 | 1106| -------- | ---------------------------- | 1107|2900001 | Service stopped. | 1108|2900003 | Bluetooth switch is off. | 1109|2900004 | Profile is not supported. | 1110|2900099 | Operation failed. | 1111 1112**示例:** 1113 1114```js 1115import { BusinessError } from '@ohos.base'; 1116let serverNumber = -1; 1117function serverSocket(code: BusinessError, number: number) { 1118 console.log('bluetooth error code: ' + code.code); 1119 if (code.code == 0) { 1120 console.log('bluetooth serverSocket Number: ' + number); 1121 serverNumber = number; 1122 } 1123} 1124let clientNumber = -1; 1125function acceptClientSocket(code: BusinessError, number: number) { 1126 console.log('bluetooth error code: ' + code.code); 1127 if (code.code == 0) { 1128 console.log('bluetooth clientSocket Number: ' + number); 1129 // 获取的clientNumber用作服务端后续读/写操作socket的id。 1130 clientNumber = number; 1131 } 1132} 1133try { 1134 bluetoothManager.sppAccept(serverNumber, acceptClientSocket); 1135} catch (err) { 1136 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1137} 1138``` 1139 1140 1141## bluetoothManager.sppConnect<sup>(deprecated)</sup> 1142 1143sppConnect(device: string, option: SppOption, callback: AsyncCallback<number>): void 1144 1145客户端向远端设备发起spp连接。 1146 1147> **说明:**<br/> 1148> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.sppConnect](js-apis-bluetooth-socket.md#socketsppconnect)替代。 1149 1150**需要权限**:ohos.permission.USE_BLUETOOTH 1151 1152**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1153 1154**参数:** 1155 1156| 参数名 | 类型 | 必填 | 说明 | 1157| -------- | --------------------------- | ---- | ------------------------------ | 1158| device | string | 是 | 对端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 1159| option | [SppOption](#sppoptiondeprecated) | 是 | spp客户端连接配置参数。 | 1160| callback | AsyncCallback<number> | 是 | 表示回调函数的入参,客户端socket的id。 | 1161 1162**错误码**: 1163 1164以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1165 1166| 错误码ID | 错误信息 | 1167| -------- | ---------------------------- | 1168|2900001 | Service stopped. | 1169|2900003 | Bluetooth switch is off. | 1170|2900004 | Profile is not supported. | 1171|2900099 | Operation failed. | 1172 1173**示例:** 1174 1175```js 1176import { BusinessError } from '@ohos.base'; 1177 1178let clientNumber = -1; 1179function clientSocket(code: BusinessError, number: number) { 1180 if (code.code != 0 || code == null) { 1181 return; 1182 } 1183 console.log('bluetooth serverSocket Number: ' + number); 1184 // 获取的clientNumber用作客户端后续读/写操作socket的id。 1185 clientNumber = number; 1186} 1187let sppOption: bluetoothManager.SppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0}; 1188try { 1189 bluetoothManager.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket); 1190} catch (err) { 1191 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1192} 1193``` 1194 1195 1196## bluetoothManager.sppCloseServerSocket<sup>(deprecated)</sup> 1197 1198sppCloseServerSocket(socket: number): void 1199 1200关闭服务端监听Socket,入参socket由sppListen接口返回。 1201 1202> **说明:**<br/> 1203> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.sppCloseServerSocket](js-apis-bluetooth-socket.md#socketsppcloseserversocket)替代。 1204 1205**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1206 1207**参数:** 1208 1209| 参数名 | 类型 | 必填 | 说明 | 1210| ------ | ------ | ---- | --------------- | 1211| socket | number | 是 | 服务端监听socket的id。 | 1212 1213**错误码**: 1214 1215以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1216 1217| 错误码ID | 错误信息 | 1218| -------- | ---------------------------- | 1219|2900001 | Service stopped. | 1220|2900099 | Operation failed. | 1221 1222**示例:** 1223 1224```js 1225import { BusinessError } from '@ohos.base'; 1226let serverNumber = -1; 1227function serverSocket(code: BusinessError, number: number) { 1228 console.log('bluetooth error code: ' + code.code); 1229 if (code.code == 0) { 1230 console.log('bluetooth serverSocket Number: ' + number); 1231 serverNumber = number; 1232 } 1233} 1234try { 1235 bluetoothManager.sppCloseServerSocket(serverNumber); 1236} catch (err) { 1237 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1238} 1239``` 1240 1241 1242## bluetoothManager.sppCloseClientSocket<sup>(deprecated)</sup> 1243 1244sppCloseClientSocket(socket: number): void 1245 1246关闭客户端socket,入参socket由sppAccept或sppConnect接口获取。 1247 1248> **说明:**<br/> 1249> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.sppCloseClientSocket](js-apis-bluetooth-socket.md#socketsppcloseclientsocket)替代。 1250 1251**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1252 1253**参数:** 1254 1255| 参数名 | 类型 | 必填 | 说明 | 1256| ------ | ------ | ---- | ------------- | 1257| socket | number | 是 | 客户端socket的id。 | 1258 1259**错误码**: 1260 1261以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1262 1263| 错误码ID | 错误信息 | 1264| -------- | ---------------------------- | 1265|2900001 | Service stopped. | 1266|2900099 | Operation failed. | 1267 1268**示例:** 1269 1270```js 1271import { BusinessError } from '@ohos.base'; 1272let clientNumber = -1; 1273function clientSocket(code: BusinessError, number: number) { 1274 if (code.code != 0 || code == null) { 1275 return; 1276 } 1277 console.log('bluetooth serverSocket Number: ' + number); 1278 // 获取的clientNumber用作客户端后续读/写操作socket的id。 1279 clientNumber = number; 1280} 1281try { 1282 bluetoothManager.sppCloseClientSocket(clientNumber); 1283} catch (err) { 1284 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1285} 1286``` 1287 1288 1289## bluetoothManager.sppWrite<sup>(deprecated)</sup> 1290 1291sppWrite(clientSocket: number, data: ArrayBuffer): void 1292 1293通过socket向远端发送数据,入参clientSocket由sppAccept或sppConnect接口获取。 1294 1295> **说明:**<br/> 1296> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.sppWrite](js-apis-bluetooth-socket.md#socketsppwrite)替代。 1297 1298**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1299 1300**参数:** 1301 1302| 参数名 | 类型 | 必填 | 说明 | 1303| ------------ | ----------- | ---- | ------------- | 1304| clientSocket | number | 是 | 客户端socket的id。 | 1305| data | ArrayBuffer | 是 | 写入的数据。 | 1306 1307**错误码**: 1308 1309以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1310 1311| 错误码ID | 错误信息 | 1312| -------- | ---------------------------- | 1313|2901054 | IO error. | 1314|2900099 | Operation failed. | 1315 1316**示例:** 1317 1318```js 1319import { BusinessError } from '@ohos.base'; 1320let clientNumber = -1; 1321function clientSocket(code: BusinessError, number: number) { 1322 if (code.code != 0 || code == null) { 1323 return; 1324 } 1325 console.log('bluetooth serverSocket Number: ' + number); 1326 // 获取的clientNumber用作客户端后续读/写操作socket的id。 1327 clientNumber = number; 1328} 1329let arrayBuffer = new ArrayBuffer(8); 1330let data = new Uint8Array(arrayBuffer); 1331data[0] = 123; 1332try { 1333 bluetoothManager.sppWrite(clientNumber, arrayBuffer); 1334} catch (err) { 1335 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1336} 1337``` 1338 1339 1340## bluetoothManager.on('sppRead')<sup>(deprecated)</sup> 1341 1342on(type: "sppRead", clientSocket: number, callback: Callback<ArrayBuffer>): void 1343 1344订阅spp读请求事件,入参clientSocket由sppAccept或sppConnect接口获取。 1345 1346> **说明:**<br/> 1347> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.on('sppRead')](js-apis-bluetooth-socket.md#socketonsppread)替代。 1348 1349**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1350 1351**参数:** 1352 1353| 参数名 | 类型 | 必填 | 说明 | 1354| ------------ | --------------------------- | ---- | -------------------------- | 1355| type | string | 是 | 填写"sppRead"字符串,表示spp读请求事件。 | 1356| clientSocket | number | 是 | 客户端socket的id。 | 1357| callback | Callback<ArrayBuffer> | 是 | 表示回调函数的入参,读取到的数据。 | 1358 1359**错误码**: 1360 1361以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1362 1363| 错误码ID | 错误信息 | 1364| -------- | ---------------------------- | 1365|2901054 | IO error. | 1366|2900099 | Operation failed. | 1367 1368**示例:** 1369 1370```js 1371import { BusinessError } from '@ohos.base'; 1372let clientNumber = -1; 1373function clientSocket(code: BusinessError, number: number) { 1374 if (code.code != 0 || code == null) { 1375 return; 1376 } 1377 console.log('bluetooth serverSocket Number: ' + number); 1378 // 获取的clientNumber用作客户端后续读/写操作socket的id。 1379 clientNumber = number; 1380} 1381function dataRead(dataBuffer: ArrayBuffer) { 1382 let data = new Uint8Array(dataBuffer); 1383 console.log('bluetooth data is: ' + data[0]); 1384} 1385try { 1386 bluetoothManager.on('sppRead', clientNumber, dataRead); 1387} catch (err) { 1388 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1389} 1390``` 1391 1392 1393## bluetoothManager.off('sppRead')<sup>(deprecated)</sup> 1394 1395off(type: "sppRead", clientSocket: number, callback?: Callback<ArrayBuffer>): void 1396 1397取消订阅spp读请求事件,入参clientSocket由sppAccept或sppConnect接口获取。 1398 1399> **说明:**<br/> 1400> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.off('sppRead')](js-apis-bluetooth-socket.md#socketoffsppread)替代。 1401 1402**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1403 1404**参数:** 1405 1406| 参数名 | 类型 | 必填 | 说明 | 1407| ------------ | --------------------------- | ---- | ---------------------------------------- | 1408| type | string | 是 | 填写"sppRead"字符串,表示spp读请求事件。 | 1409| clientSocket | number | 是 | 客户端Socket的id。 | 1410| callback | Callback<ArrayBuffer> | 否 | 表示取消订阅spp读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1411 1412**示例:** 1413 1414```js 1415import { BusinessError } from '@ohos.base'; 1416let clientNumber = -1; 1417function clientSocket(code: BusinessError, number: number) { 1418 if (code.code != 0 || code == null) { 1419 return; 1420 } 1421 console.log('bluetooth serverSocket Number: ' + number); 1422 // 获取的clientNumber用作客户端后续读/写操作socket的id。 1423 clientNumber = number; 1424} 1425try { 1426 bluetoothManager.off('sppRead', clientNumber); 1427} catch (err) { 1428 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1429} 1430``` 1431 1432## bluetoothManager.getProfileInstance<sup>(deprecated)</sup> 1433 1434getProfileInstance(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile | HidHostProfile | PanProfile 1435 1436通过ProfileId,获取profile的对象实例,API9新增了HidHostProfile,PanProfile。 1437 1438**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1439 1440**参数:** 1441 1442| 参数名 | 类型 | 必填 | 说明 | 1443| --------- | --------- | ---- | ------------------------------------- | 1444| profileId | [ProfileId](#profileiddeprecated) | 是 | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 | 1445 1446**返回值:** 1447 1448| 类型 | 说明 | 1449| ------------------------------------------------------------ | ------------------------------------------------------------ | 1450| [A2dpSourceProfile](#a2dpsourceprofile)或 [HandsFreeAudioGatewayProfile](#handsfreeaudiogatewayprofiledeprecated)或[HidHostProfile](#hidhostprofiledeprecated)或[PanProfile](#panprofile) | 对应的profile的对象实例,当前支持A2dpSourceProfile/HandsFreeAudioGatewayProfile/HidHostProfile/PanProfile。 | 1451 1452**示例:** 1453 1454```js 1455import { BusinessError } from '@ohos.base'; 1456try { 1457 let hidHost: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST); 1458} catch (err) { 1459 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1460} 1461``` 1462 1463 1464## bluetoothManager.BLE 1465 1466BLE模块提供了对蓝牙操作和管理的方法。 1467 1468### createGattServer<sup>(deprecated)</sup> 1469 1470createGattServer(): GattServer 1471 1472创建一个可使用的GattServer实例。 1473 1474> **说明:**<br/> 1475> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.createGattServer](js-apis-bluetooth-ble.md#blecreategattserver)替代。 1476 1477**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1478 1479**返回值:** 1480 1481| 类型 | 说明 | 1482| ------------------------- | ------------------------------------ | 1483| [GattServer](#gattserver) | server端类,使用server端方法之前需要创建该类的实例进行操作。 | 1484 1485**示例:** 1486 1487```js 1488let gattServer: bluetoothManager.GattServer = bluetoothManager.BLE.createGattServer(); 1489``` 1490 1491 1492### createGattClientDevice<sup>(deprecated)</sup> 1493 1494createGattClientDevice(deviceId: string): GattClientDevice 1495 1496创建一个可使用的GattClientDevice实例。 1497 1498> **说明:**<br/> 1499> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.createGattClientDevice](js-apis-bluetooth-ble.md#blecreategattclientdevice)替代。 1500 1501**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1502 1503**参数:** 1504 1505| 参数名 | 类型 | 必填 | 说明 | 1506| -------- | ------ | ---- | ------------------------------------ | 1507| deviceId | string | 是 | 对端设备地址, 例如:"XX:XX:XX:XX:XX:XX"。 | 1508 1509**返回值:** 1510 1511| 类型 | 说明 | 1512| ------------------------------------- | ------------------------------------ | 1513| [GattClientDevice](#gattclientdevice) | client端类,使用client端方法之前需要创建该类的实例进行操作。 | 1514 1515**示例:** 1516 1517```js 1518import { BusinessError } from '@ohos.base'; 1519try { 1520 let device: bluetoothManager.GattClientDevice = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 1521} catch (err) { 1522 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1523} 1524``` 1525 1526 1527### getConnectedBLEDevices<sup>(deprecated)</sup> 1528 1529getConnectedBLEDevices(): Array<string> 1530 1531获取和当前设备连接的BLE设备。 1532 1533> **说明:**<br/> 1534> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.getConnectedBLEDevices](js-apis-bluetooth-ble.md#blegetconnectedbledevices)替代。 1535 1536**需要权限**:ohos.permission.USE_BLUETOOTH 1537 1538**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1539 1540**返回值:** 1541 1542| 类型 | 说明 | 1543| ------------------- | ------------------- | 1544| Array<string> | 返回当前设备作为Server端时连接BLE设备地址集合。 | 1545 1546**错误码**: 1547 1548以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1549 1550| 错误码ID | 错误信息 | 1551| -------- | ---------------------------- | 1552|2900001 | Service stopped. | 1553|2900003 | Bluetooth switch is off. | 1554|2900099 | Operation failed. | 1555 1556**示例:** 1557 1558```js 1559import { BusinessError } from '@ohos.base'; 1560try { 1561 let result: Array<string> = bluetoothManager.BLE.getConnectedBLEDevices(); 1562} catch (err) { 1563 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1564} 1565``` 1566 1567 1568### startBLEScan<sup>(deprecated)</sup> 1569 1570startBLEScan(filters: Array<ScanFilter>, options?: ScanOptions): void 1571 1572发起BLE扫描流程。 1573 1574> **说明:**<br/> 1575> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.startBLEScan](js-apis-bluetooth-ble.md#blestartblescan)替代。 1576 1577**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 和 ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 1578 1579**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1580 1581**参数:** 1582 1583| 参数名 | 类型 | 必填 | 说明 | 1584| ------- | -------------------------------------- | ---- | ----------------------------------- | 1585| filters | Array<[ScanFilter](#scanfilterdeprecated)> | 是 | 表示扫描结果过滤策略集合,如果不使用过滤的方式,该参数设置为null。 | 1586| options | [ScanOptions](#scanoptionsdeprecated) | 否 | 表示扫描的参数配置,可选参数。 | 1587 1588**错误码**: 1589 1590以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1591 1592| 错误码ID | 错误信息 | 1593| -------- | ---------------------------- | 1594|2900001 | Service stopped. | 1595|2900003 | Bluetooth switch is off. | 1596|2900099 | Operation failed. | 1597 1598**示例:** 1599 1600```js 1601import { BusinessError } from '@ohos.base'; 1602function onReceiveEvent(data: Array<bluetoothManager.ScanResult>) { 1603 console.info('BLE scan device find result = '+ JSON.stringify(data)); 1604} 1605try { 1606 bluetoothManager.BLE.on("BLEDeviceFind", onReceiveEvent); 1607 let scanfilter: bluetoothManager.ScanFilter = { 1608 deviceId:"XX:XX:XX:XX:XX:XX", 1609 name:"test", 1610 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb" 1611 }; 1612 let scanoptions: bluetoothManager.ScanOptions = { 1613 interval: 500, 1614 dutyMode: bluetoothManager.ScanDuty.SCAN_MODE_LOW_POWER, 1615 matchMode: bluetoothManager.MatchMode.MATCH_MODE_AGGRESSIVE, 1616 } 1617 bluetoothManager.BLE.startBLEScan([scanfilter], scanoptions); 1618} catch (err) { 1619 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1620} 1621``` 1622 1623 1624### stopBLEScan<sup>(deprecated)</sup> 1625 1626stopBLEScan(): void 1627 1628停止BLE扫描流程。 1629 1630> **说明:**<br/> 1631> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.startBLEScan](js-apis-bluetooth-ble.md#blestopblescan)替代。 1632 1633**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 1634 1635**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1636 1637**错误码**: 1638 1639以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1640 1641| 错误码ID | 错误信息 | 1642| -------- | ---------------------------- | 1643|2900001 | Service stopped. | 1644|2900003 | Bluetooth switch is off. | 1645|2900099 | Operation failed. | 1646 1647**示例:** 1648 1649```js 1650import { BusinessError } from '@ohos.base'; 1651try { 1652 bluetoothManager.BLE.stopBLEScan(); 1653} catch (err) { 1654 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1655} 1656``` 1657 1658 1659### on('BLEDeviceFind')<sup>(deprecated)</sup> 1660 1661on(type: "BLEDeviceFind", callback: Callback<Array<ScanResult>>): void 1662 1663订阅BLE设备发现上报事件。 1664 1665> **说明:**<br/> 1666> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.on('BLEDeviceFind')](js-apis-bluetooth-ble.md#bleonbledevicefind)替代。 1667 1668**需要权限**:ohos.permission.USE_BLUETOOTH 1669 1670**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1671 1672**参数:** 1673 1674| 参数名 | 类型 | 必填 | 说明 | 1675| -------- | ---------------------------------------- | ---- | ----------------------------------- | 1676| type | string | 是 | 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。 | 1677| callback | Callback<Array<[ScanResult](#scanresultdeprecated)>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | 1678 1679**错误码**: 1680 1681以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1682 1683| 错误码ID | 错误信息 | 1684| -------- | ---------------------------- | 1685|2900099 | Operation failed. | 1686 1687**示例:** 1688 1689```js 1690import { BusinessError } from '@ohos.base'; 1691function onReceiveEvent(data: Array<bluetoothManager.ScanResult>) { 1692 console.info('bluetooth device find = '+ JSON.stringify(data)); 1693} 1694try { 1695 bluetoothManager.BLE.on('BLEDeviceFind', onReceiveEvent); 1696} catch (err) { 1697 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1698} 1699``` 1700 1701 1702### off('BLEDeviceFind')<sup>(deprecated)</sup> 1703 1704off(type: "BLEDeviceFind", callback?: Callback<Array<ScanResult>>): void 1705 1706取消订阅BLE设备发现上报事件。 1707 1708> **说明:**<br/> 1709> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.off('BLEDeviceFind')](js-apis-bluetooth-ble.md#bleoffbledevicefind)替代。 1710 1711**需要权限**:ohos.permission.USE_BLUETOOTH 1712 1713**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1714 1715**参数:** 1716 1717| 参数名 | 类型 | 必填 | 说明 | 1718| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1719| type | string | 是 | 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。 | 1720| callback | Callback<Array<[ScanResult](#scanresultdeprecated)>> | 否 | 表示取消订阅BLE设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1721 1722**错误码**: 1723 1724以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1725 1726| 错误码ID | 错误信息 | 1727| -------- | ---------------------------- | 1728|2900099 | Operation failed. | 1729 1730**示例:** 1731 1732```js 1733import { BusinessError } from '@ohos.base'; 1734function onReceiveEvent(data: Array<bluetoothManager.ScanResult>) { 1735 console.info('bluetooth device find = '+ JSON.stringify(data)); 1736} 1737try { 1738 bluetoothManager.BLE.on('BLEDeviceFind', onReceiveEvent); 1739 bluetoothManager.BLE.off('BLEDeviceFind', onReceiveEvent); 1740} catch (err) { 1741 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1742} 1743``` 1744 1745 1746## BaseProfile 1747 1748profile基类。 1749 1750 1751### getConnectionDevices<sup>(deprecated)</sup> 1752 1753getConnectionDevices(): Array<string> 1754 1755获取已连接设备列表。 1756 1757> **说明:**<br/> 1758> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.getConnectedDevices](js-apis-bluetooth-baseProfile.md#baseprofilegetconnecteddevices)替代。 1759 1760**需要权限**:ohos.permission.USE_BLUETOOTH 1761 1762**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1763 1764**返回值:** 1765 1766| 类型 | 说明 | 1767| ------------------- | ------------- | 1768| Array<string> | 返回已连接设备的地址列表。 | 1769 1770**错误码**: 1771 1772以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1773 1774| 错误码ID | 错误信息 | 1775| -------- | ---------------------------- | 1776|2900001 | Service stopped. | 1777|2900003 | Bluetooth switch is off. | 1778|2900004 | Profile is not supported. | 1779|2900099 | Operation failed. | 1780 1781**示例:** 1782 1783```js 1784import { BusinessError } from '@ohos.base'; 1785try { 1786 let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1787 let retArray: Array<string> = a2dpSrc.getConnectionDevices(); 1788} catch (err) { 1789 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1790} 1791``` 1792 1793### getDeviceState<sup>(deprecated)</sup> 1794 1795getDeviceState(device: string): ProfileConnectionState 1796 1797获取设备profile的连接状态。 1798 1799> **说明:**<br/> 1800> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.getConnectionState](js-apis-bluetooth-baseProfile.md#baseprofilegetconnectionstate)替代。 1801 1802**需要权限**:ohos.permission.USE_BLUETOOTH 1803 1804**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1805 1806**参数:** 1807 1808| 参数名 | 类型 | 必填 | 说明 | 1809| ------ | ------ | ---- | ------- | 1810| device | string | 是 | 远端设备地址。 | 1811 1812**返回值:** 1813 1814| 类型 | 说明 | 1815| ------------------------------------------------- | ----------------------- | 1816| [ProfileConnectionState](#profileconnectionstatedeprecated) | 返回profile的连接状态。 | 1817 1818**错误码**: 1819 1820以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1821 1822| 错误码ID | 错误信息 | 1823| -------- | ---------------------------- | 1824|2900001 | Service stopped. | 1825|2900003 | Bluetooth switch is off. | 1826|2900004 | Profile is not supported. | 1827|2900099 | Operation failed. | 1828 1829**示例:** 1830 1831```js 1832import { BusinessError } from '@ohos.base'; 1833try { 1834 let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1835 let ret: bluetoothManager.ProfileConnectionState = a2dpSrc.getDeviceState('XX:XX:XX:XX:XX:XX'); 1836} catch (err) { 1837 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1838} 1839``` 1840 1841 1842## A2dpSourceProfile 1843 1844使用A2dpSourceProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。 1845 1846> **说明:**<br/> 1847> 从API version 9开始支持,从API version 10开始废弃。建议使用[a2dp.A2dpSourceProfile](js-apis-bluetooth-a2dp.md#a2dpsourceprofile)替代。 1848 1849 1850### connect<sup>(deprecated)</sup> 1851 1852connect(device: string): void 1853 1854发起设备的A2dp服务连接请求。 1855 1856> **说明:**<br/> 1857> 从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 1858 1859**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 1860 1861**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1862 1863**参数:** 1864 1865| 参数名 | 类型 | 必填 | 说明 | 1866| ------ | ------ | ---- | ------- | 1867| device | string | 是 | 远端设备地址。 | 1868 1869**错误码**: 1870 1871以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1872 1873| 错误码ID | 错误信息 | 1874| -------- | ---------------------------- | 1875|2900001 | Service stopped. | 1876|2900003 | Bluetooth switch is off. | 1877|2900004 | Profile is not supported. | 1878|2900099 | Operation failed. | 1879 1880**示例:** 1881 1882```js 1883import { BusinessError } from '@ohos.base'; 1884try { 1885 let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1886 a2dpSrc.connect('XX:XX:XX:XX:XX:XX'); 1887} catch (err) { 1888 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1889} 1890``` 1891 1892 1893### disconnect<sup>(deprecated)</sup> 1894 1895disconnect(device: string): void 1896 1897断开设备的a2dp服务连接。 1898 1899> **说明:**<br/> 1900> 从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 1901 1902**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 1903 1904**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1905 1906**参数:** 1907 1908| 参数名 | 类型 | 必填 | 说明 | 1909| ------ | ------ | ---- | ------- | 1910| device | string | 是 | 远端设备地址。 | 1911 1912**错误码**: 1913 1914以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1915 1916| 错误码ID | 错误信息 | 1917| -------- | ---------------------------- | 1918|2900001 | Service stopped. | 1919|2900003 | Bluetooth switch is off. | 1920|2900004 | Profile is not supported. | 1921|2900099 | Operation failed. | 1922 1923**示例:** 1924 1925```js 1926import { BusinessError } from '@ohos.base'; 1927try { 1928 let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1929 a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX'); 1930} catch (err) { 1931 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1932} 1933``` 1934 1935 1936### on('connectionStateChange')<sup>(deprecated)</sup> 1937 1938on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 1939 1940订阅a2dp连接状态变化事件。 1941 1942> **说明:**<br/> 1943> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.on('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileonconnectionstatechange)替代。 1944 1945**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1946 1947**参数:** 1948 1949| 参数名 | 类型 | 必填 | 说明 | 1950| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1951| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 1952| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 是 | 表示回调函数的入参。 | 1953 1954**返回值:** 1955 1956无 1957 1958**示例:** 1959 1960```js 1961import { BusinessError } from '@ohos.base'; 1962function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 1963 console.info('a2dp state = '+ JSON.stringify(data)); 1964} 1965try { 1966let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 1967a2dpSrc.on('connectionStateChange', onReceiveEvent); 1968} catch (err) { 1969 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1970} 1971``` 1972 1973 1974### off('connectionStateChange')<sup>(deprecated)</sup> 1975 1976off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 1977 1978取消订阅a2dp连接状态变化事件。 1979 1980> **说明:**<br/> 1981> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.off('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileoffconnectionstatechange)替代。 1982 1983**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1984 1985**参数:** 1986 1987| 参数名 | 类型 | 必填 | 说明 | 1988| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1989| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 1990| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 否 | 表示回调函数的入参。 | 1991 1992**返回值:** 1993 1994无 1995 1996**示例:** 1997 1998```js 1999import { BusinessError } from '@ohos.base'; 2000function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2001 console.info('a2dp state = '+ JSON.stringify(data)); 2002} 2003try { 2004let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 2005a2dpSrc.on('connectionStateChange', onReceiveEvent); 2006a2dpSrc.off('connectionStateChange', onReceiveEvent); 2007} catch (err) { 2008 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2009} 2010``` 2011 2012 2013### getPlayingState<sup>(deprecated)</sup> 2014 2015getPlayingState(device: string): PlayingState 2016 2017获取设备的播放状态。 2018 2019> **说明:**<br/> 2020> 从API version 9开始支持,从API version 10开始废弃。建议使用[a2dp.A2dpSourceProfile#getPlayingState](js-apis-bluetooth-a2dp.md#getPlayingState)替代。 2021 2022**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2023 2024**参数:** 2025 2026| 参数名 | 类型 | 必填 | 说明 | 2027| ------ | ------ | ---- | ------- | 2028| device | string | 是 | 远端设备地址。 | 2029 2030**返回值:** 2031 2032| 类型 | 说明 | 2033| ----------------------------- | ---------- | 2034| [PlayingState](#playingstatedeprecated) | 远端设备的播放状态。 | 2035 2036**错误码**: 2037 2038以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2039 2040| 错误码ID | 错误信息 | 2041| -------- | ---------------------------- | 2042|2900001 | Service stopped. | 2043|2900003 | Bluetooth switch is off. | 2044|2900004 | Profile is not supported. | 2045|2900099 | Operation failed. | 2046 2047**示例:** 2048 2049```js 2050import { BusinessError } from '@ohos.base'; 2051try { 2052 let a2dpSrc: bluetoothManager.A2dpSourceProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE) as bluetoothManager.A2dpSourceProfile; 2053 let state: bluetoothManager.PlayingState = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX'); 2054} catch (err) { 2055 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2056} 2057``` 2058 2059 2060## HandsFreeAudioGatewayProfile<sup>(deprecated)</sup> 2061 2062使用HandsFreeAudioGatewayProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。 2063 2064> **说明:**<br/> 2065> 从API version 9开始支持,从API version 10开始废弃。建议使用[hfp.HandsFreeAudioGatewayProfile](js-apis-bluetooth-hfp.md#HandsFreeAudioGatewayProfile)替代。 2066 2067 2068### connect 2069 2070connect(device: string): void 2071 2072连接设备的HFP服务。 2073 2074> **说明:**<br/> 2075> 从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 2076 2077**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 2078 2079**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2080 2081**参数:** 2082 2083| 参数名 | 类型 | 必填 | 说明 | 2084| ------ | ------ | ---- | ------- | 2085| device | string | 是 | 远端设备地址。 | 2086 2087**错误码**: 2088 2089以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2090 2091| 错误码ID | 错误信息 | 2092| -------- | ---------------------------- | 2093|2900001 | Service stopped. | 2094|2900003 | Bluetooth switch is off. | 2095|2900004 | Profile is not supported. | 2096|2900099 | Operation failed. | 2097 2098**示例:** 2099 2100```js 2101import { BusinessError } from '@ohos.base'; 2102try { 2103 let hfpAg: bluetoothManager.HandsFreeAudioGatewayProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as bluetoothManager.HandsFreeAudioGatewayProfile; 2104 hfpAg.connect('XX:XX:XX:XX:XX:XX'); 2105} catch (err) { 2106 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2107} 2108``` 2109 2110 2111### disconnect<sup>(deprecated)</sup> 2112 2113disconnect(device: string): void 2114 2115断开连接设备的HFP服务。 2116 2117> **说明:**<br/> 2118> 从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 2119 2120**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 2121 2122**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2123 2124**参数:** 2125 2126| 参数名 | 类型 | 必填 | 说明 | 2127| ------ | ------ | ---- | ------- | 2128| device | string | 是 | 远端设备地址。 | 2129 2130**错误码**: 2131 2132以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2133 2134| 错误码ID | 错误信息 | 2135| -------- | ---------------------------- | 2136|2900001 | Service stopped. | 2137|2900003 | Bluetooth switch is off. | 2138|2900004 | Profile is not supported. | 2139|2900099 | Operation failed. | 2140 2141**示例:** 2142 2143```js 2144import { BusinessError } from '@ohos.base'; 2145try { 2146 let hfpAg: bluetoothManager.HandsFreeAudioGatewayProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as bluetoothManager.HandsFreeAudioGatewayProfile; 2147 hfpAg.disconnect('XX:XX:XX:XX:XX:XX'); 2148} catch (err) { 2149 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2150} 2151``` 2152 2153 2154### on('connectionStateChange')<sup>(deprecated)</sup> 2155 2156on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 2157 2158订阅HFP连接状态变化事件。 2159 2160> **说明:**<br/> 2161> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.on('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileonconnectionstatechange)替代。 2162 2163**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2164 2165**参数:** 2166 2167| 参数名 | 类型 | 必填 | 说明 | 2168| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2169| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 2170| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 是 | 表示回调函数的入参。 | 2171 2172**示例:** 2173 2174```js 2175import { BusinessError } from '@ohos.base'; 2176function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2177 console.info('hfp state = '+ JSON.stringify(data)); 2178} 2179try { 2180let hfpAg: bluetoothManager.HandsFreeAudioGatewayProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as 2181 bluetoothManager.HandsFreeAudioGatewayProfile; 2182hfpAg.on('connectionStateChange', onReceiveEvent); 2183} catch (err) { 2184 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2185} 2186``` 2187 2188 2189### off('connectionStateChange')<sup>(deprecated)</sup> 2190 2191off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 2192 2193取消订阅HFP连接状态变化事件。 2194 2195> **说明:**<br/> 2196> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.off('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileoffconnectionstatechange)替代。 2197 2198**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2199 2200**参数:** 2201 2202| 参数名 | 类型 | 必填 | 说明 | 2203| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2204| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 2205| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 否 | 表示回调函数的入参。 | 2206 2207**示例:** 2208 2209```js 2210import { BusinessError } from '@ohos.base'; 2211function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2212 console.info('hfp state = '+ JSON.stringify(data)); 2213} 2214try { 2215let hfpAg: bluetoothManager.HandsFreeAudioGatewayProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY) as 2216 bluetoothManager.HandsFreeAudioGatewayProfile; 2217hfpAg.on('connectionStateChange', onReceiveEvent); 2218hfpAg.off('connectionStateChange', onReceiveEvent); 2219} catch (err) { 2220 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2221} 2222``` 2223 2224 2225## HidHostProfile<sup>(deprecated)</sup> 2226 2227使用HidHostProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。 2228 2229 2230 2231 2232 2233 2234 2235### on('connectionStateChange')<sup>(deprecated)</sup> 2236 2237on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 2238 2239订阅HidHost连接状态变化事件。 2240 2241> **说明:**<br/> 2242> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.on('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileonconnectionstatechange)替代。 2243 2244**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2245 2246**参数:** 2247 2248| 参数名 | 类型 | 必填 | 说明 | 2249| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2250| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 2251| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 是 | 表示回调函数的入参。 | 2252 2253**示例:** 2254 2255```js 2256import { BusinessError } from '@ohos.base'; 2257function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2258 console.info('hidHost state = '+ JSON.stringify(data)); 2259} 2260try { 2261let hidHost: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 2262hidHost.on('connectionStateChange', onReceiveEvent); 2263} catch (err) { 2264 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2265} 2266``` 2267 2268 2269### off('connectionStateChange')<sup>(deprecated)</sup> 2270 2271off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 2272 2273取消订阅HidHost连接状态变化事件。 2274 2275> **说明:**<br/> 2276> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.off('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileoffconnectionstatechange)替代。 2277 2278**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2279 2280**参数:** 2281 2282| 参数名 | 类型 | 必填 | 说明 | 2283| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- | 2284| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 2285| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 否 | 表示回调函数的入参。 | 2286 2287**示例:** 2288 2289```js 2290import { BusinessError } from '@ohos.base'; 2291function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2292 console.info('hidHost state = '+ JSON.stringify(data)); 2293} 2294try { 2295let hidHost: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 2296hidHost.on('connectionStateChange', onReceiveEvent); 2297hidHost.off('connectionStateChange', onReceiveEvent); 2298} catch (err) { 2299 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2300} 2301``` 2302 2303 2304## PanProfile 2305 2306使用PanProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。 2307 2308> **说明:**<br/> 2309> 从API version 9开始支持,从API version 10开始废弃。建议使用[pan.PanProfile](js-apis-bluetooth-pan.md#panprofile)替代。 2310 2311 2312 2313 2314### on('connectionStateChange')<sup>(deprecated)</sup> 2315 2316on(type: "connectionStateChange", callback: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 2317 2318订阅Pan连接状态变化事件。 2319 2320> **说明:**<br/> 2321> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.on('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileonconnectionstatechange)替代。 2322 2323**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2324 2325**参数:** 2326 2327| 参数名 | 类型 | 必填 | 说明 | 2328| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2329| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 2330| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 是 | 表示回调函数的入参。 | 2331 2332**示例:** 2333 2334```js 2335import { BusinessError } from '@ohos.base'; 2336function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2337 console.info('pan state = '+ JSON.stringify(data)); 2338} 2339try { 2340let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2341panProfile.on('connectionStateChange', onReceiveEvent); 2342} catch (err) { 2343 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2344} 2345``` 2346 2347 2348### off('connectionStateChange')<sup>(deprecated)</sup> 2349 2350off(type: "connectionStateChange", callback?: Callback<[StateChangeParam](#statechangeparamdeprecated)>): void 2351 2352取消订阅Pan连接状态变化事件。 2353 2354> **说明:**<br/> 2355> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.off('connectionStateChange')](js-apis-bluetooth-baseProfile.md#baseprofileoffconnectionstatechange)替代。 2356 2357**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2358 2359**参数:** 2360 2361| 参数名 | 类型 | 必填 | 说明 | 2362| -------- | ----------------------------------------------------- | ---- | --------------------------------------------------------- | 2363| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 2364| callback | Callback<[StateChangeParam](#statechangeparamdeprecated)> | 否 | 表示回调函数的入参。 | 2365 2366**示例:** 2367 2368```js 2369import { BusinessError } from '@ohos.base'; 2370function onReceiveEvent(data: bluetoothManager.StateChangeParam) { 2371 console.info('pan state = '+ JSON.stringify(data)); 2372} 2373try { 2374let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 2375panProfile.on('connectionStateChange', onReceiveEvent); 2376panProfile.off('connectionStateChange', onReceiveEvent); 2377} catch (err) { 2378 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2379} 2380``` 2381 2382 2383## GattServer 2384 2385server端类,使用server端方法之前需要创建该类的实例进行操作,通过createGattServer()方法构造此实例。 2386 2387> **说明:**<br/> 2388> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer](js-apis-bluetooth-ble.md#GattServer)替代。 2389 2390 2391### startAdvertising<sup>(deprecated)</sup> 2392 2393startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void 2394 2395开始发送BLE广播。 2396 2397> **说明:**<br/> 2398> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.startAdvertising](js-apis-bluetooth-ble.md#blestartadvertising)替代。 2399 2400**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 2401 2402**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2403 2404**参数:** 2405 2406| 参数名 | 类型 | 必填 | 说明 | 2407| ----------- | ------------------------------------- | ---- | -------------- | 2408| setting | [AdvertiseSetting](#advertisesettingdeprecated) | 是 | BLE广播的相关参数。 | 2409| advData | [AdvertiseData](#advertisedatadeprecated) | 是 | BLE广播包内容。 | 2410| advResponse | [AdvertiseData](#advertisedatadeprecated) | 否 | BLE回复扫描请求回复响应。 | 2411 2412**错误码**: 2413 2414以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2415 2416| 错误码ID | 错误信息 | 2417| -------- | ---------------------------- | 2418|2900001 | Service stopped. | 2419|2900003 | Bluetooth switch is off. | 2420|2900099 | Operation failed. | 2421 2422**示例:** 2423 2424```js 2425import { BusinessError } from '@ohos.base'; 2426let manufactureValueBuffer = new Uint8Array(4); 2427manufactureValueBuffer[0] = 1; 2428manufactureValueBuffer[1] = 2; 2429manufactureValueBuffer[2] = 3; 2430manufactureValueBuffer[3] = 4; 2431 2432let serviceValueBuffer = new Uint8Array(4); 2433serviceValueBuffer[0] = 4; 2434serviceValueBuffer[1] = 6; 2435serviceValueBuffer[2] = 7; 2436serviceValueBuffer[3] = 8; 2437console.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 2438console.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 2439let gattServer = bluetoothManager.BLE.createGattServer(); 2440try { 2441 let setting: bluetoothManager.AdvertiseSetting = { 2442 interval:150, 2443 txPower:0, 2444 connectable:true, 2445 }; 2446 let manufactureDataUnit: bluetoothManager.ManufactureData = { 2447 manufactureId:4567, 2448 manufactureValue:manufactureValueBuffer.buffer 2449 }; 2450 let serviceDataUnit: bluetoothManager.ServiceData = { 2451 serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 2452 serviceValue:serviceValueBuffer.buffer 2453 }; 2454 let advData: bluetoothManager.AdvertiseData = { 2455 serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 2456 manufactureData:[manufactureDataUnit], 2457 serviceData:[serviceDataUnit], 2458 }; 2459 let advResponse: bluetoothManager.AdvertiseData = { 2460 serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 2461 manufactureData:[manufactureDataUnit], 2462 serviceData:[serviceDataUnit], 2463 }; 2464 gattServer.startAdvertising(setting, advData ,advResponse); 2465} catch (err) { 2466 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2467} 2468``` 2469 2470 2471### stopAdvertising<sup>(deprecated)</sup> 2472 2473stopAdvertising(): void 2474 2475停止发送BLE广播。 2476 2477> **说明:**<br/> 2478> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.stopAdvertising](js-apis-bluetooth-ble.md#blestopadvertising)替代。 2479 2480**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 2481 2482**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2483 2484**错误码**: 2485 2486以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2487 2488| 错误码ID | 错误信息 | 2489| -------- | ---------------------------- | 2490|2900001 | Service stopped. | 2491|2900003 | Bluetooth switch is off. | 2492|2900099 | Operation failed. | 2493 2494**示例:** 2495 2496```js 2497import { BusinessError } from '@ohos.base'; 2498let server = bluetoothManager.BLE.createGattServer(); 2499try { 2500 server.stopAdvertising(); 2501} catch (err) { 2502 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2503} 2504``` 2505 2506 2507### addService<sup>(deprecated)</sup> 2508 2509addService(service: GattService): void 2510 2511server端添加服务。 2512 2513> **说明:**<br/> 2514> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#addService](js-apis-bluetooth-ble.md#addservice)替代。 2515 2516**需要权限**:ohos.permission.USE_BLUETOOTH 2517 2518**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2519 2520**参数:** 2521 2522| 参数名 | 类型 | 必填 | 说明 | 2523| ------- | --------------------------- | ---- | ------------------------ | 2524| service | [GattService](#gattservicedeprecated) | 是 | 服务端的service数据。BLE广播的相关参数 | 2525 2526**错误码**: 2527 2528以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2529 2530| 错误码ID | 错误信息 | 2531| -------- | ---------------------------- | 2532|2900001 | Service stopped. | 2533|2900003 | Bluetooth switch is off. | 2534|2900099 | Operation failed. | 2535 2536**示例:** 2537 2538```js 2539import { BusinessError } from '@ohos.base'; 2540// 创建descriptors 2541let descriptors: Array<bluetoothManager.BLEDescriptor> = []; 2542let arrayBuffer = new ArrayBuffer(8); 2543let descV = new Uint8Array(arrayBuffer); 2544descV[0] = 11; 2545let descriptor: bluetoothManager.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2546 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2547 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 2548descriptors[0] = descriptor; 2549 2550// 创建characteristics 2551let characteristics: Array<bluetoothManager.BLECharacteristic> = []; 2552let arrayBufferC = new ArrayBuffer(8); 2553let cccV = new Uint8Array(arrayBufferC); 2554cccV[0] = 1; 2555let characteristic: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2556 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2557let characteristicN: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2558 characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2559characteristics[0] = characteristic; 2560 2561// 创建gattService 2562let gattService: bluetoothManager.GattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]}; 2563 2564let gattServer = bluetoothManager.BLE.createGattServer(); 2565try { 2566 gattServer.addService(gattService); 2567} catch (err) { 2568 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2569} 2570``` 2571 2572 2573### removeService<sup>(deprecated)</sup> 2574 2575removeService(serviceUuid: string): void 2576 2577删除已添加的服务。 2578 2579> **说明:**<br/> 2580> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#removeService](js-apis-bluetooth-ble.md#removeservice)替代。 2581 2582**需要权限**:ohos.permission.USE_BLUETOOTH 2583 2584**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2585 2586**参数:** 2587 2588| 参数名 | 类型 | 必填 | 说明 | 2589| ----------- | ------ | ---- | ---------------------------------------- | 2590| serviceUuid | string | 是 | service的UUID,例如“00001810-0000-1000-8000-00805F9B34FB”。 | 2591 2592**错误码**: 2593 2594以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2595 2596| 错误码ID | 错误信息 | 2597| -------- | ---------------------------- | 2598|2900001 | Service stopped. | 2599|2900003 | Bluetooth switch is off. | 2600|2900004 | Profile is not supported. | 2601|2900099 | Operation failed. | 2602 2603**示例:** 2604 2605```js 2606import { BusinessError } from '@ohos.base'; 2607let server = bluetoothManager.BLE.createGattServer(); 2608try { 2609 server.removeService('00001810-0000-1000-8000-00805F9B34FB'); 2610} catch (err) { 2611 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2612} 2613``` 2614 2615 2616### close<sup>(deprecated)</sup> 2617 2618close(): void 2619 2620关闭服务端功能,去注册server在协议栈的注册,调用该接口后[GattServer](#gattserver)实例将不能再使用。 2621 2622> **说明:**<br/> 2623> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#close](js-apis-bluetooth-ble.md#close)替代。 2624 2625**需要权限**:ohos.permission.USE_BLUETOOTH 2626 2627**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2628 2629**错误码**: 2630 2631以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2632 2633| 错误码ID | 错误信息 | 2634| -------- | ---------------------------- | 2635|2900001 | Service stopped. | 2636|2900003 | Bluetooth switch is off. | 2637|2900099 | Operation failed. | 2638 2639**示例:** 2640 2641```js 2642import { BusinessError } from '@ohos.base'; 2643let server = bluetoothManager.BLE.createGattServer(); 2644try { 2645 server.close(); 2646} catch (err) { 2647 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2648} 2649``` 2650 2651 2652### notifyCharacteristicChanged<sup>(deprecated)</sup> 2653 2654notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): void 2655 2656server端特征值发生变化时,主动通知已连接的client设备。 2657 2658> **说明:**<br/> 2659> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#notifyCharacteristicChanged](js-apis-bluetooth-ble.md#notifycharacteristicchanged)替代。 2660 2661**需要权限**:ohos.permission.USE_BLUETOOTH 2662 2663**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2664 2665**参数:** 2666 2667| 参数名 | 类型 | 必填 | 说明 | 2668| -------------------- | ---------------------------------------- | ---- | --------------------------------------- | 2669| deviceId | string | 是 | 接收通知的client端设备地址,例如“XX:XX:XX:XX:XX:XX”。 | 2670| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristicdeprecated) | 是 | 通知的特征值数据。 | 2671 2672**错误码**: 2673 2674以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2675 2676| 错误码ID | 错误信息 | 2677| -------- | ---------------------------- | 2678|2900001 | Service stopped. | 2679|2900003 | Bluetooth switch is off. | 2680|2900099 | Operation failed. | 2681 2682**示例:** 2683 2684```js 2685import { BusinessError } from '@ohos.base'; 2686// 创建descriptors 2687let descriptors: Array<bluetoothManager.BLEDescriptor> = []; 2688let arrayBuffer = new ArrayBuffer(8); 2689let descV = new Uint8Array(arrayBuffer); 2690descV[0] = 11; 2691let descriptor: bluetoothManager.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2692 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2693 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 2694descriptors[0] = descriptor; 2695let arrayBufferC = new ArrayBuffer(8); 2696let characteristic: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2697 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 2698let notifyCharacteristic: bluetoothManager.NotifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2699 characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false}; 2700let server = bluetoothManager.BLE.createGattServer(); 2701try { 2702 server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic); 2703} catch (err) { 2704 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2705} 2706``` 2707 2708 2709### sendResponse<sup>(deprecated)</sup> 2710 2711sendResponse(serverResponse: ServerResponse): void 2712 2713server端回复client端的读写请求。 2714 2715> **说明:**<br/> 2716> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#sendResponse](js-apis-bluetooth-ble.md#sendresponse)替代。 2717 2718**需要权限**:ohos.permission.USE_BLUETOOTH 2719 2720**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2721 2722**参数:** 2723 2724| 参数名 | 类型 | 必填 | 说明 | 2725| -------------- | --------------------------------- | ---- | --------------- | 2726| serverResponse | [ServerResponse](#serverresponsedeprecated) | 是 | server端回复的响应数据。 | 2727 2728**错误码**: 2729 2730以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2731 2732| 错误码ID | 错误信息 | 2733| -------- | ---------------------------- | 2734|2900001 | Service stopped. | 2735|2900003 | Bluetooth switch is off. | 2736|2900099 | Operation failed. | 2737 2738**示例:** 2739 2740```js 2741import { BusinessError } from '@ohos.base'; 2742/* send response */ 2743let arrayBufferCCC = new ArrayBuffer(8); 2744let cccValue = new Uint8Array(arrayBufferCCC); 2745cccValue[0] = 1123; 2746let serverResponse: bluetoothManager.ServerResponse = { 2747 deviceId: 'XX:XX:XX:XX:XX:XX', 2748 transId: 0, 2749 status: 0, 2750 offset: 0, 2751 value: arrayBufferCCC, 2752}; 2753 2754let gattServer = bluetoothManager.BLE.createGattServer(); 2755try { 2756 gattServer.sendResponse(serverResponse); 2757} catch (err) { 2758 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2759} 2760``` 2761 2762 2763### on('characteristicRead')<sup>(deprecated)</sup> 2764 2765on(type: "characteristicRead", callback: Callback<CharacteristicReadRequest>): void 2766 2767server端订阅特征值读请求事件。 2768 2769> **说明:**<br/> 2770> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#on('characteristicRead')](js-apis-bluetooth-ble.md#oncharacteristicread)替代。 2771 2772**需要权限**:ohos.permission.USE_BLUETOOTH 2773 2774**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2775 2776**参数:** 2777 2778| 参数名 | 类型 | 必填 | 说明 | 2779| -------- | ---------------------------------------- | ---- | ------------------------------------- | 2780| type | string | 是 | 填写"characteristicRead"字符串,表示特征值读请求事件。 | 2781| callback | Callback<[CharacteristicReadRequest](#characteristicreadrequestdeprecated)> | 是 | 表示回调函数的入参,client端发送的读请求数据。 | 2782 2783**示例:** 2784 2785```js 2786import { BusinessError } from '@ohos.base'; 2787let arrayBufferCCC = new ArrayBuffer(8); 2788let cccValue = new Uint8Array(arrayBufferCCC); 2789cccValue[0] = 1123; 2790function ReadCharacteristicReq(characteristicReadRequest: bluetoothManager.CharacteristicReadRequest) { 2791 let deviceId: string = characteristicReadRequest.deviceId; 2792 let transId: number = characteristicReadRequest.transId; 2793 let offset: number = characteristicReadRequest.offset; 2794 let characteristicUuid: string = characteristicReadRequest.characteristicUuid; 2795 2796 let serverResponse: bluetoothManager.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 2797 2798 try { 2799 gattServer.sendResponse(serverResponse); 2800 } catch (err) { 2801 console.error('errCode: ' + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2802 } 2803} 2804 2805let gattServer = bluetoothManager.BLE.createGattServer(); 2806gattServer.on("characteristicRead", ReadCharacteristicReq); 2807``` 2808 2809 2810### off('characteristicRead')<sup>(deprecated)</sup> 2811 2812off(type: "characteristicRead", callback?: Callback<CharacteristicReadRequest>): void 2813 2814server端取消订阅特征值读请求事件。 2815 2816> **说明:**<br/> 2817> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#off('characteristicRead')](js-apis-bluetooth-ble.md#offcharacteristicread)替代。 2818 2819**需要权限**:ohos.permission.USE_BLUETOOTH 2820 2821**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2822 2823**参数:** 2824 2825| 参数名 | 类型 | 必填 | 说明 | 2826| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2827| type | string | 是 | 填写"characteristicRead"字符串,表示特征值读请求事件。 | 2828| callback | Callback<[CharacteristicReadRequest](#characteristicreadrequestdeprecated)> | 否 | 表示取消订阅特征值读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 2829 2830**示例:** 2831 2832```js 2833import { BusinessError } from '@ohos.base'; 2834try { 2835let gattServer = bluetoothManager.BLE.createGattServer(); 2836gattServer.off("characteristicRead"); 2837} catch (err) { 2838 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2839} 2840``` 2841 2842 2843### on('characteristicWrite')<sup>(deprecated)</sup> 2844 2845on(type: "characteristicWrite", callback: Callback<CharacteristicWriteRequest>): void 2846 2847server端订阅特征值写请求事件。 2848 2849> **说明:**<br/> 2850> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#on('characteristicWrite')](js-apis-bluetooth-ble.md#oncharacteristicwrite)替代。 2851 2852**需要权限**:ohos.permission.USE_BLUETOOTH 2853 2854**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2855 2856**参数:** 2857 2858| 参数名 | 类型 | 必填 | 说明 | 2859| -------- | ---------------------------------------- | ---- | -------------------------------------- | 2860| type | string | 是 | 填写"characteristicWrite"字符串,表示特征值写请求事件。 | 2861| callback | Callback<[CharacteristicWriteRequest](#characteristicwriterequestdeprecated)> | 是 | 表示回调函数的入参,client端发送的写请求数据。 | 2862 2863**示例:** 2864 2865```js 2866import { BusinessError } from '@ohos.base'; 2867let arrayBufferCCC = new ArrayBuffer(8); 2868let cccValue = new Uint8Array(arrayBufferCCC); 2869function WriteCharacteristicReq(characteristicWriteRequest: bluetoothManager.CharacteristicWriteRequest) { 2870 let deviceId: string = characteristicWriteRequest.deviceId; 2871 let transId: number = characteristicWriteRequest.transId; 2872 let offset: number = characteristicWriteRequest.offset; 2873 let isPrep: boolean = characteristicWriteRequest.isPrep; 2874 let needRsp: boolean = characteristicWriteRequest.needRsp; 2875 let value: Uint8Array = new Uint8Array(characteristicWriteRequest.value); 2876 let characteristicUuid: string = characteristicWriteRequest.characteristicUuid; 2877 2878 cccValue[0] = value[0]; 2879 let serverResponse: bluetoothManager.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 2880 2881 try { 2882 gattServer.sendResponse(serverResponse); 2883 } catch (err) { 2884 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2885 } 2886} 2887 2888let gattServer = bluetoothManager.BLE.createGattServer(); 2889gattServer.on("characteristicWrite", WriteCharacteristicReq); 2890``` 2891 2892 2893### off('characteristicWrite')<sup>(deprecated)</sup> 2894 2895off(type: "characteristicWrite", callback?: Callback<CharacteristicWriteRequest>): void 2896 2897server端取消订阅特征值写请求事件。 2898 2899> **说明:**<br/> 2900> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#off('characteristicWrite')](js-apis-bluetooth-ble.md#offcharacteristicwrite)替代。 2901 2902**需要权限**:ohos.permission.USE_BLUETOOTH 2903 2904**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2905 2906**参数:** 2907 2908| 参数名 | 类型 | 必填 | 说明 | 2909| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2910| type | string | 是 | 填写"characteristicWrite"字符串,表示特征值写请求事件。 | 2911| callback | Callback<[CharacteristicWriteRequest](#characteristicwriterequestdeprecated)> | 否 | 表示取消订阅特征值写请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 2912 2913**示例:** 2914 2915```js 2916import { BusinessError } from '@ohos.base'; 2917try { 2918let gattServer = bluetoothManager.BLE.createGattServer(); 2919gattServer.off("characteristicWrite"); 2920} catch (err) { 2921 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2922} 2923``` 2924 2925 2926### on('descriptorRead')<sup>(deprecated)</sup> 2927 2928on(type: "descriptorRead", callback: Callback<DescriptorReadRequest>): void 2929 2930server端订阅描述符读请求事件。 2931 2932> **说明:**<br/> 2933> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#on('descriptorRead')](js-apis-bluetooth-ble.md#ondescriptorread)替代。 2934 2935**需要权限**:ohos.permission.USE_BLUETOOTH 2936 2937**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2938 2939**参数:** 2940 2941| 参数名 | 类型 | 必填 | 说明 | 2942| -------- | ---------------------------------------- | ---- | --------------------------------- | 2943| type | string | 是 | 填写"descriptorRead"字符串,表示描述符读请求事件。 | 2944| callback | Callback<[DescriptorReadRequest](#descriptorreadrequestdeprecated)> | 是 | 表示回调函数的入参,client端发送的读请求数据。 | 2945 2946**示例:** 2947 2948```js 2949import { BusinessError } from '@ohos.base'; 2950let arrayBufferDesc = new ArrayBuffer(8); 2951let descValue = new Uint8Array(arrayBufferDesc); 2952descValue[0] = 1101; 2953function ReadDescriptorReq(descriptorReadRequest: bluetoothManager.DescriptorReadRequest) { 2954 let deviceId: string = descriptorReadRequest.deviceId; 2955 let transId: number = descriptorReadRequest.transId; 2956 let offset: number = descriptorReadRequest.offset; 2957 let descriptorUuid: string = descriptorReadRequest.descriptorUuid; 2958 2959 let serverResponse: bluetoothManager.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 2960 2961 try { 2962 gattServer.sendResponse(serverResponse); 2963 } catch (err) { 2964 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2965 } 2966} 2967 2968let gattServer = bluetoothManager.BLE.createGattServer(); 2969gattServer.on("descriptorRead", ReadDescriptorReq); 2970``` 2971 2972 2973### off('descriptorRead')<sup>(deprecated)</sup> 2974 2975off(type: "descriptorRead", callback?: Callback<DescriptorReadRequest>): void 2976 2977server端取消订阅描述符读请求事件。 2978 2979> **说明:**<br/> 2980> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#off('descriptorRead')](js-apis-bluetooth-ble.md#offdescriptorread)替代。 2981 2982**需要权限**:ohos.permission.USE_BLUETOOTH 2983 2984**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2985 2986**参数:** 2987 2988| 参数名 | 类型 | 必填 | 说明 | 2989| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2990| type | string | 是 | 填写"descriptorRead"字符串,表示描述符读请求事件。 | 2991| callback | Callback<[DescriptorReadRequest](#descriptorreadrequestdeprecated)> | 否 | 表示取消订阅描述符读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 2992 2993**示例:** 2994 2995```js 2996import { BusinessError } from '@ohos.base'; 2997try { 2998let gattServer = bluetoothManager.BLE.createGattServer(); 2999gattServer.off("descriptorRead"); 3000} catch (err) { 3001 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3002} 3003``` 3004 3005 3006### on('descriptorWrite')<sup>(deprecated)</sup> 3007 3008on(type: "descriptorWrite", callback: Callback<DescriptorWriteRequest>): void 3009 3010server端订阅描述符写请求事件。 3011 3012> **说明:**<br/> 3013> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#on('descriptorWrite')](js-apis-bluetooth-ble.md#ondescriptorwrite)替代。 3014 3015**需要权限**:ohos.permission.USE_BLUETOOTH 3016 3017**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3018 3019**参数:** 3020 3021| 参数名 | 类型 | 必填 | 说明 | 3022| -------- | ---------------------------------------- | ---- | ---------------------------------- | 3023| type | string | 是 | 填写"descriptorWrite"字符串,表示描述符写请求事件。 | 3024| callback | Callback<[DescriptorWriteRequest](#descriptorwriterequestdeprecated)> | 是 | 表示回调函数的入参,client端发送的写请求数据。 | 3025 3026**示例:** 3027 3028```js 3029import { BusinessError } from '@ohos.base'; 3030let arrayBufferDesc = new ArrayBuffer(8); 3031let descValue = new Uint8Array(arrayBufferDesc); 3032function WriteDescriptorReq(descriptorWriteRequest: bluetoothManager.DescriptorWriteRequest) { 3033 let deviceId: string = descriptorWriteRequest.deviceId; 3034 let transId: number = descriptorWriteRequest.transId; 3035 let offset: number = descriptorWriteRequest.offset; 3036 let isPrep: boolean = descriptorWriteRequest.isPrep; 3037 let needRsp: boolean = descriptorWriteRequest.needRsp; 3038 let value: Uint8Array = new Uint8Array(descriptorWriteRequest.value); 3039 let descriptorUuid: string = descriptorWriteRequest.descriptorUuid; 3040 3041 descValue[0] = value[0]; 3042 let serverResponse: bluetoothManager.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 3043 3044 try { 3045 gattServer.sendResponse(serverResponse); 3046 } catch (err) { 3047 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3048 } 3049} 3050 3051let gattServer = bluetoothManager.BLE.createGattServer(); 3052gattServer.on("descriptorWrite", WriteDescriptorReq); 3053``` 3054 3055 3056### off('descriptorWrite')<sup>(deprecated)</sup> 3057 3058off(type: "descriptorWrite", callback?: Callback<DescriptorWriteRequest>): void 3059 3060server端取消订阅描述符写请求事件。 3061 3062> **说明:**<br/> 3063> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#off('descriptorWrite')](js-apis-bluetooth-ble.md#offdescriptorwrite)替代。 3064 3065**需要权限**:ohos.permission.USE_BLUETOOTH 3066 3067**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3068 3069**参数:** 3070 3071| 参数名 | 类型 | 必填 | 说明 | 3072| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3073| type | string | 是 | 填写"descriptorWrite"字符串,表示描述符写请求事件。 | 3074| callback | Callback<[DescriptorWriteRequest](#descriptorwriterequestdeprecated)> | 否 | 表示取消订阅描述符写请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 3075 3076**示例:** 3077 3078```js 3079import { BusinessError } from '@ohos.base'; 3080try { 3081let gattServer = bluetoothManager.BLE.createGattServer(); 3082gattServer.off("descriptorWrite"); 3083} catch (err) { 3084 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3085} 3086``` 3087 3088 3089### on('connectStateChange')<sup>(deprecated)</sup> 3090 3091on(type: "connectStateChange", callback: Callback<BLEConnectChangedState>): void 3092 3093server端订阅BLE连接状态变化事件。 3094 3095> **说明:**<br/> 3096> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#on('connectionStateChange')](js-apis-bluetooth-ble.md#onconnectionstatechange)替代。 3097 3098**需要权限**:ohos.permission.USE_BLUETOOTH 3099 3100**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3101 3102**参数:** 3103 3104| 参数名 | 类型 | 必填 | 说明 | 3105| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3106| type | string | 是 | 填写"connectStateChange"字符串,表示BLE连接状态变化事件。 | 3107| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstatedeprecated)> | 是 | 表示回调函数的入参,连接状态。 | 3108 3109**示例:** 3110 3111```js 3112import { BusinessError } from '@ohos.base'; 3113function Connected(BLEConnectChangedState: bluetoothManager.BLEConnectChangedState) { 3114 let deviceId: string = BLEConnectChangedState.deviceId; 3115 let status: bluetoothManager.ProfileConnectionState = BLEConnectChangedState.state; 3116} 3117try { 3118let gattServer = bluetoothManager.BLE.createGattServer(); 3119gattServer.on("connectStateChange", Connected); 3120} catch (err) { 3121 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3122} 3123``` 3124 3125 3126### off('connectStateChange')<sup>(deprecated)</sup> 3127 3128off(type: "connectStateChange", callback?: Callback<BLEConnectChangedState>): void 3129 3130server端取消订阅BLE连接状态变化事件。 3131 3132> **说明:**<br/> 3133> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattServer#off('connectionStateChange')](js-apis-bluetooth-ble.md#offconnectionstatechange)替代。 3134 3135**需要权限**:ohos.permission.USE_BLUETOOTH 3136 3137**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3138 3139**参数:** 3140 3141| 参数名 | 类型 | 必填 | 说明 | 3142| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3143| type | string | 是 | 填写"connectStateChange"字符串,表示BLE连接状态变化事件。 | 3144| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstatedeprecated)> | 否 | 表示取消订阅BLE连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。 | 3145 3146**示例:** 3147 3148```js 3149import { BusinessError } from '@ohos.base'; 3150try { 3151let gattServer = bluetoothManager.BLE.createGattServer(); 3152gattServer.off("connectStateChange"); 3153} catch (err) { 3154 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3155} 3156``` 3157 3158 3159## GattClientDevice 3160 3161client端类,使用client端方法之前需要创建该类的实例进行操作,通过createGattClientDevice(deviceId: string)方法构造此实例。 3162 3163> **说明:**<br/> 3164> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice](js-apis-bluetooth-ble.md#gattclientdevice)替代。 3165 3166 3167### connect<sup>(deprecated)</sup> 3168 3169connect(): void 3170 3171client端发起连接远端蓝牙低功耗设备。 3172 3173> **说明:**<br/> 3174> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#connect](js-apis-bluetooth-ble.md#connect)替代。 3175 3176**需要权限**:ohos.permission.USE_BLUETOOTH 3177 3178**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3179 3180**错误码**: 3181 3182以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3183 3184| 错误码ID | 错误信息 | 3185| -------- | ---------------------------- | 3186|2900001 | Service stopped. | 3187|2900003 | Bluetooth switch is off. | 3188|2900099 | Operation failed. | 3189 3190**示例:** 3191 3192```js 3193import { BusinessError } from '@ohos.base'; 3194try { 3195 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3196 device.connect(); 3197} catch (err) { 3198 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3199} 3200``` 3201 3202 3203### disconnect<sup>(deprecated)</sup> 3204 3205disconnect(): void 3206 3207client端断开与远端蓝牙低功耗设备的连接。 3208 3209> **说明:**<br/> 3210> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#disconnect](js-apis-bluetooth-ble.md#disconnect)替代。 3211 3212**需要权限**:ohos.permission.USE_BLUETOOTH 3213 3214**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3215 3216**错误码**: 3217 3218以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3219 3220| 错误码ID | 错误信息 | 3221| -------- | ---------------------------- | 3222|2900001 | Service stopped. | 3223|2900003 | Bluetooth switch is off. | 3224|2900099 | Operation failed. | 3225 3226**示例:** 3227 3228```js 3229import { BusinessError } from '@ohos.base'; 3230try { 3231 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3232 device.disconnect(); 3233} catch (err) { 3234 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3235} 3236``` 3237 3238 3239### close<sup>(deprecated)</sup> 3240 3241close(): void 3242 3243关闭客户端功能,注销client在协议栈的注册,调用该接口后[GattClientDevice](#gattclientdevice)实例将不能再使用。 3244 3245> **说明:**<br/> 3246> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#close](js-apis-bluetooth-ble.md#close)替代。 3247 3248**需要权限**:ohos.permission.USE_BLUETOOTH 3249 3250**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3251 3252**错误码**: 3253 3254以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3255 3256| 错误码ID | 错误信息 | 3257| -------- | ---------------------------- | 3258|2900001 | Service stopped. | 3259|2900003 | Bluetooth switch is off. | 3260|2900099 | Operation failed. | 3261 3262**示例:** 3263 3264```js 3265import { BusinessError } from '@ohos.base'; 3266try { 3267 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3268 device.close(); 3269} catch (err) { 3270 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3271} 3272``` 3273 3274 3275 3276 3277### getServices<sup>(deprecated)</sup> 3278 3279getServices(callback: AsyncCallback<Array<GattService>>): void 3280 3281client端获取蓝牙低功耗设备的所有服务,即服务发现。 3282 3283> **说明:**<br/> 3284> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#getServices](js-apis-bluetooth-ble.md#getservices)替代。 3285 3286**需要权限**:ohos.permission.USE_BLUETOOTH 3287 3288**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3289 3290**参数:** 3291 3292| 参数名 | 类型 | 必填 | 说明 | 3293| -------- | ---------------------------------------- | ---- | ------------------------ | 3294| callback | AsyncCallback<Array<[GattService](#gattservicedeprecated)>> | 是 | client进行服务发现,通过注册回调函数获取。 | 3295 3296**错误码**: 3297 3298以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3299 3300| 错误码ID | 错误信息 | 3301| -------- | ---------------------------- | 3302|2900001 | Service stopped. | 3303|2900099 | Operation failed. | 3304 3305**示例:** 3306 3307```js 3308import { BusinessError } from '@ohos.base'; 3309// callback 模式 3310function getServices(code: BusinessError, gattServices: Array<bluetoothManager.GattService>) { 3311 if (code.code == 0) { 3312 let services: Array<bluetoothManager.GattService> = gattServices; 3313 console.log('bluetooth code is ' + code.code); 3314 console.log("bluetooth services size is ", services.length); 3315 3316 for (let i = 0; i < services.length; i++) { 3317 console.log('bluetooth serviceUuid is ' + services[i].serviceUuid); 3318 } 3319 } 3320} 3321 3322try { 3323 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3324 device.connect(); 3325 device.getServices(getServices); 3326} catch (err) { 3327 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3328} 3329``` 3330 3331 3332### getServices<sup>(deprecated)</sup> 3333 3334getServices(): Promise<Array<GattService>> 3335 3336client端获取蓝牙低功耗设备的所有服务,即服务发现。 3337 3338> **说明:**<br/> 3339> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#getServices](js-apis-bluetooth-ble.md#getservices-1)替代。 3340 3341**需要权限**:ohos.permission.USE_BLUETOOTH 3342 3343**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3344 3345**返回值:** 3346 3347| 类型 | 说明 | 3348| ---------------------------------------- | --------------------------- | 3349| Promise<Array<[GattService](#gattservicedeprecated)>> | client进行服务发现,通过promise形式获取。 | 3350 3351**错误码**: 3352 3353以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3354 3355| 错误码ID | 错误信息 | 3356| -------- | ---------------------------- | 3357|2900001 | Service stopped. | 3358|2900099 | Operation failed. | 3359 3360**示例:** 3361 3362```js 3363import { BusinessError } from '@ohos.base'; 3364// Promise 模式 3365try { 3366 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3367 device.connect(); 3368 device.getServices().then(result => { 3369 console.info("getServices successfully:" + JSON.stringify(result)); 3370 }); 3371} catch (err) { 3372 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3373} 3374``` 3375 3376 3377### readCharacteristicValue<sup>(deprecated)</sup> 3378 3379readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback<BLECharacteristic>): void 3380 3381client端读取蓝牙低功耗设备特定服务的特征值。 3382 3383> **说明:**<br/> 3384> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#readCharacteristicValue](js-apis-bluetooth-ble.md#readcharacteristicvalue)替代。 3385 3386**需要权限**:ohos.permission.USE_BLUETOOTH 3387 3388**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3389 3390**参数:** 3391 3392| 参数名 | 类型 | 必填 | 说明 | 3393| -------------- | ---------------------------------------- | ---- | ----------------------- | 3394| characteristic | [BLECharacteristic](#blecharacteristicdeprecated) | 是 | 待读取的特征值。 | 3395| callback | AsyncCallback<[BLECharacteristic](#blecharacteristicdeprecated)> | 是 | client读取特征值,通过注册回调函数获取。 | 3396 3397**错误码**: 3398 3399以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3400 3401| 错误码ID | 错误信息 | 3402| -------- | ---------------------------- | 3403|2900001 | Service stopped. | 3404|2901000 | Read forbidden. | 3405|2900099 | Operation failed. | 3406 3407**示例:** 3408 3409```js 3410import { BusinessError } from '@ohos.base'; 3411function readCcc(code: BusinessError, BLECharacteristic: bluetoothManager.BLECharacteristic) { 3412 if (code.code != 0) { 3413 return; 3414 } 3415 console.log('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid); 3416 let value = new Uint8Array(BLECharacteristic.characteristicValue); 3417 console.log('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 3418} 3419 3420let descriptors: Array<bluetoothManager.BLEDescriptor> = []; 3421let bufferDesc = new ArrayBuffer(8); 3422let descV = new Uint8Array(bufferDesc); 3423descV[0] = 11; 3424let descriptor: bluetoothManager.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3425 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3426 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3427descriptors[0] = descriptor; 3428 3429let bufferCCC = new ArrayBuffer(8); 3430let cccV = new Uint8Array(bufferCCC); 3431cccV[0] = 1; 3432let characteristic: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3433 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3434 characteristicValue: bufferCCC, descriptors:descriptors}; 3435 3436try { 3437 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3438 device.readCharacteristicValue(characteristic, readCcc); 3439} catch (err) { 3440 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3441} 3442``` 3443 3444 3445### readCharacteristicValue<sup>(deprecated)</sup> 3446 3447readCharacteristicValue(characteristic: BLECharacteristic): Promise<BLECharacteristic> 3448 3449client端读取蓝牙低功耗设备特定服务的特征值。 3450 3451> **说明:**<br/> 3452> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#readCharacteristicValue](js-apis-bluetooth-ble.md#readcharacteristicvalue-1)替代。 3453 3454**需要权限**:ohos.permission.USE_BLUETOOTH 3455 3456**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3457 3458**参数:** 3459 3460| 参数名 | 类型 | 必填 | 说明 | 3461| -------------- | --------------------------------------- | ---- | -------- | 3462| characteristic | [BLECharacteristic](#blecharacteristicdeprecated) | 是 | 待读取的特征值。 | 3463 3464**返回值:** 3465 3466| 类型 | 说明 | 3467| ---------------------------------------- | -------------------------- | 3468| Promise<[BLECharacteristic](#blecharacteristicdeprecated)> | client读取特征值,通过promise形式获取。 | 3469 3470**错误码**: 3471 3472以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3473 3474| 错误码ID | 错误信息 | 3475| -------- | ---------------------------- | 3476|2900001 | Service stopped. | 3477|2901000 | Read forbidden. | 3478|2900099 | Operation failed. | 3479 3480**示例:** 3481 3482```js 3483import { BusinessError } from '@ohos.base'; 3484let descriptors: Array<bluetoothManager.BLEDescriptor> = []; 3485let bufferDesc = new ArrayBuffer(8); 3486let descV = new Uint8Array(bufferDesc); 3487descV[0] = 11; 3488let descriptor: bluetoothManager.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3489 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3490 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3491descriptors[0] = descriptor; 3492 3493let bufferCCC = new ArrayBuffer(8); 3494let cccV = new Uint8Array(bufferCCC); 3495cccV[0] = 1; 3496let characteristic: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3497 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3498 characteristicValue: bufferCCC, descriptors:descriptors}; 3499 3500try { 3501 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3502 device.readCharacteristicValue(characteristic); 3503} catch (err) { 3504 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3505} 3506``` 3507 3508 3509### readDescriptorValue<sup>(deprecated)</sup> 3510 3511readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<BLEDescriptor>): void 3512 3513client端读取蓝牙低功耗设备特定的特征包含的描述符。 3514 3515> **说明:**<br/> 3516> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#readDescriptorValue](js-apis-bluetooth-ble.md#readdescriptorvalue)替代。 3517 3518**需要权限**:ohos.permission.USE_BLUETOOTH 3519 3520**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3521 3522**参数:** 3523 3524| 参数名 | 类型 | 必填 | 说明 | 3525| ---------- | ---------------------------------------- | ---- | ----------------------- | 3526| descriptor | [BLEDescriptor](#bledescriptordeprecated) | 是 | 待读取的描述符。 | 3527| callback | AsyncCallback<[BLEDescriptor](#bledescriptordeprecated)> | 是 | client读取描述符,通过注册回调函数获取。 | 3528 3529**错误码**: 3530 3531以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3532 3533| 错误码ID | 错误信息 | 3534| -------- | ---------------------------- | 3535|2900001 | Service stopped. | 3536|2901000 | Read forbidden. | 3537|2900099 | Operation failed. | 3538 3539**示例:** 3540 3541```js 3542import { BusinessError } from '@ohos.base'; 3543function readDesc(code: BusinessError, BLEDescriptor: bluetoothManager.BLEDescriptor) { 3544 if (code.code != 0) { 3545 return; 3546 } 3547 console.log('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid); 3548 let value = new Uint8Array(BLEDescriptor.descriptorValue); 3549 console.log('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 3550} 3551 3552let bufferDesc = new ArrayBuffer(8); 3553let descV = new Uint8Array(bufferDesc); 3554descV[0] = 11; 3555let descriptor: bluetoothManager.BLEDescriptor = { 3556 serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3557 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3558 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3559 descriptorValue: bufferDesc 3560}; 3561try { 3562 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3563 device.readDescriptorValue(descriptor, readDesc); 3564} catch (err) { 3565 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3566} 3567``` 3568 3569 3570### readDescriptorValue<sup>(deprecated)</sup> 3571 3572readDescriptorValue(descriptor: BLEDescriptor): Promise<BLEDescriptor> 3573 3574client端读取蓝牙低功耗设备特定的特征包含的描述符。 3575 3576> **说明:**<br/> 3577> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#readDescriptorValue](js-apis-bluetooth-ble.md#readdescriptorvalue-1)替代。 3578 3579**需要权限**:ohos.permission.USE_BLUETOOTH 3580 3581**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3582 3583**参数:** 3584 3585| 参数名 | 类型 | 必填 | 说明 | 3586| ---------- | ------------------------------- | ---- | -------- | 3587| descriptor | [BLEDescriptor](#bledescriptordeprecated) | 是 | 待读取的描述符。 | 3588 3589**返回值:** 3590 3591| 类型 | 说明 | 3592| ---------------------------------------- | -------------------------- | 3593| Promise<[BLEDescriptor](#bledescriptordeprecated)> | client读取描述符,通过promise形式获取。 | 3594 3595**错误码**: 3596 3597以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3598 3599| 错误码ID | 错误信息 | 3600| -------- | ---------------------------- | 3601|2900001 | Service stopped. | 3602|2901000 | Read forbidden. | 3603|2900099 | Operation failed. | 3604 3605**示例:** 3606 3607```js 3608import { BusinessError } from '@ohos.base'; 3609let bufferDesc = new ArrayBuffer(8); 3610let descV = new Uint8Array(bufferDesc); 3611descV[0] = 11; 3612let descriptor: bluetoothManager.BLEDescriptor = { 3613 serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3614 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3615 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3616 descriptorValue: bufferDesc 3617}; 3618try { 3619 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3620 device.readDescriptorValue(descriptor); 3621} catch (err) { 3622 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3623} 3624``` 3625 3626 3627### writeCharacteristicValue<sup>(deprecated)</sup> 3628 3629writeCharacteristicValue(characteristic: BLECharacteristic): void 3630 3631client端向低功耗蓝牙设备写入特定的特征值。 3632 3633> **说明:**<br/> 3634> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#writeCharacteristicValue](js-apis-bluetooth-ble.md#writecharacteristicvalue)替代。 3635 3636**需要权限**:ohos.permission.USE_BLUETOOTH 3637 3638**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3639 3640**参数:** 3641 3642| 参数名 | 类型 | 必填 | 说明 | 3643| -------------- | --------------------------------------- | ---- | ------------------- | 3644| characteristic | [BLECharacteristic](#blecharacteristicdeprecated) | 是 | 蓝牙设备特征对应的二进制值及其它参数。 | 3645 3646**错误码**: 3647 3648以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3649 3650| 错误码ID | 错误信息 | 3651| -------- | ---------------------------- | 3652|2900001 | Service stopped. | 3653|2901001 | Write forbidden. | 3654|2900099 | Operation failed. | 3655 3656**示例:** 3657 3658```js 3659import { BusinessError } from '@ohos.base'; 3660let descriptors: Array<bluetoothManager.BLEDescriptor> = []; 3661let bufferDesc = new ArrayBuffer(8); 3662let descV = new Uint8Array(bufferDesc); 3663descV[0] = 11; 3664let descriptor: bluetoothManager.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3665 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3666 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3667descriptors[0] = descriptor; 3668 3669let bufferCCC = new ArrayBuffer(8); 3670let cccV = new Uint8Array(bufferCCC); 3671cccV[0] = 1; 3672let characteristic: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3673 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3674 characteristicValue: bufferCCC, descriptors:descriptors}; 3675try { 3676 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3677 device.writeCharacteristicValue(characteristic); 3678} catch (err) { 3679 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3680} 3681``` 3682 3683 3684### writeDescriptorValue<sup>(deprecated)</sup> 3685 3686writeDescriptorValue(descriptor: BLEDescriptor): void 3687 3688client端向低功耗蓝牙设备特定的描述符写入二进制数据。 3689 3690> **说明:**<br/> 3691> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#writeCharacteristicValue](js-apis-bluetooth-ble.md#writecharacteristicvalue-1)替代。 3692 3693**需要权限**:ohos.permission.USE_BLUETOOTH 3694 3695**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3696 3697**参数:** 3698 3699| 参数名 | 类型 | 必填 | 说明 | 3700| ---------- | ------------------------------- | ---- | ------------------ | 3701| descriptor | [BLEDescriptor](#bledescriptordeprecated) | 是 | 蓝牙设备描述符的二进制值及其它参数。 | 3702 3703**错误码**: 3704 3705以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3706 3707| 错误码ID | 错误信息 | 3708| -------- | ---------------------------- | 3709|2900001 | Service stopped. | 3710|2901001 | Write forbidden. | 3711|2900099 | Operation failed. | 3712 3713**示例:** 3714 3715```js 3716import { BusinessError } from '@ohos.base'; 3717let bufferDesc = new ArrayBuffer(8); 3718let descV = new Uint8Array(bufferDesc); 3719descV[0] = 22; 3720let descriptor: bluetoothManager.BLEDescriptor = { 3721 serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3722 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3723 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3724 descriptorValue: bufferDesc 3725}; 3726try { 3727 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3728 device.writeDescriptorValue(descriptor); 3729} catch (err) { 3730 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3731} 3732``` 3733 3734 3735### setBLEMtuSize<sup>(deprecated)</sup> 3736 3737setBLEMtuSize(mtu: number): void 3738 3739client协商远端蓝牙低功耗设备的最大传输单元(Maximum Transmission Unit, MTU),调用[connect](#connect)接口连接成功后才能使用。 3740 3741> **说明:**<br/> 3742> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#setBLEMtuSize](js-apis-bluetooth-ble.md#setBLEMtuSize)替代。 3743 3744**需要权限**:ohos.permission.USE_BLUETOOTH 3745 3746**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3747 3748**参数:** 3749 3750| 参数名 | 类型 | 必填 | 说明 | 3751| ---- | ------ | ---- | -------------- | 3752| mtu | number | 是 | 设置范围为22~512字节。 | 3753 3754**错误码**: 3755 3756以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3757 3758| 错误码ID | 错误信息 | 3759| -------- | ---------------------------- | 3760|2900001 | Service stopped. | 3761|2900099 | Operation failed. | 3762 3763**示例:** 3764 3765```js 3766import { BusinessError } from '@ohos.base'; 3767try { 3768 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3769 device.setBLEMtuSize(128); 3770} catch (err) { 3771 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3772} 3773``` 3774 3775 3776### setNotifyCharacteristicChanged<sup>(deprecated)</sup> 3777 3778setNotifyCharacteristicChanged(characteristic: BLECharacteristic, enable: boolean): void 3779 3780向服务端发送设置通知此特征值请求。 3781 3782> **说明:**<br/> 3783> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#setCharacteristicChangeNotification](js-apis-bluetooth-ble.md#setcharacteristicchangenotification)替代。 3784 3785**需要权限**:ohos.permission.USE_BLUETOOTH 3786 3787**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3788 3789**参数:** 3790 3791| 参数名 | 类型 | 必填 | 说明 | 3792| -------------- | --------------------------------------- | ---- | ----------------------------- | 3793| characteristic | [BLECharacteristic](#blecharacteristicdeprecated) | 是 | 蓝牙低功耗特征。 | 3794| enable | boolean | 是 | 启用接收notify设置为true,否则设置为false。 | 3795 3796**错误码**: 3797 3798以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3799 3800| 错误码ID | 错误信息 | 3801| -------- | ---------------------------- | 3802|2900001 | Service stopped. | 3803|2900099 | Operation failed. | 3804 3805**示例:** 3806 3807```js 3808import { BusinessError } from '@ohos.base'; 3809// 创建descriptors 3810let descriptors: Array<bluetoothManager.BLEDescriptor> = []; 3811let bufferDesc = new ArrayBuffer(8); 3812let descV = new Uint8Array(bufferDesc); 3813descV[0] = 11; 3814let descriptor: bluetoothManager.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3815 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3816 descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 3817descriptors[0] = descriptor; 3818 3819let bufferCCC = new ArrayBuffer(8); 3820let cccV = new Uint8Array(bufferCCC); 3821cccV[0] = 1; 3822let characteristic: bluetoothManager.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3823 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3824 characteristicValue: bufferCCC, descriptors:descriptors}; 3825try { 3826 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3827 device.setNotifyCharacteristicChanged(characteristic, false); 3828} catch (err) { 3829 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3830} 3831 3832``` 3833 3834 3835### on('BLECharacteristicChange')<sup>(deprecated)</sup> 3836 3837on(type: "BLECharacteristicChange", callback: Callback<BLECharacteristic>): void 3838 3839订阅蓝牙低功耗设备的特征值变化事件。需要先调用setNotifyCharacteristicChanged接口才能接收server端的通知。 3840 3841> **说明:**<br/> 3842> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#on('BLECharacteristicChange')](js-apis-bluetooth-ble.md#onblecharacteristicchange)替代。 3843 3844**需要权限**:ohos.permission.USE_BLUETOOTH 3845 3846**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3847 3848**参数:** 3849 3850| 参数名 | 类型 | 必填 | 说明 | 3851| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3852| type | string | 是 | 填写"BLECharacteristicChange"字符串,表示特征值变化事件。 | 3853| callback | Callback<[BLECharacteristic](#blecharacteristicdeprecated)> | 是 | 表示蓝牙低功耗设备的特征值变化事件的回调函数。 | 3854 3855**示例:** 3856 3857```js 3858import { BusinessError } from '@ohos.base'; 3859function CharacteristicChange(characteristicChangeReq: ble.BLECharacteristic) { 3860 let serviceUuid: string = characteristicChangeReq.serviceUuid; 3861 let characteristicUuid: string = characteristicChangeReq.characteristicUuid; 3862 let value: Uint8Array = new Uint8Array(characteristicChangeReq.characteristicValue); 3863} 3864try { 3865 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3866 device.on('BLECharacteristicChange', CharacteristicChange); 3867} catch (err) { 3868 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3869} 3870``` 3871 3872 3873### off('BLECharacteristicChange')<sup>(deprecated)</sup> 3874 3875off(type: "BLECharacteristicChange", callback?: Callback<BLECharacteristic>): void 3876 3877取消订阅蓝牙低功耗设备的特征值变化事件。 3878 3879> **说明:**<br/> 3880> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#off('BLECharacteristicChange')](js-apis-bluetooth-ble.md#offblecharacteristicchange)替代。 3881 3882**需要权限**:ohos.permission.USE_BLUETOOTH 3883 3884**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3885 3886**参数:** 3887 3888| 参数名 | 类型 | 必填 | 说明 | 3889| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3890| type | string | 是 | 填写"BLECharacteristicChange"字符串,表示特征值变化事件。 | 3891| callback | Callback<[BLECharacteristic](#blecharacteristicdeprecated)> | 否 | 表示取消订阅蓝牙低功耗设备的特征值变化事件。不填该参数则取消订阅该type对应的所有回调。 | 3892 3893**示例:** 3894 3895```js 3896import { BusinessError } from '@ohos.base'; 3897try { 3898 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3899 device.off('BLECharacteristicChange'); 3900} catch (err) { 3901 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3902} 3903``` 3904 3905 3906### on('BLEConnectionStateChange')<sup>(deprecated)</sup> 3907 3908on(type: "BLEConnectionStateChange", callback: Callback<BLEConnectChangedState>): void 3909 3910client端订阅蓝牙低功耗设备的连接状态变化事件。 3911 3912> **说明:**<br/> 3913> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#on('BLEConnectionStateChange')](js-apis-bluetooth-ble.md#onbleconnectionstatechange)替代。 3914 3915**需要权限**:ohos.permission.USE_BLUETOOTH 3916 3917**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3918 3919**参数:** 3920 3921| 参数名 | 类型 | 必填 | 说明 | 3922| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3923| type | string | 是 | 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。 | 3924| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstatedeprecated)> | 是 | 表示连接状态,已连接或断开。 | 3925 3926**示例:** 3927 3928```js 3929import { BusinessError } from '@ohos.base'; 3930function ConnectStateChanged(state: bluetoothManager.BLEConnectChangedState) { 3931 console.log('bluetooth connect state changed'); 3932 let connectState: bluetoothManager.ProfileConnectionState = state.state; 3933} 3934try { 3935 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3936 device.on('BLEConnectionStateChange', ConnectStateChanged); 3937} catch (err) { 3938 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3939} 3940``` 3941 3942 3943### off('BLEConnectionStateChange')<sup>(deprecated)</sup> 3944 3945off(type: "BLEConnectionStateChange", callback?: Callback<BLEConnectChangedState>): void 3946 3947取消订阅蓝牙低功耗设备的连接状态变化事件。 3948 3949> **说明:**<br/> 3950> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#off('BLEConnectionStateChange')](js-apis-bluetooth-ble.md#offbleconnectionstatechange)替代。 3951 3952**需要权限**:ohos.permission.USE_BLUETOOTH 3953 3954**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3955 3956**参数:** 3957 3958| 参数名 | 类型 | 必填 | 说明 | 3959| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3960| type | string | 是 | 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。 | 3961| callback | Callback<[BLEConnectChangedState](#bleconnectchangedstatedeprecated)> | 否 | 表示取消订阅蓝牙低功耗设备的连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。 | 3962 3963**示例:** 3964 3965```js 3966import { BusinessError } from '@ohos.base'; 3967try { 3968 let device = bluetoothManager.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3969 device.off('BLEConnectionStateChange'); 3970} catch (err) { 3971 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 3972} 3973``` 3974 3975 3976### getDeviceName<sup>(deprecated)</sup> 3977 3978getDeviceName(callback: AsyncCallback<string>): void 3979 3980client获取远端蓝牙低功耗设备名。 3981 3982> **说明:**<br/> 3983> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#getDeviceName](js-apis-bluetooth-ble.md#getdevicename)替代。 3984 3985**需要权限**:ohos.permission.USE_BLUETOOTH 3986 3987**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3988 3989**参数:** 3990 3991| 参数名 | 类型 | 必填 | 说明 | 3992| -------- | --------------------------- | ---- | ------------------------------- | 3993| callback | AsyncCallback<string> | 是 | client获取对端server设备名,通过注册回调函数获取。 | 3994 3995**错误码**: 3996 3997以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3998 3999| 错误码ID | 错误信息 | 4000| -------- | ---------------------------- | 4001|2900001 | Service stopped. | 4002|2900099 | Operation failed. | 4003 4004**示例:** 4005 4006```js 4007import { BusinessError } from '@ohos.base'; 4008// callback 4009try { 4010 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 4011 gattClient.connect(); 4012 let deviceName = gattClient.getDeviceName((err, data)=> { 4013 console.info('device name err ' + JSON.stringify(err)); 4014 console.info('device name' + JSON.stringify(data)); 4015 }) 4016} catch (err) { 4017 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 4018} 4019``` 4020 4021 4022### getDeviceName<sup>(deprecated)</sup> 4023 4024getDeviceName(): Promise<string> 4025 4026client获取远端蓝牙低功耗设备名。 4027 4028> **说明:**<br/> 4029> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#getDeviceName](js-apis-bluetooth-ble.md#getdevicename-1)替代。 4030 4031**需要权限**:ohos.permission.USE_BLUETOOTH 4032 4033**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4034 4035**返回值:** 4036 4037| 类型 | 说明 | 4038| --------------------- | ---------------------------------- | 4039| Promise<string> | client获取对端server设备名,通过promise形式获取。 | 4040 4041**错误码**: 4042 4043以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 4044 4045| 错误码ID | 错误信息 | 4046| -------- | ---------------------------- | 4047|2900001 | Service stopped. | 4048|2900099 | Operation failed. | 4049 4050**示例:** 4051 4052```js 4053import { BusinessError } from '@ohos.base'; 4054// promise 4055try { 4056 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 4057 gattClient.connect(); 4058 let deviceName = gattClient.getDeviceName().then((data) => { 4059 console.info('device name' + JSON.stringify(data)); 4060 }) 4061} catch (err) { 4062 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 4063} 4064``` 4065 4066 4067### getRssiValue<sup>(deprecated)</sup> 4068 4069getRssiValue(callback: AsyncCallback<number>): void 4070 4071client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用[connect](#connect)接口连接成功后才能使用。 4072 4073> **说明:**<br/> 4074> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#getRssiValue](js-apis-bluetooth-ble.md#getrssivalue)替代。 4075 4076**需要权限**:ohos.permission.USE_BLUETOOTH 4077 4078**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4079 4080**参数:** 4081 4082| 参数名 | 类型 | 必填 | 说明 | 4083| -------- | --------------------------- | ---- | ------------------------------ | 4084| callback | AsyncCallback<number> | 是 | 返回信号强度,单位 dBm,通过注册回调函数获取。 | 4085 4086**错误码**: 4087 4088以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 4089 4090| 错误码ID | 错误信息 | 4091| -------- | ---------------------------- | 4092|2900099 | Operation failed. | 4093 4094**示例:** 4095 4096```js 4097import { BusinessError } from '@ohos.base'; 4098// callback 4099try { 4100 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 4101 gattClient.connect(); 4102 let rssi = gattClient.getRssiValue((err: BusinessError, data: number)=> { 4103 console.info('rssi err ' + JSON.stringify(err)); 4104 console.info('rssi value' + JSON.stringify(data)); 4105 }) 4106} catch (err) { 4107 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 4108} 4109``` 4110 4111 4112### getRssiValue<sup>(deprecated)</sup> 4113 4114getRssiValue(): Promise<number> 4115 4116client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用[connect](#connect)接口连接成功后才能使用。 4117 4118> **说明:**<br/> 4119> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattClientDevice#getRssiValue](js-apis-bluetooth-ble.md#getrssivalue-1)替代。 4120 4121**需要权限**:ohos.permission.USE_BLUETOOTH 4122 4123**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4124 4125**返回值:** 4126 4127| 类型 | 说明 | 4128| --------------------- | --------------------------------- | 4129| Promise<number> | 返回信号强度,单位 dBm,通过promise形式获取。 | 4130 4131**错误码**: 4132 4133以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 4134 4135| 错误码ID | 错误信息 | 4136| -------- | ---------------------------- | 4137|2900099 | Operation failed. | 4138 4139**示例:** 4140 4141```js 4142import { BusinessError } from '@ohos.base'; 4143// promise 4144try { 4145 let gattClient = bluetoothManager.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 4146 let rssi = gattClient.getRssiValue().then((data: number) => { 4147 console.info('rssi' + JSON.stringify(data)); 4148 }) 4149} catch (err) { 4150 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 4151} 4152``` 4153 4154## ScanMode<sup>(deprecated)</sup> 4155 4156枚举,扫描模式。 4157 4158> **说明:**<br/> 4159> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.ScanMode](js-apis-bluetooth-connection.md#scanmode)替代。 4160 4161**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4162 4163| 名称 | 值 | 说明 | 4164| ---------------------------------------- | ---- | --------------- | 4165| SCAN_MODE_NONE | 0 | 没有扫描模式。 | 4166| SCAN_MODE_CONNECTABLE | 1 | 可连接扫描模式。 | 4167| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | general发现模式。 | 4168| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | limited发现模式。 | 4169| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | 可连接general发现模式。 | 4170| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | 可连接limited发现模式。 | 4171 4172## BondState<sup>(deprecated)</sup> 4173 4174枚举,配对状态。 4175 4176> **说明:**<br/> 4177> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.BondState](js-apis-bluetooth-connection.md#bondstate)替代。 4178 4179**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4180 4181| 名称 | 值 | 说明 | 4182| ------------------ | ---- | ------ | 4183| BOND_STATE_INVALID | 0 | 无效的配对。 | 4184| BOND_STATE_BONDING | 1 | 正在配对。 | 4185| BOND_STATE_BONDED | 2 | 已配对。 | 4186 4187 4188## SppOption<sup>(deprecated)</sup> 4189 4190描述spp的配置参数。 4191 4192> **说明:**<br/> 4193> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.SppOption](js-apis-bluetooth-socket.md#sppoptions)替代。 4194 4195**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4196 4197| 名称 | 类型 | 可读 | 可写 | 说明 | 4198| ------ | ------------------- | ---- | ---- | ----------- | 4199| uuid | string | 是 | 是 | spp单据的uuid。 | 4200| secure | boolean | 是 | 是 | 是否是安全通道。 | 4201| type | [SppType](#spptypedeprecated) | 是 | 是 | Spp链路类型。 | 4202 4203 4204## SppType<sup>(deprecated)</sup> 4205 4206枚举,Spp链路类型。 4207 4208> **说明:**<br/> 4209> 从API version 9开始支持,从API version 10开始废弃。建议使用[socket.SppType](js-apis-bluetooth-socket.md#spptype)替代。 4210 4211**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4212 4213| 名称 | 值 | 说明 | 4214| ---------- | ---- | ------------- | 4215| SPP_RFCOMM | 0 | 表示rfcomm链路类型。 | 4216 4217 4218## GattService<sup>(deprecated)</sup> 4219 4220描述service的接口参数定义。 4221 4222> **说明:**<br/> 4223> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.GattService](js-apis-bluetooth-ble.md#gattservice)替代。 4224 4225**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4226 4227| 名称 | 类型 | 可读 | 可写 | 说明 | 4228| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 4229| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4230| isPrimary | boolean | 是 | 是 | 如果是主服务设置为true,否则设置为false。 | 4231| characteristics | Array<[BLECharacteristic](#blecharacteristicdeprecated)> | 是 | 是 | 当前服务包含的特征列表。 | 4232| includeServices | Array<[GattService](#gattservicedeprecated)> | 是 | 是 | 当前服务依赖的其它服务。 | 4233 4234 4235## BLECharacteristic<sup>(deprecated)</sup> 4236 4237描述characteristic的接口参数定义。 4238 4239> **说明:**<br/> 4240> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.BLECharacteristic](js-apis-bluetooth-ble.md#blecharacteristic)替代。 4241 4242**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4243 4244| 名称 | 类型 | 可读 | 可写 | 说明 | 4245| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 4246| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4247| characteristicUuid | string | 是 | 是 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4248| characteristicValue | ArrayBuffer | 是 | 是 | 特征对应的二进制值。 | 4249| descriptors | Array<[BLEDescriptor](#bledescriptordeprecated)> | 是 | 是 | 特定特征的描述符列表。 | 4250 4251 4252## BLEDescriptor<sup>(deprecated)</sup> 4253 4254描述descriptor的接口参数定义。 4255 4256> **说明:**<br/> 4257> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.BLEDescriptor](js-apis-bluetooth-ble.md#bledescriptor)替代。 4258 4259**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4260 4261| 名称 | 类型 | 可读 | 可写 | 说明 | 4262| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 4263| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4264| characteristicUuid | string | 是 | 是 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4265| descriptorUuid | string | 是 | 是 | 描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 4266| descriptorValue | ArrayBuffer | 是 | 是 | 描述符对应的二进制值。 | 4267 4268 4269## NotifyCharacteristic<sup>(deprecated)</sup> 4270 4271描述server端特征值变化时发送的特征通知参数定义。 4272 4273> **说明:**<br/> 4274> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.NotifyCharacteristic](js-apis-bluetooth-ble.md#notifycharacteristic)替代。 4275 4276**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4277 4278| 名称 | 类型 | 可读 | 可写 | 说明 | 4279| ------------------- | ----------- | ---- | ---- | ---------------------------------------- | 4280| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4281| characteristicUuid | string | 是 | 是 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4282| characteristicValue | ArrayBuffer | 是 | 是 | 特征对应的二进制值。 | 4283| confirm | boolean | 是 | 是 | 如果是notification则对端回复确认设置为true,如果是indication则对端不需要回复确认设置为false。 | 4284 4285 4286## CharacteristicReadRequest<sup>(deprecated)</sup> 4287 4288描述server端订阅后收到的特征值读请求事件参数结构。 4289 4290> **说明:**<br/> 4291> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.CharacteristicReadRequest](js-apis-bluetooth-ble.md#characteristicreadrequest)替代。 4292 4293**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4294 4295| 名称 | 类型 | 可读 | 可写 | 说明 | 4296| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 4297| deviceId | string | 是 | 否 | 表示发送特征值读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4298| transId | number | 是 | 否 | 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。 | 4299| offset | number | 是 | 否 | 表示读特征值数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。 | 4300| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4301| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4302 4303 4304## CharacteristicWriteRequest<sup>(deprecated)</sup> 4305 4306描述server端订阅后收到的特征值写请求事件参数结构。 4307 4308> **说明:**<br/> 4309> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.CharacteristicWriteRequest](js-apis-bluetooth-ble.md#characteristicwriterequest)替代。 4310 4311**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4312 4313| 名称 | 类型 | 可读 | 可写 | 说明 | 4314| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 4315| deviceId | string | 是 | 否 | 表示发送特征值写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4316| transId | number | 是 | 否 | 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。 | 4317| offset | number | 是 | 否 | 表示写特征值数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。 | 4318| descriptorUuid | string | 是 | 否 | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 4319| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4320| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4321 4322 4323## DescriptorReadRequest<sup>(deprecated)</sup> 4324 4325描述server端订阅后收到的描述符读请求事件参数结构。 4326 4327> **说明:**<br/> 4328> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.DescriptorReadRequest](js-apis-bluetooth-ble.md#descriptorreadrequest)替代。 4329 4330**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4331 4332| 名称 | 类型 | 可读 | 可写 | 说明 | 4333| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 4334| deviceId | string | 是 | 否 | 表示发送描述符读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4335| transId | number | 是 | 否 | 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。 | 4336| offset | number | 是 | 否 | 表示读描述符数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。 | 4337| descriptorUuid | string | 是 | 否 | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 4338| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4339| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4340 4341 4342## DescriptorWriteRequest<sup>(deprecated)</sup> 4343 4344描述server端订阅后收到的描述符写请求事件参数结构。 4345 4346> **说明:**<br/> 4347> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.DescriptorWriteRequest](js-apis-bluetooth-ble.md#descriptorwriterequest)替代。 4348 4349**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4350 4351| 名称 | 类型 | 可读 | 可写 | 说明 | 4352| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 4353| deviceId | string | 是 | 否 | 表示发送描述符写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4354| transId | number | 是 | 否 | 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。 | 4355| offset | number | 是 | 否 | 表示写描述符数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。 | 4356| isPrep | boolean | 是 | 否 | 表示写请求是否立即执行。 | 4357| needRsp | boolean | 是 | 否 | 表示是否要给client端回复响应。 | 4358| value | ArrayBuffer | 是 | 否 | 表示写入的描述符二进制数据。 | 4359| descriptorUuid | string | 是 | 否 | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 4360| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 4361| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4362 4363 4364## ServerResponse<sup>(deprecated)</sup> 4365 4366描述server端回复client端读/写请求的响应参数结构。 4367 4368> **说明:**<br/> 4369> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ServerResponse](js-apis-bluetooth-ble.md#serverresponse)替代。 4370 4371**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4372 4373| 名称 | 类型 | 可读 | 可写 | 说明 | 4374| -------- | ----------- | ---- | ---- | -------------------------------------- | 4375| deviceId | string | 是 | 否 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4376| transId | number | 是 | 否 | 表示请求的传输ID,与订阅的读/写请求事件携带的ID保持一致。 | 4377| status | number | 是 | 否 | 表示响应的状态,设置为0即可,表示正常。 | 4378| offset | number | 是 | 否 | 表示请求的读/写起始位置,与订阅的读/写请求事件携带的offset保持一致。 | 4379| value | ArrayBuffer | 是 | 否 | 表示回复响应的二进制数据。 | 4380 4381 4382## BLEConnectChangedState<sup>(deprecated)</sup> 4383 4384描述Gatt profile连接状态。 4385 4386> **说明:**<br/> 4387> 从API version 9开始支持,从API version 10开始废弃。建议使用[BLEConnectionChangeState](js-apis-bluetooth-ble.md#bleconnectionchangestate)替代。 4388 4389**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4390 4391| 名称 | 类型 | 可读 | 可写 | 说明 | 4392| -------- | ------------------------------------------------- | ---- | ---- | --------------------------------------------- | 4393| deviceId | string | 是 | 否 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4394| state | [ProfileConnectionState](#profileconnectionstatedeprecated) | 是 | 是 | 表示BLE连接状态的枚举。 | 4395 4396 4397## ProfileConnectionState<sup>(deprecated)</sup> 4398 4399枚举,蓝牙设备的profile连接状态。 4400 4401> **说明:**<br/> 4402> 从API version 9开始支持,从API version 10开始废弃。建议使用[constant.ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate)替代。 4403 4404**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4405 4406| 名称 | 值 | 说明 | 4407| ------------------- | ---- | -------------- | 4408| STATE_DISCONNECTED | 0 | 表示profile已断连。 | 4409| STATE_CONNECTING | 1 | 表示profile正在连接。 | 4410| STATE_CONNECTED | 2 | 表示profile已连接。 | 4411| STATE_DISCONNECTING | 3 | 表示profile正在断连。 | 4412 4413 4414## ScanFilter<sup>(deprecated)</sup> 4415 4416扫描过滤参数。 4417 4418> **说明:**<br/> 4419> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ScanFilter](js-apis-bluetooth-ble.md#scanfilter)替代。 4420 4421**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4422 4423| 名称 | 类型 | 可读 | 可写 | 说明 | 4424| ---------------------------------------- | ----------- | ---- | ---- | ------------------------------------------------------------ | 4425| deviceId | string | 是 | 是 | 表示过滤的BLE设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4426| name | string | 是 | 是 | 表示过滤的BLE设备名。 | 4427| serviceUuid | string | 是 | 是 | 表示过滤包含该UUID服务的设备,例如:00001888-0000-1000-8000-00805f9b34fb。 | 4428| serviceUuidMask | string | 是 | 是 | 表示过滤包含该UUID服务掩码的设备,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。 | 4429| serviceSolicitationUuid | string | 是 | 是 | 表示过滤包含该UUID服务请求的设备,例如:00001888-0000-1000-8000-00805F9B34FB。 | 4430| serviceSolicitationUuidMask | string | 是 | 是 | 表示过滤包含该UUID服务请求掩码的设备,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。 | 4431| serviceData | ArrayBuffer | 是 | 是 | 表示过滤包含该服务相关数据的设备,例如:[0x90,0x00,0xF1,0xF2]。 | 4432| serviceDataMask | ArrayBuffer | 是 | 是 | 表示过滤包含该服务相关数据掩码的设备,例如:[0xFF,0xFF,0xFF,0xFF]。 | 4433| manufactureId | number | 是 | 是 | 表示过滤包含该制造商ID的设备,例如:0x0006。 | 4434| manufactureData | ArrayBuffer | 是 | 是 | 表示过滤包含该制造商相关数据的设备,例如:[0x1F,0x2F,0x3F]。 | 4435| manufactureDataMask | ArrayBuffer | 是 | 是 | 表示过滤包含该制造商相关数据掩码的设备,例如:[0xFF,0xFF,0xFF]。 | 4436 4437 4438## ScanOptions<sup>(deprecated)</sup> 4439 4440扫描的配置参数。 4441 4442> **说明:**<br/> 4443> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ScanOptions](js-apis-bluetooth-ble.md#scanoptions)替代。 4444 4445**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4446 4447| 名称 | 类型 | 可读 | 可写 | 说明 | 4448| --------- | ----------------------- | ---- | ---- | -------------------------------------- | 4449| interval | number | 是 | 是 | 表示扫描结果上报延迟时间,默认值为0。 | 4450| dutyMode | [ScanDuty](#scandutydeprecated) | 是 | 是 | 表示扫描模式,默认值为SCAN_MODE_LOW_POWER。 | 4451| matchMode | [MatchMode](#matchmodedeprecated) | 是 | 是 | 表示硬件的过滤匹配模式,默认值为MATCH_MODE_AGGRESSIVE。 | 4452 4453 4454## ScanDuty<sup>(deprecated)</sup> 4455 4456枚举,扫描模式。 4457 4458> **说明:**<br/> 4459> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ScanDuty](js-apis-bluetooth-ble.md#scanduty)替代。 4460 4461**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4462 4463| 名称 | 值 | 说明 | 4464| --------------------- | ---- | ------------ | 4465| SCAN_MODE_LOW_POWER | 0 | 表示低功耗模式,默认值。 | 4466| SCAN_MODE_BALANCED | 1 | 表示均衡模式。 | 4467| SCAN_MODE_LOW_LATENCY | 2 | 表示低延迟模式。 | 4468 4469 4470## MatchMode<sup>(deprecated)</sup> 4471 4472枚举,硬件过滤匹配模式。 4473 4474> **说明:**<br/> 4475> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.MatchMode](js-apis-bluetooth-ble.md#matchmode)替代。 4476 4477**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4478 4479| 名称 | 值 | 说明 | 4480| --------------------- | ---- | ---------------------------------------- | 4481| MATCH_MODE_AGGRESSIVE | 1 | 表示硬件上报扫描结果门限较低,比如扫描到的功率较低或者一段时间扫描到的次数较少也触发上报,默认值。 | 4482| MATCH_MODE_STICKY | 2 | 表示硬件上报扫描结果门限较高,更高的功率门限以及扫描到多次才会上报。 | 4483 4484 4485## ScanResult<sup>(deprecated)</sup> 4486 4487扫描结果上报数据。 4488 4489> **说明:**<br/> 4490> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ScanResult](js-apis-bluetooth-ble.md#scanresult)替代。 4491 4492**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4493 4494| 名称 | 类型 | 可读 | 可写 | 说明 | 4495| -------- | ----------- | ---- | ---- | ---------------------------------- | 4496| deviceId | string | 是 | 否 | 表示扫描到的设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 4497| rssi | number | 是 | 否 | 表示扫描到的设备的rssi值。 | 4498| data | ArrayBuffer | 是 | 否 | 表示扫描到的设备发送的广播包。 | 4499 4500 4501## BluetoothState<sup>(deprecated)</sup> 4502 4503枚举,蓝牙开关状态。 4504 4505> **说明:**<br/> 4506> 从API version 9开始支持,从API version 10开始废弃。建议使用[access.BluetoothState](js-apis-bluetooth-access.md#bluetoothstate)替代。 4507 4508**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4509 4510| 名称 | 值 | 说明 | 4511| --------------------- | ---- | ------------------ | 4512| STATE_OFF | 0 | 表示蓝牙已关闭。 | 4513| STATE_TURNING_ON | 1 | 表示蓝牙正在打开。 | 4514| STATE_ON | 2 | 表示蓝牙已打开。 | 4515| STATE_TURNING_OFF | 3 | 表示蓝牙正在关闭。 | 4516| STATE_BLE_TURNING_ON | 4 | 表示蓝牙正在打开LE-only模式。 | 4517| STATE_BLE_ON | 5 | 表示蓝牙正处于LE-only模式。 | 4518| STATE_BLE_TURNING_OFF | 6 | 表示蓝牙正在关闭LE-only模式。 | 4519 4520 4521## AdvertiseSetting<sup>(deprecated)</sup> 4522 4523描述蓝牙低功耗设备发送广播的参数。 4524 4525> **说明:**<br/> 4526> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.AdvertiseSetting](js-apis-bluetooth-ble.md#advertisesetting)替代。 4527 4528**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4529 4530| 名称 | 类型 | 可读 | 可写 | 说明 | 4531| ----------- | ------- | ---- | ---- | ---------------------------------------- | 4532| interval | number | 是 | 是 | 表示广播间隔,最小值设置32个slot表示20ms,最大值设置16384个slot,默认值设置为1600个slot表示1s。 | 4533| txPower | number | 是 | 是 | 表示发送功率,最小值设置-127,最大值设置1,默认值设置-7,单位dbm。 | 4534| connectable | boolean | 是 | 是 | 表示是否是可连接广播,默认值设置为true。 | 4535 4536 4537## AdvertiseData<sup>(deprecated)</sup> 4538 4539描述BLE广播数据包的内容。 4540 4541> **说明:**<br/> 4542> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.AdvertiseData](js-apis-bluetooth-ble.md#advertisedata)替代。 4543 4544**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4545 4546| 名称 | 类型 | 可读 | 可写 | 说明 | 4547| --------------- | ---------------------------------------- | ---- | ---- | --------------------------- | 4548| serviceUuids | Array<string> | 是 | 是 | 表示要广播的服务 UUID 列表。 | 4549| manufactureData | Array<[ManufactureData](#manufacturedatadeprecated)> | 是 | 是 | 表示要广播的广播的制造商信息列表。 | 4550| serviceData | Array<[ServiceData](#servicedatadeprecated)> | 是 | 是 | 表示要广播的服务数据列表。 | 4551 4552 4553## ManufactureData<sup>(deprecated)</sup> 4554 4555描述BLE广播数据包的内容。 4556 4557> **说明:**<br/> 4558> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ManufactureData](js-apis-bluetooth-ble.md#manufacturedata)替代。 4559 4560**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4561 4562| 名称 | 类型 | 可读 | 可写 | 说明 | 4563| ---------------- | ------------------- | ---- | ---- | ------------------ | 4564| manufactureId | number | 是 | 是 | 表示制造商的ID,由蓝牙SIG分配。 | 4565| manufactureValue | ArrayBuffer | 是 | 是 | 表示制造商发送的制造商数据。 | 4566 4567 4568## ServiceData<sup>(deprecated)</sup> 4569 4570描述广播包中服务数据内容。 4571 4572> **说明:**<br/> 4573> 从API version 9开始支持,从API version 10开始废弃。建议使用[ble.ServiceData](js-apis-bluetooth-ble.md#servicedata)替代。 4574 4575**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4576 4577| 名称 | 类型 | 可读 | 可写 | 说明 | 4578| ------------ | ----------- | ---- | ---- | ---------- | 4579| serviceUuid | string | 是 | 是 | 表示服务的UUID。 | 4580| serviceValue | ArrayBuffer | 是 | 是 | 表示服务数据。 | 4581 4582 4583## PinRequiredParam<sup>(deprecated)</sup> 4584 4585描述配对请求参数。 4586 4587> **说明:**<br/> 4588> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.PinRequiredParam](js-apis-bluetooth-connection.md#pinrequiredparam)替代。 4589 4590**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4591 4592| 名称 | 类型 | 可读 | 可写 | 说明 | 4593| -------- | ------ | ---- | ---- | ----------- | 4594| deviceId | string | 是 | 否 | 表示要配对的设备ID。 | 4595| pinCode | string | 是 | 否 | 表示要配对的密钥。 | 4596 4597 4598## BondStateParam<sup>(deprecated)</sup> 4599 4600描述配对状态参数。 4601 4602> **说明:**<br/> 4603> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.BondStateParam](js-apis-bluetooth-connection.md#bondstateparam)替代。 4604 4605**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4606 4607| 名称 | 类型 | 可读 | 可写 | 说明 | 4608| -------- | ------ | ---- | ---- | ----------- | 4609| deviceId | string | 是 | 否 | 表示要配对的设备ID。 | 4610| state | BondState | 是 | 否 | 表示配对设备的状态。 | 4611 4612 4613## StateChangeParam<sup>(deprecated)</sup> 4614 4615描述profile状态改变参数。 4616 4617> **说明:**<br/> 4618> 从API version 9开始支持,从API version 10开始废弃。建议使用[baseProfile.StateChangeParam](js-apis-bluetooth-baseProfile.md#statechangeparam)替代。 4619 4620**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4621 4622| 名称 | 类型 | 可读 | 可写 | 说明 | 4623| -------- | ------------------------------------------------- | ---- | ---- | ------------------------------- | 4624| deviceId | string | 是 | 否 | 表示蓝牙设备地址。 | 4625| state | [ProfileConnectionState](#profileconnectionstatedeprecated) | 是 | 否 | 表示蓝牙设备的profile连接状态。 | 4626 4627 4628## DeviceClass<sup>(deprecated)</sup> 4629 4630描述蓝牙设备的类别。 4631 4632> **说明:**<br/> 4633> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.DeviceClass](js-apis-bluetooth-connection.md#deviceclass)替代。 4634 4635**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4636 4637| 名称 | 类型 | 可读 | 可写 | 说明 | 4638| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 4639| majorClass | [MajorClass](#majorclassdeprecated) | 是 | 否 | 表示蓝牙设备主要类别的枚举。 | 4640| majorMinorClass | [MajorMinorClass](#majorminorclassdeprecated) | 是 | 否 | 表示主要次要蓝牙设备类别的枚举。 | 4641| classOfDevice | number | 是 | 否 | 表示设备类别。 | 4642 4643 4644## MajorClass<sup>(deprecated)</sup> 4645 4646枚举,蓝牙设备主要类别。 4647 4648> **说明:**<br/> 4649> 从API version 9开始支持,从API version 10开始废弃。建议使用[constant.MajorClass](js-apis-bluetooth-constant.md#majorclass)替代。 4650 4651**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4652 4653| 名称 | 值 | 说明 | 4654| ------------------- | ------ | ---------- | 4655| MAJOR_MISC | 0x0000 | 表示杂项设备。 | 4656| MAJOR_COMPUTER | 0x0100 | 表示计算机设备。 | 4657| MAJOR_PHONE | 0x0200 | 表示手机设备。 | 4658| MAJOR_NETWORKING | 0x0300 | 表示网络设备。 | 4659| MAJOR_AUDIO_VIDEO | 0x0400 | 表示音频和视频设备。 | 4660| MAJOR_PERIPHERAL | 0x0500 | 表示外围设备。 | 4661| MAJOR_IMAGING | 0x0600 | 表示成像设备。 | 4662| MAJOR_WEARABLE | 0x0700 | 表示可穿戴设备。 | 4663| MAJOR_TOY | 0x0800 | 表示玩具设备。 | 4664| MAJOR_HEALTH | 0x0900 | 表示健康设备。 | 4665| MAJOR_UNCATEGORIZED | 0x1F00 | 表示未分类设备。 | 4666 4667 4668## MajorMinorClass<sup>(deprecated)</sup> 4669 4670枚举,主要次要蓝牙设备类别。 4671 4672> **说明:**<br/> 4673> 从API version 9开始支持,从API version 10开始废弃。建议使用[constant.MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass)替代。 4674 4675**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4676 4677| 名称 | 值 | 说明 | 4678| ---------------------------------------- | ------ | --------------- | 4679| COMPUTER_UNCATEGORIZED | 0x0100 | 表示未分类计算机设备。 | 4680| COMPUTER_DESKTOP | 0x0104 | 表示台式计算机设备。 | 4681| COMPUTER_SERVER | 0x0108 | 表示服务器设备。 | 4682| COMPUTER_LAPTOP | 0x010C | 表示便携式计算机设备。 | 4683| COMPUTER_HANDHELD_PC_PDA | 0x0110 | 表示手持式计算机设备。 | 4684| COMPUTER_PALM_SIZE_PC_PDA | 0x0114 | 表示掌上电脑设备。 | 4685| COMPUTER_WEARABLE | 0x0118 | 表示可穿戴计算机设备。 | 4686| COMPUTER_TABLET | 0x011C | 表示平板电脑设备。 | 4687| PHONE_UNCATEGORIZED | 0x0200 | 表示未分类手机设备。 | 4688| PHONE_CELLULAR | 0x0204 | 表示便携式手机设备。 | 4689| PHONE_CORDLESS | 0x0208 | 表示无线电话设备。 | 4690| PHONE_SMART | 0x020C | 表示智能手机设备。 | 4691| PHONE_MODEM_OR_GATEWAY | 0x0210 | 表示调制解调器或网关手机设备。 | 4692| PHONE_ISDN | 0x0214 | 表示ISDN手机设备。 | 4693| NETWORK_FULLY_AVAILABLE | 0x0300 | 表示网络完全可用设备。 | 4694| NETWORK_1_TO_17_UTILIZED | 0x0320 | 表示使用网络1到17设备。 | 4695| NETWORK_17_TO_33_UTILIZED | 0x0340 | 表示使用网络17到33设备。 | 4696| NETWORK_33_TO_50_UTILIZED | 0x0360 | 表示使用网络33到50设备。 | 4697| NETWORK_60_TO_67_UTILIZED | 0x0380 | 表示使用网络60到67设备。 | 4698| NETWORK_67_TO_83_UTILIZED | 0x03A0 | 表示使用网络67到83设备。 | 4699| NETWORK_83_TO_99_UTILIZED | 0x03C0 | 表示使用网络83到99设备。 | 4700| NETWORK_NO_SERVICE | 0x03E0 | 表示网络无服务设备。 | 4701| AUDIO_VIDEO_UNCATEGORIZED | 0x0400 | 表示未分类音频视频设备。 | 4702| AUDIO_VIDEO_WEARABLE_HEADSET | 0x0404 | 表示可穿戴式音频视频设备。 | 4703| AUDIO_VIDEO_HANDSFREE | 0x0408 | 表示免提音频视频设备。 | 4704| AUDIO_VIDEO_MICROPHONE | 0x0410 | 表示麦克风音频视频设备。 | 4705| AUDIO_VIDEO_LOUDSPEAKER | 0x0414 | 表示扬声器音频视频设备。 | 4706| AUDIO_VIDEO_HEADPHONES | 0x0418 | 表示头戴式音频视频设备。 | 4707| AUDIO_VIDEO_PORTABLE_AUDIO | 0x041C | 表示便携式音频视频设备。 | 4708| AUDIO_VIDEO_CAR_AUDIO | 0x0420 | 表示汽车音频视频设备。 | 4709| AUDIO_VIDEO_SET_TOP_BOX | 0x0424 | 表示机顶盒音频视频设备。 | 4710| AUDIO_VIDEO_HIFI_AUDIO | 0x0428 | 表示高保真音响设备。 | 4711| AUDIO_VIDEO_VCR | 0x042C | 表示录像机音频视频设备。 | 4712| AUDIO_VIDEO_VIDEO_CAMERA | 0x0430 | 表示照相机音频视频设备。 | 4713| AUDIO_VIDEO_CAMCORDER | 0x0434 | 表示摄像机音频视频设备。 | 4714| AUDIO_VIDEO_VIDEO_MONITOR | 0x0438 | 表示监视器音频视频设备。 | 4715| AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER | 0x043C | 表示视频显示器和扬声器设备。 | 4716| AUDIO_VIDEO_VIDEO_CONFERENCING | 0x0440 | 表示音频视频会议设备。 | 4717| AUDIO_VIDEO_VIDEO_GAMING_TOY | 0x0448 | 表示游戏玩具音频视频设备。 | 4718| PERIPHERAL_NON_KEYBOARD_NON_POINTING | 0x0500 | 表示非键盘非指向外围设备。 | 4719| PERIPHERAL_KEYBOARD | 0x0540 | 表示外设键盘设备。 | 4720| PERIPHERAL_POINTING_DEVICE | 0x0580 | 表示定点装置外围设备。 | 4721| PERIPHERAL_KEYBOARD_POINTING | 0x05C0 | 表示键盘指向外围设备。 | 4722| PERIPHERAL_UNCATEGORIZED | 0x0500 | 表示未分类外围设备。 | 4723| PERIPHERAL_JOYSTICK | 0x0504 | 表示周边操纵杆设备。 | 4724| PERIPHERAL_GAMEPAD | 0x0508 | 表示周边游戏板设备。 | 4725| PERIPHERAL_REMOTE_CONTROL | 0x05C0 | 表示远程控制外围设备。 | 4726| PERIPHERAL_SENSING_DEVICE | 0x0510 | 表示外围传感设备设备。 | 4727| PERIPHERAL_DIGITIZER_TABLET | 0x0514 | 表示外围数字化仪平板电脑设备。 | 4728| PERIPHERAL_CARD_READER | 0x0518 | 表示外围读卡器设备。 | 4729| PERIPHERAL_DIGITAL_PEN | 0x051C | 表示外设数码笔设备。 | 4730| PERIPHERAL_SCANNER_RFID | 0x0520 | 表示射频识别扫描仪外围设备。 | 4731| PERIPHERAL_GESTURAL_INPUT | 0x0522 | 表示手势输入外围设备。 | 4732| IMAGING_UNCATEGORIZED | 0x0600 | 表示未分类的图像设备。 | 4733| IMAGING_DISPLAY | 0x0610 | 表示图像显示设备。 | 4734| IMAGING_CAMERA | 0x0620 | 表示成像照相机设备。 | 4735| IMAGING_SCANNER | 0x0640 | 表示成像扫描仪设备。 | 4736| IMAGING_PRINTER | 0x0680 | 表示成像打印机设备。 | 4737| WEARABLE_UNCATEGORIZED | 0x0700 | 表示未分类的可穿戴设备。 | 4738| WEARABLE_WRIST_WATCH | 0x0704 | 表示可穿戴腕表设备。 | 4739| WEARABLE_PAGER | 0x0708 | 表示可穿戴寻呼机设备。 | 4740| WEARABLE_JACKET | 0x070C | 表示夹克可穿戴设备。 | 4741| WEARABLE_HELMET | 0x0710 | 表示可穿戴头盔设备。 | 4742| WEARABLE_GLASSES | 0x0714 | 表示可穿戴眼镜设备。 | 4743| TOY_UNCATEGORIZED | 0x0800 | 表示未分类的玩具设备。 | 4744| TOY_ROBOT | 0x0804 | 表示玩具机器人设备。 | 4745| TOY_VEHICLE | 0x0808 | 表示玩具车设备。 | 4746| TOY_DOLL_ACTION_FIGURE | 0x080C | 表示人形娃娃玩具设备。 | 4747| TOY_CONTROLLER | 0x0810 | 表示玩具控制器设备。 | 4748| TOY_GAME | 0x0814 | 表示玩具游戏设备。 | 4749| HEALTH_UNCATEGORIZED | 0x0900 | 表示未分类健康设备。 | 4750| HEALTH_BLOOD_PRESSURE | 0x0904 | 表示血压健康设备。 | 4751| HEALTH_THERMOMETER | 0x0908 | 表示温度计健康设备。 | 4752| HEALTH_WEIGHING | 0x090C | 表示体重健康设备。 | 4753| HEALTH_GLUCOSE | 0x0910 | 表示葡萄糖健康设备。 | 4754| HEALTH_PULSE_OXIMETER | 0x0914 | 表示脉搏血氧仪健康设备。 | 4755| HEALTH_PULSE_RATE | 0x0918 | 表示脉搏率健康设备。 | 4756| HEALTH_DATA_DISPLAY | 0x091C | 表示数据显示健康设备。 | 4757| HEALTH_STEP_COUNTER | 0x0920 | 表示阶梯计数器健康设备。 | 4758| HEALTH_BODY_COMPOSITION_ANALYZER | 0x0924 | 表示身体成分分析仪健康设备。 | 4759| HEALTH_PEAK_FLOW_MONITOR | 0x0928 | 表示湿度计健康设备。 | 4760| HEALTH_MEDICATION_MONITOR | 0x092C | 表示药物监视仪健康设备。 | 4761| HEALTH_KNEE_PROSTHESIS | 0x0930 | 表示膝盖假肢健康设备。 | 4762| HEALTH_ANKLE_PROSTHESIS | 0x0934 | 表示脚踝假肢健康设备。 | 4763| HEALTH_GENERIC_HEALTH_MANAGER | 0x0938 | 表示通用健康管理设备。 | 4764| HEALTH_PERSONAL_MOBILITY_DEVICE | 0x093C | 表示个人移动健康设备。 | 4765 4766 4767## PlayingState<sup>(deprecated)</sup> 4768 4769枚举,蓝牙A2DP 播放状态。 4770 4771> **说明:**<br/> 4772> 从API version 9开始支持,从API version 10开始废弃。建议使用[a2dp.PlayingState](js-apis-bluetooth-a2dp.md#playingstate)替代。 4773 4774**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4775 4776| 名称 | 值 | 说明 | 4777| ----------------- | ------ | ------- | 4778| STATE_NOT_PLAYING | 0x0000 | 表示未播放。 | 4779| STATE_PLAYING | 0x0001 | 表示正在播放。 | 4780 4781 4782## ProfileId<sup>(deprecated)</sup> 4783 4784蓝牙profile枚举,API9新增PROFILE_HID_HOST,PROFILE_PAN_NETWORK。 4785 4786> **说明:**<br/> 4787> 从API version 9开始支持,从API version 10开始废弃。建议使用[constant.ProfileId](js-apis-bluetooth-constant.md#profileid)替代。 4788 4789**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4790 4791| 名称 | 值 | 说明 | 4792| -------------------------------- | ------ | --------------- | 4793| PROFILE_A2DP_SOURCE | 1 | 表示A2DP profile。 | 4794| PROFILE_HANDS_FREE_AUDIO_GATEWAY | 4 | 表示HFP profile。 | 4795| PROFILE_HID_HOST | 6 | 表示HID profile。 | 4796| PROFILE_PAN_NETWORK | 7 | 表示PAN profile。 | 4797