Tutorials

Bluetooth A2DP sink in a Raspberry Pi running RASPBMC

Note: the developer of RASPBMC stopped developing it and have moved on to the development of OSMC. RASPBMC doesn't run in a Raspberry Pi 2.

You can only go through this tutorial if you have a Raspberry Pi Model A, A+, B or B+ and don't mind installing a distribution that will not be upgraded in the future.

There is a version of OSMC for the Raspberry Pi 2 but it is still in alpha mode. I have personally tried it and even though it looks promising, you can't even set up wireless adapters with it!

This tutorial WILL NOT work in OSMC!

For the time being, OpenElec or XBian seem to be the most viable alternatives if you have a Raspberry Pi 2.

OpenElec is included in NOOBS which makes it easier to install.

XBian has Raspbian running underneath, which makes it easier to fiddle with.

Please check the XBian version of this document, which is the current one.

Bluetooth A2DP in a Raspberry Pi Running Raspbmc / OSMC

 

Ever wanted to send music wirelessly from your phone to your stereo?

With this tutorial you'll learn how to convert your Raspberry Pi into a Bluetooth A2DP receiver
and use the Pi 3.5mm audio output to connect to your stereo or amplifier.

Note, your stereo or amplifier must have an audio input for this to work. You can also use cheap computer speakers.

Overview

If you use your Pi as a media centre running XBMC /Kodi using the RASPBMC distro, chances are your Raspberry Pi is connected to your TV using the HDMI connector, but you still have the analog output of the Pi that can be connected  to an amplifier.

Why would you like to do this? You might have apps in your phone you can't run in XBMC / Kodi and you'd like to play the sound out those apps in your speakers wireslessly. Maybe your Pi is not connected to a router, so you would not be able to send audio wirelessly with airplay or uPNP.

I was able to find an excellent tutorial in the Instructables website:

Turn your Raspberry Pi into a Wireless Portable Bluetooth Audio System A2DP

This tutorial goes in detail in how to do it running Raspbian, but I didn't want to use another Raspberry Pi just to play A2DP audio, as I already had one running RASPBMC as my media centre and connected to my amplifier.

I found the other pieces of information I needed from the OSMC forum in this link http://forum.osmc.tv/showthread.php?tid=9445 where they explain that RASPBMC comes with Pulse Audio version 3, but it doesn't have Bluetooth compiled on it. However, they did not explain how to do it.

I'm mixing the information of the Instructables tutorial, recompiling Pulse Audio version 3 in RASPBMC and adding up a few bits to make the whole thing work.

Now let's get into action to make this work.

What You'll Need?

This is an advanced tutorial, your Raspberry Pi should be connected to the Internet and you might need to SSH your pi.

Installing all the software to recompile Pulse Audio version 3 will use about 1.5GB so you better have a micro SD card with enough space.

A USB Bluetooth Adapter that supports voice and data transmission.

These are the tutorials you might need to check again before continuing with this tutorial:

You can get the Bluetooth Open Media Centre kit. It's SD card has been done following this tutorial. Or you can go to the end of this tutorial where you can buy the bits and pieces you might need put it together yourself.

Could not find any products

Compiling Pulse Audio 3 in RASPBMC / OSMC

I know you shouldn't use the root account for doing this, but I find it easier, so from now on I'll work as root. To do so, after you log in as the user pi use the following command to become root:

sudo su -

Before being able to compile Pulse Audio 3 you should update your Pi:

apt-get update

After the update, you can install all the required packages to be able to compile Pulse Audio 3, I'll do it in 4 commands, this will take a while so be patient:

apt-get install bluetooth bluez  python-gobject python-gobject-2 bluez-tools bluez-alsa bluez-gstreamer
apt-get install subversion libjson0-dev libsndfile1-dev dbus-*de bluez-utils libspeexdsp-dev
apt-get install libbluetooth3 libbluetooth-dev libc6-dev intltool libasound2-dev build-essential
apt-get install git pkg-config libtool  libcurl4-openssl-dev libncurses5-dev libudev-dev autoconf automake

There is an additional requirement to be able to compile Pulse Audio 3 with Bluetooth, SBC. To install it, enter the following commands:

cd /root
wget http://www.kernel.org/pub/linux/bluetooth/sbc-1.3.tar.gz
tar xvfz sbc-1.3.tar.gz
cd sbc-1.3
./configure --prefix=/usr
make
make install

Now to finally compile Pulse Audio 3, enter the following commands:

cd /root
svn co http://svn.stmlabs.com/svn/raspbmc/patches/pulseaudio-3.0/
cd pulseaudio-3.0/
./configure --prefix=/usr
make
make install

Now, with a new compiled Pulse Audio 3 we can proceed to put all the pieces together.

Step 1

Add the user pi to the Pulse audio group so the user can play audio through it

usermod -a -G lp pi

Step 2

To enable A2DP in our Bluetooth configuration. Run the following command:

nano /etc/bluetooth/audio.conf

This will open the text editor nano with that config file. Add the follow text under [General]:

[General]
Enable=Source,Sink

Hit CTRL+O and ENTER to save and then CTRL+X to exit nano.

Step 3

Change the device's Class parameter to 0x4E041C so it behaves like a car audio stereo.

To do so edit the file /etc/bluetooth/main.conf

nano /etc/bluetooth/main.conf

Search for the line Class = 0x000100 and change it to Class = 0x4E041C

Step 4

Modify the Pulse daemon configuration file to change its resampling method.

nano /etc/pulse/daemon.conf

Look for lines starting with: resample-method

If the lines don't have a ; in front of it, please put a ; there so it looks like:
; resample-method = xxxxx

And then add the following line underneath that.
resample-method = trivial

In my file it looks like this:

;resample-method = ffmpeg
;resample-method = speex-float-3
resample-method = trivial

Step 5

Automate Bluetooth discovery and auto accepting of connections.

In order to achieve this, we'll need to create an init script that will execute when the Pi is booted up.

nano /etc/init.d/bluetooth-agent

copy and paste the following script and then save the file /etc/init.d/bluetooth-agent

### BEGIN INIT INFO
# Provides: bluetooth-agent
# Required-Start: $remote_fs $syslog bluetooth
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Makes Bluetooth discoverable and connectable to 0000
# Description: Start Bluetooth-Agent at boot time.
### END INIT INFO
#! /bin/sh
# /etc/init.d/bluetooth-agent
USER=root
HOME=/root
export USER HOME
case "$1" in
start)
echo "setting bluetooth discoverable"
sudo hciconfig hci0 piscan
start-stop-daemon -S -x /usr/bin/bluetooth-agent -c pi -b -- 0000
echo "bluetooth-agent startet pw: 0000"
;;
stop)
echo "Stopping bluetooth-agent"
start-stop-daemon -K -x /usr/bin/bluetooth-agent
;;
*)
echo "Usage: /etc/init.d/bluetooth-agent {start|stop}"
exit 1
;;
esac
exit 0

Give this script execute permissions.

chmod 755 /etc/init.d/bluetooth-agent

Step 6

Add the snd_bcm2835 module to the list of modules to load when the system starts up.

echo snd_bcm2835 >> /etc/modules

Step 7

When I was trying to start the system, D-Bus wasn't working and was giving this error:

openmediacentre pulseaudio[1620]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

After some research, the solution was to add a few lines to /etc/rc.local:

DBUS_SESSION_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket
DBUS_SESSION_BUS_PID=`cat /run/dbus/pid`

This is what my rc.local looks like:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

DBUS_SESSION_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket
DBUS_SESSION_BUS_PID=`cat /run/dbus/pid`
pulseaudio --start

sudo -u pi  amixer cset numid=3 1
# Uncomment the following command to output through hdmi instead
#sudo -u pi  amixer cset numid=3 2

exit 0

Notice I added the line pulseaudio --start to start the pulseaudio deamon when the system starts.

Step 8

Make dbus, bluetooth and bluetooth-agent start automatically when the system starts

update-rc.d dbus defaults
update-rc.d bluetooth defaults
update-rc.d bluetooth-agent defaults

Step 9

Go back to the user pi, run pulseaudio so the system generates a few required files.

exit
pulseaudio --start

Step 10

Reboot your system

sudo reboot

Testing

After your system has rebooted, activate bluetooth in your mobile phone, it should be able to see your Raspberry Pi, just pair it and now your phone should be able to output sound through your Raspberry Pi.

Finding and Troubleshooting Compatible Bluetooth adapters

Please make sure your Bluetooth adapter supports the A2DP profile (audio streaming compatible) and starts in HCI mode. This page explains the different Bluetooth profiles: http://en.wikipedia.org/wiki/List_of_Bluetooth_profiles

Make sure the  Bluetooth adapter is compatible with the Raspberry Pi. There is a compatible adapters in http://elinux.org/RPi_USB_Bluetooth_adapters

There are some adapters like the Sitecom CN-524. CSR 8510 a10, USB ID 0a12:0001 that don't work in Linux out of the box.

To configure them, you'll have to use a Windows program:

  • Download the BlueSuite to modifiy “BlueCore Persistent Store” : http://www.kcwirefree.com/docs/BlueSuite_2_5.zip
  • Unplug your Bluetooth dongle
  • Install the BlueSuite Software (default options)
  • Plug your Bluetooth dongle in another port than previously
  • Run CSR BlueSuite 2.5.0 / PSTool as Administrator
  • With the first window : Choose Transport = USB then Port = \\.\csr0 and click OK
  • In the Filter box, type bootmode then click on the item “Initial device bootmode”
  • Note the hex number in case something goes wrong (mine was 0002), then change to 0000
  • Click on the Set button then click on the Close button
  • Wait some seconds then unplug your Bluetooth dongle.

With this modification, your Bluetooth dongle, will always start in HCI mode.

Some information about the dual-mode in Bluetooth USB : www.ie.ksu.edu.tw/data/bluetooth/28/docs/hid_dual_boot_dongle.html

Parts List

If you only need to buy some parts, these are the ones you might need:

USB Bluetooth Adapter V 4.0

This class 2 Bluetooth v4.0 device enables your Raspberry Pi or computer to communicate
with Bluetooth enabled devices such as mobile phones, printers, Bluetooth headsets and bluetooth speakers.
It supports Bluetooth voice and data transmission. It is backwards compatible with legacy Bluetooth equipment.

Not in stock, please contact us for more info
Raspberry Pi Noobs Kit Transparent - WiFi

Kit includes: Raspberry Pi 2 Model B, Noobs 8GB micro SD card with Adapter, Raspberry Pi black enclosure, HDMI 2m cable, USB power adapter - 5V 2A, USB type A to micro B 1m cable, Network RJ45 1m cable and WiFi USB adaptor with 5dBi antenna: better reception equals faster transfers and no hickups with HD movies, making this adapter ideal for media centres.

$100.80 In stock.
Raspberry Pi Noobs Kit Black - WiFi

Kit includes: Raspberry Pi 2 Model B, Noobs 8GB micro SD card with Adapter, Raspberry Pi black enclosure, HDMI 2m cable, USB power adapter - 5V 2A, USB type A to micro B 1m cable, Network RJ45 1m cable and WiFi USB adaptor with 5dBi antenna: better reception equals faster transfers and no hickups with HD movies, making this adapter ideal for media centres.

$101.80 In stock.
Raspberry Pi Noobs Kit Frosted - WiFi

Kit includes: Raspberry Pi 2 Model B, Noobs 8GB micro SD card with Adapter, Raspberry Pi frosted enclosure, HDMI 2m cable, USB power adapter - 5V 2A, USB type A to micro B 1m cable, Network RJ45 1m cable and WiFi USB adaptor with 5dBi antenna: better reception equals faster transfers and no hickups with HD movies, making this adapter ideal for media centres.

$101.80 In stock.
Raspberry Pi Noobs Kit Rainbow - WiFi

Kit includes: Raspberry Pi 2 Model B, Noobs 8GB micro SD card with Adapter, Raspberry Pi rainbow enclosure, HDMI 2m cable, USB power adapter - 5V 2A, USB type A to micro B 1m cable, Network RJ45 1m cable and WiFi USB adaptor with 5dBi antenna: better reception equals faster transfers and no hickups with HD movies, making this adapter ideal for media centres.

$107.80 In stock.
3.5mm to RCA Stereo 1.2m Black
  • Connector Type A: 3.5mm Stereo Jack Plug
  • Connector Type B: Phono (RCA) Plug, x 2
  • Jacket Colour: Black
  • Cable Length: 1.2m
  • No. of Poles: 3
$3.50 In stock.
3.5mm to 3.5mm Stereo 1.2m
  • 3.5mm Stereo Jack Plug to 3.5mm Stereo Jack Plug - Black
  • Cable Length: 1.2m
  • No. of Poles: 3

 

$7.50 In stock.
Miniature Wireless USB Keyboard and Touchpad

Add a miniature wireless controller to your computer project with this combination keyboard and touchpad. We found the smallest wireless USB keyboard available, a mere 152mm x 59mm x 12.5mm! It's small but usable to make a great accompaniment to a computer or Raspberry Pi. The keyboard itself is battery powered. The keyboard communicates back to the computer via 2.4 GHz wireless link (not Bluetooth).

$33.15 In stock.
Wireless Keyboard + Wireless Mouse + Learning TV Remote

The perfect media centre remote control. It combines a wireless keyboard, a wireless mouse and a learning infrared remote control. The wireless keyboard and mouse use a 2.4GHz USB dongle. The learning infrared allows the remote control to learn the commands from your existing infrared TV / Amplifier / DVD Player / Blue Ray Player remote controls. Simplify your life with a single remote control.

$52.00 In stock.

Comments

Mark, 02-12-14 07:22
cd /root
svn co svn.stmlabs.com/svn/raspbmc/patches/pulseaudio-3.0/
cd pulseaudio-3.0/
./configure --prefix=/usr
make
make install

Just to let you know it needs to be:

svn co http://svn.stmlabs.com/svn/raspbmc/patches/pulseaudio-3.0/

Thanks
Mark
Edgar, 02-12-14 10:49
Thank you for pointing that out Mark.

When I copied and pasted the tutorial it was actually right, the CMS did its magic and got rid of all the http://

It has been fixed now.

Regards
Edgar
Stefan, 21-12-14 18:15
got problems by installing all the required packages.

sudo apt-get update

AND

sudo apt-get dist-upgrade

worked

Thanks
Stefan
Wolfgang, 19-01-15 06:31
Hi Edgar,

Thanks for the great tutorial. I am able to pair my pi with my mobile and i can select to share media audio through Bluetooth. The phone seems to stream the audio, but I don't hear anything on my pi.

What debug possibilities to I have here? I'm very thankful for any suggestions.

br,
Wolfgang

PS: I had to add two more options to my audio.conf to be able to stream media audio to my pi.

Enable=Source,Sink,Media,Socket
Edgar, 19-01-15 08:25
Hi Wolfgang,
I just realised I didn't put a couple of commands in /etc/rc.local, I have already fixed it and it is corrected in the page of this tutorial.

These are the commands:

sudo -u pi amixer cset numid=3 1
# Uncomment the following command to output through hdmi instead
#sudo -u pi amixer cset numid=3 2

They are used to send the audio out to the analog port or the hdmi port of the raspberry pi.

I have tried to send audio to both ports at the same time, but haven't been able to do it. If someone knows how to do it please let me know and I'll add it to the tutorial.
Z, 24-01-15 08:27
I'm stuck. I did everything correct (also reffered to your comments bellow the article). I'm able to pair the device and it connects. I can change the bluetooth volume on the phone and nothing more. No sound comes out through 3,5mm or HDMI.

Durring the pairig process, my phone (Motorola G, Android 5.0) shows some sort of code like 123456 and asks me if I want to pair. Maybe I should enter that code on my Pi?

Please help me. My bluetooth dongle has CSR8510 chipset and I have model B, not B+. Maybe this is an issue, different audio outputs numbers?
Z, 24-01-15 08:46
One more thing. I tried to initalize the module manually, and this it the resulut:

pi@raspbmc:~$ pactl list sinks short
0 alsa_output.platform-bcm2835_AUD0.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz IDLE
pi@raspbmc:~$ pactl load-module module-loopback source=bluez_source.B8_C6_8E_52_E8_CA sink=alsa_output.platform-bcm2835_AUD0.0.analog-stereo
Failure: Module initialization failed
Edgar, 24-01-15 10:40
Hi Z,
I just added some information in the tutorial that relates to the Bluetooth adapter you're using.
The section is called Finding and Troubleshooting Compatible Bluetooth adapters.
Have a look and let us know if it worked for you.
Z, 24-01-15 20:27
Hi, thank you for your help.

I've tried your solution for this chip, but it didn't help. Still no sound.

There is one more thing. My phone sometimes doesn't connect after reboot, it says "unable to connect to raspbmc-0. I have to try 3 or 4 times.

Maybe I will have to do clean installation of Raspbmc one day and then try to set it up.
Z, 15-02-15 23:25
Hey, it's me again.

Today I've tried to set up bluetooth again. I think, I found the reason why it's not working properly. Please check this out:

root@raspbmc:~/pulseaudio-3.0# update-rc.d dbus defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'S17dbus' missing LSB tags and overrides
insserv: warning: script 'dbus' missing LSB tags and overrides
insserv: warning: current stop runlevel(s) (empty) of script `dbus' overrides LSB defaults (0 1 6).
insserv: warning: script 'dbus$' missing LSB tags and overrides

root@raspbmc:~# update-rc.d bluetooth defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'S17dbus' missing LSB tags and overrides
insserv: warning: script 'dbus$' missing LSB tags and overrides
insserv: warning: script 'dbus' missing LSB tags and overrides

root@raspbmc:~/pulseaudio-3.0# update-rc.d bluetooth-agent defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'S17dbus' missing LSB tags and overrides
insserv: warning: script 'dbus$' missing LSB tags and overrides
insserv: warning: script 'dbus' missing LSB tags and overrides
thomas, 24-02-15 02:54
Hi,

I'm having trouble with :

svn co http://svn.stmlabs.com/svn/raspbmc/patches/pulseaudio-3.0/

It says :

svn: E175002: Unable to connect to a repository at URL 'http://svn.stmlabs.com/svn/raspbmc/patch es/pulseaudio-3.0'
svn: E175002: OPTIONS request on '/svn/raspbmc/patches/pulseaudio-3.0' failed: 503 Service Unava ilable

I don't know if it come from the server or from me but I can't find a solution :(

Thank by advance for any tips
Edgar, 24-02-15 15:48
Hi Thomas, I just tried the command in one of my servers and it worked.
Do you have internet access?
Try doing a:
ping google.com and see if it works.
If it does, try:
ping svn.stmlabs.com
Maybe when you tried the server you were trying to download the package from was down?
You have to install subversion for this to work:
apt-get install subversion
Good luck.
thomas, 24-02-15 22:04
When I ping google and stmlabs it's working...

Subversion is installed but still have the same error...

I'm running OSMC and not raspbmc, maybe that's why ?
Marco, 05-03-15 05:38
Hi Edgar,
first of all thanks a lot for this tutorial.
I have a problem; I am able to pair my phone with raspi as Media device, but when I play something I am not able to ear anything. The phone is mute because the audio output is on the BT device (raspi), but I don't ear anything bot using HDMI and Audio jack.
I followed also the steps you have added in Finding and Troubleshooting Compatible Bluetooth adapters, but nothing has changed.
Could you suggest me something else to do?
Thanks again.
Marco
Edgar, 09-03-15 14:05
Hi Marco, the only thing that I can think of is that the audio is muted in the Pi.

Can you watch Videos or listen to music when you connect your Pi to your TV using the HDMI connector?

I have the feeling that is related to these commands:

#for analog:
sudo -u pi amixer cset numid=3 1

for hdmi:
sudo -u pi amixer cset numid=3 2

Have you tried to use the alsamixer and check that everything in unmuted?

run alsamixer and check volumes as well.
Sunjian, 10-03-15 23:00
Hi,

First of all, thanks to this tutorial, this is the only tutorial I can find to get the A2DP working on Raspbmc. But my phone can not always connect to the pi successfully, so I added "Media,Sink" follow to the "Source,Sink" in audio.conf, then I got a problem, the phone can connect to pi easily, but the sound can not be heard from the pi any more. Then I removed "Media,Sink" in audio.conf, it is still not working, I plan to re-install Raspbmc and start it over.

Sorry for my English, I'm a Chinese.
Sunjian, 13-03-15 21:38
I found where the issue is, looks like because the pulseaudio is not run, so my phone can not connect to pi, I run the pulseaudio manually by typing the commond "pulseaudio --start"(it's already in the rc.local), then the pi can be connected.
JT, 22-03-15 01:58
XBMC:~ # apt-get update

There is no working 'apt-get'.

'apt-get' is a command to install, update and remove software which
is stored in a non local repo. 'apt-get' does nothing then connecting to such
repo, downloads the software, unpacks the software, updates a big
local database with all filepaths and other informations about the
installed software or removes or updates installed Software.

With OpenELEC it is not possible to change the system for security and
stability reasons so even 'apt-get' would not be able to do this.
We also dont have and want to maintain such a repo for various other
great reasons.
Julio, 02-05-15 10:40
So I was trying your troubleshooting tips, but when I get to PSTool there is nothing under port. I know the bluetooth module works because I'm able to sync it from my PC to my phone. Is there something I'm missing?
Edgar, 03-05-15 10:18
Hi Julio, even if the adapter is working in Windows it doesn't mean that is compatible with PSTool software. Even if you can sync it it doesn't mean that it has the A2DP audio profile either. Does it look like the one in the picture of this page?
Julio, 03-05-15 14:15
Thanks for replying!
Yup, it looks exactly like that one. Down to the lettering.
I'm pretty sure it does have the A2DP profile because I'm able to stream music to my PC.
Roma, 26-12-15 22:56
Hallo leute, i have problem

"cd /root
svn co http://svn.stmlabs.com/svn/raspbmc/patches/pulseaudio-3.0/
cd pulseaudio-3.0/
./configure --prefix=/usr
make
make install"

existiert nicht.

can you help me?

Thank you
Add comment

* - required field

*




CAPTCHA image for SPAM prevention If you can't read the word, click here.

*
*
Continue Shoppping View Cart
Continue Shoppping View Wishlist