Receive audio via Bluetooth in Android

No. Trying to write an Android application that handles this will not be the solution. At least if you want to use A2DP Sink role.

The fact is that Android, as you mentioned it, does not implement the API calls to BlueZ (the bluetooth stack Android uses till Jelly Bean 4.1) regarding A2DP sink capabilities. You have to implement them yourself. I will try to guide you, as I was also interested in doing this my self in the near past.

Your bluetooth-enabled Android device is advertising itself as an A2DP source device by default. You have to change this first, so nearby devices may recognize your device as a sink. To do this, you must modify the audio.conf file (usally located in /etc/bluetooth/) and make sure the Enable key exists and the value Source is attached to this key, so you will get something like :

Enable=Source

Reboot, nearby devices should now recognize your device as an A2DP sink.

Now you will have to interact with BlueZ to react appropriately when an A2DP source device will start to stream audio to your phone.

Android and BlueZ are talking to each other via D-BUS. In fact, Android connects to the DBUS_SYSTEM channel and listens to every BlueZ advertisement, such as events, file descriptors …

I remember having successfully bound my self using a native application to this d-bus channel and got access to the various events BlueZ was posting. This is relatively easy to achieve using as reference, the BlueZ API available here. If you go this way, you will have to build a native application (C/C++) and compile it for your platform. You must be able to do this using the Android NDK.

If you find it difficult to use D-BUS, you can try this Java library I just found that handles the communication to D-BUS for you : http://jbluez.sourceforge.net/. I have never used it but it is worth a try in my opinion.

What you really have to do is find out when an A2DP source device is paired to your phone and when he starts to stream music. You can retrieve these events through D-BUS. Once somebody will try to stream music, you need to tell BlueZ that your native application is going to handle it. There is a pretty good document that explains the flow of events that you should handle to do this. This document is accessible here. The part you’re interested in comes on page 7. The sink application in the given example is PulseAudio but it could be your application as well.

BlueZ will forward you a UNIX socket when you will call the org.bluez.MediaTransport.Acquire method. Reading on this socket will give you the data that are currently streamed by the remote device. But I remember having been told by a guy working on the BlueZ stack that the data read on this socket are not PCM pure audio, but encoded audio content instead. The data are generally encoded in a format called SBC (Low Complexity Subband Coding).

Decoding SBC is not very difficult, you can find a decoder right here.

The ultimate step would be to forward the PCM audio to your speakers.

To prevent you from getting stuck and in order to test your application in an easier manner, you can use the d-bus binary that should be available on your Android system. He is located in /system/bin.

Quick tests you can make before doing anything of the above might be :

Get Devices list :

dbus-send –system –dest=org.bluez –print-reply /
org.bluez.Manager.GetProperties

This returns an array of adapters with their paths. Once you have these path(s) you can retrieve the list of all the bluetooth devices paired with your adapter(s).

Get paired devices :

dbus-send –system –print-reply –dest=org.bluez
/org/bluez/{pid}/hci0 org.bluez.Adapter.GetProperties

This gives you the list of paired devices whithin the Devices array field.

Once you have the list of devices paired to your Bluetooth Adapter, you can know if it is connected to the AudioSource interface.

Get the devices connected to the AudioSource interface :

dbus-send –system –print-reply –dest=org.bluez
/org/bluez/{pid}/hci0/dev_XX_XX_XX_XX_XX_XX
org.bluez.AudioSource.GetProperties
org.bluez.Manager.GetProperties

Hope this helps.

Leave a Comment