HOWTO – Stream Internet Radio Stations to a Bluetooth Speaker with the Raspberry Pi

As an ongoing effort in automating all the electronic equipment which I have in my home I was looking for a solution to stream internet radio stations from the Raspberry Pi to my home amplifier which is hooked up to a bluetooth receiver. At least then I wouldn’t need to dedicate another device like my Ipad or smartphone anymore as streaming source.


A quick note about pitfalls, I tried to build this solution first with the latest Raspbian Jessie image, but was quite disappointed since the support for for the bluetooth software on this image seems to be rather broken, hence I  switched back to using the Raspbian Wheezy image, which delivered me a working solution.

The Solution is built on Alsa sound and not on Pulse Audio.

Get the right hardware

To get started, you will need a compatible Bluetooth dongle for your Raspberry Pi.

My bluetooth dongle seems to have a Broadcom Chipset (bcm2046) which is fully compliant with the Bluetooth 2.1 specification according to Broadcom’s website.

You can find this information as per below screenshot:

```
lsusb
lsusb -t
```

Getting Bluetooth Ready

To use our bluetooth dongle we will need to install some software packages:

  • sudo apt-get install -y bluetooth bluez-utils

After the installation of our bluetooth software modules, we need to make a modification to the following configuration file: /etc/bluetooth/audio.conf

  • sudo nano /etc/bluetooth/audio.conf

Under the [General] section insert the following line:

  • Enable=Source,Sink,Media,Socket,Control

Now reload your bluetooth configuration

  • sudo service bluetooth restart

Time to hook it up

Now it’s time to connect our Raspberry Pi to our Bluetooth Speaker.

First put your Bluetooth speaker in discovery mode, then scan with the Raspberry Pi for it:

  • hcitool scan

Once that u found your Bluetooth Speaker with it’s mac address: xx:xx:xx:xx:xx:xx

Pair your Bluetooth Speaker with your Raspberry Pi, you only need to do this once to get the devices to trust each other.

  • bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx

If u see below error:

Creating device failed: org.bluez.Error.ConnectionAttemptFailed: Page Timeout

wake up your bluetooth receiver again, it fell asleep.


Or if u get below error:

Creating device failed: org.bluez.Error.AuthenticationRejected: Authentication Rejected

This is a bug in bluez-simple-agent. By default, bluez-simple-agent wants to use a capability called “KeyboardDisplay” but this will not work for us in this case. We need to change that to “DisplayYesNo” so that we can get a Yes/No prompt on the command line when pairing devices.

pi@raspberrypi ~ $ grep KeyboardDisplay /usr/bin/bluez-simple-agent   capability = "KeyboardDisplay" 
pi@raspberrypi ~ $ sudo perl -i -pe 's/KeyboardDisplay/DisplayYesNo/' /usr/bin/bluez-simple-agent
pi@raspberrypi ~ $ grep DisplayYesNo /usr/bin/bluez-simple-agent capability = "DisplayYesNo"
pi@raspberrypi ~ $


This will add your Bluetooth Speaker to your Raspberry Pi’s trusted Bluetooth device list so that your Raspberry Pi can connect automatically to it afterwards:

bluez-test-device trusted xx:xx:xx:xx:xx:xx yes

This will connect the audio sink of your Bluetooth Speaker to your Raspberry Pi:


bluez-test-audio connect xx:xx:xx:xx:xx:xx


Plugging the software

Now that we have connected this audio sink we need to make this audio sink available to Alsa, the audio software from the Raspberry Pi.

For this we need to create a config file:

   

sudo nano /etc/asound.conf
pcm.btheadset {
type plug
slave {
pcm {
type bluetooth
device xx:xx:xx:xx:xx:xx
profile “auto”
}
}
hint {
show on
description “My bluetooth device”
}
}
ctl.btheadset {
type bluetooth
}

This concludes the configuration of the Bluetooth and Alsa Audio software, we will be able to play music now from our Raspberry Pi to our Bluetooth Speaker.


Let’s go streaming

Now to stream internet radio, we need to make use of another piece of audio software on linux called Media Player Daemon.

It’s a versatile daemon which allows us to build a playlist with various sources, the media player daemon just plays this playlist.

We will make use of mpc, (media player control) to control whats happening with the media player daemon, add the various resources in the playlist, start and stop playing, and so on.

Let’s first install the required software:

  • sudo apt-get install -y mpd mpc

Mpd makes use of the Alsa software to output it’s audio signal, to play over the Bluetooth connection we will need to add the correct output into mpd.

  • sudo nano /etc/mpd.conf

At the output sections, add below section as output connection for mpd.

audio_output {

  type “alsa”

  name “btheadset”

  device “btheadset”

}

After adding this, reload your mpd daemon:

  • sudo service mpd restart

Everything is in place now to make our mpd daemon play internet radio streams to our bluetooth speaker, we just need to add the internet radio url’s to the mpd playlist and play them.

The following is an example to play the q-music internet radio stream:

To view what’s in your playlist:

  • mpc playlist