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