• 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
11## Modules to Import
12
13```js
14import a2dp from '@ohos.bluetooth.a2dp';
15import { BusinessError } from '@ohos.base';
16```
17
18
19## a2dp.createA2dpSrcProfile<a name="createA2dpSrcProfile"></a>
20
21createA2dpSrcProfile(): A2dpSourceProfile
22
23Creates an **A2dpSrcProfile** instance.
24
25**System capability**: SystemCapability.Communication.Bluetooth.Core
26
27**Return value**
28
29| Type                           | Description        |
30| ----------------------------- | ---------- |
31| A2dpSourceProfile | **A2dpSrcProfile** instance created.|
32
33**Example**
34
35```js
36try {
37    let a2dpProfile = a2dp.createA2dpSrcProfile();
38    console.info('a2dp success');
39} catch (err) {
40    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
41}
42```
43
44
45## A2dpSourceProfile
46
47Provides APIs for using the A2DP. Before using any API of **A2dpSourceProfile**, you need to create an instance of this class by using **createA2dpSrcProfile()**.
48
49
50### connect<a name="a2dp-connect"></a>
51
52connect(deviceId: string): void
53
54Connects to an A2DP device.
55
56**System API**: This is a system API.
57
58**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
59
60**System capability**: SystemCapability.Communication.Bluetooth.Core
61
62**Parameters**
63
64| Name   | Type    | Mandatory  | Description     |
65| ------ | ------ | ---- | ------- |
66| deviceId | string | Yes   | Address of the device to connect. |
67
68**Error codes**
69
70For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
71
72| ID| Error Message|
73| -------- | ---------------------------- |
74|2900001 | Service stopped.               |
75|2900003 | Bluetooth switch is off.       |
76|2900004 | Profile is not supported.      |
77|2900099 | Operation failed.              |
78
79**Example**
80
81```js
82try {
83    let a2dpSrc = a2dp.createA2dpSrcProfile();
84    a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
85} catch (err) {
86    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
87}
88```
89
90
91### disconnect<a name="a2dp-disconnect"></a>
92
93disconnect(deviceId: string): void
94
95Disconnects from an A2DP device.
96
97**System API**: This is a system API.
98
99**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
100
101**System capability**: SystemCapability.Communication.Bluetooth.Core
102
103**Parameters**
104
105| Name   | Type    | Mandatory  | Description     |
106| ------ | ------ | ---- | ------- |
107| deviceId | string | Yes   | Address of the device to disconnect. |
108
109**Error codes**
110
111For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
112
113| ID| Error Message|
114| -------- | ---------------------------- |
115|2900001 | Service stopped.                         |
116|2900003 | Bluetooth switch is off.                 |
117|2900004 | Profile is not supported.                |
118|2900099 | Operation failed.                        |
119
120**Example**
121
122```js
123try {
124    let a2dpSrc = a2dp.createA2dpSrcProfile();
125    a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
126} catch (err) {
127    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
128}
129```
130
131
132### getPlayingState
133
134getPlayingState(deviceId: string): PlayingState
135
136Obtains the playing state of a device.
137
138**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
139
140**System capability**: SystemCapability.Communication.Bluetooth.Core
141
142**Parameters**
143
144| Name   | Type    | Mandatory  | Description     |
145| ------ | ------ | ---- | ------- |
146| device | string | Yes   | Address of the remote device.|
147
148**Return value**
149
150| Type                           | Description        |
151| ----------------------------- | ---------- |
152| [PlayingState](#PlayingState) | Playing state of the remote device obtained.|
153
154**Error codes**
155
156For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
157
158| ID| Error Message|
159| -------- | ---------------------------- |
160|2900001 | Service stopped.                         |
161|2900003 | Bluetooth switch is off.                 |
162|2900004 | Profile is not supported.                |
163|2900099 | Operation failed.                        |
164
165**Example**
166
167```js
168try {
169    let a2dpSrc = a2dp.createA2dpSrcProfile();
170    let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
171} catch (err) {
172    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
173}
174```
175
176## PlayingState<a name="PlayingState"></a>
177
178Enumerates the A2DP playing states.
179
180**System capability**: SystemCapability.Communication.Bluetooth.Core
181
182| Name               | Value   | Description     |
183| ----------------- | ------ | ------- |
184| STATE_NOT_PLAYING | 0x0000 | Not playing. |
185| STATE_PLAYING     | 0x0001 | Playing.|
186