• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 '@ohos.bluetooth.a2dp';
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|2900001 | Service stopped.               |
43|2900003 | Bluetooth switch is off.       |
44|2900004 | Profile is not supported.      |
45|2900099 | Operation failed.              |
46
47**示例:**
48
49```js
50import { BusinessError } from '@ohos.base';
51try {
52    let a2dpSrc = a2dp.createA2dpSrcProfile();
53    a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
54} catch (err) {
55    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
56}
57```
58
59
60### disconnect
61
62disconnect(deviceId: string): void
63
64断开设备的a2dp服务连接。
65
66**系统接口**:此接口为系统接口。
67
68**需要权限**:ohos.permission.ACCESS_BLUETOOTH
69
70**系统能力**:SystemCapability.Communication.Bluetooth.Core71
72**参数:**
73
74| 参数名    | 类型     | 必填   | 说明      |
75| ------ | ------ | ---- | ------- |
76| deviceId | string | 是    | 远端设备地址。 |
77
78**错误码**:
79
80以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
81
82| 错误码ID | 错误信息 |
83| -------- | ---------------------------- |
84|2900001 | Service stopped.                         |
85|2900003 | Bluetooth switch is off.                 |
86|2900004 | Profile is not supported.                |
87|2900099 | Operation failed.                        |
88
89**示例:**
90
91```js
92import { BusinessError } from '@ohos.base';
93try {
94    let a2dpSrc = a2dp.createA2dpSrcProfile();
95    a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
96} catch (err) {
97    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
98}
99```
100
101
102### isAbsoluteVolumeSupported<sup>11+</sup>
103
104isAbsoluteVolumeSupported(deviceId: string, callback: AsyncCallback&lt;boolean&gt;): void
105
106获取设备是否支持绝对音量能力。
107
108**系统接口**:此接口为系统接口。
109
110**需要权限**:ohos.permission.ACCESS_BLUETOOTH
111
112**系统能力**:SystemCapability.Communication.Bluetooth.Core113
114**参数:**
115
116| 参数名    | 类型     | 必填   | 说明      |
117| ------ | ------ | ---- | ------- |
118| deviceId | string | 是    | 远端设备地址。 |
119| callback | AsyncCallback&lt;boolean&gt; | 是 | 通过注册回调函数获取设备是否支持绝对音量。如果成功,值在supported中返回。 |
120
121
122**错误码**:
123
124以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
125
126| 错误码ID | 错误信息 |
127| -------- | ---------------------------- |
128|2900001 | Service stopped.                         |
129|2900003 | Bluetooth switch is off.                 |
130|2900099 | Operation failed.                        |
131
132**示例:**
133
134```js
135import { BusinessError } from '@ohos.base';
136try {
137    let a2dpSrc = a2dp.createA2dpSrcProfile();
138    a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX', (err, supported) => {
139        console.info('device support absolute volume ' + supported);
140    });
141} catch (err) {
142    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
143}
144```
145
146### isAbsoluteVolumeSupported<sup>11+</sup>
147
148isAbsoluteVolumeSupported(deviceId: string): Promise&lt;boolean&gt;
149
150获取设备是否支持绝对音量能力。
151
152**系统接口**:此接口为系统接口。
153
154**需要权限**:ohos.permission.ACCESS_BLUETOOTH
155
156**系统能力**:SystemCapability.Communication.Bluetooth.Core157
158**参数:**
159
160| 参数名    | 类型     | 必填   | 说明      |
161| ------ | ------ | ---- | ------- |
162| deviceId | string | 是    | 远端设备地址。 |
163
164**返回值:**
165
166| 类型                            | 说明         |
167| ----------------------------- | ---------- |
168| Promise&lt;boolean&gt; | 通过promise形式获取设备是否支持绝对音量。如果成功,值在supported中返回。 |
169
170**错误码**:
171
172以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
173
174| 错误码ID | 错误信息 |
175| -------- | ---------------------------- |
176|2900001 | Service stopped.                         |
177|2900003 | Bluetooth switch is off.                 |
178|2900099 | Operation failed.                        |
179
180**示例:**
181
182```js
183import { BusinessError } from '@ohos.base';
184try {
185    let a2dpSrc = a2dp.createA2dpSrcProfile();
186    a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX').then((supported) => {
187        console.info('device support absolute volume ' + supported);
188    });
189} catch (err) {
190    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
191}
192```
193
194### isAbsoluteVolumeEnabled<sup>11+</sup>
195
196isAbsoluteVolumeEnabled(deviceId: string, callback: AsyncCallback&lt;boolean&gt;): void
197
198获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。
199
200**系统接口**:此接口为系统接口。
201
202**需要权限**:ohos.permission.ACCESS_BLUETOOTH
203
204**系统能力**:SystemCapability.Communication.Bluetooth.Core205
206**参数:**
207
208| 参数名    | 类型     | 必填   | 说明      |
209| ------ | ------ | ---- | ------- |
210| deviceId | string | 是    | 远端设备地址。 |
211| callback | AsyncCallback&lt;boolean&gt; | 是 | 通过注册回调函数获取设备绝对音量是否开启。如果成功,值在enabled中返回。 |
212
213
214**错误码**:
215
216以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
217
218| 错误码ID | 错误信息 |
219| -------- | ---------------------------- |
220|2900001 | Service stopped.                         |
221|2900003 | Bluetooth switch is off.                 |
222|2900099 | Operation failed.                        |
223
224**示例:**
225
226```js
227import { BusinessError } from '@ohos.base';
228try {
229    let a2dpSrc = a2dp.createA2dpSrcProfile();
230    a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => {
231        console.info('device absolute volume enable ' + enabled);
232    });
233} catch (err) {
234    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
235}
236```
237
238### isAbsoluteVolumeEnabled<sup>11+</sup>
239
240isAbsoluteVolumeEnabled(deviceId: string): Promise&lt;boolean&gt;
241
242获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。
243
244**系统接口**:此接口为系统接口。
245
246**需要权限**:ohos.permission.ACCESS_BLUETOOTH
247
248**系统能力**:SystemCapability.Communication.Bluetooth.Core249
250**参数:**
251
252| 参数名    | 类型     | 必填   | 说明      |
253| ------ | ------ | ---- | ------- |
254| deviceId | string | 是    | 远端设备地址。 |
255
256**返回值:**
257
258| 类型                            | 说明         |
259| ----------------------------- | ---------- |
260| Promise&lt;boolean&gt; | 通过promise形式获取设备绝对音量是否开启。如果成功,值在enabled中返回。 |
261
262**错误码**:
263
264以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
265
266| 错误码ID | 错误信息 |
267| -------- | ---------------------------- |
268|2900001 | Service stopped.                         |
269|2900003 | Bluetooth switch is off.                 |
270|2900099 | Operation failed.                        |
271
272**示例:**
273
274```js
275import { BusinessError } from '@ohos.base';
276try {
277    let a2dpSrc = a2dp.createA2dpSrcProfile();
278    a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => {
279        console.info('device absolute volume enable ' + enabled);
280    });
281} catch (err) {
282    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
283}
284```
285
286### enableAbsoluteVolume<sup>11+</sup>
287
288enableAbsoluteVolume(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
289
290开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。
291
292**系统接口**:此接口为系统接口。
293
294**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
295
296**系统能力**:SystemCapability.Communication.Bluetooth.Core297
298**参数:**
299
300| 参数名    | 类型     | 必填   | 说明      |
301| ------ | ------ | ---- | ------- |
302| deviceId | string | 是    | 远端设备地址。 |
303| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 |
304
305
306**错误码**:
307
308以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
309
310| 错误码ID | 错误信息 |
311| -------- | ---------------------------- |
312|2900001 | Service stopped.                         |
313|2900003 | Bluetooth switch is off.                 |
314|2900099 | Operation failed.                        |
315
316**示例:**
317
318```js
319import { BusinessError } from '@ohos.base';
320try {
321    let a2dpSrc = a2dp.createA2dpSrcProfile();
322    a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => {
323        if (err) {
324            console.error("enableAbsoluteVolume error");
325        }
326    });
327} catch (err) {
328    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
329}
330```
331
332### enableAbsoluteVolume<sup>11+</sup>
333
334enableAbsoluteVolume(deviceId: string): Promise&lt;void&gt;
335
336开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。
337
338**系统接口**:此接口为系统接口。
339
340**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
341
342**系统能力**:SystemCapability.Communication.Bluetooth.Core343
344**参数:**
345
346| 参数名    | 类型     | 必填   | 说明      |
347| ------ | ------ | ---- | ------- |
348| deviceId | string | 是    | 远端设备地址。 |
349
350**返回值:**
351
352| 类型                            | 说明         |
353| ----------------------------- | ---------- |
354| Promise&lt;void&gt; | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
355
356**错误码**:
357
358以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
359
360| 错误码ID | 错误信息 |
361| -------- | ---------------------------- |
362|2900001 | Service stopped.                         |
363|2900003 | Bluetooth switch is off.                 |
364|2900099 | Operation failed.                        |
365
366**示例:**
367
368```js
369import { BusinessError } from '@ohos.base';
370try {
371    let a2dpSrc = a2dp.createA2dpSrcProfile();
372    a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => {
373            console.info("enableAbsoluteVolume");
374        }
375    );
376} catch (err) {
377    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
378}
379```
380
381### disableAbsoluteVolume<sup>11+</sup>
382
383disableAbsoluteVolume(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
384
385关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。
386
387**系统接口**:此接口为系统接口。
388
389**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
390
391**系统能力**:SystemCapability.Communication.Bluetooth.Core392
393**参数:**
394
395| 参数名    | 类型     | 必填   | 说明      |
396| ------ | ------ | ---- | ------- |
397| deviceId | string | 是    | 远端设备地址。 |
398| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 |
399
400
401**错误码**:
402
403以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
404
405| 错误码ID | 错误信息 |
406| -------- | ---------------------------- |
407|2900001 | Service stopped.                         |
408|2900003 | Bluetooth switch is off.                 |
409|2900099 | Operation failed.                        |
410
411**示例:**
412
413```js
414import { BusinessError } from '@ohos.base';
415try {
416    let a2dpSrc = a2dp.createA2dpSrcProfile();
417    a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => {
418        if (err) {
419            console.error("disableAbsoluteVolume error");
420        }
421    });
422} catch (err) {
423    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
424}
425```
426
427### disableAbsoluteVolume<sup>11+</sup>
428
429disableAbsoluteVolume(deviceId: string): Promise&lt;void&gt;
430
431关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。
432
433**系统接口**:此接口为系统接口。
434
435**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
436
437**系统能力**:SystemCapability.Communication.Bluetooth.Core438
439**参数:**
440
441| 参数名    | 类型     | 必填   | 说明      |
442| ------ | ------ | ---- | ------- |
443| deviceId | string | 是    | 远端设备地址。 |
444
445**返回值:**
446
447| 类型                            | 说明         |
448| ----------------------------- | ---------- |
449| Promise&lt;void&gt; | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
450
451**错误码**:
452
453以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
454
455| 错误码ID | 错误信息 |
456| -------- | ---------------------------- |
457|2900001 | Service stopped.                         |
458|2900003 | Bluetooth switch is off.                 |
459|2900099 | Operation failed.                        |
460
461**示例:**
462
463```js
464import { BusinessError } from '@ohos.base';
465try {
466    let a2dpSrc = a2dp.createA2dpSrcProfile();
467    a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => {
468        console.info("disableAbsoluteVolume");
469    });
470} catch (err) {
471    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
472}
473```
474
475### getCurrentCodecInfo<sup>11+</sup>
476
477getCurrentCodecInfo(deviceId: string): CodecInfo
478
479获取当前编码器信息。
480
481**系统接口**:此接口为系统接口。
482
483**需要权限**:ohos.permission.ACCESS_BLUETOOTH
484
485**系统能力**:SystemCapability.Communication.Bluetooth.Core486
487**参数:**
488
489| 参数名    | 类型     | 必填   | 说明      |
490| ------ | ------ | ---- | ------- |
491| deviceId | string | 是    | 远端设备地址。 |
492
493**返回值:**
494
495| 类型                            | 说明         |
496| ----------------------------- | ---------- |
497| [CodecInfo](js-apis-bluetooth-a2dp#codecinfo11)| 当前编码器信息。 |
498
499**错误码**:
500
501以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
502
503| 错误码ID | 错误信息 |
504| -------- | ---------------------------- |
505|2900001 | Service stopped.                         |
506|2900003 | Bluetooth switch is off.                 |
507|2900099 | Operation failed.                        |
508
509**示例:**
510
511```js
512import { BusinessError } from '@ohos.base';
513try {
514    let a2dpSrc = a2dp.createA2dpSrcProfile();
515    let codecInfo : a2dp.CodecInfo = a2dpSrc.getCurrentCodecInfo('XX:XX:XX:XX:XX:XX');
516} catch (err) {
517    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
518}
519```
520
521### setCurrentCodecInfo<sup>11+</sup>
522
523setCurrentCodecInfo(deviceId: string, codecInfo: CodecInfo): void
524
525设置当前编码器信息。
526
527**系统接口**:此接口为系统接口。
528
529**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
530
531**系统能力**:SystemCapability.Communication.Bluetooth.Core532
533**参数:**
534
535| 参数名    | 类型     | 必填   | 说明      |
536| ------ | ------ | ---- | ------- |
537| deviceId | string | 是    | 远端设备地址。 |
538| codecInfo | [CodecInfo](js-apis-bluetooth-a2dp.md#codecinfo11) | 是    | 编码器信息。 |
539
540**错误码**:
541
542以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
543
544| 错误码ID | 错误信息 |
545| -------- | ---------------------------- |
546|2900001 | Service stopped.                         |
547|2900003 | Bluetooth switch is off.                 |
548|2900099 | Operation failed.                        |
549
550**示例:**
551
552```js
553import { BusinessError } from '@ohos.base';
554try {
555    let a2dpSrc = a2dp.createA2dpSrcProfile();
556    let codecInfo : a2dp.CodecInfo = {
557        codecType: 0,
558        codecBitsPerSample: 1,
559        codecChannelMode: 2,
560        codecSampleRate: 1,
561    }
562    a2dpSrc.setCurrentCodecInfo('XX:XX:XX:XX:XX:XX', codecInfo);
563} catch (err) {
564    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
565}
566```
567
568