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