1# @ohos.bluetooth.connection (蓝牙connection模块)(系统接口) 2 3connection模块提供了对蓝牙操作和管理的方法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.connection (蓝牙connection模块)](js-apis-bluetooth-connection.md) 9 10 11## 导入模块 12 13```js 14import { connection } from '@kit.ConnectivityKit'; 15``` 16 17 18## connection.pairCredibleDevice 19 20pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void 21 22向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。使用Callback异步回调。 23 24**系统接口**:此接口为系统接口。 25 26**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 27 28**系统能力**:SystemCapability.Communication.Bluetooth.Core。 29 30**参数:** 31 32| 参数名 | 类型 | 必填 | 说明 | 33| -------- | ------ | ---- | ----------------------------------- | 34| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 35| transport | [BluetoothTransport](js-apis-bluetooth-connection.md#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | 36| callback | AsyncCallback<void> | 是 | 回调函数。当发起配对成功,err为undefined,否则为错误对象。 | 37 38**错误码**: 39 40以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 41 42| 错误码ID | 错误信息 | 43| -------- | ---------------------------- | 44|201 | Permission denied. | 45|202 | Non-system applications are not allowed to use system APIs. | 46|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 47|801 | Capability not supported. | 48|2900001 | Service stopped. | 49|2900003 | Bluetooth disabled. | 50|2900099 | Operation failed. | 51 52**示例:** 53 54```js 55import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 56try { 57 connection.pairCredibleDevice('68:13:24:79:4C:8C', connection.BluetoothTransport 58 .TRANSPORT_BR_EDR, (err: BusinessError) => { 59 if (err) { 60 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 61 return; 62 } 63 console.info('pairCredibleDevice, err: ' + JSON.stringify(err)); 64 }); 65} catch (err) { 66 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 67} 68``` 69 70 71## connection.pairCredibleDevice 72 73pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void> 74 75向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。使用Promise异步回调。 76 77**系统接口**:此接口为系统接口。 78 79**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 80 81**系统能力**:SystemCapability.Communication.Bluetooth.Core。 82 83**参数:** 84 85| 参数名 | 类型 | 必填 | 说明 | 86| -------- | ------ | ---- | ----------------------------------- | 87| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 88| transport | [BluetoothTransport](js-apis-bluetooth-connection.md#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | 89 90**返回值:** 91 92| 类型 | 说明 | 93| ------------------------------------------------- | ------------------- | 94| Promise<void> | 返回promise对象。 | 95 96**错误码**: 97 98以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 99 100| 错误码ID | 错误信息 | 101| -------- | ---------------------------- | 102|201 | Permission denied. | 103|202 | Non-system applications are not allowed to use system APIs. | 104|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 105|801 | Capability not supported. | 106|2900001 | Service stopped. | 107|2900003 | Bluetooth disabled. | 108|2900099 | Operation failed. | 109 110**示例:** 111 112```js 113import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 114try { 115 connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => { 116 console.info('PairCredibleDevice'); 117 }, (err: BusinessError) => { 118 console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message); 119 }); 120} catch (err) { 121 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 122} 123``` 124 125 126## connection.cancelPairedDevice 127 128cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void 129 130删除配对的远程设备。使用Callback异步回调。 131 132**系统接口**:此接口为系统接口。 133 134**需要权限**:ohos.permission.ACCESS_BLUETOOTH 135 136**系统能力**:SystemCapability.Communication.Bluetooth.Core。 137 138**参数:** 139 140| 参数名 | 类型 | 必填 | 说明 | 141| -------- | ------ | ---- | ------------------------------------- | 142| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 143| callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | 144 145**错误码**: 146 147以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 148 149| 错误码ID | 错误信息 | 150| -------- | ---------------------------- | 151|201 | Permission denied. | 152|202 | Non-system applications are not allowed to use system APIs. | 153|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 154|801 | Capability not supported. | 155|2900001 | Service stopped. | 156|2900003 | Bluetooth disabled. | 157|2900099 | Operation failed. | 158 159**示例:** 160 161```js 162import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 163//callback 164try { 165 connection.cancelPairedDevice('11:22:33:44:55:66', (err: BusinessError) => { 166 console.info('cancelPairedDevice, device name err:' + JSON.stringify(err)); 167 }); 168} catch (err) { 169 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 170} 171``` 172 173 174## connection.cancelPairedDevice 175 176cancelPairedDevice(deviceId: string): Promise<void> 177 178删除配对的远程设备。使用Promise异步回调。 179 180**系统接口**:此接口为系统接口。 181 182**需要权限**:ohos.permission.ACCESS_BLUETOOTH 183 184**系统能力**:SystemCapability.Communication.Bluetooth.Core。 185 186**参数:** 187 188| 参数名 | 类型 | 必填 | 说明 | 189| -------- | ------ | ---- | ------------------------------------- | 190| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 191 192**返回值:** 193 194| 类型 | 说明 | 195| ------------------- | ------------- | 196| Promise<void> | 返回promise对象。 | 197 198**错误码**: 199 200以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 201 202| 错误码ID | 错误信息 | 203| -------- | ---------------------------- | 204|201 | Permission denied. | 205|202 | Non-system applications are not allowed to use system APIs. | 206|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 207|801 | Capability not supported. | 208|2900001 | Service stopped. | 209|2900003 | Bluetooth disabled. | 210|2900099 | Operation failed. | 211 212**示例:** 213 214```js 215import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 216//promise 217try { 218 connection.cancelPairedDevice('11:22:33:44:55:66').then(() => { 219 console.info('cancelPairedDevice'); 220 }, (error: BusinessError) => { 221 console.info('cancelPairedDevice: errCode:' + error.code + ',errMessage' + error.message); 222 }) 223 224} catch (err) { 225 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 226} 227``` 228 229 230## connection.cancelPairingDevice 231 232cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void 233 234删除正在配对中的远程设备。使用Callback异步回调。 235 236**系统接口**:此接口为系统接口。 237 238**需要权限**:ohos.permission.ACCESS_BLUETOOTH 239 240**系统能力**:SystemCapability.Communication.Bluetooth.Core。 241 242**参数:** 243 244| 参数名 | 类型 | 必填 | 说明 | 245| -------- | ------ | ---- | ------------------------------------- | 246| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 247| callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | 248 249**错误码**: 250 251以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 252 253| 错误码ID | 错误信息 | 254| -------- | ---------------------------- | 255|201 | Permission denied. | 256|202 | Non-system applications are not allowed to use system APIs. | 257|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 258|801 | Capability not supported. | 259|2900001 | Service stopped. | 260|2900003 | Bluetooth disabled. | 261|2900099 | Operation failed. | 262 263**示例:** 264 265```js 266import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 267try { 268 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 269} catch (err) { 270 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 271} 272``` 273 274 275## connection.cancelPairingDevice 276 277cancelPairingDevice(deviceId: string): Promise<void> 278 279删除正在配对中的远程设备。使用Promise异步回调。 280 281**系统接口**:此接口为系统接口。 282 283**需要权限**:ohos.permission.ACCESS_BLUETOOTH 284 285**系统能力**:SystemCapability.Communication.Bluetooth.Core。 286 287**参数:** 288 289| 参数名 | 类型 | 必填 | 说明 | 290| -------- | ------ | ---- | ------------------------------------- | 291| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 292 293**返回值:** 294 295| 类型 | 说明 | 296| ------------------- | ------------- | 297| Promise<void> | 返回promise对象。 | 298 299**错误码**: 300 301以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 302 303| 错误码ID | 错误信息 | 304| -------- | ---------------------------- | 305|201 | Permission denied. | 306|202 | Non-system applications are not allowed to use system APIs. | 307|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 308|801 | Capability not supported. | 309|2900001 | Service stopped. | 310|2900003 | Bluetooth disabled. | 311|2900099 | Operation failed. | 312 313**示例:** 314 315```js 316import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 317try { 318 connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); 319} catch (err) { 320 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 321} 322``` 323 324 325## connection.getLocalProfileUuids 326 327getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void 328 329获取本地设备的profile UUID。使用Callback异步回调。 330 331**系统接口**:此接口为系统接口。 332 333**需要权限**:ohos.permission.ACCESS_BLUETOOTH 334 335**系统能力**:SystemCapability.Communication.Bluetooth.Core。 336 337**参数:** 338 339| 参数名 | 类型 | 必填 | 说明 | 340| -------- | ------ | ---- | ----------------------------------- | 341| callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 是 | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 | 342 343**错误码**: 344 345以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 346 347| 错误码ID | 错误信息 | 348| -------- | ---------------------------- | 349|201 | Permission denied. | 350|202 | Non-system applications are not allowed to use system APIs. | 351|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 352|801 | Capability not supported. | 353|2900001 | Service stopped. | 354|2900003 | Bluetooth disabled. | 355|2900099 | Operation failed. | 356 357**示例:** 358 359```js 360import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 361try { 362 connection.getLocalProfileUuids((err: BusinessError, data: Array<connection.ProfileUuids>) => { 363 console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 364 }); 365} catch (err) { 366 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 367} 368``` 369 370 371## connection.getLocalProfileUuids 372 373getLocalProfileUuids(): Promise<Array<ProfileUuids>> 374 375获取本地设备的profile UUID。使用Promise异步回调。 376 377**系统接口**:此接口为系统接口。 378 379**需要权限**:ohos.permission.ACCESS_BLUETOOTH 380 381**系统能力**:SystemCapability.Communication.Bluetooth.Core。 382 383**返回值:** 384 385| 类型 | 说明 | 386| ------------------- | ------------- | 387| Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 返回promise对象。 | 388 389**错误码**: 390 391以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 392 393| 错误码ID | 错误信息 | 394| -------- | ---------------------------- | 395|201 | Permission denied. | 396|202 | Non-system applications are not allowed to use system APIs. | 397|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 398|801 | Capability not supported. | 399|2900001 | Service stopped. | 400|2900003 | Bluetooth disabled. | 401|2900099 | Operation failed. | 402 403**示例:** 404 405```js 406import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 407try { 408 connection.getLocalProfileUuids().then(() => { 409 console.info('getLocalProfileUuids'); 410 }, (err: BusinessError) => { 411 console.error('getLocalProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 412 }); 413} catch (err) { 414 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 415} 416``` 417 418 419## connection.connectAllowedProfiles<sup>11+</sup> 420 421connectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 422 423连接远端设备所有允许连接的profiles。使用Callback异步回调。 424 425**系统接口**:此接口为系统接口。 426 427**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 428 429**系统能力**:SystemCapability.Communication.Bluetooth.Core。 430 431**参数:** 432 433| 参数名 | 类型 | 必填 | 说明 | 434| -------- | ------ | ---- | ----------------------------------- | 435| deviceId | string | 是 | 表示连接的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 436| callback | AsyncCallback<void> | 是 | 以callback形式异步返回结果。当发起连接成功,err为undefined,否则为错误对象。 | 437 438**错误码**: 439 440以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 441 442| 错误码ID | 错误信息 | 443| -------- | ---------------------------- | 444|201 | Permission denied. | 445|202 | Non-system applications are not allowed to use system APIs. | 446|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 447|801 | Capability not supported. | 448|2900001 | Service stopped. | 449|2900003 | Bluetooth disabled. | 450|2900099 | Operation failed. | 451 452**示例:** 453 454```js 455import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 456try { 457 connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 458 if (err) { 459 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 460 return; 461 } 462 console.info('connectAllowedProfiles, err: ' + JSON.stringify(err)); 463 }); 464} catch (err) { 465 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 466} 467``` 468 469 470## connection.connectAllowedProfiles<sup>11+</sup> 471 472connectAllowedProfiles(deviceId: string): Promise<void> 473 474连接远端设备所有允许连接的profiles。使用Promise异步回调。 475 476**系统接口**:此接口为系统接口。 477 478**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 479 480**系统能力**:SystemCapability.Communication.Bluetooth.Core。 481 482**参数:** 483 484| 参数名 | 类型 | 必填 | 说明 | 485| -------- | ------ | ---- | ----------------------------------- | 486| deviceId | string | 是 | 表示连接的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 487 488**返回值:** 489 490| 类型 | 说明 | 491| ------------------------------------------------- | ------------------- | 492| Promise<void> | 以Promise形式返回连接profiles的结果,返回true为成功,false为失败。 | 493 494**错误码**: 495 496以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 497 498| 错误码ID | 错误信息 | 499| -------- | ---------------------------- | 500|201 | Permission denied. | 501|202 | Non-system applications are not allowed to use system APIs. | 502|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 503|801 | Capability not supported. | 504|2900001 | Service stopped. | 505|2900003 | Bluetooth disabled. | 506|2900099 | Operation failed. | 507 508**示例:** 509 510```js 511import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 512try { 513 connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 514 console.info('connectAllowedProfiles'); 515 }, (err: BusinessError) => { 516 console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 517 }); 518} catch (err) { 519 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 520} 521``` 522 523 524## connection.disconnectAllowedProfiles<sup>11+</sup> 525 526disconnectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 527 528断开远端设备所有连接的profiles。使用Callback异步回调。 529 530**系统接口**:此接口为系统接口。 531 532**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 533 534**系统能力**:SystemCapability.Communication.Bluetooth.Core。 535 536**参数:** 537 538| 参数名 | 类型 | 必填 | 说明 | 539| -------- | ------ | ---- | ----------------------------------- | 540| deviceId | string | 是 | 表示断开的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 541| callback | AsyncCallback<void> | 是 | 以callback形式异步返回结果。当发起断开成功,err为undefined,否则为错误对象。 | 542 543**错误码**: 544 545以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 546 547| 错误码ID | 错误信息 | 548| -------- | ---------------------------- | 549|201 | Permission denied. | 550|202 | Non-system applications are not allowed to use system APIs. | 551|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 552|801 | Capability not supported. | 553|2900001 | Service stopped. | 554|2900003 | Bluetooth disabled. | 555|2900099 | Operation failed. | 556 557**示例:** 558 559```js 560import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 561try { 562 connection.disconnectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { 563 if (err) { 564 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 565 return; 566 } 567 console.info('disconnectAllowedProfiles, err: ' + JSON.stringify(err)); 568 }); 569} catch (err) { 570 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 571} 572``` 573 574 575## connection.disconnectAllowedProfiles<sup>11+</sup> 576 577disconnectAllowedProfiles(deviceId: string): Promise<void> 578 579断开远端设备所有连接的profiles。使用Promise异步回调。 580 581**系统接口**:此接口为系统接口。 582 583**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 584 585**系统能力**:SystemCapability.Communication.Bluetooth.Core。 586 587**参数:** 588 589| 参数名 | 类型 | 必填 | 说明 | 590| -------- | ------ | ---- | ----------------------------------- | 591| deviceId | string | 是 | 表示断开的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 592 593**返回值:** 594 595| 类型 | 说明 | 596| ------------------------------------------------- | ------------------- | 597| Promise<void> | 以Promise形式返回断开profiles的结果,返回true为成功,false为失败。 | 598 599**错误码**: 600 601以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 602 603| 错误码ID | 错误信息 | 604| -------- | ---------------------------- | 605|201 | Permission denied. | 606|202 | Non-system applications are not allowed to use system APIs. | 607|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 608|801 | Capability not supported. | 609|2900001 | Service stopped. | 610|2900003 | Bluetooth disabled. | 611|2900099 | Operation failed. | 612 613**示例:** 614 615```js 616import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 617try { 618 connection.disconnectAllowedProfiles('68:13:24:79:4C:8C').then(() => { 619 console.info('disconnectAllowedProfiles'); 620 }, (err: BusinessError) => { 621 console.error('disconnectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); 622 }); 623} catch (err) { 624 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 625} 626``` 627 628 629## connection.getRemoteProductId<sup>11+</sup> 630 631getRemoteProductId(deviceId: string): string 632 633获取对端蓝牙设备的Product ID。 634 635**系统接口**:此接口为系统接口。 636 637**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 638 639**系统能力**:SystemCapability.Communication.Bluetooth.Core。 640 641**参数:** 642 643| 参数名 | 类型 | 必填 | 说明 | 644| -------- | ------ | ---- | --------------------------------- | 645| deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 646 647**返回值:** 648 649| 类型 | 说明 | 650| ------ | ------------- | 651| string | 以字符串格式返回设备Product ID。 | 652 653**错误码**: 654 655以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 656 657| 错误码ID | 错误信息 | 658| -------- | ---------------------------- | 659|201 | Permission denied. | 660|202 | Non-system applications are not allowed to use system APIs. | 661|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 662|801 | Capability not supported. | 663|2900001 | Service stopped. | 664|2900003 | Bluetooth disabled. | 665|2900099 | Operation failed. | 666 667**示例:** 668 669```js 670try { 671 let remoteDeviceProductId = connection.getRemoteProductId('XX:XX:XX:XX:XX:XX'); 672} catch (err) { 673 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 674} 675``` 676 677## connection.on('discoveryResult')<sup>12+</sup> 678 679on(type: 'discoveryResult', callback: Callback<Array<DiscoveryResult>>): void 680 681订阅蓝牙设备发现上报事件。使用Callback异步回调。 682 683**系统接口**:此接口为系统接口。 684 685**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.GET_BLUETOOTH_PEERS_MAC 686 687**系统能力**:SystemCapability.Communication.Bluetooth.Core。 688 689**参数:** 690 691| 参数名 | 类型 | 必填 | 说明 | 692| -------- | ----------------------------------- | ---- | -------------------------------------- | 693| type | string | 是 | 填写"discoveryResult"字符串,表示蓝牙设备发现事件。 | 694| callback | Callback<Array<[DiscoveryResult](#discoveryresult12)>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | 695 696**错误码**: 697 698以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 699 700| 错误码ID | 错误信息 | 701| -------- | ---------------------------- | 702|201 | Permission denied. | 703|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 704|801 | Capability not supported. | 705|2900099 | Operation failed. | 706 707**示例:** 708 709```js 710import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 711let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data为蓝牙设备扫描结果集合 712 console.info('bluetooth device find = '+ JSON.stringify(data)); 713} 714try { 715 connection.on('discoveryResult', onReceiveEvent); 716} catch (err) { 717 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 718} 719``` 720 721 722## connection.off('discoveryResult')<sup>12+</sup> 723 724off(type: 'discoveryResult', callback?: Callback<Array<DiscoveryResult>>): void 725 726取消订阅蓝牙设备发现上报事件。 727 728**系统接口**:此接口为系统接口。 729 730**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.GET_BLUETOOTH_PEERS_MAC 731 732**系统能力**:SystemCapability.Communication.Bluetooth.Core。 733 734**参数:** 735 736| 参数名 | 类型 | 必填 | 说明 | 737| -------- | ----------------------------------- | ---- | ---------------------------------------- | 738| type | string | 是 | 填写"discoveryResult"字符串,表示蓝牙设备发现事件。 | 739| callback | Callback<Array<[DiscoveryResult](#discoveryresult12)>> | 否 | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | 740 741**错误码**: 742 743以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 744 745| 错误码ID | 错误信息 | 746| -------- | ---------------------------- | 747|201 | Permission denied. | 748|801 | Capability not supported. | 749|2900099 | Operation failed. | 750 751**示例:** 752 753```js 754import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 755let onReceiveEvent: (data: Array<connection.DiscoveryResult>) => void = (data: Array<connection.DiscoveryResult>) => { // data为蓝牙设备扫描结果集合 756 console.info('bluetooth device find = '+ JSON.stringify(data)); 757} 758try { 759 connection.on('discoveryResult', onReceiveEvent); 760 connection.off('discoveryResult', onReceiveEvent); 761} catch (err) { 762 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 763} 764``` 765 766 767## connection.setRemoteDeviceType<sup>12+</sup> 768 769setRemoteDeviceType(deviceId: string, type: DeviceType): Promise<void> 770 771设置蓝牙远端设备自定义类型。使用Promise异步回调。 772 773**系统接口**:此接口为系统接口。 774 775**需要权限**:ohos.permission.ACCESS_BLUETOOTH 776 777**系统能力**:SystemCapability.Communication.Bluetooth.Core。 778 779**参数:** 780 781| 参数名 | 类型 | 必填 | 说明 | 782| ------ | ------- | ---- | -------------------------------- | 783| deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | 784| type | [DeviceType](#devicetype12) | 是 | 表示设备类型。 | 785 786**返回值:** 787 788| 类型 | 说明 | 789| ------------------- | ------------- | 790| Promise<void> | 以Promise形式返回设置蓝牙远端设备类型的结果,设置失败时返回错误码信息。 | 791 792**错误码**: 793 794以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 795 796| 错误码ID | 错误信息 | 797| -------- | ---------------------------- | 798|201 | Permission denied. | 799|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 800|2900001 | Service stopped. | 801|2900003 | Bluetooth disabled. | 802 803**示例:** 804 805```js 806import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 807//promise 808try { 809 connection.setRemoteDeviceType('11:22:33:44:55:66', connection.DeviceType.DEVICE_TYPE_HEADSET).then(() => { 810 console.info('setRemoteDeviceType success'); 811 }); 812} catch (err) { 813 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 814} 815``` 816 817 818## connection.getRemoteDeviceType<sup>12+</sup> 819 820getRemoteDeviceType(deviceId: string): Promise<DeviceType> 821 822获取蓝牙远端设备自定义类型。使用Promise异步回调。 823 824**系统接口**:此接口为系统接口。 825 826**需要权限**:ohos.permission.ACCESS_BLUETOOTH 827 828**系统能力**:SystemCapability.Communication.Bluetooth.Core。 829 830**参数:** 831 832| 参数名 | 类型 | 必填 | 说明 | 833| ------ | ------- | ---- | -------------------------------- | 834| deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | 835 836**返回值:** 837 838| 类型 | 说明 | 839| ------------------- | ------------- | 840| Promise<[DeviceType](#devicetype12)> | 以Promise形式返回设置蓝牙远端设备类型的结果,返回值为设备类型。 | 841 842**错误码**: 843 844以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 845 846| 错误码ID | 错误信息 | 847| -------- | ---------------------------- | 848|201 | Permission denied. | 849|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 850|2900001 | Service stopped. | 851|2900003 | Bluetooth disabled. | 852 853**示例:** 854 855```js 856import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 857//promise 858try { 859 connection.getRemoteDeviceType('11:22:33:44:55:66').then((data: connection.DeviceType) => { 860 console.info('getRemoteDeviceType success, DeviceType:' + JSON.stringify(data)); 861 }); 862} catch (err) { 863 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 864} 865``` 866 867 868## PinRequiredParam 869 870描述配对请求参数。 871 872**系统能力**:SystemCapability.Communication.Bluetooth.Core。 873 874| 名称 | 类型 | 可读 | 可写 | 说明 | 875| -------- | ------ | ---- | ---- | ----------- | 876| pinType | [PinType](#pintype) | 是 | 否 | 表示要配对的设备类型。<br/>此接口为系统接口。 | 877 878## PinType 879 880枚举,蓝牙配对类型。 881 882**系统接口:** 此接口为系统接口。 883 884**系统能力**:SystemCapability.Communication.Bluetooth.Core。 885 886| 名称 | 值 | 说明 | 887| -------------------------------- | ------ | --------------- | 888| PIN_TYPE_ENTER_PIN_CODE | 0 | 用户需要输入对端设备上显示的PIN码。<br/>此接口为系统接口。 | 889| PIN_TYPE_ENTER_PASSKEY | 1 | 用户需要输入对端设备上显示的PASSKEY。<br/>此接口为系统接口。 | 890| PIN_TYPE_CONFIRM_PASSKEY | 2 | 用户需要确认本地设备上显示的PASSKEY。<br/>此接口为系统接口。 | 891| PIN_TYPE_NO_PASSKEY_CONSENT | 3 | 无PASSKEY,用户需要接受或拒绝配对请求。<br/>此接口为系统接口。 | 892| PIN_TYPE_NOTIFY_PASSKEY | 4 | 本地设备显示PASSKEY,用户需要在对端设备上输入该PASSKEY。<br/>此接口为系统接口。 | 893| PIN_TYPE_DISPLAY_PIN_CODE | 5 | bluetooth 2.0设备,用户需要输入对端设备上显示的PIN码。<br/>此接口为系统接口。 | 894| PIN_TYPE_OOB_CONSENT | 6 | 用户需要接受或拒绝OOB配对请求。<br/>此接口为系统接口。 | 895| PIN_TYPE_PIN_16_DIGITS | 7 | 用户需要输入对端设备上显示的16位PIN码。<br/>此接口为系统接口。 | 896 897 898## DiscoveryResult<sup>12+</sup> 899 900描述扫描设备状态参数。 901 902**系统接口:** 此接口为系统接口。 903 904**系统能力**:SystemCapability.Communication.Bluetooth.Core。 905 906| 名称 | 类型 | 可读 | 可写 | 说明 | 907| -------- | ------ | ---- | ---- | ----------- | 908| deviceId<sup>12+</sup> | string | 是 | 否 | 表示扫描到的设备ID。<br/>此接口为系统接口。 | 909| rssi<sup>12+</sup> | number | 是 | 否 | 表示扫描到的设备的信号强度。<br/>此接口为系统接口。 | 910| deviceName<sup>12+</sup> | string | 是 | 否 | 表示扫描到的设备的设备名称。<br/>此接口为系统接口。 | 911| deviceClass<sup>12+</sup> | DeviceClass | 是 | 否 | 表示扫描到的设备的设备类别。<br/>此接口为系统接口。 | 912 913 914## DeviceType<sup>12+</sup> 915 916枚举,蓝牙远程设备的自定义类型。 917 918**系统接口:** 此接口为系统接口。 919 920**系统能力**:SystemCapability.Communication.Bluetooth.Core。 921 922| 名称 | 值 | 说明 | 923| -------------------------------- | ------ | --------------- | 924| DEVICE_TYPE_DEFAULT<sup>12+</sup> | 0 | 默认设备类型,与原类型一致。<br/>此接口为系统接口。 | 925| DEVICE_TYPE_CAR<sup>12+</sup> | 1 | 汽车。<br/>此接口为系统接口。 | 926| DEVICE_TYPE_HEADSET<sup>12+</sup> | 2 | 耳机。<br/>此接口为系统接口。 | 927| DEVICE_TYPE_HEARING<sup>12+</sup> | 3 | 助听器<br/>此接口为系统接口。 | 928| DEVICE_TYPE_GLASSES<sup>12+</sup> | 4 | 眼镜。<br/>此接口为系统接口。 | 929| DEVICE_TYPE_WATCH<sup>12+</sup> | 5 | 手表。<br/>此接口为系统接口。 | 930| DEVICE_TYPE_SPEAKER<sup>12+</sup> | 6 | 音响。<br/>此接口为系统接口。 | 931| DEVICE_TYPE_OTHERS<sup>12+</sup> | 7 | 其他设备。<br/>此接口为系统接口。 | 932 933 934## BatteryInfo<sup>12+</sup> 935 936描述电量信息的内容。 937 938**系统能力**:SystemCapability.Communication.Bluetooth.Core。 939 940| 名称 | 类型 | 可读 | 可写 | 说明 | 941| -------- | ------ | ---- | ---- | ----------- | 942| deviceId | string | 是 | 否 | 表示远端设备的MAC地址。<br/>此接口为系统接口。 | 943 944