1# @ohos.bluetooth.a2dp (蓝牙a2dp模块)(系统接口) 2 3<!--Kit: Connectivity Kit--> 4<!--Subsystem: Communication--> 5<!--Owner: @enjoy_sunshine--> 6<!--Designer: @chengguohong; @tangjia15--> 7<!--Tester: @wangfeng517--> 8<!--Adviser: @zhang_yixin13--> 9 10a2dp模块提供了访问蓝牙音频接口的方法。 11 12> **说明:** 13> 14> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.a2dp (蓝牙a2dp模块)](js-apis-bluetooth-a2dp.md)。 16 17 18## 导入模块 19 20```js 21import { a2dp } from '@kit.ConnectivityKit'; 22``` 23 24 25### connect 26 27connect(deviceId: string): void 28 29发起设备的A2dp服务连接请求。 30 31**系统接口**:此接口为系统接口。 32 33**需要权限**:ohos.permission.ACCESS_BLUETOOTH 34 35**系统能力**:SystemCapability.Communication.Bluetooth.Core 36 37**参数:** 38 39| 参数名 | 类型 | 必填 | 说明 | 40| ------ | ------ | ---- | ------- | 41| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 42 43**错误码**: 44 45以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 46 47| 错误码ID | 错误信息 | 48| -------- | ---------------------------- | 49|201 | Permission denied. | 50|202 | Non-system applications are not allowed to use system APIs. | 51|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 52|801 | Capability not supported. | 53|2900001 | Service stopped. | 54|2900003 | Bluetooth disabled. | 55|2900004 | Profile not supported. | 56|2900099 | Operation failed. | 57 58**示例:** 59 60```js 61import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 62try { 63 let a2dpSrc = a2dp.createA2dpSrcProfile(); 64 a2dpSrc.connect('XX:XX:XX:XX:XX:XX'); 65} catch (err) { 66 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 67} 68``` 69 70 71### disconnect 72 73disconnect(deviceId: string): void 74 75断开设备的a2dp服务连接。 76 77**系统接口**:此接口为系统接口。 78 79**需要权限**:ohos.permission.ACCESS_BLUETOOTH 80 81**系统能力**:SystemCapability.Communication.Bluetooth.Core 82 83**参数:** 84 85| 参数名 | 类型 | 必填 | 说明 | 86| ------ | ------ | ---- | ------- | 87| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 88 89**错误码**: 90 91以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 92 93| 错误码ID | 错误信息 | 94| -------- | ---------------------------- | 95|201 | Permission denied. | 96|202 | Non-system applications are not allowed to use system APIs. | 97|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 98|801 | Capability not supported. | 99|2900001 | Service stopped. | 100|2900003 | Bluetooth disabled. | 101|2900004 | Profile not supported. | 102|2900099 | Operation failed. | 103 104**示例:** 105 106```js 107import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 108try { 109 let a2dpSrc = a2dp.createA2dpSrcProfile(); 110 a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX'); 111} catch (err) { 112 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 113} 114``` 115 116 117### isAbsoluteVolumeSupported<sup>11+</sup> 118 119isAbsoluteVolumeSupported(deviceId: string, callback: AsyncCallback<boolean>): void 120 121获取设备是否支持绝对音量能力。使用Callback异步回调。 122 123**系统接口**:此接口为系统接口。 124 125**需要权限**:ohos.permission.ACCESS_BLUETOOTH 126 127**系统能力**:SystemCapability.Communication.Bluetooth.Core 128 129**参数:** 130 131| 参数名 | 类型 | 必填 | 说明 | 132| ------ | ------ | ---- | ------- | 133| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 134| callback | AsyncCallback<boolean> | 是 | 回调函数。当接口调用返回成功,err为undefined,data为获取到的设备是否支持绝对音量结果;否则为错误对象。 | 135 136 137**错误码**: 138 139以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 140 141| 错误码ID | 错误信息 | 142| -------- | ---------------------------- | 143|201 | Permission denied. | 144|202 | Non-system applications are not allowed to use system APIs. | 145|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 146|801 | Capability not supported. | 147|2900001 | Service stopped. | 148|2900003 | Bluetooth disabled. | 149|2900099 | Operation failed. | 150 151**示例:** 152 153```js 154import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 155try { 156 let a2dpSrc = a2dp.createA2dpSrcProfile(); 157 a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX', (err, supported) => { 158 console.info('device support absolute volume ' + supported); 159 }); 160} catch (err) { 161 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 162} 163``` 164 165### isAbsoluteVolumeSupported<sup>11+</sup> 166 167isAbsoluteVolumeSupported(deviceId: string): Promise<boolean> 168 169获取设备是否支持绝对音量能力。使用Promise异步回调。 170 171**系统接口**:此接口为系统接口。 172 173**需要权限**:ohos.permission.ACCESS_BLUETOOTH 174 175**系统能力**:SystemCapability.Communication.Bluetooth.Core 176 177**参数:** 178 179| 参数名 | 类型 | 必填 | 说明 | 180| ------ | ------ | ---- | ------- | 181| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 182 183**返回值:** 184 185| 类型 | 说明 | 186| ----------------------------- | ---------- | 187| Promise<boolean> | Promise对象。返回true表示设备支持绝对音量能力;返回false表示设备不支持绝对音量能力。 | 188 189**错误码**: 190 191以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 192 193| 错误码ID | 错误信息 | 194| -------- | ---------------------------- | 195|201 | Permission denied. | 196|202 | Non-system applications are not allowed to use system APIs. | 197|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 198|801 | Capability not supported. | 199|2900001 | Service stopped. | 200|2900003 | Bluetooth disabled. | 201|2900099 | Operation failed. | 202 203**示例:** 204 205```js 206import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 207try { 208 let a2dpSrc = a2dp.createA2dpSrcProfile(); 209 a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX').then((supported) => { 210 console.info('device support absolute volume ' + supported); 211 }); 212} catch (err) { 213 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 214} 215``` 216 217### isAbsoluteVolumeEnabled<sup>11+</sup> 218 219isAbsoluteVolumeEnabled(deviceId: string, callback: AsyncCallback<boolean>): void 220 221获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。使用Callback异步回调。 222 223**系统接口**:此接口为系统接口。 224 225**需要权限**:ohos.permission.ACCESS_BLUETOOTH 226 227**系统能力**:SystemCapability.Communication.Bluetooth.Core 228 229**参数:** 230 231| 参数名 | 类型 | 必填 | 说明 | 232| ------ | ------ | ---- | ------- | 233| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 234| callback | AsyncCallback<boolean> | 是 | 回调函数。当接口调用返回成功,err为undefined,data为获取到的绝对音量能力开启结果,true表示设备支持绝对音量能力,返回false表示设备不支持绝对音量能力;否则为错误对象。 | 235 236 237**错误码**: 238 239以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 240 241| 错误码ID | 错误信息 | 242| -------- | ---------------------------- | 243|201 | Permission denied. | 244|202 | Non-system applications are not allowed to use system APIs. | 245|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 246|801 | Capability not supported. | 247|2900001 | Service stopped. | 248|2900003 | Bluetooth disabled. | 249|2900099 | Operation failed. | 250 251**示例:** 252 253```js 254import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 255try { 256 let a2dpSrc = a2dp.createA2dpSrcProfile(); 257 a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => { 258 console.info('device absolute volume enable ' + enabled); 259 }); 260} catch (err) { 261 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 262} 263``` 264 265### isAbsoluteVolumeEnabled<sup>11+</sup> 266 267isAbsoluteVolumeEnabled(deviceId: string): Promise<boolean> 268 269获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。使用Promise异步回调。 270 271**系统接口**:此接口为系统接口。 272 273**需要权限**:ohos.permission.ACCESS_BLUETOOTH 274 275**系统能力**:SystemCapability.Communication.Bluetooth.Core 276 277**参数:** 278 279| 参数名 | 类型 | 必填 | 说明 | 280| ------ | ------ | ---- | ------- | 281| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 282 283**返回值:** 284 285| 类型 | 说明 | 286| ----------------------------- | ---------- | 287| Promise<boolean> | Promise对象。返回true表示设备绝对音量能力开启;返回false表示设备绝对音量能力未开启。 | 288 289**错误码**: 290 291以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 292 293| 错误码ID | 错误信息 | 294| -------- | ---------------------------- | 295|201 | Permission denied. | 296|202 | Non-system applications are not allowed to use system APIs. | 297|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 298|801 | Capability not supported. | 299|2900001 | Service stopped. | 300|2900003 | Bluetooth disabled. | 301|2900099 | Operation failed. | 302 303**示例:** 304 305```js 306import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 307try { 308 let a2dpSrc = a2dp.createA2dpSrcProfile(); 309 a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => { 310 console.info('device absolute volume enable ' + enabled); 311 }); 312} catch (err) { 313 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 314} 315``` 316 317### enableAbsoluteVolume<sup>11+</sup> 318 319enableAbsoluteVolume(deviceId: string, callback: AsyncCallback<void>): void 320 321开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。使用Callback异步回调。 322 323**系统接口**:此接口为系统接口。 324 325**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 326 327**系统能力**:SystemCapability.Communication.Bluetooth.Core 328 329**参数:** 330 331| 参数名 | 类型 | 必填 | 说明 | 332| ------ | ------ | ---- | ------- | 333| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 334| callback | AsyncCallback<void> | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 | 335 336 337**错误码**: 338 339以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 340 341| 错误码ID | 错误信息 | 342| -------- | ---------------------------- | 343|201 | Permission denied. | 344|202 | Non-system applications are not allowed to use system APIs. | 345|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 346|801 | Capability not supported. | 347|2900001 | Service stopped. | 348|2900003 | Bluetooth disabled. | 349|2900099 | Operation failed. | 350 351**示例:** 352 353```js 354import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 355try { 356 let a2dpSrc = a2dp.createA2dpSrcProfile(); 357 a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => { 358 if (err) { 359 console.error("enableAbsoluteVolume error"); 360 } 361 }); 362} catch (err) { 363 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 364} 365``` 366 367### enableAbsoluteVolume<sup>11+</sup> 368 369enableAbsoluteVolume(deviceId: string): Promise<void> 370 371开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。使用Promise异步回调。 372 373**系统接口**:此接口为系统接口。 374 375**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 376 377**系统能力**:SystemCapability.Communication.Bluetooth.Core 378 379**参数:** 380 381| 参数名 | 类型 | 必填 | 说明 | 382| ------ | ------ | ---- | ------- | 383| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 384 385**返回值:** 386 387| 类型 | 说明 | 388| ----------------------------- | ---------- | 389| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 390 391**错误码**: 392 393以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 394 395| 错误码ID | 错误信息 | 396| -------- | ---------------------------- | 397|201 | Permission denied. | 398|202 | Non-system applications are not allowed to use system APIs. | 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 { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 409try { 410 let a2dpSrc = a2dp.createA2dpSrcProfile(); 411 a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => { 412 console.info("enableAbsoluteVolume"); 413 } 414 ); 415} catch (err) { 416 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 417} 418``` 419 420### disableAbsoluteVolume<sup>11+</sup> 421 422disableAbsoluteVolume(deviceId: string, callback: AsyncCallback<void>): void 423 424关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。使用Callback异步回调。 425 426**系统接口**:此接口为系统接口。 427 428**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 429 430**系统能力**:SystemCapability.Communication.Bluetooth.Core 431 432**参数:** 433 434| 参数名 | 类型 | 必填 | 说明 | 435| ------ | ------ | ---- | ------- | 436| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 437| callback | AsyncCallback<void> | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 | 438 439 440**错误码**: 441 442以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 443 444| 错误码ID | 错误信息 | 445| -------- | ---------------------------- | 446|201 | Permission denied. | 447|202 | Non-system applications are not allowed to use system APIs. | 448|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 449|801 | Capability not supported. | 450|2900001 | Service stopped. | 451|2900003 | Bluetooth disabled. | 452|2900099 | Operation failed. | 453 454**示例:** 455 456```js 457import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 458try { 459 let a2dpSrc = a2dp.createA2dpSrcProfile(); 460 a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => { 461 if (err) { 462 console.error("disableAbsoluteVolume error"); 463 } 464 }); 465} catch (err) { 466 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 467} 468``` 469 470### disableAbsoluteVolume<sup>11+</sup> 471 472disableAbsoluteVolume(deviceId: string): Promise<void> 473 474关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。使用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的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 493 494**错误码**: 495 496以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](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 let a2dpSrc = a2dp.createA2dpSrcProfile(); 514 a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => { 515 console.info("disableAbsoluteVolume"); 516 }); 517} catch (err) { 518 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 519} 520``` 521 522### getCurrentCodecInfo<sup>11+</sup> 523 524getCurrentCodecInfo(deviceId: string): CodecInfo 525 526获取当前编码器信息。 527 528**系统接口**:此接口为系统接口。 529 530**需要权限**:ohos.permission.ACCESS_BLUETOOTH 531 532**系统能力**:SystemCapability.Communication.Bluetooth.Core 533 534**参数:** 535 536| 参数名 | 类型 | 必填 | 说明 | 537| ------ | ------ | ---- | ------- | 538| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 539 540**返回值:** 541 542| 类型 | 说明 | 543| ----------------------------- | ---------- | 544| [CodecInfo](js-apis-bluetooth-a2dp.md#codecinfo11)| 当前编码器信息。 | 545 546**错误码**: 547 548以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 549 550| 错误码ID | 错误信息 | 551| -------- | ---------------------------- | 552|201 | Permission denied. | 553|202 | Non-system applications are not allowed to use system APIs. | 554|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 555|801 | Capability not supported. | 556|2900001 | Service stopped. | 557|2900003 | Bluetooth disabled. | 558|2900099 | Operation failed. | 559 560**示例:** 561 562```js 563import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 564try { 565 let a2dpSrc = a2dp.createA2dpSrcProfile(); 566 let codecInfo : a2dp.CodecInfo = a2dpSrc.getCurrentCodecInfo('XX:XX:XX:XX:XX:XX'); 567} catch (err) { 568 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 569} 570``` 571 572### getCurrentFullCodecInfo<sup>19+</sup> 573 574getCurrentFullCodecInfo(deviceId: string): CodecInfoList[] 575 576获取当前设备支持的全量编码器能力集合。 577 578**系统接口**:此接口为系统接口。 579 580**需要权限**:ohos.permission.ACCESS_BLUETOOTH 581 582**系统能力**:SystemCapability.Communication.Bluetooth.Core 583 584**参数:** 585 586| 参数名 | 类型 | 必填 | 说明 | 587| ------ | ------ | ---- | ------- | 588| deviceId | string | 是 | 对端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 589 590**返回值:** 591 592| 类型 | 说明 | 593| ----------------------------- | ---------- | 594| [CodecInfoList](js-apis-bluetooth-a2dp.md#codecinfolist19)[]| 当前设备支持的编码器能力集合。 | 595 596**错误码**: 597 598以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 599 600| 错误码ID | 错误信息 | 601| -------- | ---------------------------- | 602|201 | Permission denied. | 603|202 | Non-system applications are not allowed to use system APIs. | 604|801 | Capability not supported. | 605|2900001 | Service stopped. | 606|2900003 | Bluetooth disabled. | 607|2900099 | Operation failed. | 608|2902008 | Current device is not an active device. | 609 610**示例:** 611 612```js 613import { BusinessError } from '@kit.BasicServicesKit'; 614try { 615 let a2dpSrc = a2dp.createA2dpSrcProfile(); 616 let codecInfoList : a2dp.CodecInfoList[] = a2dpSrc.getCurrentFullCodecInfo('XX:XX:XX:XX:XX:XX'); 617} catch (err) { 618 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 619} 620``` 621 622### setCurrentCodecInfo<sup>11+</sup> 623 624setCurrentCodecInfo(deviceId: string, codecInfo: CodecInfo): void 625 626设置当前编码器信息。 627 628**系统接口**:此接口为系统接口。 629 630**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 631 632**系统能力**:SystemCapability.Communication.Bluetooth.Core 633 634**参数:** 635 636| 参数名 | 类型 | 必填 | 说明 | 637| ------ | ------ | ---- | ------- | 638| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 639| codecInfo | [CodecInfo](js-apis-bluetooth-a2dp.md#codecinfo11) | 是 | 编码器信息。 | 640 641**错误码**: 642 643以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 644 645| 错误码ID | 错误信息 | 646| -------- | ---------------------------- | 647|201 | Permission denied. | 648|202 | Non-system applications are not allowed to use system APIs. | 649|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 650|801 | Capability not supported. | 651|2900001 | Service stopped. | 652|2900003 | Bluetooth disabled. | 653|2900099 | Operation failed. | 654 655**示例:** 656 657```js 658import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 659try { 660 let a2dpSrc = a2dp.createA2dpSrcProfile(); 661 let codecInfo : a2dp.CodecInfo = { 662 codecType: 0, 663 codecBitsPerSample: 1, 664 codecChannelMode: 2, 665 codecSampleRate: 1 666 } 667 a2dpSrc.setCurrentCodecInfo('XX:XX:XX:XX:XX:XX', codecInfo); 668} catch (err) { 669 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 670} 671``` 672 673 674### disableAutoPlay<sup>12+</sup> 675 676disableAutoPlay(deviceId: string, duration: number): Promise<void> 677 678限制设备在连接成功的若干毫秒内播放音乐。使用Promise异步回调。 679 680**系统接口**:此接口为系统接口。 681 682**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 683 684**系统能力**:SystemCapability.Communication.Bluetooth.Core 685 686**参数:** 687 688| 参数名 | 类型 | 必填 | 说明 | 689| ------ | ------ | ---- | ------- | 690| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 691| duration | number | 是 | 拦截时长,取值范围:[3000, 20000],单位:ms。 | 692 693**返回值:** 694 695| 类型 | 说明 | 696| ----------------------------- | ---------- | 697| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 698 699**错误码**: 700 701以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 702 703| 错误码ID | 错误信息 | 704| -------- | ---------------------------- | 705|201 | Permission denied. | 706|202 | Non-system applications are not allowed to use system APIs. | 707|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 708|801 | Capability not supported. | 709|2900001 | Service stopped. | 710|2900003 | Bluetooth disabled. | 711|2900099 | Operation failed. | 712 713**示例:** 714 715```js 716import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 717try { 718 let a2dpSrc = a2dp.createA2dpSrcProfile(); 719 let durationNumber = 1000; 720 a2dpSrc.disableAutoPlay('XX:XX:XX:XX:XX:XX', durationNumber).then(() => { 721 console.info("disableAutoPlay"); 722 }); 723} catch (err) { 724 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 725} 726``` 727 728 729### enableAutoPlay<sup>12+</sup> 730 731enableAutoPlay(deviceId: string): Promise<void> 732 733允许设备在连接成功后自动播放音乐。使用Promise异步回调。 734 735**系统接口**:此接口为系统接口。 736 737**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 738 739**系统能力**:SystemCapability.Communication.Bluetooth.Core 740 741**参数:** 742 743| 参数名 | 类型 | 必填 | 说明 | 744| ------ | ------ | ---- | ------- | 745| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 746 747**返回值:** 748 749| 类型 | 说明 | 750| ----------------------------- | ---------- | 751| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 752 753**错误码**: 754 755以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 756 757| 错误码ID | 错误信息 | 758| -------- | ---------------------------- | 759|201 | Permission denied. | 760|202 | Non-system applications are not allowed to use system APIs. | 761|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 762|801 | Capability not supported. | 763|2900001 | Service stopped. | 764|2900003 | Bluetooth disabled. | 765|2900099 | Operation failed. | 766 767**示例:** 768 769```js 770import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 771try { 772 let a2dpSrc = a2dp.createA2dpSrcProfile(); 773 a2dpSrc.enableAutoPlay('XX:XX:XX:XX:XX:XX').then(() => { 774 console.info("enableAutoPlay"); 775 }); 776} catch (err) { 777 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 778} 779``` 780 781 782### getAutoPlayDisabledDuration<sup>12+</sup> 783 784getAutoPlayDisabledDuration(deviceId: string): Promise<number> 785 786获取拦截时长或自动播放开关。使用Promise异步回调。 787 788**系统接口**:此接口为系统接口。 789 790**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 791 792**系统能力**:SystemCapability.Communication.Bluetooth.Core 793 794**参数:** 795 796| 参数名 | 类型 | 必填 | 说明 | 797| ------ | ------ | ---- | ------- | 798| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 799 800**返回值:** 801 802| 类型 | 说明 | 803| ----------------------------- | ---------- | 804| Promise<number> | 以Promise的形式返回结果。number为返回的拦截时长,单位:ms。如果返回-1,则表示允许设备在连接成功后自动播放音乐。 | 805 806**错误码**: 807 808以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 809 810| 错误码ID | 错误信息 | 811| -------- | ---------------------------- | 812|201 | Permission denied. | 813|202 | Non-system applications are not allowed to use system APIs. | 814|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 815|801 | Capability not supported. | 816|2900001 | Service stopped. | 817|2900003 | Bluetooth disabled. | 818|2900099 | Operation failed. | 819 820**示例:** 821 822```js 823import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 824try { 825 let a2dpSrc = a2dp.createA2dpSrcProfile(); 826 a2dpSrc.getAutoPlayDisabledDuration('XX:XX:XX:XX:XX:XX').then((data: number) => { 827 console.info('number' + JSON.stringify(data)); 828 }); 829} catch (err) { 830 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 831} 832```