# @ohos.bluetooth.a2dp (Bluetooth A2DP Module)
The **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.
> **NOTE**
>
> 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.
## Modules to Import
```js
import a2dp from '@ohos.bluetooth.a2dp';
```
## a2dp.createA2dpSrcProfile
createA2dpSrcProfile(): A2dpSourceProfile
Creates an **A2dpSrcProfile** instance.
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| A2dpSourceProfile | **A2dpSrcProfile** instance created.|
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpProfile = a2dp.createA2dpSrcProfile();
console.info('a2dp success');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
## A2dpSourceProfile
Provides APIs for using the A2DP. Before using any API of **A2dpSourceProfile**, you need to create an instance of this class by using **createA2dpSrcProfile()**.
### connect
connect(deviceId: string): void
Connects to an A2DP device.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the device to connect. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.connect('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### disconnect
disconnect(deviceId: string): void
Disconnects from an A2DP device.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the device to disconnect. |
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### getPlayingState
getPlayingState(deviceId: string): PlayingState
Obtains the playing state of a device.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| device | string | Yes | Address of the target device. |
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| [PlayingState](#playingstate) | Playing state of the remote device obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### isAbsoluteVolumeSupported11+
isAbsoluteVolumeSupported(deviceId: string, callback: AsyncCallback<boolean>): void
Checks whether a device supports the absolute volume capability. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the device to check. |
| callback | AsyncCallback<boolean> | Yes| Callback invoked to return the result. If the device supports absolute volume, **supported** is returned.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX', (err, supported) => {
console.info('device support absolute volume ' + supported);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### isAbsoluteVolumeSupported11+
isAbsoluteVolumeSupported(deviceId: string): Promise<boolean>
Checks whether a device supports the absolute volume capability. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the device to check. |
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| Promise<boolean> | Promise used to return the result. If the device supports absolute volume, **supported** is returned.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.isAbsoluteVolumeSupported('XX:XX:XX:XX:XX:XX').then((supported) => {
console.info('device support absolute volume ' + supported);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### isAbsoluteVolumeEnabled11+
isAbsoluteVolumeEnabled(deviceId: string, callback: AsyncCallback<boolean>): void
Checks whether the absolute volume capability is enabled for a device. This API uses an asynchronous callback to return the result. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the device to check. |
| callback | AsyncCallback<boolean> | Yes| Callback invoked to return the result. If absolute volume is enabled, **enabled** is returned.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => {
console.info('device absolute volume enable ' + enabled);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### isAbsoluteVolumeEnabled11+
isAbsoluteVolumeEnabled(deviceId: string): Promise<boolean>
Checks whether the absolute volume capability is enabled for a device. This API uses a promise to return the result. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the device to check. |
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| Promise<boolean> | Promise used to return the result. If absolute volume is enabled, **enabled** is returned.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.isAbsoluteVolumeEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => {
console.info('device absolute volume enable ' + enabled);
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### enableAbsoluteVolume11+
enableAbsoluteVolume(deviceId: string, callback: AsyncCallback<void>): void
Enables the absolute volume capability for a device. This API uses an asynchronous callback to return the result. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the target device. |
| callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => {
if (err) {
console.error("enableAbsoluteVolume error");
}
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### enableAbsoluteVolume11+
enableAbsoluteVolume(deviceId: string): Promise<void>
Enables the absolute volume capability for a device. This API uses a promise to return the result. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the target device. |
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.enableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => {
console.info("enableAbsoluteVolume");
}
);
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### disableAbsoluteVolume11+
disableAbsoluteVolume(deviceId: string, callback: AsyncCallback<void>): void
Disables the absolute volume capability for a device. This API uses an asynchronous callback to return the result. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the target device. |
| callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX', (err) => {
if (err) {
console.error("disableAbsoluteVolume error");
}
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### disableAbsoluteVolume11+
disableAbsoluteVolume(deviceId: string): Promise<void>
Disables the absolute volume capability for a device. This API uses a promise to return the result. Before using this API, use [isAbsoluteVolumeSupported](#isabsolutevolumesupported11) to check whether the device supports the absolute volume capability.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the target device. |
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.disableAbsoluteVolume('XX:XX:XX:XX:XX:XX').then(() => {
console.info("disableAbsoluteVolume");
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### getCurrentCodecInfo11+
getCurrentCodecInfo(deviceId: string): CodecInfo
Obtains the current codec information.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the remote device.|
**Return value**
| Type | Description |
| ----------------------------- | ---------- |
| [CodecInfo](#codecinfo11)| Codec information obtained.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let codecInfo : a2dp.CodecInfo = a2dpSrc.getCurrentCodecInfo('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### setCurrentCodecInfo11+
setCurrentCodecInfo(deviceId: string, codecInfo: CodecInfo): void
Sets the current codec information.
**System API**: This is a system API.
**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
**System capability**: SystemCapability.Communication.Bluetooth.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | ------- |
| deviceId | string | Yes | Address of the remote device.|
| codecInfo | [CodecInfo](#codecinfo11) | Yes | Codec information to set.|
**Error codes**
For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
| ID| Error Message|
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900099 | Operation failed. |
**Example**
```js
import { BusinessError } from '@ohos.base';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let codecInfo : a2dp.CodecInfo = {
codecType: 0,
codecBitsPerSample: 1,
codecChannelMode: 2,
codecSampleRate: 1,
}
a2dpSrc.setCurrentCodecInfo('XX:XX:XX:XX:XX:XX', codecInfo);
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
## PlayingState
Enumerates the A2DP playing states.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ----------------- | ------ | ------- |
| STATE_NOT_PLAYING | 0x0000 | Not playing. |
| STATE_PLAYING | 0x0001 | Playing.|
## CodecInfo11+
Defines the codec information.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Type | Readable | Writable | Description |
| ------------------- | ----------------------- | ---- | ---- | -------------------------------------- |
| codecType11+ | [CodecType](#codectype11) | Yes | Yes | Codec type. The default value is **CODEC_TYPE_SBC**.|
| codecBitsPerSample11+ | [CodecBitsPerSample](#codecbitspersample11) | Yes | Yes | Number of bits of each sample. The default value is **SCAN_MODE_LOW_POWER**.|
| codecChannelMode11+ | [CodecChannelMode](#codecchannelmode11) | Yes | Yes | Channel mode of the codec. The default value is **CODEC_CHANNEL_MODE_NONE**.|
| codecSampleRate11+ | [CodecSampleRate](#codecsamplerate11) | Yes | Yes | Sampling rate of the codec. The default value is **CODEC_BITS_PER_SAMPLE_NONE**.|
## CodecType11+
Enumerates the Bluetooth codec types.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ----------------- | ------ | ------- |
| CODEC_TYPE_INVALID11+ | -1 | Unknown type. |
| CODEC_TYPE_SBC11+ | 0 | SBC.|
| CODEC_TYPE_AAC11+ | 1 | AAC.|
| CODEC_TYPE_L2HC11+ | 2 | L2HC.|
## CodecChannelMode11+
Enumerates the channel modes of the Bluetooth codec.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ----------------- | ------ | ------- |
| CODEC_CHANNEL_MODE_NONE11+ | 0 | Unknown.|
| CODEC_CHANNEL_MODE_MONO11+ | 1 | Mono. |
| CODEC_CHANNEL_MODE_STEREO11+ | 2 | Stereo. |
## CodecBitsPerSample11+
Enumerates the number of bits per sample for the Bluetooth codec.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ----------------- | ------ | ------- |
| CODEC_BITS_PER_SAMPLE_NONE11+ | 0 | Unknown.|
| CODEC_BITS_PER_SAMPLE_1611+ | 1 | 16 bits per sample.|
| CODEC_BITS_PER_SAMPLE_2411+ | 2 | 24 bits per sample.|
| CODEC_BITS_PER_SAMPLE_3211+ | 3 | 32 bits per sample.|
## CodecSampleRate11+
Enumerates the sampling rates of the Bluetooth codec.
**System capability**: SystemCapability.Communication.Bluetooth.Core
| Name | Value | Description |
| ----------------- | ------ | ------- |
| CODEC_SAMPLE_RATE_NONE11+ | 0 | Unknown.|
| CODEC_SAMPLE_RATE_4410011+ | 1 | 44.1 kHz.|
| CODEC_SAMPLE_RATE_4800011+ | 2 | 48 kHz.|
| CODEC_SAMPLE_RATE_8820011+ | 3 | 88.2 kHz.|
| CODEC_SAMPLE_RATE_9600011+ | 4 | 96 kHz.|
| CODEC_SAMPLE_RATE_17640011+ | 5 | 176.4 kHz.|
| CODEC_SAMPLE_RATE_19200011+ | 6 | 192 kHz.|