1# @ohos.bluetooth.connection (蓝牙connection模块) 2 3connection模块提供了对蓝牙操作和管理的方法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10 11## 导入模块 12 13```js 14import connection from '@ohos.bluetooth.connection'; 15``` 16 17 18## connection.pairDevice<a name="pairDevice"></a> 19 20pairDevice(deviceId: string, callback: AsyncCallback<void>): void 21 22发起蓝牙配对。 23 24**需要权限**:ohos.permission.ACCESS_BLUETOOTH 25 26**系统能力**:SystemCapability.Communication.Bluetooth.Core。 27 28**参数:** 29 30| 参数名 | 类型 | 必填 | 说明 | 31| -------- | ------ | ---- | ----------------------------------- | 32| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 33| callback | AsyncCallback<void> | 是 | 回调函数。当配对成功,err为undefined,否则为错误对象。 | 34 35**错误码**: 36 37以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 38 39| 错误码ID | 错误信息 | 40| -------- | ---------------------------- | 41|2900001 | Service stopped. | 42|2900003 | Bluetooth switch is off. | 43|2900099 | Operation failed. | 44 45**示例:** 46 47```js 48import { BusinessError } from '@ohos.base'; 49try { 50 // 实际的地址可由扫描流程获取 51 connection.pairDevice('XX:XX:XX:XX:XX:XX'); 52} catch (err) { 53 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 54} 55``` 56 57 58## connection.pairDevice<a name="pairDevice"></a> 59 60pairDevice(deviceId: string): Promise<void> 61 62发起蓝牙配对。 63 64**需要权限**:ohos.permission.ACCESS_BLUETOOTH 65 66**系统能力**:SystemCapability.Communication.Bluetooth.Core。 67 68**参数:** 69 70| 参数名 | 类型 | 必填 | 说明 | 71| -------- | ------ | ---- | ----------------------------------- | 72| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 73 74**返回值:** 75 76| 类型 | 说明 | 77| ------------------- | ------------- | 78| Promise<void> | 返回promise对象。 | 79 80**错误码**: 81 82以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 83 84| 错误码ID | 错误信息 | 85| -------- | ---------------------------- | 86|2900001 | Service stopped. | 87|2900003 | Bluetooth switch is off. | 88|2900099 | Operation failed. | 89 90**示例:** 91 92```js 93import { BusinessError } from '@ohos.base'; 94try { 95 // 实际的地址可由扫描流程获取 96 connection.pairDevice('XX:XX:XX:XX:XX:XX'); 97} catch (err) { 98 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 99} 100``` 101 102 103## connection.pairCredibleDevice 104 105pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void 106 107向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。 108 109**系统接口**:此接口为系统接口。 110 111**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 112 113**系统能力**:SystemCapability.Communication.Bluetooth.Core。 114 115**参数:** 116 117| 参数名 | 类型 | 必填 | 说明 | 118| -------- | ------ | ---- | ----------------------------------- | 119| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 120| transport | [BluetoothTransport](#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | 121| callback | AsyncCallback<void> | 是 | 回调函数。当发起配对成功,err为undefined,否则为错误对象。 | 122 123**错误码**: 124 125以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 126 127| 错误码ID | 错误信息 | 128| -------- | ---------------------------- | 129|2900001 | Service stopped. | 130|2900003 | Bluetooth switch is off. | 131|2900099 | Operation failed. | 132 133**示例:** 134 135```js 136import { BusinessError } from '@ohos.base'; 137try { 138 connection.pairCredibleDevice('68:13:24:79:4C:8C', connection.BluetoothTransport 139 .TRANSPORT_BR_EDR, (err: BusinessError) => { 140 if (err) { 141 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 142 return; 143 } 144 console.info('pairCredibleDevice, err: ' + JSON.stringify(err)); 145 }); 146} catch (err) { 147 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 148} 149``` 150 151 152## connection.pairCredibleDevice 153 154pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void> 155 156向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。 157 158**系统接口**:此接口为系统接口。 159 160**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 161 162**系统能力**:SystemCapability.Communication.Bluetooth.Core。 163 164**参数:** 165 166| 参数名 | 类型 | 必填 | 说明 | 167| -------- | ------ | ---- | ----------------------------------- | 168| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 169| transport | [BluetoothTransport](#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | 170 171**返回值:** 172 173| 类型 | 说明 | 174| ------------------------------------------------- | ------------------- | 175| Promise<void> | 返回promise对象。 | 176 177**错误码**: 178 179以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 180 181| 错误码ID | 错误信息 | 182| -------- | ---------------------------- | 183|2900001 | Service stopped. | 184|2900003 | Bluetooth switch is off. | 185|2900099 | Operation failed. | 186 187**示例:** 188 189```js 190import { BusinessError } from '@ohos.base'; 191try { 192 connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => { 193 console.info('PairCredibleDevice'); 194 }, (err: BusinessError) => { 195 console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message); 196 }); 197} catch (err) { 198 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 199} 200``` 201 202 203## connection.cancelPairedDevice<a name="cancelPairedDevice"></a> 204 205cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void 206 207删除配对的远程设备。 208 209**系统接口**:此接口为系统接口。 210 211**需要权限**:ohos.permission.ACCESS_BLUETOOTH 212 213**系统能力**:SystemCapability.Communication.Bluetooth.Core。 214 215**参数:** 216 217| 参数名 | 类型 | 必填 | 说明 | 218| -------- | ------ | ---- | ------------------------------------- | 219| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 220| callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | 221 222**错误码**: 223 224以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 225 226| 错误码ID | 错误信息 | 227| -------- | ---------------------------- | 228|2900001 | Service stopped. | 229|2900003 | Bluetooth switch is off. | 230|2900099 | Operation failed. | 231 232**示例:** 233 234```js 235import { BusinessError } from '@ohos.base'; 236try { 237 connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX'); 238} catch (err) { 239 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 240} 241``` 242 243 244## connection.cancelPairedDevice<a name="cancelPairedDevice"></a> 245 246cancelPairedDevice(deviceId: string): Promise<void> 247 248删除配对的远程设备。 249 250**系统接口**:此接口为系统接口。 251 252**需要权限**:ohos.permission.ACCESS_BLUETOOTH 253 254**系统能力**:SystemCapability.Communication.Bluetooth.Core。 255 256**参数:** 257 258| 参数名 | 类型 | 必填 | 说明 | 259| -------- | ------ | ---- | ------------------------------------- | 260| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 261 262**返回值:** 263 264| 类型 | 说明 | 265| ------------------- | ------------- | 266| Promise<void> | 返回promise对象。 | 267 268**错误码**: 269 270以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 271 272| 错误码ID | 错误信息 | 273| -------- | ---------------------------- | 274|2900001 | Service stopped. | 275|2900003 | Bluetooth switch is off. | 276|2900099 | Operation failed. | 277 278**示例:** 279 280```js 281import { BusinessError } from '@ohos.base'; 282try { 283 connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX'); 284} catch (err) { 285 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 286} 287``` 288 289 290## connection.cancelPairingDevice<a name="cancelPairingDevice"></a> 291 292cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void 293 294删除正在配对中的远程设备。 295 296**系统接口**:此接口为系统接口。 297 298**需要权限**:ohos.permission.ACCESS_BLUETOOTH 299 300**系统能力**:SystemCapability.Communication.Bluetooth.Core。 301 302**参数:** 303 304| 参数名 | 类型 | 必填 | 说明 | 305| -------- | ------ | ---- | ------------------------------------- | 306| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 307| callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | 308 309**错误码**: 310 311以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 312 313| 错误码ID | 错误信息 | 314| -------- | ---------------------------- | 315|2900001 | Service stopped. | 316|2900003 | Bluetooth switch is off. | 317|2900099 | Operation failed. | 318 319**示例:** 320 321```js 322import { BusinessError } from '@ohos.base'; 323try { 324 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 325} catch (err) { 326 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 327} 328``` 329 330 331## connection.cancelPairingDevice<a name="cancelPairingDevice"></a> 332 333cancelPairingDevice(deviceId: string): Promise<void> 334 335删除正在配对中的远程设备。 336 337**系统接口**:此接口为系统接口。 338 339**需要权限**:ohos.permission.ACCESS_BLUETOOTH 340 341**系统能力**:SystemCapability.Communication.Bluetooth.Core。 342 343**参数:** 344 345| 参数名 | 类型 | 必填 | 说明 | 346| -------- | ------ | ---- | ------------------------------------- | 347| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 348 349**返回值:** 350 351| 类型 | 说明 | 352| ------------------- | ------------- | 353| Promise<void> | 返回promise对象。 | 354 355**错误码**: 356 357以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 358 359| 错误码ID | 错误信息 | 360| -------- | ---------------------------- | 361|2900001 | Service stopped. | 362|2900003 | Bluetooth switch is off. | 363|2900099 | Operation failed. | 364 365**示例:** 366 367```js 368import { BusinessError } from '@ohos.base'; 369try { 370 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 371} catch (err) { 372 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 373} 374``` 375 376 377## connection.getRemoteDeviceName<a name="getRemoteDeviceName"></a> 378 379getRemoteDeviceName(deviceId: string): string 380 381获取对端蓝牙设备的名称。 382 383**需要权限**:ohos.permission.ACCESS_BLUETOOTH 384 385**系统能力**:SystemCapability.Communication.Bluetooth.Core。 386 387**参数:** 388 389| 参数名 | 类型 | 必填 | 说明 | 390| -------- | ------ | ---- | --------------------------------- | 391| deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 392 393**返回值:** 394 395| 类型 | 说明 | 396| ------ | ------------- | 397| string | 以字符串格式返回设备名称。 | 398 399**错误码**: 400 401以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 402 403| 错误码ID | 错误信息 | 404| -------- | ---------------------------- | 405|2900001 | Service stopped. | 406|2900003 | Bluetooth switch is off. | 407|2900099 | Operation failed. | 408 409**示例:** 410 411```js 412import { BusinessError } from '@ohos.base'; 413try { 414 let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX'); 415} catch (err) { 416 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 417} 418``` 419 420 421## connection.getRemoteDeviceClass<a name="getRemoteDeviceClass"></a> 422 423getRemoteDeviceClass(deviceId: string): DeviceClass 424 425获取对端蓝牙设备的类别。 426 427**需要权限**:ohos.permission.ACCESS_BLUETOOTH 428 429**系统能力**:SystemCapability.Communication.Bluetooth.Core。 430 431**参数:** 432 433| 参数名 | 类型 | 必填 | 说明 | 434| -------- | ------ | ---- | --------------------------------- | 435| deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 436 437**返回值:** 438 439| 类型 | 说明 | 440| --------------------------- | -------- | 441| [DeviceClass](#deviceclass) | 远程设备的类别。 | 442 443**错误码**: 444 445以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 446 447| 错误码ID | 错误信息 | 448| -------- | ---------------------------- | 449|2900001 | Service stopped. | 450|2900003 | Bluetooth switch is off. | 451|2900099 | Operation failed. | 452 453**示例:** 454 455```js 456import { BusinessError } from '@ohos.base'; 457try { 458 let remoteDeviceClass: connection.DeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX'); 459} catch (err) { 460 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 461} 462``` 463 464 465## connection.getLocalName<a name="getLocalName"></a> 466 467getLocalName(): string 468 469获取蓝牙本地设备名称。 470 471**需要权限**:ohos.permission.ACCESS_BLUETOOTH 472 473**系统能力**:SystemCapability.Communication.Bluetooth.Core。 474 475**返回值:** 476 477| 类型 | 说明 | 478| ------ | --------- | 479| string | 蓝牙本地设备名称。 | 480 481**错误码**: 482 483以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 484 485| 错误码ID | 错误信息 | 486| -------- | ---------------------------- | 487|2900001 | Service stopped. | 488|2900099 | Operation failed. | 489 490**示例:** 491 492```js 493import { BusinessError } from '@ohos.base'; 494try { 495 let localName: string = connection.getLocalName(); 496} catch (err) { 497 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 498} 499``` 500 501 502## connection.getPairedDevices<a name="getPairedDevices"></a> 503 504getPairedDevices(): Array<string> 505 506获取蓝牙配对列表。 507 508**需要权限**:ohos.permission.ACCESS_BLUETOOTH 509 510**系统能力**:SystemCapability.Communication.Bluetooth.Core。 511 512**返回值:** 513 514| 类型 | 说明 | 515| ------------------- | ------------- | 516| Array<string> | 已配对蓝牙设备的地址列表。 | 517 518**错误码**: 519 520以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 521 522| 错误码ID | 错误信息 | 523| -------- | ---------------------------- | 524|2900001 | Service stopped. | 525|2900003 | Bluetooth switch is off. | 526|2900099 | Operation failed. | 527 528**示例:** 529 530```js 531import { BusinessError } from '@ohos.base'; 532try { 533 let devices: Array<string> = connection.getPairedDevices(); 534} catch (err) { 535 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 536} 537``` 538 539 540## connection.getProfileConnectionState<a name="getProfileConnectionState"></a> 541 542getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState 543 544获取蓝牙Profile的连接状态,其中ProfileId为可选参数。如果携带ProfileId,则返回的是当前Profile的连接状态。如果未携带ProfileId,任一Profile已连接则返回[STATE_CONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate),否则返回[STATE_DISCONNECTED](js-apis-bluetooth-constant.md#profileconnectionstate)。 545 546**需要权限**:ohos.permission.ACCESS_BLUETOOTH 547 548**系统能力**:SystemCapability.Communication.Bluetooth.Core。 549 550**参数:** 551 552| 参数名 | 类型 | 必填 | 说明 | 553| --------- | --------- | ---- | ------------------------------------- | 554| ProfileId | [profileId](js-apis-bluetooth-constant.md#profileid) | 否 | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 | 555 556**返回值:** 557 558| 类型 | 说明 | 559| ------------------------------------------------- | ------------------- | 560| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | profile的连接状态。 | 561 562**错误码**: 563 564以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 565 566| 错误码ID | 错误信息 | 567| -------- | ---------------------------- | 568|2900001 | Service stopped. | 569|2900003 | Bluetooth switch is off. | 570|2900004 | Profile is not supported. | 571|2900099 | Operation failed. | 572 573**示例:** 574 575```js 576import { BusinessError } from '@ohos.base'; 577import constant from '@ohos.bluetooth.constant'; 578try { 579 let result: connection.ProfileConnectionState = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE); 580} catch (err) { 581 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 582} 583``` 584 585 586## connection.setDevicePairingConfirmation<a name="setDevicePairingConfirmation"></a> 587 588setDevicePairingConfirmation(deviceId: string, accept: boolean): void 589 590设置设备配对请求确认。 591 592**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 593 594**系统能力**:SystemCapability.Communication.Bluetooth.Core。 595 596**参数:** 597 598| 参数名 | 类型 | 必填 | 说明 | 599| ------ | ------- | ---- | -------------------------------- | 600| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 601| accept | boolean | 是 | 接受配对请求设置为true,否则设置为false。 | 602 603**错误码**: 604 605以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 606 607| 错误码ID | 错误信息 | 608| -------- | ---------------------------- | 609|2900001 | Service stopped. | 610|2900003 | Bluetooth switch is off. | 611|2900099 | Operation failed. | 612 613**示例:** 614 615```js 616import { BusinessError } from '@ohos.base'; 617// 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认 618function onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data为配对请求的入参,配对请求参数 619 console.info('pin required = '+ JSON.stringify(data)); 620 connection.setDevicePairingConfirmation(data.deviceId, true); 621} 622try { 623 connection.on('pinRequired', onReceivePinRequiredEvent); 624} catch (err) { 625 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 626} 627``` 628 629 630## connection.setDevicePinCode<a name="setDevicePinCode"></a> 631 632setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void 633 634当蓝牙配对类型[PinType](#pintype)为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。 635 636**需要权限**:ohos.permission.ACCESS_BLUETOOTH 637 638**系统能力**:SystemCapability.Communication.Bluetooth.Core。 639 640**参数:** 641 642| 参数名 | 类型 | 必填 | 说明 | 643| ------ | ------- | ---- | -------------------------------- | 644| deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | 645| code | string | 是 | 用户输入的PIN码。 | 646| callback | AsyncCallback<void> | 是 | 回调函数,当设置PinCode成功,err为undefined,否则为错误对象。 | 647 648**错误码**: 649 650以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 651 652| 错误码ID | 错误信息 | 653| -------- | ---------------------------- | 654|2900001 | Service stopped. | 655|2900003 | Bluetooth switch is off. | 656|2900099 | Operation failed. | 657 658**示例:** 659 660```js 661import { BusinessError } from '@ohos.base'; 662//callback 663try { 664 connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => { 665 console.info('setDevicePinCode,device name err:' + JSON.stringify(err)); 666 }); 667} catch (err) { 668 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 669} 670``` 671 672 673## connection.setDevicePinCode<a name="setDevicePinCode-1"></a> 674 675setDevicePinCode(deviceId: string, code: string): Promise<void> 676 677当蓝牙配对类型[PinType](#pintype)为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。 678 679**需要权限**:ohos.permission.ACCESS_BLUETOOTH 680 681**系统能力**:SystemCapability.Communication.Bluetooth.Core。 682 683**参数:** 684 685| 参数名 | 类型 | 必填 | 说明 | 686| ------ | ------- | ---- | -------------------------------- | 687| deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | 688| code | string | 是 | 用户输入的PIN码。 | 689 690**返回值:** 691 692| 类型 | 说明 | 693| ------------------- | ------------- | 694| Promise<void> | 返回promise对象。 | 695 696**错误码**: 697 698以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 699 700| 错误码ID | 错误信息 | 701| -------- | ---------------------------- | 702|2900001 | Service stopped. | 703|2900003 | Bluetooth switch is off. | 704|2900099 | Operation failed. | 705 706**示例:** 707 708```js 709import { BusinessError } from '@ohos.base'; 710//promise 711try { 712 connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => { 713 console.info('setDevicePinCode'); 714 }, (error: BusinessError) => { 715 console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message); 716 }) 717 718} catch (err) { 719 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 720} 721``` 722 723 724## connection.setLocalName<a name="setLocalName"></a> 725 726setLocalName(name: string): void 727 728设置蓝牙本地设备名称。 729 730**需要权限**:ohos.permission.ACCESS_BLUETOOTH 731 732**系统能力**:SystemCapability.Communication.Bluetooth.Core。 733 734**参数:** 735 736| 参数名 | 类型 | 必填 | 说明 | 737| ---- | ------ | ---- | --------------------- | 738| name | string | 是 | 要设置的蓝牙名称,最大长度为248字节数。 | 739 740**错误码**: 741 742以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 743 744| 错误码ID | 错误信息 | 745| -------- | ---------------------------- | 746|2900001 | Service stopped. | 747|2900003 | Bluetooth switch is off. | 748|2900099 | Operation failed. | 749 750**示例:** 751 752```js 753import { BusinessError } from '@ohos.base'; 754try { 755 connection.setLocalName('device_name'); 756} catch (err) { 757 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 758} 759``` 760 761 762## connection.setBluetoothScanMode<a name="setBluetoothScanMode"></a> 763 764setBluetoothScanMode(mode: ScanMode, duration: number): void 765 766设置蓝牙扫描模式,可以被远端设备发现。 767 768**需要权限**:ohos.permission.ACCESS_BLUETOOTH 769 770**系统能力**:SystemCapability.Communication.Bluetooth.Core。 771 772**参数:** 773 774| 参数名 | 类型 | 必填 | 说明 | 775| -------- | --------------------- | ---- | ---------------------------- | 776| mode | [ScanMode](#scanmode) | 是 | 蓝牙扫描模式。 | 777| duration | number | 是 | 设备可被发现的持续时间,单位为毫秒;设置为0则持续可发现。 | 778 779**错误码**: 780 781以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 782 783| 错误码ID | 错误信息 | 784| -------- | ---------------------------- | 785|2900001 | Service stopped. | 786|2900003 | Bluetooth switch is off. | 787|2900099 | Operation failed. | 788 789**示例:** 790 791```js 792import { BusinessError } from '@ohos.base'; 793try { 794 // 设置为可连接可发现才可被远端设备扫描到,可以连接。 795 connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); 796} catch (err) { 797 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 798} 799``` 800 801 802## connection.getBluetoothScanMode<a name="getBluetoothScanMode"></a> 803 804getBluetoothScanMode(): ScanMode 805 806获取蓝牙扫描模式。 807 808**需要权限**:ohos.permission.ACCESS_BLUETOOTH 809 810**系统能力**:SystemCapability.Communication.Bluetooth.Core。 811 812**返回值:** 813 814| 类型 | 说明 | 815| --------------------- | ------- | 816| [ScanMode](#scanmode) | 蓝牙扫描模式。 | 817 818**错误码**: 819 820以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 821 822| 错误码ID | 错误信息 | 823| -------- | ---------------------------- | 824|2900001 | Service stopped. | 825|2900003 | Bluetooth switch is off. | 826|2900099 | Operation failed. | 827 828**示例:** 829 830```js 831import { BusinessError } from '@ohos.base'; 832try { 833 let scanMode: connection.ScanMode = connection.getBluetoothScanMode(); 834} catch (err) { 835 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 836} 837``` 838 839 840## connection.startBluetoothDiscovery<a name="startBluetoothDiscovery"></a> 841 842startBluetoothDiscovery(): void 843 844开启蓝牙扫描,可以发现远端设备。 845 846**需要权限**:ohos.permission.ACCESS_BLUETOOTH 847 848**系统能力**:SystemCapability.Communication.Bluetooth.Core。 849 850**错误码**: 851 852以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 853 854| 错误码ID | 错误信息 | 855| -------- | ---------------------------- | 856|2900001 | Service stopped. | 857|2900003 | Bluetooth switch is off. | 858|2900099 | Operation failed. | 859 860**示例:** 861 862```js 863import { BusinessError } from '@ohos.base'; 864function onReceiveEvent(data: Array<string>) { 865 console.log('data length' + data.length); 866} 867try { 868 connection.on('bluetoothDeviceFind', onReceiveEvent); 869 connection.startBluetoothDiscovery(); 870} catch (err) { 871 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 872} 873``` 874 875 876## connection.stopBluetoothDiscovery<a name="stopBluetoothDiscovery"></a> 877 878stopBluetoothDiscovery(): void 879 880关闭蓝牙扫描。 881 882**需要权限**:ohos.permission.ACCESS_BLUETOOTH 883 884**系统能力**:SystemCapability.Communication.Bluetooth.Core。 885 886**错误码**: 887 888以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 889 890| 错误码ID | 错误信息 | 891| -------- | ---------------------------- | 892|2900001 | Service stopped. | 893|2900003 | Bluetooth switch is off. | 894|2900099 | Operation failed. | 895 896**示例:** 897 898```js 899import { BusinessError } from '@ohos.base'; 900try { 901 connection.stopBluetoothDiscovery(); 902} catch (err) { 903 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 904} 905``` 906 907 908## connection.getLocalProfileUuids<a name="getLocalProfileUuids"></a> 909 910getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void 911 912获取本地设备的profile UUID。 913 914**系统接口**:此接口为系统接口。 915 916**需要权限**:ohos.permission.ACCESS_BLUETOOTH 917 918**系统能力**:SystemCapability.Communication.Bluetooth.Core。 919 920**参数:** 921 922| 参数名 | 类型 | 必填 | 说明 | 923| -------- | ------ | ---- | ----------------------------------- | 924| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 是 | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 | 925 926**错误码**: 927 928以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 929 930| 错误码ID | 错误信息 | 931| -------- | ---------------------------- | 932|2900001 | Service stopped. | 933|2900003 | Bluetooth switch is off. | 934|2900099 | Operation failed. | 935 936**示例:** 937 938```js 939import { BusinessError } from '@ohos.base'; 940try { 941 connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => { 942 console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 943 }); 944} catch (err) { 945 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 946} 947``` 948 949 950## connection.getLocalProfileUuids<a name="getLocalProfileUuids"></a> 951 952getLocalProfileUuids(): Promise<Array<ProfileUuids>> 953 954获取本地设备的profile UUID。 955 956**系统接口**:此接口为系统接口。 957 958**需要权限**:ohos.permission.ACCESS_BLUETOOTH 959 960**系统能力**:SystemCapability.Communication.Bluetooth.Core。 961 962**返回值:** 963 964| 类型 | 说明 | 965| ------------------- | ------------- | 966| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 返回promise对象。 | 967 968**错误码**: 969 970以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 971 972| 错误码ID | 错误信息 | 973| -------- | ---------------------------- | 974|2900001 | Service stopped. | 975|2900003 | Bluetooth switch is off. | 976|2900099 | Operation failed. | 977 978**示例:** 979 980```js 981import { BusinessError } from '@ohos.base'; 982try { 983 connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { 984 console.info('getLocalProfileUuids'); 985 }, (err: BusinessError) => { 986 console.error('getLocalProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 987 }); 988} catch (err) { 989 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 990} 991``` 992 993 994## connection.getRemoteProfileUuids<a name="getRemoteProfileUuids"></a> 995 996getRemoteProfileUuids(deviceId: string, callback: AsyncCallback<Array<ProfileUuids>>): void 997 998获取对端蓝牙设备支持的Profile UUID。 999 1000**系统接口**:此接口为系统接口。 1001 1002**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1003 1004**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1005 1006**参数:** 1007 1008| 参数名 | 类型 | 必填 | 说明 | 1009| -------- | ------ | ---- | ----------------------------------- | 1010| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 1011| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 是 | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 | 1012 1013**错误码**: 1014 1015以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1016 1017| 错误码ID | 错误信息 | 1018| -------- | ---------------------------- | 1019|2900001 | Service stopped. | 1020|2900003 | Bluetooth switch is off. | 1021|2900099 | Operation failed. | 1022 1023**示例:** 1024 1025```js 1026import { BusinessError } from '@ohos.base'; 1027try { 1028 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => { 1029 console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 1030 }); 1031} catch (err) { 1032 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1033} 1034 1035``` 1036 1037 1038## connection.getRemoteProfileUuids<a name="getRemoteProfileUuids"></a> 1039 1040getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>> 1041 1042获取对端蓝牙设备支持的Profile UUID。 1043 1044**系统接口**:此接口为系统接口。 1045 1046**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1047 1048**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1049 1050**参数:** 1051 1052| 参数名 | 类型 | 必填 | 说明 | 1053| -------- | ------ | ---- | ----------------------------------- | 1054| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 1055 1056**返回值:** 1057 1058| 类型 | 说明 | 1059| ------------------- | ------------- | 1060| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 返回promise对象。 | 1061 1062**错误码**: 1063 1064以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1065 1066| 错误码ID | 错误信息 | 1067| -------- | ---------------------------- | 1068|2900001 | Service stopped. | 1069|2900003 | Bluetooth switch is off. | 1070|2900099 | Operation failed. | 1071 1072**示例:** 1073 1074```js 1075import { BusinessError } from '@ohos.base'; 1076try { 1077 connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { 1078 console.info('getRemoteProfileUuids'); 1079 }, (err: BusinessError) => { 1080 console.error('getRemoteProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1081 }); 1082} catch (err) { 1083 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1084} 1085``` 1086 1087 1088## connection.on('bluetoothDeviceFind') 1089 1090on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void 1091 1092订阅蓝牙设备发现上报事件。 1093 1094**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1095 1096**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1097 1098**参数:** 1099 1100| 参数名 | 类型 | 必填 | 说明 | 1101| -------- | ----------------------------------- | ---- | -------------------------------------- | 1102| type | string | 是 | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 | 1103| callback | Callback<Array<string>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | 1104 1105**错误码**: 1106 1107以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1108 1109| 错误码ID | 错误信息 | 1110| -------- | ---------------------------- | 1111|2900099 | Operation failed. | 1112 1113**示例:** 1114 1115```js 1116import { BusinessError } from '@ohos.base'; 1117function onReceiveEvent(data: Array<string>) { // data为蓝牙设备地址集合 1118 console.info('bluetooth device find = '+ JSON.stringify(data)); 1119} 1120try { 1121 connection.on('bluetoothDeviceFind', onReceiveEvent); 1122} catch (err) { 1123 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1124} 1125``` 1126 1127 1128## connection.off('bluetoothDeviceFind') 1129 1130off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void 1131 1132取消订阅蓝牙设备发现上报事件。 1133 1134**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1135 1136**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1137 1138**参数:** 1139 1140| 参数名 | 类型 | 必填 | 说明 | 1141| -------- | ----------------------------------- | ---- | ---------------------------------------- | 1142| type | string | 是 | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 | 1143| callback | Callback<Array<string>> | 否 | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1144 1145**错误码**: 1146 1147以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1148 1149| 错误码ID | 错误信息 | 1150| -------- | ---------------------------- | 1151|2900099 | Operation failed. | 1152 1153**示例:** 1154 1155```js 1156import { BusinessError } from '@ohos.base'; 1157function onReceiveEvent(data: Array<string>) { 1158 console.info('bluetooth device find = '+ JSON.stringify(data)); 1159} 1160try { 1161 connection.on('bluetoothDeviceFind', onReceiveEvent); 1162 connection.off('bluetoothDeviceFind', onReceiveEvent); 1163} catch (err) { 1164 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1165} 1166``` 1167 1168 1169## connection.on('bondStateChange') 1170 1171on(type: 'bondStateChange', callback: Callback<BondStateParam>): void 1172 1173订阅蓝牙配对状态改变事件。 1174 1175**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1176 1177**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1178 1179**参数:** 1180 1181| 参数名 | 类型 | 必填 | 说明 | 1182| -------- | ---------------------------------------- | ---- | ------------------------------------ | 1183| type | string | 是 | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 | 1184| callback | Callback<[BondStateParam](#BondStateParam)> | 是 | 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。 | 1185 1186**错误码**: 1187 1188以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1189 1190| 错误码ID | 错误信息 | 1191| -------- | ---------------------------- | 1192|2900099 | Operation failed. | 1193 1194**示例:** 1195 1196```js 1197import { BusinessError } from '@ohos.base'; 1198function onReceiveEvent(data: connection.BondStateParam) { // data为回调函数入参,表示配对的状态 1199 console.info('pair state = '+ JSON.stringify(data)); 1200} 1201try { 1202 connection.on('bondStateChange', onReceiveEvent); 1203} catch (err) { 1204 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1205} 1206``` 1207 1208 1209## connection.off('bondStateChange') 1210 1211off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void 1212 1213取消订阅蓝牙配对状态改变事件。 1214 1215**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1216 1217**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1218 1219**参数:** 1220 1221| 参数名 | 类型 | 必填 | 说明 | 1222| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1223| type | string | 是 | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 | 1224| callback | Callback<[BondStateParam](#BondStateParam)> | 否 | 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1225 1226**错误码**: 1227 1228以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1229 1230| 错误码ID | 错误信息 | 1231| -------- | ---------------------------- | 1232|2900099 | Operation failed. | 1233 1234**示例:** 1235 1236```js 1237import { BusinessError } from '@ohos.base'; 1238function onReceiveEvent(data: connection.BondStateParam) { 1239 console.info('bond state = '+ JSON.stringify(data)); 1240} 1241try { 1242 connection.on('bondStateChange', onReceiveEvent); 1243 connection.off('bondStateChange', onReceiveEvent); 1244} catch (err) { 1245 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1246} 1247``` 1248 1249 1250## connection.on('pinRequired') 1251 1252on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void 1253 1254订阅远端蓝牙设备的配对请求事件。 1255 1256**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1257 1258**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1259 1260**参数:** 1261 1262| 参数名 | 类型 | 必填 | 说明 | 1263| -------- | ---------------------------------------- | ---- | -------------------------------- | 1264| type | string | 是 | 填写"pinRequired"字符串,表示配对请求事件。 | 1265| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | 是 | 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。 | 1266 1267**错误码**: 1268 1269以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1270 1271| 错误码ID | 错误信息 | 1272| -------- | ---------------------------- | 1273|2900099 | Operation failed. | 1274 1275**示例:** 1276 1277```js 1278import { BusinessError } from '@ohos.base'; 1279function onReceiveEvent(data: connection.PinRequiredParam) { // data为配对请求参数 1280 console.info('pin required = '+ JSON.stringify(data)); 1281} 1282try { 1283 connection.on('pinRequired', onReceiveEvent); 1284} catch (err) { 1285 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1286} 1287``` 1288 1289 1290## connection.off('pinRequired') 1291 1292off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void 1293 1294取消订阅远端蓝牙设备的配对请求事件。 1295 1296**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1297 1298**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1299 1300**参数:** 1301 1302| 参数名 | 类型 | 必填 | 说明 | 1303| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1304| type | string | 是 | 填写"pinRequired"字符串,表示配对请求事件。 | 1305| callback | Callback<[PinRequiredParam](#pinrequiredparam)> | 否 | 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。 | 1306 1307**错误码**: 1308 1309以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 1310 1311| 错误码ID | 错误信息 | 1312| -------- | ---------------------------- | 1313|2900099 | Operation failed. | 1314 1315**示例:** 1316 1317```js 1318import { BusinessError } from '@ohos.base'; 1319function onReceiveEvent(data: connection.PinRequiredParam) { 1320 console.info('pin required = '+ JSON.stringify(data)); 1321} 1322try { 1323 connection.on('pinRequired', onReceiveEvent); 1324 connection.off('pinRequired', onReceiveEvent); 1325} catch (err) { 1326 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1327} 1328``` 1329 1330 1331## BondStateParam<a name="BondStateParam"></a> 1332 1333描述配对状态参数。 1334 1335**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1336 1337| 名称 | 类型 | 可读 | 可写 | 说明 | 1338| -------- | ------ | ---- | ---- | ----------- | 1339| deviceId | string | 是 | 否 | 表示要配对的设备ID。 | 1340| state | BondState | 是 | 否 | 表示配对设备的状态。 | 1341 1342 1343## PinRequiredParam<a name="PinRequiredParam"></a> 1344 1345描述配对请求参数。 1346 1347**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1348 1349| 名称 | 类型 | 可读 | 可写 | 说明 | 1350| -------- | ------ | ---- | ---- | ----------- | 1351| deviceId | string | 是 | 否 | 表示要配对的设备ID。 | 1352| pinCode | string | 是 | 否 | 表示要配对的密钥。 | 1353| pinType | [PinType](#pintype) | 是 | 否 | 表示要配对的设备类型。<br/>此接口为系统接口。 | 1354 1355 1356## DeviceClass<a name="DeviceClass"></a> 1357 1358描述蓝牙设备的类别。 1359 1360**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1361 1362| 名称 | 类型 | 可读 | 可写 | 说明 | 1363| --------------- | ----------------------------------- | ---- | ---- | ---------------- | 1364| majorClass | [MajorClass](js-apis-bluetooth-constant.md#majorclass) | 是 | 否 | 表示蓝牙设备主要类别的枚举。 | 1365| majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | 是 | 否 | 表示主要次要蓝牙设备类别的枚举。 | 1366| classOfDevice | number | 是 | 否 | 表示设备类别。 | 1367 1368 1369## BluetoothTransport<a name="BluetoothTransport"></a> 1370 1371枚举,表示设备类型。例如传统蓝牙设备或低功耗蓝牙设备,支持双模默认使用TRANSPORT_BR_EDR。 1372 1373**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1374 1375| 名称 | 值 | 说明 | 1376| -------------------------------- | ------ | --------------- | 1377| TRANSPORT_BR_EDR | 0 | 表示传统蓝牙(BR/EDR)设备。 | 1378| TRANSPORT_LE | 1 | 表示低功耗蓝牙(BLE)设备。 | 1379 1380 1381## ScanMode<a name="ScanMode"></a> 1382 1383枚举,扫描模式。 1384 1385**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1386 1387| 名称 | 值 | 说明 | 1388| ---------------------------------------- | ---- | --------------- | 1389| SCAN_MODE_NONE | 0 | 没有扫描模式。 | 1390| SCAN_MODE_CONNECTABLE | 1 | 可连接扫描模式。 | 1391| SCAN_MODE_GENERAL_DISCOVERABLE | 2 | general发现模式。 | 1392| SCAN_MODE_LIMITED_DISCOVERABLE | 3 | limited发现模式。 | 1393| SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | 可连接general发现模式。 | 1394| SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | 可连接limited发现模式。 | 1395 1396 1397## BondState<a name="BondState"></a> 1398 1399枚举,配对状态。 1400 1401**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1402 1403| 名称 | 值 | 说明 | 1404| ------------------ | ---- | ------ | 1405| BOND_STATE_INVALID | 0 | 无效的配对。 | 1406| BOND_STATE_BONDING | 1 | 正在配对。 | 1407| BOND_STATE_BONDED | 2 | 已配对。 | 1408 1409 1410## PinType<a name="PinType"></a> 1411 1412枚举,蓝牙配对类型。 1413 1414**系统接口:** 此接口为系统接口。 1415 1416**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1417 1418| 名称 | 值 | 说明 | 1419| -------------------------------- | ------ | --------------- | 1420| PIN_TYPE_ENTER_PIN_CODE | 0 | 用户需要输入对端设备上显示的PIN码。<br/>此接口为系统接口。 | 1421| PIN_TYPE_ENTER_PASSKEY | 1 | 用户需要输入对端设备上显示的PASSKEY。<br/>此接口为系统接口。 | 1422| PIN_TYPE_CONFIRM_PASSKEY | 2 | 用户需要确认本地设备上显示的PASSKEY。<br/>此接口为系统接口。 | 1423| PIN_TYPE_NO_PASSKEY_CONSENT | 3 | 无PASSKEY,用户需要接受或拒绝配对请求。<br/>此接口为系统接口。 | 1424| PIN_TYPE_NOTIFY_PASSKEY | 4 | 本地设备显示PASSKEY,用户需要在对端设备上输入该PASSKEY。<br/>此接口为系统接口。 | 1425| PIN_TYPE_DISPLAY_PIN_CODE | 5 | bluetooth 2.0设备,用户需要输入对端设备上显示的PIN码。<br/>此接口为系统接口。 | 1426| PIN_TYPE_OOB_CONSENT | 6 | 用户需要接受或拒绝OOB配对请求。<br/>此接口为系统接口。 | 1427| PIN_TYPE_PIN_16_DIGITS | 7 | 用户需要输入对端设备上显示的16位PIN码。<br/>此接口为系统接口。 |