openharmony 鸿蒙 js-apis-bluetooth-a2dp

2025-06-12 浏览 (1)

@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

import { a2dp } from '@kit.ConnectivityKit';

BaseProfile

type BaseProfile = baseProfile.BaseProfile

Defines BaseProfile APIs.

System capability: SystemCapability.Communication.Bluetooth.Core

TypeDescription
baseProfile.BaseProfileBaseProfile API definition.

a2dp.createA2dpSrcProfile

createA2dpSrcProfile(): A2dpSourceProfile

Creates an A2dpSrcProfile instance.

System capability: SystemCapability.Communication.Bluetooth.Core

Return value

TypeDescription
A2dpSourceProfileA2dpSrcProfile instance created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.

Example

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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().

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

NameTypeMandatoryDescription
deviceIdstringYesAddress of the remote device.

Return value

TypeDescription
PlayingStatePlaying state of the remote device obtained.

Error codes

For details about the error codes, see Universal Error Codes and Bluetooth Error Codes.

IDError Message
201Permission denied.
401Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801Capability not supported.
2900001Service stopped.
2900003Bluetooth disabled.
2900004Profile not supported.
2900099Operation failed.

Example

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
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);
}

PlayingState

Enumerates the A2DP playing states.

System capability: SystemCapability.Communication.Bluetooth.Core

NameValueDescription
STATE_NOT_PLAYING0x0000Not playing.
STATE_PLAYING0x0001Playing.

CodecInfo11+

Defines the codec information.

System capability: SystemCapability.Communication.Bluetooth.Core

NameTypeReadableWritableDescription
codecTypeCodecTypeYesYesCodec type. The default value is CODEC_TYPE_SBC.
codecBitsPerSampleCodecBitsPerSampleYesYesNumber of bits of each sample. The default value is SCAN_MODE_LOW_POWER.
codecChannelModeCodecChannelModeYesYesChannel mode of the codec. The default value is CODEC_CHANNEL_MODE_NONE.
codecSampleRateCodecSampleRateYesYesSampling 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

NameValueDescription
CODEC_TYPE_INVALID-1Unknown type.
CODEC_TYPE_SBC0SBC.
CODEC_TYPE_AAC1AAC.
CODEC_TYPE_L2HC2L2HC.
CODEC_TYPE_L2HCST13+3L2HCST.
CODEC_TYPE_LDAC13+4LDAC.

CodecChannelMode11+

Enumerates the channel modes of the Bluetooth codec.

System capability: SystemCapability.Communication.Bluetooth.Core

NameValueDescription
CODEC_CHANNEL_MODE_NONE0Unknown.
CODEC_CHANNEL_MODE_MONO1Mono.
CODEC_CHANNEL_MODE_STEREO2Stereo.

CodecBitsPerSample11+

Enumerates the number of bits per sample for the Bluetooth codec.

System capability: SystemCapability.Communication.Bluetooth.Core

NameValueDescription
CODEC_BITS_PER_SAMPLE_NONE0Unknown.
CODEC_BITS_PER_SAMPLE_16116 bits per sample.
CODEC_BITS_PER_SAMPLE_24224 bits per sample.
CODEC_BITS_PER_SAMPLE_32332 bits per sample.

CodecSampleRate11+

Enumerates the sampling rates of the Bluetooth codec.

System capability: SystemCapability.Communication.Bluetooth.Core

NameValueDescription
CODEC_SAMPLE_RATE_NONE0Unknown.
CODEC_SAMPLE_RATE_44100144.1 kHz.
CODEC_SAMPLE_RATE_48000248 kHz.
CODEC_SAMPLE_RATE_88200388.2 kHz.
CODEC_SAMPLE_RATE_96000496 kHz.
CODEC_SAMPLE_RATE_1764005176.4 kHz.
CODEC_SAMPLE_RATE_1920006192 kHz.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Connectivity Kit (Short-Range Communication Service)

harmony 鸿蒙Bluetooth

harmony 鸿蒙Wifi

harmony 鸿蒙Bluetooth Error Codes

harmony 鸿蒙NFC Error Codes

harmony 鸿蒙SecureElement Error Codes

harmony 鸿蒙Wi-Fi Error Codes

harmony 鸿蒙@ohos.bluetooth.a2dp (Bluetooth A2DP Module) (System API)

harmony 鸿蒙@ohos.bluetooth.access (Bluetooth Access Module) (System API)

harmony 鸿蒙@ohos.bluetooth.access (Bluetooth Access Module)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/jrzRLf