Howdy folks! I had the idea of turning my Raspberry Pi into something like the Beats By Dre portable audio system found here:

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:

• 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:

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.

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. :(
<p>Thanks for your wonderful posting.</p><p>But I have some error while following your writing.</p><p>I am using Raspbian jessie.</p><p>Whenever I tried to register the service, I met this error.</p><p>pi@raspberrypi ~ $ sudo update-rc.d bluetooth-agent defaults<br>insserv: Service pulseaudio has to be enabled to start service bluetooth-agent<br>insserv: exiting now!<br>update-rc.d: error: insserv rejected the script header</p><p>Also I have installed 'pulseaudio'.</p><p>pi@raspberrypi ~ $ sudo apt-get install pulseaudio<br>Reading package lists... Done<br>Building dependency tree<br>Reading state information... Done<br>pulseaudio is already the newest version.<br>0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.</p><p>This is my scrpt header of '/etc/init.d/bluetooth-agent'.<br># Required-Start: $remote_fs $syslog bluetooth pulseaudio</p><p># Required-Start: $remote_fs $syslog bluetooth<br>If I change the header like above by omitting 'pulseaudio', 'sudo update-rc.d bluetooth-agent defaults' doen't make any error. But the bluetooth-agent service doesn't start at boot time of my Raspbian.</p><p>How can I fix my problem? Please help me. Thank you very much.<br>-from Brian</p>
<p>Hi Guys,</p><p>this tutorial is working great, but after disconnecting with any device its not possible to connect whit an other device. So i made a little script that restart the bluetooth service after a disconnect. This works fine and now i can play sound from any device at any time.</p><p>Here the details:</p><p>-&gt; sudo touch /usr/local/bin/bluewatch.sh</p><p>-&gt; sudo nano /usr/local/bin/bluewatch.sh</p><p>-&gt; add this and save</p><p>________________</p><p>#!/bin/bash</p><p>tail -f -n0 /var/log/bluetooth_dev |grep --line-buffered &quot;|remove|&quot; | while read line; do sleep 3; /etc/init.d/bluetooth restart ; sleep 2; /etc/init.d/bluetooth-agent start ;done<br></p><p>________________</p><p>-&gt; sudo chmod 775 /usr/local/bin/bluewatch.sh</p><p>-&gt; sudo nano /etc/rc.local</p><p>-&gt; Insert this line directly above &quot;exit 0&quot; :</p><p><strong><em>________________</em></strong></p><p><strong><em>/usr/local/bin/bluewatch.sh &amp;</em></strong></p><p>________________</p><p>-&gt; Save the file and close nano.</p><p>-&gt; Reboot your PI</p><p>Thats all.</p>
<p>Hi ChristianM85,</p><p>Following your post: as we say in France: &quot; Je ne t'embrasse pas, mais le coeur y est!&quot; </p><p>A big big thank you!</p>
<p>Hi guys! <strong>I Made it!</strong></p><p>It works nice, but I had some problems. These:</p><p>1&deg; <b>sudo usermod &ndash;a &ndash;G lp pi doesn't found</b></p><p>when I copied and pasted it didn't work. terminal didn't found it! then I manually wrote it exactly same and then worked perfectly. I don't know why</p><p>2&deg; <strong>output audio doesn't work</strong></p><p>in this file: /usr/lib/udev/bluetooth :</p><p>Replace this line:</p><p>AUDIOSINK=&quot;alsa_output.platform-bcm2835_AUD0.0.analog-stereo&quot;</p><p>With this:</p><p>AUDIOSINK=&quot;alsa_output.0.analog-stereo&quot;</p><p>3&deg; <strong>output audio after 20 seconds stopped</strong></p><p>Edit daemon configuration:</p><p>sudo nano /etc/pulse/daemon.conf</p><p>Change</p><p>; exit-idle-time 20</p><p>to</p><p>exit-idle-time -1</p><p>4&deg; <strong>bluetooth devices doesn't connect</strong></p><p>this is a proble about auto-login in tty1; I resolved this re-writing the <strong>/etc/inittab </strong>file like this, from:</p><blockquote>#1:2345:respawn:/sbin/getty 38400 tty1 <br>1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2&gt;&amp;1 <br>2:23:respawn:/sbin/getty 38400 tty2 <br>3:23:respawn:/sbin/getty 38400 tty3 <br><br>4:23:respawn:/sbin/getty 38400 tty4 <br><br>5:23:respawn:/sbin/getty 38400 tty5 <br><br>6:23:respawn:/sbin/getty 38400 tty6</blockquote><p>to:<br></p><blockquote>1:2345:respawn:/sbin/getty 38400 tty6 <br>2:23:respawn:/sbin/getty 38400 tty2 <br>3:23:respawn:/sbin/getty 38400 tty3 <br>4:23:respawn:/sbin/getty 38400 tty4 <br>5:23:respawn:/sbin/getty 38400 tty5 <br>#6:23:respawn:/sbin/getty 38400 tty6 <br>6:23:respawn:/bin/login -f pi tty1 /dev/tty1 2&gt;&amp;1</blockquote><p><strong>Enjoy!! Thanks @dantheman_213</strong><br></p><p>Finally, this is my 5+1 Hi-Fi with bluetooth receiver :)</p><p>I'll upload a video tomorrow :)</p>
<p>Your comment made my setup finally work, thanks!</p>
<p>goddammit i really regret not reading comments had to debug the hell out of it and came to the same solution. could have saved alot of time. but thank you for posting so i dont have to ;P</p>
<p>&gt;1&deg; <b>sudo usermod &ndash;a &ndash;G lp pi doesn't found</b></p><p>Ya, I have a same error and found the cause of it.</p><p>`&ndash;` are not same with `-`!</p><p>Maybe the character are replaced by instructables's system.</p>
<p>I also added a two lines to play a sound when connecting and disconnecting: </p><p><em>This requires a connected and disconnected wav file in the /etc/bluetooth-sounds directory. Thanks to the OP, I love it!</em></p><p><em><br></em></p><p><em>For connect sound:</em></p><p><em><br></em></p><p><em><br></em></p><p><em>sudo -u pi pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 &gt;&gt; /var/log/bluetooth_dev</em></p><p><em>omxplayer /etc/bluetooth-sounds/connected.wav</em></p><p><em><br></em></p><p><em><br></em></p><p><em>For disconnect sound:</em></p><p><em><br></em></p><p><em>if [ &quot;$ACTION&quot; = &quot;remove&quot; ]<br>then<br># Turn on bluetooth discovery if device disconnects<br>sudo hciconfig hci0 piscan</em></p><p><em>omxplayer /etc/bluetooth-sounds/disconnected.wav<br>fi</em></p>
<p>NICE writeup. Thanks for posting.</p><p>I got it working but had sporadic popping noises even when not playing anything. The following seemed to fix the issue (well at least for the last 24 hrs - which is good).</p><p>From: <a href="https://dbader.org/blog/crackle-free-audio-on-the-raspberry-pi-with-mpd-and-pulseaudio" rel="nofollow">https://dbader.org/blog/crackle-free-audio-on-the-...</a></p><p>Edit /etc/pulse/default.pa and remove or comment outload-module module-suspend-on-idle. This is the important part that prevents PulseAudio from sending the audio hardware to sleep. I&rsquo;ve read that simply using PulseAudio lessened the problem for some people. But on my Pi only disabling idle suspend really helped.</p><p>Also some other info here: <a href="https://wiki.archlinux.org/index.php/PulseAudio/Troubleshooting#Audio_quality" rel="nofollow">https://wiki.archlinux.org/index.php/PulseAudio/Tr...</a></p><p>HTH.</p>
<p>Don't Work for Me ! My Phone can connect, but no sound. <br><br>Any Solutions ?</p>
<p>this wirks for me, but i need to run it manually everytime i power up RPi</p><p>try this, but you need to type this in everytime you turn off RPi, i am still looking for solution</p><p>type</p><p>pactl list sources short <br></p><p>(you should get)</p><p>0 alsa_output.platform-bcm2835_AUD0.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED</p><p>1 bluez_source.B8_C6_8E_52_E8_CA module-bluetooth-device.c s16le 2ch 44100Hz SUSPENDED</p><p>than type</p><p>pactl list sinks short <br></p><p>(you should get)</p><p>0 <strong>alsa_output.platform-bcm2835_AUD0.0.analog-stereo</strong> module-alsa-card.c s16le 2ch 44100Hz SUSPENDED</p><p>type (replace your own bluez_source and alsa_output) <br></p><p>pactl load-module module-loopback source=<em>bluez_source.B8_C6_8E_52_E8_CA</em>sink=<em>alsa_output.platform-bcm2835_AUD0.0.analog-stereo</em></p>
<p>Thanks, this worked for me, you are right, I need to run it every single time I reconnect the BT:</p><p>pi@raspberrypi ~ $ pactl list sources short</p><p>0 alsa_output.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED</p><p>1 bluez_source.B8_E8_56_BA_48_2D module-bluetooth-device.c s16le 2ch 44100Hz SUSPENDED</p>
<p>I forgot this part: </p><p>pi@raspberrypi ~ $ pactl load-module module-loopback source=bluez_source.B8_E8_56_BA_48_2D sink=alsa_output.0.analog-stereo</p>
<p>Hey Thanks for your help, I have the same problem and can able to solve with this. Now my beats are on my speakers but Can I do this without giving command (like - &quot;pi@raspberrypi ~ $ pactl load-module module-loopback source=bluez_source.B8_E8_56_BA_48_2D sink=alsa_output.0.analog-stereo&quot;)? And do connect automatically?</p>
I just get 'connection refused' from this. :(
Did you Ty pushing audio out the 3.5mm jack in raspi-confit?
how do you do this?
<p>Yes, and i See My phone into capture device, i think Pulse don't work with new versions, or the script don't work</p>
<p>I'm having the same exact problem.</p>
<p>Hey guys! I am using HC-05 Bluetooth module and have seem to stuck on step 4. </p><p>I can't seem to understand the line &quot;<em>sudo nano /var/lib/bluetooth/&lt;bluetooth mac address&gt;/config.&quot; </em></p><p>No matter whatever I write in the bluetooth mac address area, a new file gets opened and there is nothing to edit. Please help me with this step. How should I proceed.</p>
<p>I found the MAC address using this command: hcitool dev</p>
<p>Have you tried to use type `sudo nano /var/lib/bluetooth/` and then double-press tab? It should come up with a list of options (as in files and directores in the directory `/var/lib/bluetooth`). See if your MAC-address is there. If it fails, rinse and repeat the previous step(s).</p>
<p>Oh, make sure you're root by the way. The directory can't be read by anyone non-root apparently...</p>
<p>I found this error, parring working with Android but when I try to activate it like multimedia device It doesn't work:</p><p>on syslog I found (no bluetooth file found)</p><p>syslog:Oct 9 00:09:46 rasp1 bluetoothd[1959]: input-headset driver probe failed for device 18:3A:2D:C1:33:87<br>syslog:Oct 9 00:15:40 rasp1 bluetoothd[1959]: Unable to get a audio source object</p>
<p>amixer call inside <em>/usr/lib/udev/bluetooth needs </em>alsa-utils package (I'm coming from a minbian distro)</p><p>Unfortunately I did not manage yet to hear any audio neither from HDMI nor analog AV 3,5 jack while BT pairing and connection works well.</p><p>Another problem is that upon boot the bluetooth-agent do not start and I did not understand why.</p><p>However the guide is very well done, thanks</p>
<p>Few minutes after my post I managed to hear audio thanks to <a href="/member/ŽanK" rel="nofollow">ŽanK</a> contribution</p>
Great instructable, very clear, but doesn't work for me either. Tried two phones. They pair but get no sound. I added the extra lines in the comments below too, still nothing. Shame. Thanks anyway.
<p>Hi, this instructable is very well done and just what I wanted. However I have a problem....</p><p>I followed the instructions and my phone connects with my Pi via the Bluetooth. However the only sound I hear if I play any music is a buzzing. I have my Pi line out plugged into the Line in of my stereo amp. Is that correct? Other options would be CD, Phono or Tape.</p><p>Any idea why I just get buzzing?</p><p>Thx.</p>
<p>Any suggestions anyone?</p>
<p>Excellent tutorial! Really easy to follow to completion in ~15 minutes. Works fantastic. One minor thing I will say is that the audio lags by about .5 seconds, so maybe not perfect if you're watching a video (this is probably a limitation of the pi itself) But for audio alone, it works exactly the way I hoped it would!</p>
<p>I'm having the same issue as Philrlewis</p><p>&quot;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?&quot;</p>
change class as 0x4e041C in /var/lib/bluetooth ,and it's work!
<p>Hi all, the same for me... I think this file is generated at boot.</p><p>When editing /etc/bluetooth/main.conf, I set the device's Class parameter by 'Class = 0x20041C' instead of 'Class 0x20041C' regarding the classic syntax for all parameters of the file.</p>
<p>just chmod to 444. fixes the problem.</p>
<p>how to solve it?</p><p>i get this issue too, i'm so frustated..</p>
<p>Thank you very much for a very useful guide.<br><br>I got this up and running on my Pi 2. The only changes I made to the guide above were items 2 and 3 from gliuzzo's comment below and I skipped step 7 above, autologin. (I think gliuzzo's third tweak possibly takes care of this issue?)<br><br>Although it seems to be working nicely through the analogue output, I'd like it to work through HDMI, and I can't for the life of me work out how. I've tried commenting out the amixer line in /usr/lib/udev/bluetooth, without success. I'm assuming that the AUDIOSINK line of that file needs to be changed too, but to what? Can anyone help.<br><br>Many thanks in advance for any help.</p>
<p>Ah-hah. Changing the last number in the (first) amixer line from a 1 to a 2 did the trick!</p><p>There's a delay of maybe about 1.5 seconds. I was optimistically hoping to use it for audio output on Skype, so that might be just a little bit too long for it to be bearable (oh well!). Other than that, though, it seems to work a treat.</p><p>Thanks again,</p><p>Gareth</p>
<p>Great guide, i only have one problem,<br>when connecting with my android phone it connects without any problem and i can stream audio, but when connecting with my macbook air it connects on the macbook but not in the /var/log/bluetooth file. This has the result of not being able to stream audio from my macbook air.</p>
<p>This is a great guide, Dan. I had to make some changes to how pulse connects, but I don't think I would have made it through the process without this guide, which covers all the steps to automate the bluetooth connection.<br>Sincere thanks - I salute you!<br>The symptom/problem I had was that bluetooth audio would stream for a few seconds and then the pulseaudio daemon would quit.<br><br>Just in case it helps anyone, this is how I got pulse to stream from bluetooth with the current version of Raspbian, which, as of this writing is:<br>Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux<br><br>nohup pulseaudio --disallow-exit --disallow-module-loading=0 --high-priority --load=&quot;module-loopback source=bluez_source.XX_XX_XX_XX_XX_XX sink=0 &quot; 1&gt;&gt;/var/log/bluetooth_dev 2&gt;&gt;/var/log/bluetooth_dev &amp; <br><br>I hope this helps somebody.</p>
<p>Running this on the rpi2 i had to make the changes gliuzzo did below, and also added a sleep to the bluetooth-agent script, since it needed a delay before running.</p>
<p>also, changed Master to PCM in the bluetooth script to adjust volume properly.</p><p>If you're using the crappy analog audio output, you might want to also add the following line to /boot/config.txt:</p><p>disable_audio_dither=1</p><p>I'm actually using a USB audio output since its worlds better.</p><p>AUDIOSINK=&quot;alsa_output.usb-C-Media_Electronics_Inc._USB_PnP_Sound_Device-00-Device.analog-stereo&quot;</p>
<p>I've been stuck on the 'sudo usermod -a -G Ip pi'</p><p>whenever i put it in, i get this:</p><p>usermod: group 'Ip' does not exist</p><p>Ive tried all the suggestions in the comments so far but nothing works. Does anybody know why its not working?</p>
<p>I actually figured this one out, instead of 'sudo usermod -a -G Ip pi'</p><p>I used 'sudo adduser pi bluetooth'</p>
<p>So I finally got it working... Sort of.</p><p>The Raspberry Pi comes up, I can connect, But it still plays through my ipod and not the Raspberry pi</p><p>How do I fix this?</p>
<p>Does anybody knows of this also works with the raspberry pi model 2B? I had it working a few times, but with a older raspbian image. I want to use the version of 5/5/2015. Thanks</p>
<p>Worked great for me after changing the sink as other users have mentioned.</p><p>The one thing I changed to avoid having to enter pins was the following line in the bluetooth-agent</p><p>sudo hciconfig hci0 sspmode 0</p>
<p>There was also a crazy long delay (about 30 seconds) which was fixed by following https://wiki.archlinux.org/index.php/PulseAudio/Troubleshooting#Bluetooth_headset_replay_problems</p>
<p>Hi, finally got this to work. </p><p>But what about Navi-Commands: next left ... next right. Is this possible too ?</p>
<p>If you're looking for a instructable for Raspberry Pi on Debian Jessie/SystemD (e.g. OSMC), maybe you'll find this one useful: http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/</p>
<p>Thanks for a nice guide and the helpful comments below to tweak it. I just got it working with latest Raspberian. I understood from @NathanH11's comments that the reason I got no sound was related to pulse and access rights. Instead of switching to system mode I set the DISPLAY environment variable for the commands that run pulse commands as pi, changing:</p><p><em>CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev`</em></p><p><em>to</em></p><p><em>CONFIRM=`sudo -u pi DISPLAY=:0 pactl list short | grep $bluez_dev`</em></p><p><em>and</em></p><p><em>sudo -u pi pactl load-module module-loopback source=$bluez_dev</em></p><p><em>to</em></p><p><em>sudo -u pi DISPLAY=:0 pactl load-module module-loopback source=$bluez_dev</em></p><p>...that might be helpful for more people.<br></p>

About This Instructable


345 favorites


Bio: 20-something programmer.
More by dantheman_213: Turn your Raspberry Pi into a Wireless Portable Bluetooth Audio System A2DP
Add instructable to: