Instructables

Turn your Raspberry Pi into a Wireless Portable Bluetooth Audio System A2DP

Picture of Turn your Raspberry Pi into a Wireless Portable Bluetooth Audio System A2DP
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
 
Remove these adsRemove these ads by Signing Up
1-40 of 138Next »
cyber72 days ago

Farley easy with older WHEEZY release!

I added the LCD repositories, anyone knows if there is a way to get the song-detail displayed? (What details can you get from pulseaudio?)

cyber7-out

(aka Aubrey Kloppers: http://aubreykloppersw.wordpress.com)

Snorey23 months ago

Good Guide! i am having an issue though. I can pair my device to the pi, but have no option to connect once paired. Once paired i get a message saying: "Confirm passkey is ###### to pair to [my pis name]."

The 6 digit number is different each time (not sequential looks randomly generated).

Anyone seen this or any ideas?

Thanks!

Snorey2 Snorey23 months ago

now its working, not sure what i changed but just click ok and its connecting after pairing now.

Snorey2, I'm having the same issue. Did you do anything in particular to resolve it or did it just fix itself?

davidcom Snorey23 months ago

Hi Snorey2. Thanks for giving us an update. There have been quite a few comments that the instructions no longer work because of changes to the kernal, etc. Can you let us know if you made any changes to the instructions to get yours to work?

My raspberry pi arrives in a couple days and any feedback on how to do this successfully would be greatly appreciated!

citaine1 month ago

Hi,

I followed the tutorial and worked well at first :D

then the sound stop and the only way to get the sound back is to use this command that I found on an other forum :

pactl load-module module-loopback source=1 sink=0

then the sound come back for 10 or 20 more sec and then stop.

If i use my command again, It will work for an other secs.

Could you help me with it ?

Regards,

Cap

fdragone1 month ago

hi all works perfectly, what about the siri audio? is possible to run the siri audio via bluetooth? emulating like the parrot bluetooth for cars?

thank you

steckums2 months ago

Any idea if it is possible to automatically pair to the last device (or one of x?) on boot? I'm using this for car audio, and would like my phone to automatically connect when it after it boots up.

bob_binz steckums2 months ago

A bit of a hack, and I'm sure there's better ways (I'm no scripter) but this "seems" to do the trick. I say seems because I haven't tested it with many devices.

In the bluetooth script, where it says:

mac=$(cat "$dev/name" | sed 's/:/_/g')

Add this, underneath:
cat "$dev/name" > /etc/btdevs

This will create a file containing the device ID when a device connects. Then, in /etc/rc.local, add this at the end (before exit 0):

if [ -f /etc/btdevs ]; then
btdev=`head -n 1 /etc/btdevs`
if [ "$btdev" ]; then
echo "connecting to device " "$btdev"
rfcomm release /dev/rfcomm0 > /dev/null&
rfcomm connect hci0 $btdev 3 > /dev/null&
fi
fi

The rfcomm release seems to be needed to allow a subsequent connection. Hopefully, when you reboot, it should connect to your device. It may be worth testing the rfcomm commands from the command line first though, to make sure it connects to your device.

jcaisse bob_binz2 months ago

Gave it a try on my Pi/Nexus 5 setup and no change. it all starts up but I still have to initiate the connection from my phone. I've searched high and low for a way to do this... seems to be dead-ends everywhere.

TEHumbert6 months ago

in the log file I'm not seeing the out of:

echo "Setting bluez_source to: $bluez_dev" >> /var/log/bluetooth_dev

I'm wondering what the command CONFIRM = 'sudo -u pi pactl list short | grep $bluez_dev#' does? what's up with the weird quotes around it? they're like half apostrophes...

Those "back tics" (`), on a US keyboard are left of the 1, under the ~. These tell the shell to execute what is inside and the result of that is stuffed into CONFIRM.

squirrelsnuts2 months ago

Great post! I am playing with this on Fedora (I know, I'll have to do some tweaking) and I didn't have the /etc/bluetooth directory, nor any of the files. Can you post the entire files as you have them?

jcaisse4 months ago

FWIW I bought a USB soundcard to get better sound (I'm installing this into a car to have a headless sound system powered by my phone) and then spent 2 nights trying to figure out why it would only play from the headphone jack... in the above scrips you'll see a line "AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"" This forces it to the headphone jack. To output to your USB soundcard, run "pacmd list-sinks |grep name:" and insert: "AUDIOSINK="name-of-your-device"" Hope this helps someone else too...

TexyUK jcaisse2 months ago

Thankyou jcaisse - you solution worked for me with a pcm2704 based usb sound card. My usr/lib/udev/bluetooth files now looks like this :

!/bin/sh

echo "Executing bluetooth script...|$ACTION|" >> /var/log/bluetooth_dev

echo "Executing bluetooth script...|$ACTION|"

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

if [ "$ACTION" = "add" ]

then

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=alsa_output.usb-Burr-Brown_from_TI_USB_Audio_DAC-00-DAC.analog-stereo rate=44100 adju$

sudo -u pi pactl load-module module-loopback source=$bluez_dev sink=alsa_output.usb-Burr-Brown_from_TI_USB_Audio_DAC-00-DAC.analog-stereo rate=4410$

fi

fi

done

fi

Texy

Hi jcaisse, I am trying to make it work with usb speakers, I changed the AUDIOSINK name but still is not working. What else do you recommend doing?

kirichof2 months ago

Got the following error when updating rc.d!:

$ sudo update-rc.d bluetooth-agent defaults

update-rc.d: using dependency based boot sequencing

insserv: Service pulseaudio has to be enabled to start service bluetooth-agent

insserv: exiting now!

update-rc.d: error: insserv rejected the script header

sideeffekt2 months ago

I had issues with the udev section, my device was not showing up as an input device from udevadm monitor. So I had to change /etc/udev/rules.d/99-input.rules to correctly function when the device wasn't showing up as an input. Hopefully this will be helpful is anyone else comes across the same issue. I changed it to:

KERNEL=="hci0:12", SUBSYSTEM=="bluetooth", RUN+="/usr/lib/udev/bluetooth"
KERNEL=="hci0:11", SUBSYSTEM=="bluetooth", RUN+="/usr/lib/udev/bluetooth"
KERNEL=="hci0:13", SUBSYSTEM=="bluetooth", RUN+-"/usr/lib/udev/bluetooth"

This now covers the different ways my device is identified when it connects. It is almost always one of the devices above.

Then to get the /usr/lib/udev/bluetooth script to work I had to go through and completely piece together a new one that autopopulated the appropriate sink and source sections without relying on /sys/devices/virtual/input/ so my bluetooth script looks like this:

#!/bin/bash

#Set User to run portions of the script as
BUSER=pi

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

#Set Volume
sudo -u "$BUSER" amixer set Master 100%

#Set Sink Volume
sudo -u "$BUSER" pacmd << !
set-sink-volume 0 65537
!

#Log add/remove status
echo "Executing bluetooth script...|$ACTION|" >> /var/log/bluetooth_dev

#Create audio sink variable as udev su/translated to user $BUSER for pactl command then script to log

BSINK=$(sudo -u "$BUSER" pactl list sinks short | tail -n 1 | awk '{print $2;}')

echo BSINK=$(sudo -u "$BUSER" pactl list sinks short | tail -n 1 | awk '{print $2;}') >> /var/log/bluetooth_dev

#Create buetooth source variable as udev su/translated to user $BUSER for pactl command then script to log

BSOURCE=$(sudo -u "$BUSER" pactl list sources short | grep bluez | awk '{print $2;}')

echo BSOURCE=$(sudo -u "$BUSER" pactl list sources short | grep bluez | awk '{print $2;}') >> /var/log/bluetooth_dev

#Run final pactl command as user $BUSER, then plug in variables, and finally write command to log file
sudo -u "$BUSER" pactl load-module module-loopback source="$BSOURCE" sink="$BSINK" rate=44100 adjust_time=0

echo sudo -u "$BUSER" pactl load-module module-loopback source="$BSOURCE" sink="$BSINK" rate=44100 adjust_time=0 >> /var/log/bluetooth_dev

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

Thank you! The rewrite did the trick. So for everyone wondering how to get this working:

Get the image from Zonx, The one from 2 september 2013. then the following things right as the pi is booted up:

1. sudo apt-get update (never never never ever apt-get upgrade)

2. sudo apt-get install rpi-update

3. sudo BRANCH=next rpi-update

4. Then follow the instructions as normal

5. At the step 5 with the udev part add these extra things from sideeffekt:

KERNEL=="hci0:12", SUBSYSTEM=="bluetooth", RUN+="/usr/lib/udev/bluetooth"
KERNEL=="hci0:11", SUBSYSTEM=="bluetooth", RUN+="/usr/lib/udev/bluetooth"
KERNEL=="hci0:13", SUBSYSTEM=="bluetooth", RUN+-"/usr/lib/udev/bluetooth"

6. get the new script from sideeffekt.

With the original script i could connect but no sound, and with the new image from January this year I've had the famous 3 second sound bug en PI 'bluescreens'

Also, get a good bluetooth 4.0 dongle, just in case.

Big thanks to Dantheman, Zonx and Sideeffekt!

plasticniki2 months ago

Hi there.

I've run all the steps and my phone can see my Pi but it won't connect at all. It's paired fine, just connecting I'm having issues with. I've gone through all the steps several times and as far as I can tell I've not done anything wrong. Yes, I've disconnected all other USB things so it's just the bluetooth receiver (which is flashing so I assume it's good). Any help would be appreciated!

Use this image, https://www.dropbox.com/s/3zz6uazhvtc0uey/2013-02-... The newest image's kernal has problems. Also, instead of running sudo apt-get upgrade, run sudo apt-get update, don't run the upgrade, or it might get f'd up. I'm just repeating what is in the comments lower though.

Thanks. I ran the update and it worked then cut out after about 3 seconds, then I ran the sudo BRANCH=next rpi-update command and now it works amazingly well. Quite pleased. Thanks to the OP and everyone else.

Zonx plasticniki2 months ago

How did you run that? When I run it I get "sudo: rpi-update: command not found"

Zonx Zonx2 months ago

Looks like I would have to update to get that to work. But i dont want to have to do that because it messes with this process. Thats why I ask

Zonx Zonx2 months ago

I did some searching and came up with this. The suggested image is older and did not include rpi-update. You have to manually install it with "sudo apt-get install rpi-update". After that i was able to run "sudo BRANCH=next rpi-update" and now I am having no problems. Hope this helps someone

TexyUK2 months ago

Hi,

do you know a way of the Pi getting the iD3 tag information from the stream?

Obviously it is sent, but how to access it?

thanks,

Texy

aortwein6 months ago
Has anyone tried this lately? I just installed a fresh copy of Raspbian and followed these instructions, but I am getting some serious problems now. My iPhone connects to the bluetooth device, but no audio plays. If I disable all the automatic scripts and execute the commands manually, I get some audio that plays (choppy) for about 20 seconds and then drops. I'm following the same steps that I did before, so the only thing I can think of that has changed is perhaps an update to bluez or pulse audio or something.

Use this image, https://www.dropbox.com/s/3zz6uazhvtc0uey/2013-02-... The newest image's kernal has problems. Also, instead of running sudo apt-get upgrade, run sudo apt-get update, don't run the upgrade, or it might get f'd up. I'm just repeating what is in the comments lower though.

pesqair aortwein4 months ago

i'm having the same problem. did you manage to get it working?

aortwein pesqair4 months ago
No, unfortunately I have not. I even tried someone's suggestion of using a much older version of Raspbian, thinking the bug was in the kernel, but the problem still remains. Every now and then I check to see if someone has solved this, but I'm not putting much time into it on my own.
aortwein aortwein3 months ago

While reading a blog entry (http://delx.net.au/blog/2014/01/bluetooth-audio-a2... on using A2DP on a Raspberry Pi, I stumbled across a fix for the "20 seconds of audio" issue. As suspected, there was a change made in the 3.10 kernel that causes a kernel panic when built-in audio is used in at the same time as a bluetooth adapter. The blog entry lists several reported incidents and they all suggest the same fix: update to the very latest firmware.

I ran "sudo BRANCH=next rpi-update" as suggested. It took awhile and required a reboot, but so far this little audio box is working once again!

Snorey23 months ago
The additional steps that i ran include updating packages and updating the kernel. After that i went through the whole guide again and caught 2 steps below that i either missed or for some reason got overwritten.

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

When i first executed this step, the text file was empty. second time through i could see [General] and other text.

2. My class did not get saved in the step below, and i had to reupdate.
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

Updating Packages:
sudo apt-get update && sudo apt-get upgrade

Updating the Kernel:
sudo apt-get update && sudo apt-get -y dist-upgrade
lukeg013 months ago

hmmm..... seems to work, got a device with a headset sign in the list.... now just ONE terrible problem.... i can't find the PIN code.... 0000 and 1234 did not help

Luke

lukeg01 lukeg013 months ago

fixed it using bluez agent, now... no audio not even for 20 sec, maybe worth noting, i'm using "Xbian" instead of raspbian, meaning everything with alas needs
needs the "sudo" command

Luke

philrlewis3 months ago

Hey

Loving this guide, but I am having a bit of a nightmare. Not being that technical I don't have the skills to troubleshoot myself so I was hoping for a bit of advise. Every time I try to set my class as 0x20041C in /var/lib/bluetooth/ it saves, but on reboot the class has reverted back to 0x4e041c. Any ideas how to stop this?

Much appreciated and thanks again for the guide!

Phil

falos philrlewis3 months ago

Phil, I have exactly the same issue. Does anybody have an idea on how to fix this? If I don't set the class after a reset, I can't pair with my Pi...

So I reset my class as shown in this tut, and then restart the bluetooth service. Then all works fine.

sbrumbaugh3 months ago

I have gotten all of the steps ready to go, but I can not get anything to pair with it. My devices see it, but when i try to pair it always fails. it never asks me for a pin either. I am at a loss. Any Ideas out there?

jmisiddall3 months ago

Very good tutorial, I got it all working with my Galaxy S4 and hooked it up to car radio.

Is there a way to use this to make and receive calls over the Bluetooth though?

I tried a different Bluetooth class but it made no difference.

Oliverhall4 months ago

Nice project you got here! I found this webstore, they are selling mini bluetooth dongles: http://voltatek.com/en/raspberry-pi/21-mini-usb-bl...

sh1ku4 months ago

Uhm, sorry to bump this, but it makes my raspi crash. I tried with 2 BT dongles, but all I get is 2/3 secs of music, and bam, alsa-sink crashes. No one?

1-40 of 138Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!