# @ohos.bluetooth.a2dp (蓝牙a2dp模块)(系统接口)
a2dp模块提供了访问蓝牙音频接口的方法。
> **说明:**
>
> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.a2dp (蓝牙a2dp模块)](js-apis-bluetooth-a2dp.md)
## 导入模块
```js
import { a2dp } from '@kit.ConnectivityKit';
```
### connect
connect(deviceId: string): void
发起设备的A2dp服务连接请求。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900004 | Profile not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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
断开设备的a2dp服务连接。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900004 | Profile not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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);
}
```
### isAbsoluteVolumeSupported11+
isAbsoluteVolumeSupported(deviceId: string, callback: AsyncCallback<boolean>): void
获取设备是否支持绝对音量能力。使用Callback异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
| callback | AsyncCallback<boolean> | 是 | 通过注册回调函数获取设备是否支持绝对音量。如果成功,值在supported中返回。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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>
获取设备是否支持绝对音量能力。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<boolean> | 通过promise形式获取设备是否支持绝对音量。如果成功,值在supported中返回。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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
获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。使用Callback异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
| callback | AsyncCallback<boolean> | 是 | 通过注册回调函数获取设备绝对音量是否开启。如果成功,值在enabled中返回。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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>
获取设备绝对音量能力是否开启。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再获取设备绝对音量能力是否开启。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<boolean> | 通过promise形式获取设备绝对音量是否开启。如果成功,值在enabled中返回。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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
开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。使用Callback异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
| callback | AsyncCallback<void> | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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>
开启设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再开启设备绝对音量能力。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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
关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。使用Callback异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
| callback | AsyncCallback<void> | 是 | 回调函数。如果成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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>
关闭设备绝对音量能力。需要在设备支持绝对音量的情况下(参考[isAbsoluteVolumeSupported](#isabsolutevolumesupported11)),再关闭设备绝对音量能力。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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
获取当前编码器信息。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| [CodecInfo](js-apis-bluetooth-a2dp#codecinfo11)| 当前编码器信息。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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
设置当前编码器信息。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
| codecInfo | [CodecInfo](js-apis-bluetooth-a2dp.md#codecinfo11) | 是 | 编码器信息。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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);
}
```
### disableAutoPlay12+
disableAutoPlay(deviceId: string, duration: number): Promise<void>
限制设备在连接成功的若干毫秒内播放音乐。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址,例如:"11:22:33:AA:BB:FF"。 |
| duration | number | 是 | 拦截时长,单位毫秒。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let durationNumber = 1000;
a2dpSrc.disableAutoPlay('XX:XX:XX:XX:XX:XX', durationNumber).then(() => {
console.info("disableAutoPlay");
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### enableAutoPlay12+
enableAutoPlay(deviceId: string): Promise<void>
允许设备在连接成功后自动播放音乐。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址,例如:"11:22:33:AA:BB:FF"。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.enableAutoPlay('XX:XX:XX:XX:XX:XX').then(() => {
console.info("enableAutoPlay");
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```
### getAutoPlayDisabledDuration12+
getAutoPlayDisabledDuration(deviceId: string): Promise<number>
获取拦截时长或自动播放开关。使用Promise异步回调。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址,例如:"11:22:33:AA:BB:FF"。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | ---------- |
| Promise<number> | 以Promise的形式返回结果。number为返回的拦截时长,单位为毫秒;如果返回-1,则表示允许设备在连接成功后自动播放音乐。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|201 | Permission denied. |
|202 | Non-system applications are not allowed to use system APIs. |
|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
|801 | Capability not supported. |
|2900001 | Service stopped. |
|2900003 | Bluetooth disabled. |
|2900099 | Operation failed. |
**示例:**
```js
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.getAutoPlayDisabledDuration('XX:XX:XX:XX:XX:XX').then((data: number) => {
console.info('number' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
```