Instructables
loading
loading

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 220Next »
Slapshot13513 days ago

I've been stuck on the 'sudo usermod -a -G Ip pi'

whenever i put it in, i get this:

usermod: group 'Ip' does not exist

Ive tried all the suggestions in the comments so far but nothing works. Does anybody know why its not working?

I actually figured this one out, instead of 'sudo usermod -a -G Ip pi'

I used 'sudo adduser pi bluetooth'

Slapshot13511 days ago

So I finally got it working... Sort of.

The Raspberry Pi comes up, I can connect, But it still plays through my ipod and not the Raspberry pi

How do I fix this?

hottroc13 days ago

Hi, this instructable is very well done and just what I wanted. However I have a problem....

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.

Any idea why I just get buzzing?

Thx.

gliuzzo3 months ago

Hi guys! I Made it!

It works nice, but I had some problems. These:

sudo usermod –a –G lp pi doesn't found

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

output audio doesn't work

in this file: /usr/lib/udev/bluetooth :

Replace this line:

AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"

With this:

AUDIOSINK="alsa_output.0.analog-stereo"

output audio after 20 seconds stopped

Edit daemon configuration:

sudo nano /etc/pulse/daemon.conf

Change

; exit-idle-time 20

to

exit-idle-time -1

bluetooth devices doesn't connect

this is a proble about auto-login in tty1; I resolved this re-writing the /etc/inittab file like this, from:

#1:2345:respawn:/sbin/getty 38400 tty1
1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3

4:23:respawn:/sbin/getty 38400 tty4

5:23:respawn:/sbin/getty 38400 tty5

6:23:respawn:/sbin/getty 38400 tty6

to:

1:2345:respawn:/sbin/getty 38400 tty6
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
#6:23:respawn:/sbin/getty 38400 tty6
6:23:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

Enjoy!! Thanks @dantheman_213

Finally, this is my 5+1 Hi-Fi with bluetooth receiver :)

I'll upload a video tomorrow :)

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

ironsand gliuzzo3 months ago

>1° sudo usermod –a –G lp pi doesn't found

Ya, I have a same error and found the cause of it.

`–` are not same with `-`!

Maybe the character are replaced by instructables's system.

Score81 month ago

Don't Work for Me ! My Phone can connect, but no sound.

Any Solutions ?

ŽanK Score81 month ago

this wirks for me, but i need to run it manually everytime i power up RPi

try this, but you need to type this in everytime you turn off RPi, i am still looking for solution

type

pactl list sources short

(you should get)

0 alsa_output.platform-bcm2835_AUD0.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED

1 bluez_source.B8_C6_8E_52_E8_CA module-bluetooth-device.c s16le 2ch 44100Hz SUSPENDED

than type

pactl list sinks short

(you should get)

0 alsa_output.platform-bcm2835_AUD0.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED

type (replace your own bluez_source and alsa_output)

pactl load-module module-loopback source=bluez_source.B8_C6_8E_52_E8_CAsink=alsa_output.platform-bcm2835_AUD0.0.analog-stereo

RodolfoF Score81 month ago

I'm having the same exact problem.

Did you Ty pushing audio out the 3.5mm jack in raspi-confit?

Yes, and i See My phone into capture device, i think Pulse don't work with new versions, or the script don't work

wapenv1 month ago

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

GraemeY11 month ago

Worked great for me after changing the sink as other users have mentioned.

The one thing I changed to avoid having to enter pins was the following line in the bluetooth-agent

sudo hciconfig hci0 sspmode 0

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

mike967711 month ago

Hi, finally got this to work.

But what about Navi-Commands: next left ... next right. Is this possible too ?

RobertS231 month ago

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/

JonH72 months ago

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:

CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev`

to

CONFIRM=`sudo -u pi DISPLAY=:0 pactl list short | grep $bluez_dev`

and

sudo -u pi pactl load-module module-loopback source=$bluez_dev

to

sudo -u pi DISPLAY=:0 pactl load-module module-loopback source=$bluez_dev

...that might be helpful for more people.

gliuzzo made it!2 months ago

5.1 hi-fi bluetooth :)

BlahBlahBlase2 months ago
I managed to fix the audio not playing problem by forcing audio out of the 3.5mm jack in raspi-config. sudo raspi-config > advanced > Audio > force 3.5mm.
This was after following gliuzzo's fixes
and running the chmod 444 that groovechampion1 suggested
sorry for all the additional replies, I drank a couple victory beers before I thought to share how I figured it out.

I also had a problem with it being too quiet, I fixed this by running "amixer cset numid=3 100%"
NathanH113 months ago

After playing with this for 2 days I just got this working on the latest. I had to make some pretty substantial changes... I'll list them out here, but I'm working from memory; they should give you enough pointers to self-solve:

1) The biggest problem is that pulseaudio will not work unless it's in system mode. Otherwise you'll always see it fail when it tries to access dbus. The problem stems from it not working in user mode unless you're running X11 and in an X session. I tried everything I could think of, but couldn't get a user daemon of pulseaudio to successfully access the system dbus. My guess is that this is the difference between a pristine minimal raspbian image and a Noobs image with all the trimmings.

The fix: run pulseaudio as a system service. To do that, you'll need to edit the /etc/default/pulseaudio config file, and the /etc/pulseaudio/ config files (plural). They should all match and declare themselves as system config; the pulseaudio page has info about the various settings and the precedence: it's a mess. You'll also need to add the bluetooth module to the puleaudio system.pa config, and there are some other modules that try to load and fail without X11, which need to be masked. You'll figure it out sequentially by tailing /var/log/syslog; the only one that seems to not be an actual problem is that the HFP endpoint in bluez isn't being set up: I chased that error but ultimately got everything working with it still there; (it will be there if you precisely follow the guide above and it's a red herring). You'll then need to go through the steps of giving user permissions to the pulse user and the pi user. Once that's done, you need to go setup dbus to allow bluez to contact pulse and vice versa. Pulse needs to be in the 'lp' group among other things.

2) The name of the pulseaudio alsa sink has changed. That's easy. You can fix with a little one-liner script to automatically pull the endpoint name when loading the loopback module, or since it's not likely to change, just 'pactl list sinks short' and hard-code it.

3) If you happen to pair the bluetooth device in the wrong order/with the wrong profile, you'll get a bunch of random config in /var/lib/bluetooth/<mac>/. Delete those files and fix the config file. Note: if you use a comment or mis-format the config file, the BT drivers will overwrite your settings. The file must be clean. If you fubar the setup, delete the whole directory and the next time the device loads it'll be clean.

Best of luck everyone!

Finally got things working with this helpful insight from NathanH11!

I ended up following this guide:

https://www.raspberrypi.org/forums/viewtopic.php?p...

to use PulseAudio system wide and start the Bluetooth daemon. I was able to finally get sound to come out of the on-board audio and eventually through all 5.1 channels on a USB sound card (frustrating). I did however slightly tweak the script posted by sideeffekt to replace "bluez-udev" mentioned in the guide. This script is a little more "hard-coded" since I am using a USB sound card.

#!/bin/bash

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

BSINK=0

echo BSINK=0

BSOURCE=$(pactl list sources short| grep bluez_source | awk '{print $2;}')

echo BSOURCE=$(pactl list sources short| grep bluez_source | awk '{print $2;}')

pactl load-modulemodule-loopback source="$BSOURCE" sink="$BSINK"

echo pactl load-modulemodule-loopback source="$BSOURCE" sink="$BSINK"

# Turn on bluetooth discovery on disconnect

if["$ACTION"="remove" ]

then

handle=$(pactl load-modulemodule-loopback source=bluez_source."$mac" sink=0)

for handle in $(pactl list short modules | grep module-loopback | grep source="$BSOURCE"| cut -f 1);do

pactl unload-module"$handle"

echo pactl unload-module"$handle"

done

sudo hciconfig hci0 piscan

fi

Couple other notes. I also was able to use the latest Wheezy available at the time (2015-02-16) and both updated and upgraded on a RPi Model B. Bluetooth must be paired and trusted for audio streaming to work properly. I also had to overclock Pi to high to eliminate stuttering when streaming to 6 channels (default-sample-channels = 6, default-fragments = 6, default-fragment-size-msec =25). iPhones did not seem to trigger the udev input rule but Android did. iPhones were also unable to control volume but Android could. A powered USB was also needed for my project but the BT CSR 4.0 adapter worked best when connected directly to Pi.

Cut my thought short. This is with the latest default raspbian image (not NOOBS) and with a full sudo apt-get update && sudo apt-get upgrade before starting.

can you please explain it in detail ?

ehsmaes3 months ago

I am close to giving up (and I am not new to headless linux). Too much time spent. I have sort-of got it to work a couple of times but there is always something not working and in trying to fix it I loose my way and I am forced to start from a fresh memory card again.

I can get it to pair but the phone immediately disconnects and any consecutive attempts immediately disconnect. I think there is an incompatility between device classes. I get Class: 0x42041c from "bt-adapter -i".

Anyway, @danteman_213, based on all the comments I would say it is safe to say your instructable could use an update. Everybody seems to have a different opinion on what the magic solution is and it is not easy to piece it all together.

In the meanwhile - does anyone out there have a dropbox with a working SD-card-image? Please!

So I've done everything in the directions and my phone connects to the PI no problem but no audio streams. It shows that it is connected and connects right away with no problem besides the audio not playing. Anyone know what it could be? Could really use some help! Thanks!!!

tuxfamily3 months ago
nreyes18 months ago

I'm having the same issue as Philrlewis

"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?"

ThomasB18 nreyes14 months ago

Hi all, the same for me... I think this file is generated at boot.

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.

just chmod to 444. fixes the problem.

DhoniG nreyes13 months ago

how to solve it?

i get this issue too, i'm so frustated..

JonF23 months ago

hello, I followed your tutorial and didn't seem to receive any errors and while I can pair both Android and my mac to the rpi, no audio plays- suggestions?

terryo14 months ago

I have not read through all of the comments, but does anyone have this outputting through hifiberry? Are there any modifications for this?

Thumb up for this. I also have a hifiberry and bluetooth installed.

How to change the output to the hifiberry soundcard ?

solved it. streaming from phone to hifiberry is possible

change

AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"

to

AUDIOSINK="alsa_output.platform-sound.analog-stereo"

this was the right, after reading the sink source with command pactl list

gliuzzo4 months ago

It doesn't work with Bluetooth V 2?

1-40 of 220Next »