Raspberry Pi 3 and bluetooth speaker setup [command line]

Streaming sound from a Raspberry Pi 3 to a bluetooth speaker was not as easy as it should. Internet is overloaded with outdated information about this issue. So I compiled a small tutorial for the latest Raspbian (at the date of writing). My setup is composed with the following elements:

Trying to connect the device to the Raspberry Pi with the following code will raise an error:  Failed to connect: org.bluez.Error.Failed

pi@raspi:~# sudo bluetoothctl -a
Agent registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:XX:XX:XX Discovering: yes
[NEW] Device 40:EF:4C:AA:BB:CC JBL GO
[CHG] Device 40:EF:4C:AA:BB:CC UUIDs:
[bluetooth]# pair 40:EF:4C:AA:BB:CC
Attempting to pair with 40:EF:4C:AA:BB:CC
[CHG] Device 40:EF:4C:AA:BB:CC Connected: yes
[bluetooth]# trust 40:EF:4C:AA:BB:CC
[CHG] Device 40:EF:4C:AA:BB:CC Trusted: yes
Changing 40:EF:4C:AA:BB:CC trust succeeded
[CHG] Device 40:EF:4C:AA:BB:CC UUIDs:
[CHG] Device 40:EF:4C:AA:BB:CC Paired: yes
Pairing successful
[CHG] Device 40:EF:4C:AA:BB:CC Connected: no
[bluetooth]# connect 40:EF:4C:AA:BB:CC
Attempting to connect to 40:EF:4C:AA:BB:CC
Failed to connect: org.bluez.Error.Failed

It is not obvious, but what is missing to the connecting device is the answer from the host saying that it can stream sound. In order to send the correct answer to the device, pulseaudio should be started first and should correctly load the bluetooth module.

First, ensure that all requirements are installed:

pi@raspi:~# sudo apt install pi-bluetooth pulseaudio-module-bluetooth mpg123

Then start pulseaudio an check that the module is indeed loaded:

pi@raspi:~# sudo pulseaudio --start
W: [pulseaudio] main.c: This program is not intended to be run as root (unless --system is specified).
pi@raspi:~# pactl list | grep bluetooth
Name: module-bluetooth-policy
module.description = "When a bluetooth sink or source is added, load module-loopback"
Name: module-bluetooth-discover

At this point, if you try to connect your device, this should work. Most of the devices produce a sound when the connection succeed. Within bluetoothctl , it is not required to redo the pairing procedure, but before continuing you may need to ensure that the bluetooth controller is on with [bluetooth]# power on .

[bluetooth]# connect 40:EF:4C:AA:BB:CC
Attempting to connect to 40:EF:4C:AA:BB:CC
[CHG] Device 40:EF:4C:AA:BB:CC Connected: yes
Connection successful

Load a MP3 on your Raspberry Pi and play it using mpg123 your.mp3 .

If you restart your Raspberry Pi now, your device will fail to connect again.
pulseaudio is not really ment to start system-wide at boot, but in the case of embedded device it makes sense.

You could use /etc/rc.local  but it’s a bit punk, so instead let’s create a systemd service.
Add the following content to this file /lib/systemd/system/pulseaudio.service :

Description=PulseAudio Daemon


ExecStart=/usr/bin/pulseaudio --system --realtime --disallow-exit --no-cpu-limit

And start the service by hand using systemctl start pulseaudio . When running systemctl status pulseaudio , you may find a warning about starting pulseaudio system-wide, but don’t worry, with this setup it makes sense.
Add pulseaudio to the list of services to start at boot: systemctl enable pulseaudio.service

Don’t forget to add both root and pi users to the group pulse-access in /etc/groups :


Note that the group ID 115 may be different on your system.

Reboot your Raspberry Pi and you are good to go, if the speaker in ON, it should be automatically connected to your system.