1# @ohos.bluetooth.a2dp (Bluetooth A2DP Module) (System API) 2 3The **a2dp** module provides APIs for using the Bluetooth Advanced Audio Distribution Profile (A2DP), which defines how to stream high quality audio from one device to another over a Bluetooth connection. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.bluetooth.a2dp (Bluetooth A2DP Module)](js-apis-bluetooth-a2dp.md). 9 10 11## Modules to Import 12 13```js 14import { a2dp } from '@kit.ConnectivityKit'; 15``` 16 17 18### connect 19 20connect(deviceId: string): void 21 22Connects to an A2DP device. 23 24**System API**: This is a system API. 25 26**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 27 28**System capability**: SystemCapability.Communication.Bluetooth.Core 29 30**Parameters** 31 32| Name | Type | Mandatory | Description | 33| ------ | ------ | ---- | ------- | 34| deviceId | string | Yes | Address of the device to connect.| 35 36**Error codes** 37 38For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 39 40| ID| Error Message| 41| -------- | ---------------------------- | 42|201 | Permission denied. | 43|202 | Non-system applications are not allowed to use system APIs. | 44|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 45|801 | Capability not supported. | 46|2900001 | Service stopped. | 47|2900003 | Bluetooth disabled. | 48|2900004 | Profile not supported. | 49|2900099 | Operation failed. | 50 51**Example** 52 53```js 54import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 55try { 56 let a2dpSrc = a2dp.createA2dpSrcProfile(); 57 a2dpSrc.connect('XX:XX:XX:XX:XX:XX'); 58} catch (err) { 59 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 60} 61``` 62 63 64### disconnect 65 66disconnect(deviceId: string): void 67 68Disconnects from an A2DP device. 69 70**System API**: This is a system API. 71 72**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 73 74**System capability**: SystemCapability.Communication.Bluetooth.Core 75 76**Parameters** 77 78| Name | Type | Mandatory | Description | 79| ------ | ------ | ---- | ------- | 80| deviceId | string | Yes | Address of the device to disconnect.| 81 82**Error codes** 83 84For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 85 86| ID| Error Message| 87| -------- | ---------------------------- | 88|201 | Permission denied. | 89|202 | Non-system applications are not allowed to use system APIs. | 90|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 91|801 | Capability not supported. | 92|2900001 | Service stopped. | 93|2900003 | Bluetooth disabled. | 94|2900004 | Profile not supported. | 95|2900099 | Operation failed. | 96 97**Example** 98 99```js 100import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 101try { 102 let a2dpSrc = a2dp.createA2dpSrcProfile(); 103 a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX'); 104} catch (err) { 105 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 106} 107``` 108 109 110### isAbsoluteVolumeSupported<sup>11+</sup> 111 112isAbsoluteVolumeSupported(deviceId: string, callback: AsyncCallback<boolean>): void 113 114Checks whether a device supports the absolute volume capability. This API uses an asynchronous callback to return the result. 115 116**System API**: This is a system API. 117 118**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 119 120**System capability**: SystemCapability.Communication.Bluetooth.Core 121 122**Parameters** 123 124| Name | Type | Mandatory | Description | 125| ------ | ------ | ---- | ------- | 126| deviceId | string | Yes | Address of the device to check.| 127| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the device supports absolute volume, **supported** is returned.| 128 129 130**Error codes** 131 132For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 133 134| ID| Error Message| 135| -------- | ---------------------------- | 136|201 | Permission denied. | 137|202 | Non-system applications are not allowed to use system APIs. | 138|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 139|801 | Capability not supported. | 140|2900001 | Service stopped. | 141|2900003 | Bluetooth disabled. | 142|2900099 | Operation failed. | 143 144**Example** 145 146```js 147import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 148try { 149 let a2dpSrc = a2dp.createA2dpSrcProfile(); 150 a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX', (err, supported) => { 151 console.info('device support absolute volume ' + supported); 152 }); 153} catch (err) { 154 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 155} 156``` 157 158### isAbsoluteVolumeSupported<sup>11+</sup> 159 160isAbsoluteVolumeSupported(deviceId: string): Promise<boolean> 161 162Checks whether a device supports the absolute volume capability. This API uses a promise to return the result. 163 164**System API**: This is a system API. 165 166**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 167 168**System capability**: SystemCapability.Communication.Bluetooth.Core 169 170**Parameters** 171 172| Name | Type | Mandatory | Description | 173| ------ | ------ | ---- | ------- | 174| deviceId | string | Yes | Address of the device to check.| 175 176**Return value** 177 178| Type | Description | 179| ----------------------------- | ---------- | 180| Promise<boolean> | Promise used to return the result. If the device supports absolute volume, **supported** is returned.| 181 182**Error codes** 183 184For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 185 186| ID| Error Message| 187| -------- | ---------------------------- | 188|201 | Permission denied. | 189|202 | Non-system applications are not allowed to use system APIs. | 190|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 191|801 | Capability not supported. | 192|2900001 | Service stopped. | 193|2900003 | Bluetooth disabled. | 194|2900099 | Operation failed. | 195 196**Example** 197 198```js 199import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 200try { 201 let a2dpSrc = a2dp.createA2dpSrcProfile(); 202 a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX').then((supported) => { 203 console.info('device support absolute volume ' + supported); 204 }); 205} catch (err) { 206 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 207} 208``` 209 210### isAbsoluteVolumeEnabled<sup>11+</sup> 211 212isAbsoluteVolumeEnabled(deviceId: string, callback: AsyncCallback<boolean>): void 213 214Checks whether the absolute volume capability is enabled for a device. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability. This API uses an asynchronous callback to return the result. 215 216**System API**: This is a system API. 217 218**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 219 220**System capability**: SystemCapability.Communication.Bluetooth.Core 221 222**Parameters** 223 224| Name | Type | Mandatory | Description | 225| ------ | ------ | ---- | ------- | 226| deviceId | string | Yes | Address of the device to check.| 227| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If absolute volume is enabled, **enabled** is returned.| 228 229 230**Error codes** 231 232For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 233 234| ID| Error Message| 235| -------- | ---------------------------- | 236|201 | Permission denied. | 237|202 | Non-system applications are not allowed to use system APIs. | 238|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 239|801 | Capability not supported. | 240|2900001 | Service stopped. | 241|2900003 | Bluetooth disabled. | 242|2900099 | Operation failed. | 243 244**Example** 245 246```js 247import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 248try { 249 let a2dpSrc = a2dp.createA2dpSrcProfile(); 250 a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => { 251 console.info('device absolute volume enable ' + enabled); 252 }); 253} catch (err) { 254 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 255} 256``` 257 258### isAbsoluteVolumeEnabled<sup>11+</sup> 259 260isAbsoluteVolumeEnabled(deviceId: string): Promise<boolean> 261 262Checks whether the absolute volume capability is enabled for a device. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability. This API uses a promise to return the result. 263 264**System API**: This is a system API. 265 266**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 267 268**System capability**: SystemCapability.Communication.Bluetooth.Core 269 270**Parameters** 271 272| Name | Type | Mandatory | Description | 273| ------ | ------ | ---- | ------- | 274| deviceId | string | Yes | Address of the device to check.| 275 276**Return value** 277 278| Type | Description | 279| ----------------------------- | ---------- | 280| Promise<boolean> | Promise used to return the result. If absolute volume is enabled, **enabled** is returned.| 281 282**Error codes** 283 284For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 285 286| ID| Error Message| 287| -------- | ---------------------------- | 288|201 | Permission denied. | 289|202 | Non-system applications are not allowed to use system APIs. | 290|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 291|801 | Capability not supported. | 292|2900001 | Service stopped. | 293|2900003 | Bluetooth disabled. | 294|2900099 | Operation failed. | 295 296**Example** 297 298```js 299import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 300try { 301 let a2dpSrc = a2dp.createA2dpSrcProfile(); 302 a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => { 303 console.info('device absolute volume enable ' + enabled); 304 }); 305} catch (err) { 306 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 307} 308``` 309 310### enableAbsoluteVolume<sup>11+</sup> 311 312enableAbsoluteVolume(deviceId: string, callback: AsyncCallback<void>): void 313 314Enables the absolute volume capability for a device. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability. This API uses an asynchronous callback to return the result. 315 316**System API**: This is a system API. 317 318**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 319 320**System capability**: SystemCapability.Communication.Bluetooth.Core 321 322**Parameters** 323 324| Name | Type | Mandatory | Description | 325| ------ | ------ | ---- | ------- | 326| deviceId | string | Yes | Address of the target device. | 327| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 328 329 330**Error codes** 331 332For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 333 334| ID| Error Message| 335| -------- | ---------------------------- | 336|201 | Permission denied. | 337|202 | Non-system applications are not allowed to use system APIs. | 338|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 339|801 | Capability not supported. | 340|2900001 | Service stopped. | 341|2900003 | Bluetooth disabled. | 342|2900099 | Operation failed. | 343 344**Example** 345 346```js 347import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 348try { 349 let a2dpSrc = a2dp.createA2dpSrcProfile(); 350 a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => { 351 if (err) { 352 console.error("enableAbsoluteVolume error"); 353 } 354 }); 355} catch (err) { 356 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 357} 358``` 359 360### enableAbsoluteVolume<sup>11+</sup> 361 362enableAbsoluteVolume(deviceId: string): Promise<void> 363 364Enables the absolute volume capability for a device. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability. This API uses a promise to return the result. 365 366**System API**: This is a system API. 367 368**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 369 370**System capability**: SystemCapability.Communication.Bluetooth.Core 371 372**Parameters** 373 374| Name | Type | Mandatory | Description | 375| ------ | ------ | ---- | ------- | 376| deviceId | string | Yes | Address of the target device. | 377 378**Return value** 379 380| Type | Description | 381| ----------------------------- | ---------- | 382| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 383 384**Error codes** 385 386For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 387 388| ID| Error Message| 389| -------- | ---------------------------- | 390|201 | Permission denied. | 391|202 | Non-system applications are not allowed to use system APIs. | 392|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 393|801 | Capability not supported. | 394|2900001 | Service stopped. | 395|2900003 | Bluetooth disabled. | 396|2900099 | Operation failed. | 397 398**Example** 399 400```js 401import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 402try { 403 let a2dpSrc = a2dp.createA2dpSrcProfile(); 404 a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => { 405 console.info("enableAbsoluteVolume"); 406 } 407 ); 408} catch (err) { 409 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 410} 411``` 412 413### disableAbsoluteVolume<sup>11+</sup> 414 415disableAbsoluteVolume(deviceId: string, callback: AsyncCallback<void>): void 416 417Disables the absolute volume capability for a device. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability. This API uses an asynchronous callback to return the result. 418 419**System API**: This is a system API. 420 421**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 422 423**System capability**: SystemCapability.Communication.Bluetooth.Core 424 425**Parameters** 426 427| Name | Type | Mandatory | Description | 428| ------ | ------ | ---- | ------- | 429| deviceId | string | Yes | Address of the target device. | 430| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 431 432 433**Error codes** 434 435For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 436 437| ID| Error Message| 438| -------- | ---------------------------- | 439|201 | Permission denied. | 440|202 | Non-system applications are not allowed to use system APIs. | 441|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 442|801 | Capability not supported. | 443|2900001 | Service stopped. | 444|2900003 | Bluetooth disabled. | 445|2900099 | Operation failed. | 446 447**Example** 448 449```js 450import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 451try { 452 let a2dpSrc = a2dp.createA2dpSrcProfile(); 453 a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => { 454 if (err) { 455 console.error("disableAbsoluteVolume error"); 456 } 457 }); 458} catch (err) { 459 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 460} 461``` 462 463### disableAbsoluteVolume<sup>11+</sup> 464 465disableAbsoluteVolume(deviceId: string): Promise<void> 466 467Disables the absolute volume capability for a device. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability. This API uses a promise to return the result. 468 469**System API**: This is a system API. 470 471**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 472 473**System capability**: SystemCapability.Communication.Bluetooth.Core 474 475**Parameters** 476 477| Name | Type | Mandatory | Description | 478| ------ | ------ | ---- | ------- | 479| deviceId | string | Yes | Address of the target device. | 480 481**Return value** 482 483| Type | Description | 484| ----------------------------- | ---------- | 485| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 486 487**Error codes** 488 489For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 490 491| ID| Error Message| 492| -------- | ---------------------------- | 493|201 | Permission denied. | 494|202 | Non-system applications are not allowed to use system APIs. | 495|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 496|801 | Capability not supported. | 497|2900001 | Service stopped. | 498|2900003 | Bluetooth disabled. | 499|2900099 | Operation failed. | 500 501**Example** 502 503```js 504import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 505try { 506 let a2dpSrc = a2dp.createA2dpSrcProfile(); 507 a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => { 508 console.info("disableAbsoluteVolume"); 509 }); 510} catch (err) { 511 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 512} 513``` 514 515### getCurrentCodecInfo<sup>11+</sup> 516 517getCurrentCodecInfo(deviceId: string): CodecInfo 518 519Obtains the current codec information. 520 521**System API**: This is a system API. 522 523**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 524 525**System capability**: SystemCapability.Communication.Bluetooth.Core 526 527**Parameters** 528 529| Name | Type | Mandatory | Description | 530| ------ | ------ | ---- | ------- | 531| deviceId | string | Yes | Address of the remote device.| 532 533**Return value** 534 535| Type | Description | 536| ----------------------------- | ---------- | 537| [CodecInfo](js-apis-bluetooth-a2dp#codecinfo11)| Codec information obtained.| 538 539**Error codes** 540 541For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 542 543| ID| Error Message| 544| -------- | ---------------------------- | 545|201 | Permission denied. | 546|202 | Non-system applications are not allowed to use system APIs. | 547|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 548|801 | Capability not supported. | 549|2900001 | Service stopped. | 550|2900003 | Bluetooth disabled. | 551|2900099 | Operation failed. | 552 553**Example** 554 555```js 556import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 557try { 558 let a2dpSrc = a2dp.createA2dpSrcProfile(); 559 let codecInfo : a2dp.CodecInfo = a2dpSrc.getCurrentCodecInfo('XX:XX:XX:XX:XX:XX'); 560} catch (err) { 561 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 562} 563``` 564 565### setCurrentCodecInfo<sup>11+</sup> 566 567setCurrentCodecInfo(deviceId: string, codecInfo: CodecInfo): void 568 569Sets the current codec information. 570 571**System API**: This is a system API. 572 573**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 574 575**System capability**: SystemCapability.Communication.Bluetooth.Core 576 577**Parameters** 578 579| Name | Type | Mandatory | Description | 580| ------ | ------ | ---- | ------- | 581| deviceId | string | Yes | Address of the remote device.| 582| codecInfo | [CodecInfo](js-apis-bluetooth-a2dp.md#codecinfo11) | Yes | Codec information to set.| 583 584**Error codes** 585 586For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 587 588| ID| Error Message| 589| -------- | ---------------------------- | 590|201 | Permission denied. | 591|202 | Non-system applications are not allowed to use system APIs. | 592|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 593|801 | Capability not supported. | 594|2900001 | Service stopped. | 595|2900003 | Bluetooth disabled. | 596|2900099 | Operation failed. | 597 598**Example** 599 600```js 601import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 602try { 603 let a2dpSrc = a2dp.createA2dpSrcProfile(); 604 let codecInfo : a2dp.CodecInfo = { 605 codecType: 0, 606 codecBitsPerSample: 1, 607 codecChannelMode: 2, 608 codecSampleRate: 1 609 } 610 a2dpSrc.setCurrentCodecInfo('XX:XX:XX:XX:XX:XX', codecInfo); 611} catch (err) { 612 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 613} 614``` 615 616 617### disableAutoPlay<sup>12+</sup> 618 619disableAutoPlay(deviceId: string, duration: number): Promise<void> 620 621Disables auto play of music for the specified duration after a device is connected. This API uses a promise to return the result. 622 623**System API**: This is a system API. 624 625**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 626 627**System capability**: SystemCapability.Communication.Bluetooth.Core 628 629**Parameters** 630 631| Name | Type | Mandatory | Description | 632| ------ | ------ | ---- | ------- | 633| deviceId | string | Yes | Address of the remote device, for example, **11:22:33:AA:BB:FF**.| 634| duration | number | Yes | Duration for which auto play is disabled, in ms.| 635 636**Return value** 637 638| Type | Description | 639| ----------------------------- | ---------- | 640| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 641 642**Error codes** 643 644For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 645 646| ID| Error Message| 647| -------- | ---------------------------- | 648|201 | Permission denied. | 649|202 | Non-system applications are not allowed to use system APIs. | 650|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 651|801 | Capability not supported. | 652|2900001 | Service stopped. | 653|2900003 | Bluetooth disabled. | 654|2900099 | Operation failed. | 655 656**Example** 657 658```js 659import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 660try { 661 let a2dpSrc = a2dp.createA2dpSrcProfile(); 662 let durationNumber = 1000; 663 a2dpSrc.disableAutoPlay('XX:XX:XX:XX:XX:XX', durationNumber).then(() => { 664 console.info("disableAutoPlay"); 665 }); 666} catch (err) { 667 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 668} 669``` 670 671 672### enableAutoPlay<sup>12+</sup> 673 674enableAutoPlay(deviceId: string): Promise<void> 675 676Enables auto play of music after a device is connected. This API uses a promise to return the result. 677 678**System API**: This is a system API. 679 680**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 681 682**System capability**: SystemCapability.Communication.Bluetooth.Core 683 684**Parameters** 685 686| Name | Type | Mandatory | Description | 687| ------ | ------ | ---- | ------- | 688| deviceId | string | Yes | Address of the remote device, for example, **11:22:33:AA:BB:FF**.| 689 690**Return value** 691 692| Type | Description | 693| ----------------------------- | ---------- | 694| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 695 696**Error codes** 697 698For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 699 700| ID| Error Message| 701| -------- | ---------------------------- | 702|201 | Permission denied. | 703|202 | Non-system applications are not allowed to use system APIs. | 704|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 705|801 | Capability not supported. | 706|2900001 | Service stopped. | 707|2900003 | Bluetooth disabled. | 708|2900099 | Operation failed. | 709 710**Example** 711 712```js 713import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 714try { 715 let a2dpSrc = a2dp.createA2dpSrcProfile(); 716 a2dpSrc.enableAutoPlay('XX:XX:XX:XX:XX:XX').then(() => { 717 console.info("enableAutoPlay"); 718 }); 719} catch (err) { 720 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 721} 722``` 723 724 725### getAutoPlayDisabledDuration<sup>12+</sup> 726 727getAutoPlayDisabledDuration(deviceId: string): Promise<number> 728 729Obtains the auto-play disabled duration or auto play switch. This API uses a promise to return the result. 730 731**System API**: This is a system API. 732 733**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 734 735**System capability**: SystemCapability.Communication.Bluetooth.Core 736 737**Parameters** 738 739| Name | Type | Mandatory | Description | 740| ------ | ------ | ---- | ------- | 741| deviceId | string | Yes | Address of the remote device, for example, **11:22:33:AA:BB:FF**.| 742 743**Return value** 744 745| Type | Description | 746| ----------------------------- | ---------- | 747| Promise<number> | Promise used to return the result. **number** indicates the auto-play disabled duration, in ms. If **-1** is returned, the device is allowed to automatically play music once connected.| 748 749**Error codes** 750 751For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 752 753| ID| Error Message| 754| -------- | ---------------------------- | 755|201 | Permission denied. | 756|202 | Non-system applications are not allowed to use system APIs. | 757|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 758|801 | Capability not supported. | 759|2900001 | Service stopped. | 760|2900003 | Bluetooth disabled. | 761|2900099 | Operation failed. | 762 763**Example** 764 765```js 766import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 767try { 768 let a2dpSrc = a2dp.createA2dpSrcProfile(); 769 a2dpSrc.getAutoPlayDisabledDuration('XX:XX:XX:XX:XX:XX').then((data: number) => { 770 console.info('number' + JSON.stringify(data)); 771 }); 772} catch (err) { 773 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 774} 775``` 776