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
Snorey22 days 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!

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

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!

lukeg017 days 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

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

philrlewis24 days 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

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.

sbrumbaugh14 days 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?

jmisiddall15 days 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.

mattx100216 days ago

My IPhone 4 connects, but the audio won't go through the speakers. Can anyone help me?

aortwein3 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.

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

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.

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!

jcaisse1 month 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...

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?

Oliverhall25 days 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...

sh1ku29 days 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?

I'm able to connect my device but I don't get any sound, i've tried with an android, iphone and with my computer but It doesn't play the music.

Any Idea?

im having this exact problem too.

DavGerm41 month ago

So I got it all working! Props to dantheman_213 for the guide! Only problem I'm running into is the sound is really quiet, I can't seem to get the volume up any higher? Suggestions?

login to the terminal and type: alsamixer

Press F6 to chose your sound output and up down keys adjust the volume.

To save, exit and type: sudo alsactl store

Are sub-v4.0 devices totally incompatible?

It would explain why nothing happens when I try to use my v1.2 dongle :)

Branth2 months ago

I've tried this several times, and after figuring out a few bugs,
I'm still left with an issue - My audio plays for maybe 20 seconds or
more, and then my Pi crashes, giving me an error that says "alsa-sink
Not tainted." It's virtually identical to the error I found here:

http://www.raspberrypi.org/forum/viewtopic.php?f=2...

I've got a 1.8A power supply, so I doubt that's it, and I've done the FIQ patch (http://www.raspyfi.com/raspberry-pi-usb-audio-fix/... but I still get a crash after a short period of playing audio.

Any ideas on what it might be, or how to fix it?

franmmd Branth2 months ago

I have the same problem. Any solution?

Thanks in advance.

jcaisse franmmd1 month ago

Start with this image: http://downloads.raspberrypi.org/raspbian/images/2013-02-09-wheezy-raspbian/

I had also the bluetooth working for only 20 seconds, so I started from scratch with this image and it works fine now.

Seems there is a bug with the more recent releases of wheezy-raspbian, so get the one from 2013-02-09, and it should be fine!

nlbrewe Branth2 months ago
I get exactly the same message. Any ideas for followup?
bob_binz nlbrewe2 months ago

Try an older kernel. I got it to work by building and installing a 3.6.11 kernel

Zibri1 month ago
benandjj1 month ago

I have a question. I installed this on one of my RPi's and at first the music sounded great through it. Now it seems that I have a very bad echo. I have tried disconnecting and reconnecting, restarting the Pi, different speakers, all with no luck. Any thoughts? It sounds like the same song is being played 5 or 6 times with just a split second delay between each one.

I had this problem along with the 20 second problem - I tried recompiling kernels, adding bits from other instructional sites, and generally mucking about but ultimately searched for a Rasbian image that came with Kernel 3.6.11 preinstalled. (http://downloads.raspberrypi.org/raspbian/images/2013-02-09-wheezy-raspbian/) and then set off again... took about 15 minutes to rip through the instructions (it was my 3rd run) the only two things I found were 1) start with "sudo apt-get update" otherwise packages will 404 and the line to make it run headless was a little different. I commented out the most simlar line and added the one above as instructed. A little fiddly at first to get bluetooth to connect with my android, but rebooted everything and now they all see each other. Thanks a million dantheman_231!

TEHumbert3 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...

What do you reckon the chances of having this running on a Pi alongside squeezelite? Maybe with an extra command in the "bluetooth connected" script to either mute it's audio or stop the player, then restart on disconnection. Any suggestions? Cheers!
bwelton3 months ago

Great project.

I would like to go one big step further.. I would like to plug in multiple USB bluetooth adaptors.

The first one acting to receive the streamed music and another 3 or 4 connecting via their own BT streams to proper bluetooth Speakers. In this way having the RiPi acting as a multipont repeater. There may also be a need for a bit of a buffer mechanism in the code that could allow for latency adjustment between the Audi of the end play devices but that would be a follow on project if required.

What do you think to this concept ?

dantheman_213 (author) 11 months ago
Hmm I tested this extensively on the iPhone 4 and iPhone 5. Encountered 0 problems after countless connects/ disconnects. If you setup everything correctly, reboot your pi, and forget the device in your iPhone. Then wait to be seen again and connect. Should be okay! Remember to give it a second before disconnecting to reconnect.

Hi Dan, great job. I got everything working for Android, and I also tried with an old Samnsung phone with Windows mobile 5. The only problem is that iPhone is not connecting, it does not even show the Raspberry in the devices while discovering bluetooth. What could be wrong? I checked everyting many times and all looks fine. Thanks for your help.

This is the error I get


pi@BlueBerryPi ~ $ sudo update-rc.d bluetooth-agent defaults
update-rc.d: using dependency based boot sequencing
insserv: Script bluetooth-agent is broken: missing end of LSB comment.
insserv: exiting now!
update-rc.d: error: insserv rejected the script header
schmo906 months ago
after weeks of madness and no succes i got it finally working but with a complette different start script because the even kernel event did not work for me.

at first i decommended in the
sudo nano /etc/bluetooth/audio.conf
HFP=true

after that i made a script

sudo nano /EnableBTMusicStream.sh

to get my bluetooth device mac adress

#!/bin/bash
running=1
while [ 1 -lt 10 ]; do
  source=$(pactl list short sources |  grep -o "bluez.source.\(.*\)module" | sed 's/'\module'/''/g' |  sed 's/[ \t]*$//')
    if [ "$source" ]; then
      if [ $running -eq 1 ]; then
        sudo -u pi pactl load-module module-loopback source=$source sink=alsa_output.platform-bcm2835_AUD0.0.analog-stereo
        running=2
      fi
   else
     sudo hciconfig hci0 piscan
     running=1
  fi
  sleep 5
done


 than

sudo nano /etc/init.d/EnableBTMusic


#! /bin/sh
# /etc/init.d/blah
#

# Alles, was in jedem Fall ausgeführt werden soll, steht am Anfang
touch /var/lock/somefile.lock

# Bestimmte Funktionen werden per switch-case Anweisung geregelt
case "$1" in
  start)
    echo "Starte Beispielprogramm "
  sudo -u pi sh /EnableBTMusicStream.sh &
    ;;
  stop)
    echo "Stoppe Beispielprogramm"
    # ./starte_mich.sh --stop
    ;;
  *)
    echo "Usage: /etc/init.d/starscript.sh {start|stop}"
    exit 1
    ;;
esac

exit 0



 sudo chmod 755 /etc/init.d/EnableBTMusic
sudo update-rc.d EnableBTMusic defaults

with this changes i can hear music on my raspberry :-)
i hope i could help someone with that infos...
rpotee schmo906 months ago
For some reason, I need to ssh into my RPi to get this solution to work. Any work arounds? I have all of the configuration detailed for this project, plus your solution.
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!