1# @ohos.bluetooth.a2dp (蓝牙a2dp模块) 2 3a2dp模块提供了访问蓝牙音频接口的方法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```js 13import { a2dp } from '@kit.ConnectivityKit'; 14``` 15 16## BaseProfile 17 18type BaseProfile = baseProfile.BaseProfile 19 20基础Profile接口定义。 21 22**系统能力**:SystemCapability.Communication.Bluetooth.Core。 23 24| 类型 | 说明 | 25| ----------------------------- | ---------- | 26| [baseProfile.BaseProfile](js-apis-bluetooth-baseProfile.md#baseprofile) | 基础Profile接口定义。 | 27 28 29## a2dp.createA2dpSrcProfile 30 31createA2dpSrcProfile(): A2dpSourceProfile 32 33创建a2dp profile实例。 34 35**系统能力**:SystemCapability.Communication.Bluetooth.Core。 36 37**返回值:** 38 39| 类型 | 说明 | 40| ----------------------------- | ---------- | 41| [A2dpSourceProfile](#a2dpsourceprofile) | 返回该profile的实例。 | 42 43**错误码**: 44 45以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 46 47| 错误码ID | 错误信息 | 48| -------- | ---------------------------- | 49|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 50|801 | Capability not supported. | 51 52 53**示例:** 54 55```js 56import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 57try { 58 let a2dpProfile = a2dp.createA2dpSrcProfile(); 59 console.info('a2dp success'); 60} catch (err) { 61 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 62} 63``` 64 65 66## A2dpSourceProfile 67 68使用A2dpSourceProfile方法之前需要创建该类的实例进行操作,通过createA2dpSrcProfile()方法构造此实例。 69 70 71### getPlayingState 72 73getPlayingState(deviceId: string): PlayingState 74 75获取设备的播放状态。 76 77**需要权限**:ohos.permission.ACCESS_BLUETOOTH 78 79**系统能力**:SystemCapability.Communication.Bluetooth.Core。 80 81**参数:** 82 83| 参数名 | 类型 | 必填 | 说明 | 84| ------ | ------ | ---- | ------- | 85| deviceId | string | 是 | 远端设备地址。 | 86 87**返回值:** 88 89| 类型 | 说明 | 90| ----------------------------- | ---------- | 91| [PlayingState](#playingstate) | 远端设备的播放状态。 | 92 93**错误码**: 94 95以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 96 97| 错误码ID | 错误信息 | 98| -------- | ---------------------------- | 99|201 | Permission denied. | 100|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 101|801 | Capability not supported. | 102|2900001 | Service stopped. | 103|2900003 | Bluetooth disabled. | 104|2900004 | Profile not supported. | 105|2900099 | Operation failed. | 106 107**示例:** 108 109```js 110import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 111try { 112 let a2dpSrc = a2dp.createA2dpSrcProfile(); 113 let state = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX'); 114} catch (err) { 115 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 116} 117``` 118 119 120## PlayingState 121 122枚举,蓝牙A2DP 播放状态。 123 124**系统能力**:SystemCapability.Communication.Bluetooth.Core。 125 126| 名称 | 值 | 说明 | 127| ----------------- | ------ | ------- | 128| STATE_NOT_PLAYING | 0x0000 | 表示未播放。 | 129| STATE_PLAYING | 0x0001 | 表示正在播放。 | 130 131 132## CodecInfo<sup>11+</sup> 133 134编码器信息。 135 136**系统能力**:SystemCapability.Communication.Bluetooth.Core。 137 138| 名称 | 类型 | 可读 | 可写 | 说明 | 139| ------------------- | ----------------------- | ---- | ---- | -------------------------------------- | 140| codecType<sup>11+</sup> | [CodecType](#codectype11) | 是 | 是 | 表示编码器类型,默认值为CODEC_TYPE_SBC。 | 141| codecBitsPerSample<sup>11+</sup> | [CodecBitsPerSample](#codecbitspersample11) | 是 | 是 | 表示每个采样点的位数,默认值为SCAN_MODE_LOW_POWER。 | 142| codecChannelMode<sup>11+</sup> | [CodecChannelMode](#codecchannelmode11) | 是 | 是 | 表示编码器的声道模式,默认值为CODEC_CHANNEL_MODE_NONE。 | 143| codecSampleRate<sup>11+</sup> | [CodecSampleRate](#codecsamplerate11) | 是 | 是 | 表示编码器的采样率,默认值为CODEC_BITS_PER_SAMPLE_NONE。 | 144 145 146## CodecType<sup>11+</sup> 147 148枚举,蓝牙编码器类型。 149 150**系统能力**:SystemCapability.Communication.Bluetooth.Core。 151 152| 名称 | 值 | 说明 | 153| ----------------- | ------ | ------- | 154| CODEC_TYPE_INVALID<sup>11+</sup> | -1 | 未知编码类型。 | 155| CODEC_TYPE_SBC<sup>11+</sup> | 0 | SBC。 | 156| CODEC_TYPE_AAC<sup>11+</sup> | 1 | AAC。 | 157| CODEC_TYPE_L2HC<sup>11+</sup> | 2 | L2HC。| 158| CODEC_TYPE_L2HCST<sup>13+</sup> | 3 | L2HCST。 | 159| CODEC_TYPE_LDAC<sup>13+</sup> | 4 | LDAC。| 160 161 162## CodecChannelMode<sup>11+</sup> 163 164枚举,蓝牙编码器的声道模式。 165 166**系统能力**:SystemCapability.Communication.Bluetooth.Core。 167 168| 名称 | 值 | 说明 | 169| ----------------- | ------ | ------- | 170| CODEC_CHANNEL_MODE_NONE<sup>11+</sup> | 0 | 未知声道。| 171| CODEC_CHANNEL_MODE_MONO<sup>11+</sup> | 1 | 单声道。 | 172| CODEC_CHANNEL_MODE_STEREO<sup>11+</sup> | 2 | 双声道。 | 173 174 175## CodecBitsPerSample<sup>11+</sup> 176 177枚举,蓝牙编码器每个采样点的位数。 178 179**系统能力**:SystemCapability.Communication.Bluetooth.Core。 180 181| 名称 | 值 | 说明 | 182| ----------------- | ------ | ------- | 183| CODEC_BITS_PER_SAMPLE_NONE<sup>11+</sup> | 0 | 未知采样点的位数。| 184| CODEC_BITS_PER_SAMPLE_16<sup>11+</sup> | 1 | 16位采样点的位数。| 185| CODEC_BITS_PER_SAMPLE_24<sup>11+</sup> | 2 | 24位采样点的位数。| 186| CODEC_BITS_PER_SAMPLE_32<sup>11+</sup> | 3 | 32位采样点的位数。| 187 188 189## CodecSampleRate<sup>11+</sup> 190 191枚举,蓝牙编码器的采样率。 192 193**系统能力**:SystemCapability.Communication.Bluetooth.Core。 194 195| 名称 | 值 | 说明 | 196| ----------------- | ------ | ------- | 197| CODEC_SAMPLE_RATE_NONE<sup>11+</sup> | 0 | 未知采样率。| 198| CODEC_SAMPLE_RATE_44100<sup>11+</sup> | 1 | 44.1k采样率。| 199| CODEC_SAMPLE_RATE_48000<sup>11+</sup> | 2 | 48k采样率。| 200| CODEC_SAMPLE_RATE_88200<sup>11+</sup> | 3 | 88.2k采样率。| 201| CODEC_SAMPLE_RATE_96000<sup>11+</sup> | 4 | 96k位采样率。| 202| CODEC_SAMPLE_RATE_176400<sup>11+</sup> | 5 | 176.4k位采样率。| 203| CODEC_SAMPLE_RATE_192000<sup>11+</sup> | 6 | 192k位采样率。|