Raspberry Pi as A2DP receiver

Raspberry Pi as a Bluetooth Audio receiverI now have a Raspberry Pi acting as a Bluetooth Audio receiver. The initial setup followed the instructions at http://kmonkey711.blogspot.co.uk/2012/12/a2dp-audio-on-raspberry-pi.html. This paired my phone with the Raspberry Pi and installed the required Pulseaudio modules to listen to it.

The problem was that reconnecting was a manual process. I couldn’t just turn on the Pi and connect my phone. The solution I have taken was maybe a little overkill, but seems to work well and offers some chance for improvement and added features.

I set up Pulse Audio in system mode by editing /etc/default/pulseaudio. This is an embedded system which does not normally accept logins, except to administer it, so System Mode seems appropriate. It is also needed to allow Pulse to be controlled by a process running as a system user without a home directory.

# 0 = don't start in system mode, 1 = start in system mode

# Prevent users from dynamically loading modules into the PulseAudio sound
# server. Dynamic module loading enhances the flexibility of the PulseAudio
# system, but may pose a security risk.
# 0 = no, 1 = yes

I had to make changes to /etc/pulse/system.pa to load the bluetooth modules.

My administration program is developed in Java, and runs in the Tomcat web container. Tomcat runs amazingly well on the Raspberry Pi, even the older Revision 2 with only 256M RAM. It takes a minute or so to start the server, but once started it is quite fast and doesn’t consume much RAM with the application I am running. It also has a servlet through which I can access a status report.

The code is very much a quick hack at the moment and not the quality I’d hope to produce. It can be greatly improved in many ways. I can hope to upload it to GitHub if anyone is interested.

I had to allow Pulse Audio to access the bluetooth devices so that it could detect the phone when it connected. I also allowed the tomcat7 user access so my application could query the name of the phone and display that rather than the MAC address. Tomcat also needs access to Pulse Audio.

sudo adduser pulse bluetooth
sudo adduser tomcat7 bluetooth
sudo adduser tomcat7 pulse-access

The web application has a polling thread which routinely runs the commands

pactl list short sources
pactl list short modules

It can use this information to detect bluetooth sources and determine which are already patched through. It can then perform the

pactl load-module module-loopback source=$source

to load connect the source to the default output.

I note that “pactl list short” without an option gives a complete list, so I could find all the information I need in once command. There’s also a “pactl subscribe” command so I can tell when to wake up my polling thread rather than using a timer.

One reason for the polling thread was to experiment with web sockets or long polling techniques in a web browser client. This would allow the development of a screen that updated in real time. There is a lot that could be done from this simple basis, given time. Maybe even the whole solution including pairing could be achieved. At the moment I have a system where I can switch on the Raspberry Pi, connect my phone to it, and within a short time the audio will come out of the speakers which is quite neat.

I tried enabling the RTP network broadcast to listen to the stream in another room on another Raspberry Pi. Some users report success with this. I found that things went very slowly, but wonder if the fault may have been in my network infrastructure rather than the Raspberry Pis.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.