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