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><em>sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2 bluez-tools</em></p><p>I get Error:</p><p>Errors were encountered while processing:<br> libapache2-mod-php7.0<br>E: Sub-process /usr/bin/dpkg returned an error code (1)</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>Hi ZanK, thanks very much for the info. I solved the problem that the command had to be run every time manually. </p><p>type the command:</p><p><em>sudo nano /usr/lib/udev/bluetooth</em></p><p>then comment out two lines from:</p><p>mac=$(cat &quot;$dev/name&quot; | sed 's/:/_/g')</p><p> bluez_dev=bluez_source.$mac</p><p>to </p><p>#mac=$(cat &quot;$dev/name&quot; | sed 's/:/_/g')</p><p>#bluez_dev=bluez_source.$mac</p><p>finally add a new line, which is the specific MAC address of your bluetooth module:</p><p>bluez_dev=bluez_source.C4_07_2F_74_52_E9</p><p>C4_07_2F_74_52_E9 is the MAC address of my bluetooth module. Give a try, it should work.</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. :(
<p>I'm having the same exact problem.</p>
<p>I found the problem. </p><p>In the instructions, we may be adding the rules to the wrong file. For instance, mine did not have 99-inputs.rules but instead its 99-com.rules. I added that additional line to that file instead and it works without needing to type the load-module instruction.</p>
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 succeeded config raspberryPi 3 with bluetooth audio last night.</p><p>There are something new in raspberryPi 3 with newest operate system, in pure command environments.</p><p>I've recorded all the processes <a href="https://github.com/jobpassion/raspberryPi/blob/master/BluetoothSpeaker.md" rel="nofollow">https://github.com/jobpassion/raspberryPi/blob/mas...</a></p>
<p>Many thanks!</p>
<p>I followed your instruction and it worked! But the sound quality is terrible. Is there a way to improve it?</p>
<p>Pin code to pair was 0000 </p>
<p>I am running a Pi MusicBox 0.6.0 and cant get pulse audio to work. </p><p>After pactl load-module module-loopback i get error module initialization failed or module connection refused</p><p>But parecord and aplay seems to work very well.</p><p>here is my bluetooth script, the pi user needs to be in the audio group</p><p>http://pastebin.com/BFVUtSpu</p>
Also with the new raspbian updates is this still possible by using this same tutorial?
<p>Hey,<br>it didn't work for me with the latest raspbian jessie. I took wheezy from 05-05-2015 and it worked.</p><p>You can get it here: </p><p>http://downloads.raspberrypi.org/raspbian/images/</p>
Also with the new raspbian updates is this still possible by using this same tutorial?
I'm missing the audio.config file what do I need in there for it to work?
<p>Hi everyone</p><p>Thanks to dan for this nice tutorial and also a big thanks to gliuzzo for his troubleshooting.</p><p>I run a RPi B+ with Wheezy 05-05-2015 and the bluetooth audio thing works great...the first ten minutes.</p><p>Audio quality is fine and so on but after a while (about 10 to 15 minutes) i get a echo in the music that makes me crazy. The longer i let the music play, the more echo sound i hear. After a reboot of the Pi everything works again as it should.</p><p>Anyone else got that problem?</p><p>I tried the Echo/Noise-Cancellation as described in</p><p><a href="https://wiki.archlinux.org/index.php/PulseAudio/Troubleshooting#Enable_Echo.2FNoise-Cancelation" rel="nofollow">https://wiki.archlinux.org/index.php/PulseAudio/Tr...</a></p><p>but after making this, the RPi doesnt play music anymore...why? I dont get it.</p><p>Anyone gut suggestions? Thanks in advance<br>Robert</p>
<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>I have the same issue, don't know how to fix it....</p>
<p>Hi all i was tryng to make it but seems that this guide is too old for the new bluez 5 and pulse audio version infact some of the file are missing or the name is changed, could you please update the guide so i can made it?</p><p>a big thank you</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>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>

About This Instructable




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