4

I need to be able to send and receive audio over bluetooth and it seems A2DP is the thing that does this

Using the code given here, I managed to get my phone to detect and pair with the remote bluetooth device, then establish an A2DP connection which sent all the audio output of the android device to the remote one via bluetooth

I was previously able to do such two way data communication with the same remote bluetooth device via RFCOMM (the android phone needed to be the one initiating the communication though, I could not do it through the remote device though I know it is possible). Once the RFCOMM link was established, the android device could send data to the remote device, as well as receive data from it

What I need now is something similar, but for audio (preferrably using A2DP, but I am open to using whatever else, as long as it gets the job done). While snooping around SO I've got hints that receiving audio data on phone (at least an unrooted normal one, rooting is not an option for me) via A2DP is not possible (see for example, here). But I have also seen headsets that allow making phone calls entirely through bluetooth (well, wirelessly actually, I am assuming it must be bluetooth). For example, for an incoming call, you can just press a button on the bluetooth earpiece, then you can talk as well as listen to the other party. If a phone cannot be used as an audio sink, how do such headsets do it?

I need some ideas on how I should go about writing an android app to send as well as receive audio data via bluetooth (for simplicity lets consider it similar to the Bluechat sample program, but involving audio instead of text). Is it possible at all (through java)? Is it possible only through NDK? How do the devices on the market do it? Is it a different profile than A2DP?

PS: My test devices are Samsung Galaxy 2 (phone) with Gingerbread and Asus Nexus 7 (tablet) with KitKat. Incidentally the remote bluetooth device (it is a development board) comes with a sample project that is supposed to turn the board into an A2DP audio source. if I run this sample project on the board, using the phone's and tablet's built-in bluetooth settings, I can sort of pair with the remote board, but the android devices do not receive any audio.

Community
  • 1
  • 1
user13267
  • 6,871
  • 28
  • 80
  • 138

2 Answers2

5

There are two different A2DP profiles, A2DP source and A2DP sink. The A2DP source profile is supported by Android thus allowing you to stream music from your device to a sink like a bluetooth speaker. To my knowledge the A2DP sink profile is not enabled, which is why you're unable to stream music to your mobile phone and have it act as a speaker. I only know this from having googled around some time ago so things may have changed.

It is possible to enable the A2DP sink profile by editing /etc/bluetooth/audio.conf and rebooting your phone. Unfortunately this has the side-effect of disabling the A2DP source profile and thus cannot be used in a widely distributed app. The other downside is that there is not much of a java software stack for working with that profile so you'd have to do quite a bit of work to get things rolling.

To answer your question as to why a headset is able to connect to the Android, that is because headsets use a different profile called Headset profile (HSP), commonly used by headsets, or Handsfree profile (HFP) commonly used in cars. Both profiles have two sides: a client side and the gateway side (mobile phone). The audio quality of both of these profiles is considered voice quality. 8 or 16kHz sample rates and a crappy codec. There is also a lot of protocol for answering/making calls, etc... I'm not sure if you'd even get audio to transmit without an active connection to the phone network.

jaket
  • 9,140
  • 2
  • 25
  • 44
  • The quality is not so important as long as I can get some voice audio to be transferred between the android device and the remote device. I think the remote device supports HFP and HSP as well, but what I have been doing upto now seems to be a2dp. How does this work exactly? Does it use what ever profile my phone tries to connect with? Or is it that something like this is not possible, and I have to find a way to modify the code in the remote device so that it is specifically looks for HFP or HSP connection attempt? Is there any sample program (android side) that shows how to use them? – user13267 Jan 14 '15 at 02:08
  • `cat audio.conf` shows permission denied on the adb shell. I guess this needs root, and rooting is not an option for me – user13267 Jan 14 '15 at 02:21
  • @user13267. There is rather a lot of detail to how connections are initiated but in general, I think a hands free device (car or headset) initiates a connection to the gateway when it detects it. It will do a scan periodically and then initiate when it finds a paired device. I think the protocol will allow either way though. As far as determining what profiles a particular device supports, you're going to want to read up on SDP (Service Discovery Protocol). – jaket Jan 14 '15 at 03:51
1

For A2DP Sink support - Yes. It is possible. I have done it in Android JB. Android device as a receiver for A2DP profile - Please read my reply.

Community
  • 1
  • 1
theangrylama
  • 216
  • 3
  • 11