• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.a2dp (Bluetooth A2DP Module)
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
9
10## Modules to Import
11
12```js
13import { a2dp } from '@kit.ConnectivityKit';
14```
15
16## BaseProfile
17
18type BaseProfile = baseProfile.BaseProfile
19
20Defines **BaseProfile** APIs.
21
22**System capability**: SystemCapability.Communication.Bluetooth.Core
23
24| Type                           | Description        |
25| ----------------------------- | ---------- |
26| [baseProfile.BaseProfile](js-apis-bluetooth-baseProfile.md)| **BaseProfile** API definition.|
27
28
29## a2dp.createA2dpSrcProfile
30
31createA2dpSrcProfile(): A2dpSourceProfile
32
33Creates an **A2dpSrcProfile** instance.
34
35**System capability**: SystemCapability.Communication.Bluetooth.Core
36
37**Return value**
38
39| Type                           | Description        |
40| ----------------------------- | ---------- |
41| [A2dpSourceProfile](#a2dpsourceprofile) | **A2dpSrcProfile** instance created.|
42
43**Error codes**
44
45For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
46
47| ID| Error Message|
48| -------- | ---------------------------- |
49|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                         |
50|801 | Capability not supported.                |
51
52
53**Example**
54
55```js
56import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
57try {
58    let a2dpProfile = a2dp.createA2dpSrcProfile();
59    console.info('a2dp success');
60} catch (err) {
61    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
62}
63```
64
65
66## A2dpSourceProfile
67
68Provides APIs for using the A2DP. Before using any API of **A2dpSourceProfile**, you need to create an instance of this class by using [createA2dpSrcProfile()](#a2dpcreatea2dpsrcprofile).
69
70
71### getPlayingState
72
73getPlayingState(deviceId: string): PlayingState
74
75Obtains the playing state of a device.
76
77**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
78
79**System capability**: SystemCapability.Communication.Bluetooth.Core
80
81**Parameters**
82
83| Name   | Type    | Mandatory  | Description     |
84| ------ | ------ | ---- | ------- |
85| deviceId | string | Yes   | Address of the remote device.|
86
87**Return value**
88
89| Type                           | Description        |
90| ----------------------------- | ---------- |
91| [PlayingState](#playingstate) | Playing state of the remote device obtained.|
92
93**Error codes**
94
95For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
96
97| ID| Error Message|
98| -------- | ---------------------------- |
99|201 | Permission denied.                 |
100|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
101|801 | Capability not supported.          |
102|2900001 | Service stopped.                         |
103|2900003 | Bluetooth disabled.                 |
104|2900004 | Profile not supported.                |
105|2900099 | Operation failed.                        |
106
107**Example**
108
109```js
110import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
111try {
112    let a2dpSrc = a2dp.createA2dpSrcProfile();
113    let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
114} catch (err) {
115    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
116}
117```
118
119
120## PlayingState
121
122Enumerates the A2DP playing states.
123
124**System capability**: SystemCapability.Communication.Bluetooth.Core
125
126| Name               | Value   | Description     |
127| ----------------- | ------ | ------- |
128| STATE_NOT_PLAYING | 0x0000 | Not playing. |
129| STATE_PLAYING     | 0x0001 | Playing.|
130
131
132## CodecInfo<sup>11+</sup>
133
134Defines the codec information.
135
136**System capability**: SystemCapability.Communication.Bluetooth.Core
137
138| Name       | Type                   | Read-Only  | Optional  | Description                                    |
139| ------------------- | ----------------------- | ---- | ---- | -------------------------------------- |
140| codecType           | [CodecType](#codectype11)      | No   | No   | Codec type. The default value is **CODEC_TYPE_SBC**.|
141| codecBitsPerSample  | [CodecBitsPerSample](#codecbitspersample11)  | No   | No   | Number of bits per sampling point. The default value is **CODEC_BITS_PER_SAMPLE_NONE**.|
142| codecChannelMode    | [CodecChannelMode](#codecchannelmode11) | No   | No   | Channel mode of the codec. The default value is **CODEC_CHANNEL_MODE_NONE**.|
143| codecSampleRate     | [CodecSampleRate](#codecsamplerate11) | No   | No   | Sampling rate of the codec. The default value is **CODEC_BITS_PER_SAMPLE_NONE**.|
144| codecBitRate<sup>19+<sup/>     | [CodecBitRate](#codecbitrate19) | No   | Yes   | Bit rate of the codec. The default value is **CODEC_BIT_RATE_ABR**.|
145| codecFrameLength<sup>19+<sup/>     | [CodecFrameLength](#codecframelength19) | No   | Yes   | Frame length of the codec. The default value is **CODEC_FRAME_LENGTH_10MS**.|
146
147## CodecInfoList<sup>19+</sup>
148
149Defines the codec parameters.
150
151**System capability**: SystemCapability.Communication.Bluetooth.Core
152
153| Name       | Type                   | Read-Only  | Optional  | Description                                    |
154| ------------------- | ----------------------- | ---- | ---- | -------------------------------------- |
155| codecType           | [CodecType](#codectype11)      | No   | No   | Codec type.|
156| codecBitsPerSampleArray  | [CodecBitsPerSample[]](#codecbitspersample11) | No   | No   | List of the numbers of bits per sampling point for the codec.|
157| codecChannelModeArray    | [CodecChannelMode[]](#codecchannelmode11) | No   | No   | List of audio channel modes for the codec.|
158| codecSampleRateArray     | [CodecSampleRate[]](#codecsamplerate11) | No   | No   | List of sampling rates for the codec.|
159| codecBitRateArray     | [CodecBitRate[]](#codecbitrate19) | No   | No   | List of bit rates for the codec.|
160| codecFrameLengthArray     | [CodecFrameLength[]](#codecframelength19) | No   | No   | List of frame lengths for the codec.|
161
162## CodecType<sup>11+</sup>
163
164Enumerates the Bluetooth codec types.
165
166**System capability**: SystemCapability.Communication.Bluetooth.Core
167
168| Name               | Value   | Description     |
169| ----------------- | ------ | ------- |
170| CODEC_TYPE_INVALID | -1 | Unknown type. |
171| CODEC_TYPE_SBC     | 0 | SBC.|
172| CODEC_TYPE_AAC     | 1 | AAC.|
173| CODEC_TYPE_L2HC    | 2 | L2HC.|
174| CODEC_TYPE_L2HCST<sup>13+</sup>  | 3 | L2HCST.|
175| CODEC_TYPE_LDAC<sup>13+</sup>    | 4 | LDAC.|
176
177
178## CodecChannelMode<sup>11+</sup>
179
180Enumerates the channel modes of the Bluetooth codec.
181
182**System capability**: SystemCapability.Communication.Bluetooth.Core
183
184| Name               | Value   | Description     |
185| ----------------- | ------ | ------- |
186| CODEC_CHANNEL_MODE_NONE   | 0 | Unknown.|
187| CODEC_CHANNEL_MODE_MONO   | 1 | Mono. |
188| CODEC_CHANNEL_MODE_STEREO | 2 | Stereo. |
189
190
191## CodecBitsPerSample<sup>11+</sup>
192
193Enumerates the number of bits per sample for the Bluetooth codec.
194
195**System capability**: SystemCapability.Communication.Bluetooth.Core
196
197| Name               | Value   | Description     |
198| ----------------- | ------ | ------- |
199| CODEC_BITS_PER_SAMPLE_NONE | 0 | Unknown.|
200| CODEC_BITS_PER_SAMPLE_16   | 1 | 16 bits per sample.|
201| CODEC_BITS_PER_SAMPLE_24   | 2 | 24 bits per sample.|
202| CODEC_BITS_PER_SAMPLE_32   | 3 | 32 bits per sample.|
203
204
205## CodecSampleRate<sup>11+</sup>
206
207Enumerates sampling rates of the Bluetooth codec.
208
209**System capability**: SystemCapability.Communication.Bluetooth.Core
210
211| Name               | Value   | Description     |
212| ----------------- | ------ | ------- |
213| CODEC_SAMPLE_RATE_NONE    | 0 | Unknown.|
214| CODEC_SAMPLE_RATE_44100   | 1 | 44.1 kHz.|
215| CODEC_SAMPLE_RATE_48000   | 2 | 48 kHz.|
216| CODEC_SAMPLE_RATE_88200   | 3 | 88.2 kHz.|
217| CODEC_SAMPLE_RATE_96000   | 4 | 96 kHz.|
218| CODEC_SAMPLE_RATE_176400  | 5 | 176.4 kHz.|
219| CODEC_SAMPLE_RATE_192000  | 6 | 192 kHz.|
220
221## CodecBitRate<sup>19+</sup>
222
223Enumerates bit rates of the Bluetooth codec.
224
225**System capability**: SystemCapability.Communication.Bluetooth.Core
226
227| Name               | Value   | Description     |
228| ----------------- | ------ | ------- |
229| CODEC_BIT_RATE_96000    | 0 | 96 kbit/s|
230| CODEC_BIT_RATE_128000   | 1 | 128 kbit/s|
231| CODEC_BIT_RATE_192000   | 2 | 192 kbit/s|
232| CODEC_BIT_RATE_256000   | 3 | 256 kbit/s|
233| CODEC_BIT_RATE_320000   | 4 | 320 kbit/s|
234| CODEC_BIT_RATE_480000  | 5 | 480 kbit/s|
235| CODEC_BIT_RATE_640000  | 6 | 640 kbit/s|
236| CODEC_BIT_RATE_960000  | 7 | 960 kbit/s|
237| CODEC_BIT_RATE_ABR  | 8 | Adaptive bit rate.|
238
239## CodecFrameLength<sup>19+</sup>
240
241Enumerates frame lengths of the Bluetooth codec.
242
243**System capability**: SystemCapability.Communication.Bluetooth.Core
244
245| Name               | Value   | Description     |
246| ----------------- | ------ | ------- |
247| CODEC_FRAME_LENGTH_5MS    | 0 | 5 ms|
248| CODEC_FRAME_LENGTH_10MS   | 1 | 10 ms|
249