Tutorials

Bluetooth A2DP Sink in XBian

By: Edgar Pisani

Bluetooth A2DP in a Raspberry Pi Running XBian

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, 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 but they do it with Raspbian. I wanted to do it using the same Pi I was using as a media centre.

I made this work originally for Raspbmc, but then I got a Raspberry Pi 2 and Raspbmc would not work in the Pi 2, so I started looking for alternatives. The logical alternative was to use OSMC, but OSMC is still in alpha mode for the Raspberry Pi 2.

Then I found out that XBian already has a package for creating a A2DP sink.

After reading some information in the XBian forum about the package xbian-a2dp ( http://forum.xbian.org/thread-2154.html ) I found out that there are two packages available:

  • One in the XBian repository, that you would install with a simple apt-get install xbian-a2dp. That package uses pulse audio. Unfortunately it didn't work for me.
  • Then there is another version that uses a "bridge" between bluetooth and alsa that doesn't need pulse audio at all. That package can be found here: https://github.com/belese/xbian-package-a2dp

I ended up installing the second option, but it didn't work out of the box, I had to do some adjustments.

There is additional problem related to bluetooth and the alsa solution, which is the one I'm using. It would only work with a bluetooth version lower than 5. To check which version of bluetooth your XBian system is running, you can do it with any of the following commands:

dpkg --status bluez | grep '^Version:'

or:

bluetoothd -v

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.

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:

Please check the parts list at the end of this tutorial.

Building the alsa package of xbian-package-a2dp

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 xbian (password raspberry) use the following command to become root:

sudo su -

Update your Pi:

apt-get update

After the update, you can install the required packages to build xbian-a2dp:

apt-get install git fakeroot

Now you are ready to build the package. Enter the following commands:

cd /root
git init
git clone https://github.com/belese/xbian-package-a2dp
cd xbian-package-a2dp

To generate the package, you have to run the following command:

./gen.package.sh

If you have a Raspberry Pi, ./gen.package.sh will run with no problem, but if you have a Raspberry Pi 2, you'll have to do some modifications.

In ./gen.package.sh, look for a line that says arm6l and replace it with arm7l, then run the command:

./gen.package.sh

It should run with no problem.

Now you should have a debian package called:

xbian-package-a2dp1.1.deb

Proceed to install it:

dpkg -i xbian-package-a2dp1.1.deb

Troubleshooting

After doing all this, everything seemed ok, but it wasn't working.

I checked the logs and I was getting some errors.

I believe the bluetooth service was starting before the device was ready, generating an error.

The only way it worked for me, was by adding the following lines in /etc/rc.local before exit 0:

sudo -u xbian amixer cset numid=3 1
# Uncomment the following command to output through hdmi instead
#sudo -u xbian amixer cset numid=3 2
service bluetooth restart
start bluetooth-a2dp
start bluetooth-pairing
exit 0

So basically by restarting the bluetooth service and then the xbian bloutooth-a2dp and bluetooth-pairing services, the system was able to work ok.

Also, notice that I'm selecting the analog audio output of the Pi, if you want the audio to come out of the hdmi instead, uncomment the line that says sudo -u xbian amixer cset numid=3 2

Troubleshooting Alsa Sound

Before your bluetooth connection can work, you have to make sure that the alsa sound system is working properly in your Pi.

As user xbian, run the following command to divert the sound to the analog sound output:

amixer -c 0 cset numid=3 1

Then run a speaker test:

speaker-test -t wav -c 2

Press control c to stop the test. You can find mote tests in https://www.mythtv.org/wiki/Using_ALSA%27s_speaker-test_utility

If there is no sound, make sure that the PCM output is not muted and adjust the volume, you can check both wit the command:

alsamixer

Once you are happy with the output volume you can save your sound settings with:

sudo alsactl store

 

 

Troubleshooting Classes in Bluetooth

For checking bluetooth interfaces and changing the class.

hciconfig

It should give you the name of the interface, e.g., hci0.

Given that display your interface class:

hciconfig hci0 class

That should display your device class.

Finally, with super user privilege, you can change the class:

sudo hciconfig hci0 class 000408

The following website has a utility for creating class numbers. Class numbers can be altered in the file /etc/bluetooth/main.conf

 

bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html

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

To build your own media centre, you can get any of the following kits. If you prefer to get a SD card with XBian instead of NOOBS, just write it down in the special instructions when you place an order:

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

Stefan, 30-03-15 05:24
WOW!!
It works perfektly
I'm 'working' on this over a week trying it with Raspbmc, etc.. and various other tutorials
But this is the first (and good) tutorial witch worked !!!!
really good work,
Thanks
Dave, 10-06-15 09:14
hey guys

after installing the package as described in your instructions, a2dp works fine. THANK YOU! :)

But now I've got an new issue: now CEC does not longer work properly. I don't know why, but i think it's caused by a conflict with one of the new packages. Do you have a clue, what's the matter?
Edgar, 10-06-15 19:10
Hi Dave, I don't know what CEC is?
I have the feeling that when you did the apt-get update maybe something broke in Xbian?
All this tutorial does is build a debian package and install it.
Have you tried removing the package with a dpkg -r and then removing the lines from rc.local to check if that's the reason why something else got affected?
Denis, 27-11-15 03:03
Hi,

thank you, worked well for me. I have just one issue. The playbackspeed change during playback. Any Idea?
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