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## a2dp.createA2dpSrcProfile
17
18createA2dpSrcProfile(): A2dpSourceProfile
19
20Creates an **A2dpSrcProfile** instance.
21
22**System capability**: SystemCapability.Communication.Bluetooth.Core
23
24**Return value**
25
26| Type                           | Description        |
27| ----------------------------- | ---------- |
28| [A2dpSourceProfile](#a2dpsourceprofile) | **A2dpSrcProfile** instance created.|
29
30**Error codes**
31
32For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
33
34| ID| Error Message|
35| -------- | ---------------------------- |
36|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                         |
37|801 | Capability not supported.                |
38
39
40**Example**
41
42```js
43import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
44try {
45    let a2dpProfile = a2dp.createA2dpSrcProfile();
46    console.info('a2dp success');
47} catch (err) {
48    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
49}
50```
51
52
53## A2dpSourceProfile
54
55Provides APIs for using the A2DP. Before using any API of **A2dpSourceProfile**, you need to create an instance of this class by using **createA2dpSrcProfile()**.
56
57
58### getPlayingState
59
60getPlayingState(deviceId: string): PlayingState
61
62Obtains the playing state of a device.
63
64**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
65
66**System capability**: SystemCapability.Communication.Bluetooth.Core
67
68**Parameters**
69
70| Name   | Type    | Mandatory  | Description     |
71| ------ | ------ | ---- | ------- |
72| deviceId | string | Yes   | Address of the target device.|
73
74**Return value**
75
76| Type                           | Description        |
77| ----------------------------- | ---------- |
78| [PlayingState](#playingstate) | Playing state of the remote device obtained.|
79
80**Error codes**
81
82For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
83
84| ID| Error Message|
85| -------- | ---------------------------- |
86|201 | Permission denied.                 |
87|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
88|801 | Capability not supported.          |
89|2900001 | Service stopped.                         |
90|2900003 | Bluetooth disabled.                 |
91|2900004 | Profile not supported.                |
92|2900099 | Operation failed.                        |
93
94**Example**
95
96```js
97import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
98try {
99    let a2dpSrc = a2dp.createA2dpSrcProfile();
100    let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
101} catch (err) {
102    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
103}
104```
105
106
107## PlayingState
108
109Enumerates the A2DP playing states.
110
111**System capability**: SystemCapability.Communication.Bluetooth.Core
112
113| Name               | Value   | Description     |
114| ----------------- | ------ | ------- |
115| STATE_NOT_PLAYING | 0x0000 | Not playing. |
116| STATE_PLAYING     | 0x0001 | Playing.|
117
118
119## CodecInfo<sup>11+</sup>
120
121Defines the codec information.
122
123**System capability**: SystemCapability.Communication.Bluetooth.Core
124
125| Name       | Type                   | Readable  | Writable  | Description                                    |
126| ------------------- | ----------------------- | ---- | ---- | -------------------------------------- |
127| codecType<sup>11+</sup>           | [CodecType](#codectype11)      | Yes   | Yes   | Codec type. The default value is **CODEC_TYPE_SBC**.|
128| codecBitsPerSample<sup>11+</sup>  | [CodecBitsPerSample](#codecbitspersample11)  | Yes   | Yes   | Number of bits of each sample. The default value is **SCAN_MODE_LOW_POWER**.|
129| codecChannelMode<sup>11+</sup>    | [CodecChannelMode](#codecchannelmode11) | Yes   | Yes   | Channel mode of the codec. The default value is **CODEC_CHANNEL_MODE_NONE**.|
130| codecSampleRate<sup>11+</sup>     | [CodecSampleRate](#codecsamplerate11) | Yes   | Yes   | Sampling rate of the codec. The default value is **CODEC_BITS_PER_SAMPLE_NONE**.|
131
132
133## CodecType<sup>11+</sup>
134
135Enumerates the Bluetooth codec types.
136
137**System capability**: SystemCapability.Communication.Bluetooth.Core
138
139| Name               | Value   | Description     |
140| ----------------- | ------ | ------- |
141| CODEC_TYPE_INVALID<sup>11+</sup> | -1 | Unknown type. |
142| CODEC_TYPE_SBC<sup>11+</sup>     | 0 | SBC.|
143| CODEC_TYPE_AAC<sup>11+</sup>     | 1 | AAC.|
144| CODEC_TYPE_L2HC<sup>11+</sup>    | 2 | L2HC.|
145| CODEC_TYPE_L2HCST<sup>13+</sup>  | 3 | L2HCST.|
146| CODEC_TYPE_LDAC<sup>13+</sup>    | 4 | LDAC.|
147
148
149## CodecChannelMode<sup>11+</sup>
150
151Enumerates the channel modes of the Bluetooth codec.
152
153**System capability**: SystemCapability.Communication.Bluetooth.Core
154
155| Name               | Value   | Description     |
156| ----------------- | ------ | ------- |
157| CODEC_CHANNEL_MODE_NONE<sup>11+</sup>   | 0 | Unknown.|
158| CODEC_CHANNEL_MODE_MONO<sup>11+</sup>   | 1 | Mono. |
159| CODEC_CHANNEL_MODE_STEREO<sup>11+</sup> | 2 | Stereo. |
160
161
162## CodecBitsPerSample<sup>11+</sup>
163
164Enumerates the number of bits per sample for the Bluetooth codec.
165
166**System capability**: SystemCapability.Communication.Bluetooth.Core
167
168| Name               | Value   | Description     |
169| ----------------- | ------ | ------- |
170| CODEC_BITS_PER_SAMPLE_NONE<sup>11+</sup> | 0 | Unknown.|
171| CODEC_BITS_PER_SAMPLE_16<sup>11+</sup>   | 1 | 16 bits per sample.|
172| CODEC_BITS_PER_SAMPLE_24<sup>11+</sup>   | 2 | 24 bits per sample.|
173| CODEC_BITS_PER_SAMPLE_32<sup>11+</sup>   | 3 | 32 bits per sample.|
174
175
176## CodecSampleRate<sup>11+</sup>
177
178Enumerates the sampling rates of the Bluetooth codec.
179
180**System capability**: SystemCapability.Communication.Bluetooth.Core
181
182| Name               | Value   | Description     |
183| ----------------- | ------ | ------- |
184| CODEC_SAMPLE_RATE_NONE<sup>11+</sup>    | 0 | Unknown.|
185| CODEC_SAMPLE_RATE_44100<sup>11+</sup>   | 1 | 44.1 kHz.|
186| CODEC_SAMPLE_RATE_48000<sup>11+</sup>   | 2 | 48 kHz.|
187| CODEC_SAMPLE_RATE_88200<sup>11+</sup>   | 3 | 88.2 kHz.|
188| CODEC_SAMPLE_RATE_96000<sup>11+</sup>   | 4 | 96 kHz.|
189| CODEC_SAMPLE_RATE_176400<sup>11+</sup>  | 5 | 176.4 kHz.|
190| CODEC_SAMPLE_RATE_192000<sup>11+</sup>  | 6 | 192 kHz.|
191