1| Supported Targets | ESP32 | 2| ----------------- | ----- | 3 4A2DP-SINK EXAMPLE 5====================== 6 7Example of A2DP audio sink role 8 9This is the example of API implementing Advanced Audio Distribution Profile to receive an audio stream. 10 11This example involves the use of Bluetooth legacy profile A2DP for audio stream reception, AVRCP for media information notifications, and I2S for audio stream output interface. 12 13Applications such as bluetooth speakers can take advantage of this example as a reference of basic functionalities. 14 15## How to use this example 16 17### Hardware Required 18 19To play the sound, there is a need of loudspeaker and possibly an external I2S codec. Otherwise the example will only show a count of audio data packets received silently. Internal DAC can be selected and in this case external I2S codec may not be needed. 20 21For the I2S codec, pick whatever chip or board works for you; this code was written using a PCM5102 chip, but other I2S boards and chips will probably work as well. The default I2S connections are shown below, but these can be changed in menuconfig: 22 23| ESP pin | I2S signal | 24| :-------- | :----------- | 25| GPIO22 | LRCK | 26| GPIO25 | DATA | 27| GPIO26 | BCK | 28 29If the internal DAC is selected, analog audio will be available on GPIO25 and GPIO26. The output resolution on these pins will always be limited to 8 bit because of the internal structure of the DACs. 30 31### Configure the project 32 33``` 34idf.py menuconfig 35``` 36 37* Choose external I2S codec or internal DAC for audio output, and configure the output PINs under A2DP Example Configuration 38 39* Enable Classic Bluetooth and A2DP under **Component config --> Bluetooth --> Bluedroid Enable** 40 41### Build and Flash 42 43Build the project and flash it to the board, then run monitor tool to view serial output. 44 45``` 46idf.py -p PORT flash monitor 47``` 48 49(To exit the serial monitor, type ``Ctrl-]``.) 50 51## Example Output 52 53After the program is started, the example starts inquiry scan and page scan, awaiting being discovered and connected. Other bluetooth devices such as smart phones can discover a device named "ESP_SPEAKER". A smartphone or another ESP-IDF example of A2DP source can be used to connect to the local device. 54 55Once A2DP connection is set up, there will be a notification message with the remote device's bluetooth MAC address like the following: 56 57``` 58I (106427) BT_AV: A2DP connection state: Connected, [64:a2:f9:69:57:a4] 59``` 60 61If a smartphone is used to connect to local device, starting to play music with an APP will result in the transmission of audio stream. The transmitting of audio stream will be visible in the application log including a count of audio data packets, like this: 62 63``` 64I (120627) BT_AV: A2DP audio state: Started 65I (122697) BT_AV: Audio packet count 100 66I (124697) BT_AV: Audio packet count 200 67I (126697) BT_AV: Audio packet count 300 68I (128697) BT_AV: Audio packet count 400 69 70``` 71 72Also, the sound will be heard if a loudspeaker is connected and possible external I2S codec is correctly configured. For ESP32 A2DP source example, the sound is noise as the audio source generates the samples with a random sequence. 73 74## Troubleshooting 75* For current stage, the supported audio codec in ESP32 A2DP is SBC. SBC data stream is transmitted to A2DP sink and then decoded into PCM samples as output. The PCM data format is normally of 44.1kHz sampling rate, two-channel 16-bit sample stream. Other SBC configurations in ESP32 A2DP sink is supported but need additional modifications of protocol stack settings. 76* As a usage limitation, ESP32 A2DP sink can support at most one connection with remote A2DP source devices. Also, A2DP sink cannot be used together with A2DP source at the same time, but can be used with other profiles such as SPP and HFP. 77