Bluetooth on Raspberry - Audio streaming

In this post I would like to show how to install a bluetooth dongle to stream music from the Raspberry pi to bluetooth loud speakers. As I didn’t have bluetooth loud speakers I bought a cheap receiver bluetooth in eBay to stream music.

1) Characteristics

Based on the sellers mail this is what this little bluetooth can do. This bluetooth is to be connected to the LOUD SPEAKERS, no to the Raspberry. For the Raspberry you need another bluetooth dongle.

Bluetooth Standard: Bluetooth V2.0+EDR, support A2DP V1.2
Power supply: 5 V USB
Output: 3.5mm Audio interface
Distance: about 10m
Sound output rate: 44.1kHZ and 48 kHz
Pairing code: 0000

2) Installation

First of all you need to detect if the bluetooth dongle in your Raspberry is properly detected.

lsusb

In my case I get.

Bus 001 Device 004: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

After that you have to install some tools to set up the raspberry

sudo apt-get install bluetooth bluez bluez-utils bluez-alsa

Add the default user “gusa” to the lp group (you will be able to see bluetooth sources and to change some bluetooth settings) :

sudo usermod -a -G lp gusa

Change the default bluetooth audio settings :

sudo nano /etc/bluetooth/audio.conf

Add/Complete the following line in the [General] section :

Enable=Source,Sink,Socket
Disable=Media

Done it reboot the system.

sudo reboot

Make the usb dongle works:

sudo hciconfig hci0 up

Scan for your bluetooth loud speakers :

hcitool scan

Once it is done you will get a mac address or BT ADD or BlueTooth Address of the bt device. Your Bluetooth device will have a different id.

Scanning ...
	00:11:67:8C:17:80	H163

This command will show you your local mac address.

hcitool dev

In my case

hci0	00:02:5B:51:19:C0

Next, you will use bluez-simple-agent to pair the device. My device can be paired with the passphrase 0000.

bluetooth-agent --adapter hci0 0000 00:11:67:8C:17:80 

Normally you can use this instead the last line I used:

bluez-simple-agent hci0 <hadware_id>

And then you will be asked:

RequestPinCode (/org/bluez/2211/hci0/dev_<hadware_id>)
Enter PIN Code: <pin_code>;
Release
New device (/org/bluez/2211/hci0/dev_<hadware_id>)
bluez-simple-agent hci0 00:11:67:8C:17:80

Now if you want to make the remote device be able to connect back to you without user authorization:

bluez-test-device trusted 00:11:67:8C:17:80 yes

Check if id is already trusted:

bluez-test-device trusted 00:11:67:8C:17:80

The result will be 0 for not trusted and 1 for trusted. Now connect with alsa adding to YOUR user:

sudo nano  ~/.asoundrc

This content :

pcm.bluetooth {
        type bluetooth
        device 00:11:67:8C:17:80 # change this MAC address to the one you wrote down
        profile "auto"
}

3) Streaming music

Restart the bluetooth service to start streaming music:

sudo /etc/init.d/bluetooth restart

3.1) Mplayer

Finally just install for example Mplayer and play like : You have to select audio output as Alsa device with the bluetooth you set up before. You have to resample the music to avoid fulling the buffer.

mplayer -ao alsa:device=bluetooth ./05\ Is\ It\ A\ Crime.mp3  --af=resample=22100:0:0

3.2) CMUS

CMUS is a music player for the terminal.

Sudo apt-get install cmus

In order to set up the Raspberry to run the audio using the alsa bluetooth device set up before open CMUS, press 7 and change this parameters like this.

dsp.alsa.device bluetooth
output plugin alsa

3.3) MPG321

Install mpg321, a command line music player:

sudo apt-get install mpg321

After installing, just play some music:

mpg321 -a bluetooth -g 15 yourMP3.mp3

Some explanation of the switches: -a bluetooth : The audio device (the one we just add) -g xx : The gain (audio volume You can change it in runtime with / and *)

4) Referenceces

  1. Mplayer audio output
  2. Mplayer resample option
  3. CMUS tutorial
  4. CMUS web page
  5. Raspberry PI Bluetooth Wireless Speaker

Jorge Corredera

Jorge Corredera
I am a Spanish telecommunication engineer.I like social robotics, computers and almost everything with wires. Right now, I am working as a Software Engineer. Furthermore I am always happy to be involved in challenging projects.

Pcb Working Process

Published on April 30, 2018

Cyclone Pcb Factory

Published on January 01, 2018