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:
0000110d-0000-1000-8000-00805f9baabb
0000110b-0000-1000-8000-00805f9baabb
0000110e-0000-1000-8000-00805f9baabb
0000110f-0000-1000-8000-00805f9baabb
0000111e-0000-1000-8000-00805f9baabb
00001108-0000-1000-8000-00805f9baabb
00001131-0000-1000-8000-00805f9baabb
[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:
00001108-0000-1000-8000-00805f9baabb
0000110b-0000-1000-8000-00805f9baabb
0000110c-0000-1000-8000-00805f9baabb
0000110e-0000-1000-8000-00805f9baabb
0000111e-0000-1000-8000-00805f9baabb
[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 :

[Unit]
Description=PulseAudio Daemon

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
PrivateTmp=true
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 :

pulse-access:x:115:root,pi

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.


Posted

in

by

Comments

3 responses to “Raspberry Pi 3 and bluetooth speaker setup [command line]”

  1. Florent Avatar
    Florent

    Hi,

    Thanks for the post which really interest me.
    I have been trying to achieve your solution but cannot make it work.
    Actually no sound is being played when I launched the mpg123 with a mp3 file after following all the first steps:
    – I do have a pulseaudio service active and running
    – I do have done all the bluetooth settings
    I have succeeded the following steps :
    – Launch a pulseaudio deamon which successfully connect to bluetooth speaker (I can hear the pairing sound) thanks to the “pulseaudio -D” command
    – Launch a test mp3 file to confirm that sound can be stream in this configuration.
    Nevertheless, when i do launch the pulseaudio service :
    – I cannot hear the pairing sound
    – As a direct consequence I cannot hear any sound when playing mp3.

    Do you have any clues where it could come from ?
    Thanks a lot,

    Florent

    1. Florent Avatar
      Florent

      * Update :
      It comes from the config of pulseaudio in system mode.
      The config is at /etc/pulse/system.pa and some changes need to be made to :
      – Connect to bluetooth
      – Switch to new discovered sink available

      After that it’s working pretty well.
      Thanks,

  2. TO Avatar
    TO

    Hi Florent,
    Thank you for sharing. Do you mind pasting the system.pa file so I can update the post ?

    Regards,