Introduction: Turn Your Raspberry Pi Into a Wireless Portable Bluetooth Audio System A2DP

About: 20-something programmer.
Howdy folks! I had the idea of turning my Raspberry Pi into something like the Beats By Dre portable audio system found here:
http://www.beatsbydre.com/speakers/beatbox-portable-usb/beatbox-portable-usb,default,pd.html

It’s a cool product, don’t get me wrong, but I’m not really down to pay $400+ tax for some fancy looking speakers (which don't even give you rechargeable batteries, by the way). So I decided to set off to replicate the functionality without the hefty price tag.

OBJECTIVE: To create a Raspberry Pi that automatically boots into the command line and becomes discoverable via Bluetooth. Any Bluetooth-enabled device with the A2DP (Advanced Audio Distribution Profile) support will be able to push audio to the Pi through its Analog (default) or HDMI speaker output. When a Bluetooth device connects, the PI routes the connection through to Pulse audio and Bluetooth discoverability is turned off. The audio should play seamlessly from here. You can then disconnect your device (likely a smartphone) and the PI will become discoverable again via Bluetooth and another device is free to connect. The default pass key will be 0000 but most devices input this automatically so it should be hassle free!

NOTE: I did find that a lot of other people in the Linux community have attempted to do the same thing to their PI but not as successfully as I have! My setup allows the device to run headless (without a monitor) from boot and any Bluetooth device can connect and disconnect without an issue. I’ve seen a lot of other people with tutorials that make you login via command line first or login to the desktop and setup the Bluetooth connection manually. LAME! The goal of this is to be as similar to a *product* as possible. :) enjoy!

NOTE: This tutorial assumes you have a fair amount of Linux/command-line knowledge. I won’t be explaining basic commands or ideology so if this is above your head find a friend!

What is the Raspberry PI?
The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It’s a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. --Raspberry PI website

You can learn more about the PI and how to get one, here:
http://www.raspberrypi.org/

Requirements
• Raspberry Pi
• Bluetooth v4.0+ micro USB dongle
• 4GB SD Card

Requirements for [portable audio]
• Battery-powered speaker(s)
• Battery pack for Raspberry Pi
• Enclosure to keep everything in

Step 1: Prepare SD for Base Operating System

Go ahead and pop in your SD card into your card reader. For this setup we will be using Raspbian Wheezy, which is the latest Debian based OS for the PI at the time of this writing. You should be able to find the latest version here:
http://www.raspberrypi.org/downloads

Download and uncompress the image file and note the location. If you’re using a Mac it should be trivial to burn the image to the SD card. If you’re a Windows user however, go ahead and use this utility to “burn” the IMG file to the SD card.
http://sourceforge.net/projects/win32diskimager/

Make sure you select the right file and the right drive letter before hitting the WRITE button or you could have some nasty things happen. :(

Step 2: Login to the PI Through a Monitor/keyboard or Network SSH

Your PI will need to be connected to the Internet at this point, either through a LAN or a WiFi connection. I won’t be covering how to connect your PI to WiFi or login from SSH; Google is your friend!

Otherwise use a keyboard and a monitor and setup your PI the old fashion way!

NOTE: Make sure your Bluetooth dongle is plugged from here on.

NOTE: The default login is pi and the password is raspberry.

Step 3: Install Prerequisite Packages

Okay now that we’re all logged, connected to the Internet, and ready to go let’s go ahead and install all the packages so that Bluetooth can be utilized on your PI.

sudo apt-get upgrade

sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2 bluez-tools

Step 4: Modify Some Config Files to Enable Our Bluetooth Environment

First, let’s go ahead and add our user pi to the Pulse audio group so we the user can play audio through it.
sudo usermod –a –G lp pi

Now, let’s go ahead and enable A2DP in our Bluetooth configuration. Run the following command:
sudo nano /etc/bluetooth/audio.conf

This will open the text editor nano with that config file. Add the follow text under [General]:
Enable=Source,Sink,Media,Socket

Hit CTRL+O and ENTER to save and then CTRL+X to exit nano. Next, let’s modify the Pulse daemon configuration file to change its resampling method.

sudo nano /etc/pulse/daemon.conf

Look for the following line: resample-method = speex-float-3

If the line doesn’t have a ; in front of it, please put a ; there so it looks like:
; resample-method = speex-float-3

And then add the following line underneath that.
resample-method = trivial

Before moving on, let’s also go ahead and rename our Bluetooth device (odds are you don’t want the name to be raspberry-pi0) as well as redefine the Bluetooth class device from 'Computer' to 'Portable audio device'. There are two locations you should modify.

sudo nano /etc/bluetooth/main.conf

Change the Name parameter. I chose to do a play on the BeatsByDre.
Name = BeatsByDan

Change the device's Class parameter.
Class 0x20041C

Next, you’ll have to modify the config of your specific device. The directory is based on your Bluetooth device’s MAC address so just hit TAB when typing that out and you’ll find yours.

sudo nano /var/lib/bluetooth/<bluetooth mac address>/config

Edit the name directive again.
name BeatsByDan

And the device class...
class 0x20041C

Go ahead and save and exit.

Step 5: Setup the Script That Gets Executed When a Bluetooth Device Connects

Now that we have our Bluetooth environment setup, let’s go ahead and begin the process of automating Bluetooth connections and routing them to audio for us.

Let’s first setup a rule in our udev that states that whenever a device connects, our script will get executed. Run the following command to open the udev rules list.

sudo nano /etc/udev/rules.d/99-input.rules

There should be just a single line there that looks like:
SUBSYSTEM=="input", GROUP="input", MODE="0660"

Underneath it, let’s go ahead and add the following line:
KERNEL=="input[0-9]*", RUN+="/usr/lib/udev/bluetooth"

Save and exit.

Now let’s actually save that script that the above rule file is referencing. First check to see if a udev/ directory exists. Do sudo ls –la /usr/lib/udev . If it doesn’t exist go ahead and create it like so: sudo mkdir /usr/lib/udev

sudo nano /usr/lib/udev/bluetooth

Copy and paste (or type it out if you’re unlucky) the following script into your terminal.

NOTE: I did not write this script alone. I found it during research (why reinvent the wheel if not needed) and modified it to work the way I needed it to in order to be as friendly as possible, Bluetooth-wise.

#!/bin/bash
#change if you don't use default analog audio out.
#THIS DIDN'T WORK FOR MY PI SO I ADDED A amixer COMMAND BELOW
AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"
echo "Executing bluetooth script...|$ACTION|" >> /var/log/bluetooth_dev

ACTION=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")
if [ "$ACTION" = "add" ]
then

# Turn off BT discover mode before connecting existing BT device to audio
hciconfig hci0 noscan

# set the audio output to the analog
# COMMENT THIS LINE OUT IF YOU WANT TO OUTPUT AUDIO THROUGH HDMI
amixer cset numid=3 1

# Set volume level to 100 percent

amixer set Master 100%
pacmd set-sink-volume 0 65537


   for dev in $(find /sys/devices/virtual/input/ -name input*)
   do
      if [ -f "$dev/name" ]
      then
         mac=$(cat "$dev/name" | sed 's/:/_/g')
         bluez_dev=bluez_source.$mac

         sleep 1

         CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev`
         if [ ! -z "$CONFIRM" ]
         then
            echo "Setting bluez_source to:  $bluez_dev" >> /var/log/bluetooth_dev
            echo pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev
            sudo -u pi pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev
         fi
      fi
   done
fi

if [ "$ACTION" = "remove" ]
then
# Turn on bluetooth discovery if device disconnects
sudo hciconfig hci0 piscan
fi


Save and exit! Now let’s modify the permissions of the file to make it executable.

sudo chmod 774 /usr/lib/udev/bluetooth

Great! Your Raspberry PI is now capable of playing Bluetooth audio via A2DP! Whoo! Now let’s take it a step further by turning on discovery mode and auto accepting connections right when the PI logs into the command line automatically.

Step 6: Automating Bluetooth Discovery and Auto Accepting Connections

In order to achieve this, we’ll need to create an init script that will execute when the PI is booted up. Let’s do that!

sudo nano /etc/init.d/bluetooth-agent

Go ahead and copy this script into the text editor. Again, I found this script originally and then improved it to suit my needs!

### BEGIN INIT INFO
# Provides: bluetooth-agent
# Required-Start: $remote_fs $syslog bluetooth pulseaudio
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Makes Bluetooth discoverable and connectable to 0000
# Description: Start Bluetooth-Agent at boot time.
### END INIT INFO
#! /bin/sh
# /etc/init.d/bluetooth-agent
USER=root
HOME=/root
export USER HOME
case "$1" in
start)
echo "setting bluetooth discoverable"
sudo hciconfig hci0 piscan
start-stop-daemon -S -x /usr/bin/bluetooth-agent -c pi -b -- 0000
echo "bluetooth-agent startet pw: 0000"
;;
stop)
echo "Stopping bluetooth-agent"
start-stop-daemon -K -x /usr/bin/bluetooth-agent
;;
*)
echo "Usage: /etc/init.d/bluetooth-agent {start|stop}"
exit 1
;;
esac
exit 0


Save and exit nano.This script basically is just a standard start/stop script for Bluetooth discoverability.

Go ahead and give the script execute permissions and then add it to the list of programs that start on start-up.

sudo chmod 755 /etc/init.d/bluetooth-agent

sudo update-rc.d bluetooth-agent defaults

Step 7: Setup the Command-line to Auto-login

Everything is *almost* setup! For some reason Pulse audio won’t keep the stream alive unless you’re logged into the console! Lame! We want a headless, auto setup, right? Well, let’s adjust the environment so that we have a passwordless login at boot!

Let’s edit the inittab now.

sudo nano /etc/inittab

Find this line:

1:2345:respawn:/sbin/getty 115200 tty1

And comment it out, like so:

#1:2345:respawn:/sbin/getty 115200 tty1

Now add this line right below it:

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Save and exit nano.  Finally, powercycle your Raspbery Pi!

sudo reboot

Step 8: Conclusion

If you did everything correctly, you should now have an awesome A2DP Bluetooth receiver that plays audio out the Analog sound jack. You can now easily add a battery pack, a GIANT speaker, and PARTY wirelessly! : )


Creating a portable audio system

I won't go into the actual process of making the Pi portable. I'm assuming that if you're smart enough to find your way to this tutorial that you're also smart enough create an enclosure, get a battery, and figure out your speaker size needs. =]

I personally used the equipment in the picture above as a test while developing the software components. I am getting a sizable rear car speaker, an amplifier, a large battery pack for my system.  I'm also building a custom enclosure to house it all. I will be buying a Pi just for the system and screw into the inside of the body of the enclosure.

As for the enclosure itself, I haven't decided whether to make it from wood or plastic or even a 3D printing option. If anyone comes up with anything worth while please post a pic in the comment section below. Thanks!


I welcome any suggestions or improvements to this guide. Please email me at danielg@danielgillespie.net.

Regards,
Daniel Gillespie

NOTE: You should unplug any other USB devices so that just the Bluetooth dongle is there (unless you have a powered USB hub). If you see your Raspberry Pi via Bluetooth but are unable to successfully pair, this may be the reason. Your adapter may have enough power to work but not enough to work as it should.

NOTE: iOS devices seem to connect just fine to the receiver from the get go however I've seen some Android devices have a little trouble. If you're using an Android smartphone and it isn't playing audio through the receiver after connecting, try unpairing and pairing again. It seemed to work for my friends' Android devices!

Final Note:  Only connect one device at a time and disconnect it before attempting to connect another device. Should work flawlessly. :]

IF YOU FOUND THIS GUIDE USEFUL LET ME KNOW! Also, check out my iPhone app, CalcMate:
https://itunes.apple.com/us/app/calcmate/id538650487?mt=8


Troubleshooting

In the event that the Bluetooth device may not be connecting correctly, you can see when a Bluetooth device is added/removed from the Pulse audio system in real-time by using the following command:

tail -f /var/log/bluetooth_dev