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 205Next »
JonH76 days 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!7 days ago

5.1 hi-fi bluetooth :)

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%"
NathanH111 month 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 ?

gliuzzo1 month 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 :)

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

ehsmaes1 month 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!!!

tuxfamily1 month ago
nreyes16 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 nreyes12 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 nreyes11 month ago

how to solve it?

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

JonF21 month 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?

terryo12 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

gliuzzo2 months ago

It doesn't work with Bluetooth V 2?

DomR7 months ago

I have an odd problem, I've managed to get it to work, it will connect and play a song for about 10 seconds and then there will be no sound, the song will continue to play on my phone and it is still connected via bluetooth but no sound will come out!

To start it working again, I either have to restart the Pi or just restart bluetooth (sudo service bluetooth force-reload) and it will do the same thing.

Any ideas?

I have this exact same issue, I just set it up. I've looked at the running processes to see if just maybe bluetooth or pulse or maybe it died? Nope, i see them running the entire time (mine plays between 15 and 20 seconds then dies)

Ah yeah, other people have found what i found below in the comments. It's because pulseaudio is going to idle after 20 seconds. So there is a bit of code posted below to prevent that.

Just in case anyone else gets stuck at the "PulseAudio Drops After 20 seconds", here's how I resolved it:

Edit daemon configuration:

sudo nano /etc/pulse/daemon.conf

Change

; exit-idle-time 20

to

exit-idle-time -1

dtb063 months ago

Hi, it is working fine with sideeffekt's script. I'm using it in my car to play music with a Lumia 735, but I also use an USB sound card instead of the raspberry integrated port, which has also an input jack.

I was wondering : do you think it is possible to change the device class to be recognized as a music+voice device and use it with a microphone plugged in order to answer calls or use voice guidance or to send SMS with voice recognition ? Do I need someting else like an activation button ?

Thanks to everybody for their input (and sorry for my Englidh, I'm French).

cybrg dtb062 months ago

Hi,

I also want to use a raspberry PI for my car. I have tried to rebuild several BT car kits but IMO the raspberry pi could solve my "problems" best.

So, also I want to know if you think, that it is possible to use a raspberry pi to use a microphone in my car.

thx

RobertS232 months ago

The problems I had were solely to be blamed on Raspbmc distro I was trying to get this to work on. Setting this up on Raspian (on Pi 2) was a breeze :)

Thanks.

MateuszK33 months ago

Hi, everything works like a charm.

But I have a problem with pairing. I'm connecting my tablet(android), laptop(win7), phone(android) to Pi, but everything works As long as I connect for the first time. When devices are paired, and i reconnect to the PI then the script /usr/lib/udev/bluetooth doesn't start.

I checked syslog if there is an event for "ADD" when connecting my paired device to Pi but there is no such event going from KERNEL - so the line:

KERNEL=="input[0-9]*", RUN+=/usr/lib/udev/bluetooth from /etc/udev/rules.d/99-input.rules doesn't get triggered.

For the first time (connecting not-paired) the line in syslog which triggers script is:

syslog:

Jan 26 19:00:39 raspberrypi kernel: [ 856.520658] input: XX:XX:XX:XX:XX:XX as /devices/virtual/input/input0

then i can see output in

/var/log/bluetooth_dev

Mon Jan 26 19:00:39 UTC 2015 : Executing bluetooth script...|add|
Mon Jan 26 19:00:41 UTC 2015 : Setting bluez_source to: bluez_source.XX_XX_XX_XX_XX_XX
pactl load-module module-loopback source=bluez_source.XX_XX_XX_XX_XX_XX sink=alsa_output.platform-bcm2835_AUD0.0.analog-stereo rate=44100 adjust_time=0
24

For the second time (paired)

syslog:

Jan 26 19:01:05 raspberrypi bluetoothd[2008]: Discover: Connection timed out (110)

and nothing happens.

But i can ping the phone from PI (l2ping) so the connection is established?

To be honest constantly unpairing the devices is painful ;)

how can i improve this? i looked for the answers about 110 error but what I found is not relevant in my opinion.

any help?

guaycuru3 months ago

After a firmware upgrade (rpi-update) I stopped getting audio after connecting.
After some digging around, I found that I had to make this change on /usr/lib/udev/bluetooth :

Replace this line:

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

With this:

AUDIOSINK="alsa_output.0.analog-stereo"

Hope this helps someone else!

guaycuru guaycuru3 months ago

Forgot to mention: great tutorial, thank you very much!

RobertS233 months ago

I can connect and my device is playing to Pi, but I have no sound.

When I check this I get:

$ pactl list sources short
Assertion 'l' failed at pulsecore/flist.c:168, function pa_flist_pop(). Aborting.
Aborted
Ollilie4 months ago

Its working great but i have a question. Whats about the sound if i get a call?

The voice comes out of the phone and for me its interesting to hear it out of the 3,5mm jack of the Pi because it will be mountet in my car and i like to talk free hands. Have anyone an idea?

regrats from Germany

please excuse my English

Robert__S4 months ago

The bluetooth script at (what should be) /lib/udev/bluetooth executes when a USB dongle is inserted or removed - which is not the desired action. I suggest adding the following near the top of the script - so that the script only executes if a bluetooth device links with the Raspberry Pi:

DEVTYPE=$(expr "$DEVTYPE" : "\([a-zA-Z]\+\).*")
[ "$DEVTYPE" != "link" ] && { echo "DEVTYPE is $DEVTYPE - exiting" | logger; exit; }

One small issue I am having is that I have lost a lot of bass to what I would normally have (streaming from Spotify to BT as compared to having headphones in). Do you know if this is something I can address or is this just the nature of Bluetooth or the Pi?

This worked great! The only issue I had was the volume being very low. I found a link http://kmonkey711.blogspot.co.uk/ which mentions running this command

pacmd set-sink-volume 0 65537

Cool stuff :) On to an FM transmitter now!

1-40 of 205Next »