• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.a2dp (蓝牙a2dp模块)
2
3a2dp模块提供了访问蓝牙音频接口的方法。
4
5> **说明:**
6>
7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10
11## 导入模块
12
13```js
14import a2dp from '@ohos.bluetooth.a2dp';
15```
16
17## a2dp.createA2dpSrcProfile<a name="createA2dpSrcProfile"></a>
18
19createA2dpSrcProfile(): A2dpSourceProfile
20
21创建a2dp profile实例。
22
23**系统能力**:SystemCapability.Communication.Bluetooth.Core24
25**返回值:**
26
27| 类型                            | 说明         |
28| ----------------------------- | ---------- |
29| A2dpSourceProfile | 返回该profile的实例。 |
30
31**示例:**
32
33```js
34import { BusinessError } from '@ohos.base';
35try {
36    let a2dpProfile = a2dp.createA2dpSrcProfile();
37    console.info('a2dp success');
38} catch (err) {
39    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
40}
41```
42
43
44## A2dpSourceProfile
45
46使用A2dpSourceProfile方法之前需要创建该类的实例进行操作,通过createA2dpSrcProfile()方法构造此实例。
47
48
49### connect<a name="a2dp-connect"></a>
50
51connect(deviceId: string): void
52
53发起设备的A2dp服务连接请求。
54
55**系统接口**:此接口为系统接口。
56
57**需要权限**:ohos.permission.ACCESS_BLUETOOTH
58
59**系统能力**:SystemCapability.Communication.Bluetooth.Core60
61**参数:**
62
63| 参数名    | 类型     | 必填   | 说明      |
64| ------ | ------ | ---- | ------- |
65| deviceId | string | 是    | 远端设备地址。 |
66
67**错误码**:
68
69以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
70
71| 错误码ID | 错误信息 |
72| -------- | ---------------------------- |
73|2900001 | Service stopped.               |
74|2900003 | Bluetooth switch is off.       |
75|2900004 | Profile is not supported.      |
76|2900099 | Operation failed.              |
77
78**示例:**
79
80```js
81import { BusinessError } from '@ohos.base';
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
95断开设备的a2dp服务连接。
96
97**系统接口**:此接口为系统接口。
98
99**需要权限**:ohos.permission.ACCESS_BLUETOOTH
100
101**系统能力**:SystemCapability.Communication.Bluetooth.Core102
103**参数:**
104
105| 参数名    | 类型     | 必填   | 说明      |
106| ------ | ------ | ---- | ------- |
107| deviceId | string | 是    | 远端设备地址。 |
108
109**错误码**:
110
111以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
112
113| 错误码ID | 错误信息 |
114| -------- | ---------------------------- |
115|2900001 | Service stopped.                         |
116|2900003 | Bluetooth switch is off.                 |
117|2900004 | Profile is not supported.                |
118|2900099 | Operation failed.                        |
119
120**示例:**
121
122```js
123import { BusinessError } from '@ohos.base';
124try {
125    let a2dpSrc = a2dp.createA2dpSrcProfile();
126    a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
127} catch (err) {
128    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
129}
130```
131
132
133### getPlayingState
134
135getPlayingState(deviceId: string): PlayingState
136
137获取设备的播放状态。
138
139**需要权限**:ohos.permission.ACCESS_BLUETOOTH
140
141**系统能力**:SystemCapability.Communication.Bluetooth.Core142
143**参数:**
144
145| 参数名    | 类型     | 必填   | 说明      |
146| ------ | ------ | ---- | ------- |
147| device | string | 是    | 远端设备地址。 |
148
149**返回值:**
150
151| 类型                            | 说明         |
152| ----------------------------- | ---------- |
153| [PlayingState](#PlayingState) | 远端设备的播放状态。 |
154
155**错误码**:
156
157以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
158
159| 错误码ID | 错误信息 |
160| -------- | ---------------------------- |
161|2900001 | Service stopped.                         |
162|2900003 | Bluetooth switch is off.                 |
163|2900004 | Profile is not supported.                |
164|2900099 | Operation failed.                        |
165
166**示例:**
167
168```js
169import { BusinessError } from '@ohos.base';
170try {
171    let a2dpSrc = a2dp.createA2dpSrcProfile();
172    let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
173} catch (err) {
174    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
175}
176```
177
178## PlayingState<a name="PlayingState"></a>
179
180枚举,蓝牙A2DP 播放状态。
181
182**系统能力**:SystemCapability.Communication.Bluetooth.Core183
184| 名称                | 值    | 说明      |
185| ----------------- | ------ | ------- |
186| STATE_NOT_PLAYING | 0x0000 | 表示未播放。  |
187| STATE_PLAYING     | 0x0001 | 表示正在播放。 |