Android device as a receiver for A2DP profile
Solution 1:
Since Android L the BlueDriod stack does support A2DP sink, but it is disabled by default.
To enable it do the following:
/* Enable bluetooth av sink. */
#define BTA_AV_SINK_INCLUDED TRUE
in /external/bluetooth/bluedroid/include/bt_target.h. This enables sink support in the bluetooth stack.
Also you have to do this change:
<!-- Enable sink support. -->
<bool name="profile_supported_a2dp_sink">true</bool>
in /packages/apps/Bluetooth/res/values/config.xml. This enables the particular UI.
Now you can pair your devices and start streaming. Unfortunately you will hear no sound although you'll receive the packets. The reason is that there is no audio route for A2DP sink. In the commit message of this patch https://android-review.googlesource.com/#/c/98161/ you can find a sample implementation on how to fix this.
Here is a list of these changes:
- https://android-review.googlesource.com/#/c/97832/
- https://android-review.googlesource.com/#/c/97853/
- https://android-review.googlesource.com/#/c/97833/
- https://android-review.googlesource.com/#/c/98130/
- https://android-review.googlesource.com/#/c/98131/
Solution 2:
Yes. It is possible. I have done it in JB. Android internally uses "Bluedroid" stack from Broadcomm for Bluetooth. Previously this stack did not have support for A2DP Sink Role (Which you mentioned as receiver). From Lollipop release, the A2DP Sink role profile has been added in Bluedroid. But, it is not enabled to be used by framework/upper layer (Application). You need to make changes in framework to enable it or 'use' it. You may refer to the following files and relevant files in Android source code to enable it.
audio.h - put a new audio source
audio_policy.conf - put a new input source for a2dp 'inputs'
AudioManager.java
AudioPolicyManagerBase.cpp
AudioService.java
AudioSystem.java
BluetoothA2dp.java
MediaRecorder.java
A2DPStateMachine.java
etc. and implement it (this file list is not comprehensive, but you can figure it out if you have experience in relevant field). When any stream connection is established, you will get callback in a2dp state machine and from there you have to start a thread to read the decoded PCM bytes from the 'new' audio source and send it to your media player. SBC codec to PCM decoding will be done at the 'bluedroid' sbc decoder layer.
Build it and flash it to your phone and enjoy music.
EDIT: Also, you may have make changes in A2DP SDP record in Bluedroid stack to advertise the A2DP Sink role.