1# @ohos.bluetooth.a2dp (蓝牙a2dp模块)(系统接口)
2
3a2dp模块提供了访问蓝牙音频接口的方法。
4
5> **说明:**
6>
7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.a2dp (蓝牙a2dp模块)](js-apis-bluetooth-a2dp.md)
9
10
11## 导入模块
12
13```js
14import { a2dp } from '@kit.ConnectivityKit';
15```
16
17
18### connect
19
20connect(deviceId: string): void
21
22发起设备的A2dp服务连接请求。
23
24**系统接口**:此接口为系统接口。
25
26**需要权限**:ohos.permission.ACCESS_BLUETOOTH
27
28**系统能力**:SystemCapability.Communication.Bluetooth.Core29
30**参数:**
31
32| 参数名    | 类型     | 必填   | 说明      |
33| ------ | ------ | ---- | ------- |
34| deviceId | string | 是    | 远端设备地址。 |
35
36**错误码**:
37
38以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
39
40| 错误码ID | 错误信息 |
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**示例:**
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
68断开设备的a2dp服务连接。
69
70**系统接口**:此接口为系统接口。
71
72**需要权限**:ohos.permission.ACCESS_BLUETOOTH
73
74**系统能力**:SystemCapability.Communication.Bluetooth.Core75
76**参数:**
77
78| 参数名    | 类型     | 必填   | 说明      |
79| ------ | ------ | ---- | ------- |
80| deviceId | string | 是    | 远端设备地址。 |
81
82**错误码**:
83
84以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
85
86| 错误码ID | 错误信息 |
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**示例:**
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&lt;boolean&gt;): void
113
114获取设备是否支持绝对音量能力。使用Callback异步回调。
115
116**系统接口**:此接口为系统接口。
117
118**需要权限**:ohos.permission.ACCESS_BLUETOOTH
119
120**系统能力**:SystemCapability.Communication.Bluetooth.Core121
122**参数:**
123
124| 参数名    | 类型     | 必填   | 说明      |
125| ------ | ------ | ---- | ------- |
126| deviceId | string | 是    | 远端设备地址。 |
127| callback | AsyncCallback&lt;boolean&gt; | 是 | 通过注册回调函数获取设备是否支持绝对音量。如果成功,值在supported中返回。 |
128
129
130**错误码**:
131
132以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
133
134| 错误码ID | 错误信息 |
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**示例:**
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&lt;boolean&gt;
161
162获取设备是否支持绝对音量能力。使用Promise异步回调。
163
164**系统接口**:此接口为系统接口。
165
166**需要权限**:ohos.permission.ACCESS_BLUETOOTH
167
168**系统能力**:SystemCapability.Communication.Bluetooth.Core169
170**参数:**
171
172| 参数名    | 类型     | 必填   | 说明      |
173| ------ | ------ | ---- | ------- |
174| deviceId | string | 是    | 远端设备地址。 |
175
176**返回值:**
177
178| 类型                            | 说明         |
179| ----------------------------- | ---------- |
180| Promise&lt;boolean&gt; | 通过promise形式获取设备是否支持绝对音量。如果成功,值在supported中返回。 |
181
182**错误码**:
183
184以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
185
186| 错误码ID | 错误信息 |
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**示例:**
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&lt;boolean&gt;): void
213
214获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。使用Callback异步回调。
215
216**系统接口**:此接口为系统接口。
217
218**需要权限**:ohos.permission.ACCESS_BLUETOOTH
219
220**系统能力**:SystemCapability.Communication.Bluetooth.Core221
222**参数:**
223
224| 参数名    | 类型     | 必填   | 说明      |
225| ------ | ------ | ---- | ------- |
226| deviceId | string | 是    | 远端设备地址。 |
227| callback | AsyncCallback&lt;boolean&gt; | 是 | 通过注册回调函数获取设备绝对音量是否开启。如果成功,值在enabled中返回。 |
228
229
230**错误码**:
231
232以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
233
234| 错误码ID | 错误信息 |
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**示例:**
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&lt;boolean&gt;
261
262获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。使用Promise异步回调。
263
264**系统接口**:此接口为系统接口。
265
266**需要权限**:ohos.permission.ACCESS_BLUETOOTH
267
268**系统能力**:SystemCapability.Communication.Bluetooth.Core269
270**参数:**
271
272| 参数名    | 类型     | 必填   | 说明      |
273| ------ | ------ | ---- | ------- |
274| deviceId | string | 是    | 远端设备地址。 |
275
276**返回值:**
277
278| 类型                            | 说明         |
279| ----------------------------- | ---------- |
280| Promise&lt;boolean&gt; | 通过promise形式获取设备绝对音量是否开启。如果成功,值在enabled中返回。 |
281
282**错误码**:
283
284以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
285
286| 错误码ID | 错误信息 |
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**示例:**
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&lt;void&gt;): void
313
314开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。使用Callback异步回调。
315
316**系统接口**:此接口为系统接口。
317
318**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
319
320**系统能力**:SystemCapability.Communication.Bluetooth.Core321
322**参数:**
323
324| 参数名    | 类型     | 必填   | 说明      |
325| ------ | ------ | ---- | ------- |
326| deviceId | string | 是    | 远端设备地址。 |
327| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 |
328
329
330**错误码**:
331
332以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
333
334| 错误码ID | 错误信息 |
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**示例:**
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&lt;void&gt;
363
364开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。使用Promise异步回调。
365
366**系统接口**:此接口为系统接口。
367
368**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
369
370**系统能力**:SystemCapability.Communication.Bluetooth.Core371
372**参数:**
373
374| 参数名    | 类型     | 必填   | 说明      |
375| ------ | ------ | ---- | ------- |
376| deviceId | string | 是    | 远端设备地址。 |
377
378**返回值:**
379
380| 类型                            | 说明         |
381| ----------------------------- | ---------- |
382| Promise&lt;void&gt; | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
383
384**错误码**:
385
386以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
387
388| 错误码ID | 错误信息 |
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**示例:**
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&lt;void&gt;): void
416
417关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。使用Callback异步回调。
418
419**系统接口**:此接口为系统接口。
420
421**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
422
423**系统能力**:SystemCapability.Communication.Bluetooth.Core424
425**参数:**
426
427| 参数名    | 类型     | 必填   | 说明      |
428| ------ | ------ | ---- | ------- |
429| deviceId | string | 是    | 远端设备地址。 |
430| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 |
431
432
433**错误码**:
434
435以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
436
437| 错误码ID | 错误信息 |
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**示例:**
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&lt;void&gt;
466
467关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。使用Promise异步回调。
468
469**系统接口**:此接口为系统接口。
470
471**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
472
473**系统能力**:SystemCapability.Communication.Bluetooth.Core474
475**参数:**
476
477| 参数名    | 类型     | 必填   | 说明      |
478| ------ | ------ | ---- | ------- |
479| deviceId | string | 是    | 远端设备地址。 |
480
481**返回值:**
482
483| 类型                            | 说明         |
484| ----------------------------- | ---------- |
485| Promise&lt;void&gt; | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
486
487**错误码**:
488
489以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
490
491| 错误码ID | 错误信息 |
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**示例:**
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
519获取当前编码器信息。
520
521**系统接口**:此接口为系统接口。
522
523**需要权限**:ohos.permission.ACCESS_BLUETOOTH
524
525**系统能力**:SystemCapability.Communication.Bluetooth.Core526
527**参数:**
528
529| 参数名    | 类型     | 必填   | 说明      |
530| ------ | ------ | ---- | ------- |
531| deviceId | string | 是    | 远端设备地址。 |
532
533**返回值:**
534
535| 类型                            | 说明         |
536| ----------------------------- | ---------- |
537| [CodecInfo](js-apis-bluetooth-a2dp#codecinfo11)| 当前编码器信息。 |
538
539**错误码**:
540
541以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
542
543| 错误码ID | 错误信息 |
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**示例:**
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
569设置当前编码器信息。
570
571**系统接口**:此接口为系统接口。
572
573**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
574
575**系统能力**:SystemCapability.Communication.Bluetooth.Core576
577**参数:**
578
579| 参数名    | 类型     | 必填   | 说明      |
580| ------ | ------ | ---- | ------- |
581| deviceId | string | 是    | 远端设备地址。 |
582| codecInfo | [CodecInfo](js-apis-bluetooth-a2dp.md#codecinfo11) | 是    | 编码器信息。 |
583
584**错误码**:
585
586以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
587
588| 错误码ID | 错误信息 |
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**示例:**
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&lt;void&gt;
620
621限制设备在连接成功的若干毫秒内播放音乐。使用Promise异步回调。
622
623**系统接口**:此接口为系统接口。
624
625**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
626
627**系统能力**:SystemCapability.Communication.Bluetooth.Core628
629**参数:**
630
631| 参数名    | 类型     | 必填   | 说明      |
632| ------ | ------ | ---- | ------- |
633| deviceId | string | 是    | 远端设备地址,例如:"11:22:33:AA:BB:FF"。 |
634| duration | number | 是    | 拦截时长,单位毫秒。 |
635
636**返回值:**
637
638| 类型                            | 说明         |
639| ----------------------------- | ---------- |
640| Promise&lt;void&gt; | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
641
642**错误码**:
643
644以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
645
646| 错误码ID | 错误信息 |
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**示例:**
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&lt;void&gt;
675
676允许设备在连接成功后自动播放音乐。使用Promise异步回调。
677
678**系统接口**:此接口为系统接口。
679
680**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
681
682**系统能力**:SystemCapability.Communication.Bluetooth.Core683
684**参数:**
685
686| 参数名    | 类型     | 必填   | 说明      |
687| ------ | ------ | ---- | ------- |
688| deviceId | string | 是    | 远端设备地址,例如:"11:22:33:AA:BB:FF"。 |
689
690**返回值:**
691
692| 类型                            | 说明         |
693| ----------------------------- | ---------- |
694| Promise&lt;void&gt; | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
695
696**错误码**:
697
698以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
699
700| 错误码ID | 错误信息 |
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**示例:**
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&lt;number&gt;
728
729获取拦截时长或自动播放开关。使用Promise异步回调。
730
731**系统接口**:此接口为系统接口。
732
733**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
734
735**系统能力**:SystemCapability.Communication.Bluetooth.Core736
737**参数:**
738
739| 参数名    | 类型     | 必填   | 说明      |
740| ------ | ------ | ---- | ------- |
741| deviceId | string | 是    | 远端设备地址,例如:"11:22:33:AA:BB:FF"。 |
742
743**返回值:**
744
745| 类型                            | 说明         |
746| ----------------------------- | ---------- |
747| Promise&lt;number&gt; | 以Promise的形式返回结果。number为返回的拦截时长,单位为毫秒;如果返回-1,则表示允许设备在连接成功后自动播放音乐。 |
748
749**错误码**:
750
751以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
752
753| 错误码ID | 错误信息 |
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**示例:**
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```