Skip to content

Latest commit

 

History

History

BluetoothSpeaker

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Bluetooth Speaker with Raspberry PI

In order to be able to connect the Raspberry Pi (headless) to a Bluetooth speaker, you have to make various preparations. This endeavor can quickly become frustrating, so don't give up!

Objective

The aim of this tutorial is to learn to connect Bluetooth speakers via command line.

Precondition

You should already have read (and successful carried out) the following tutorials.

Install needed and/or optional packages

Install (or ensure they are installed) following packages.

# update system (optional)
$ sudo apt update -y && sudo apt upgrade -y

# install optional packages (optional)
$ sudo apt install -y vim curl

Preparation

1st issue

If you verify the status of the bluetooth.service, you may have the issue that the SAP driver initialization failed.

Example of 1st issue

# get status of bluetooth service
$ sudo systemctl status bluetooth.service
...
May 18 17:36:39 raspberrypi bluetoothd[496]: Sap driver initialization failed.
May 18 17:36:39 raspberrypi bluetoothd[496]: sap-server: Operation not permitted (1)
May 18 17:36:39 raspberrypi bluetoothd[496]: Failed to set privacy: Rejected (0x0b)
...

Solution of 1st issue

SAP (SIM Access Profile) can be disabled in most use cases directly in the bluetooth.service.

# modify file
$ sudo vim /lib/systemd/system/bluetooth.service

Add to the ExecStart command simply the argument --noplugin=sap.

ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap

Now restart the service and verify the status again.

# reload all service files
$ sudo systemctl daemon-reload

# restart service
$ sudo systemctl restart bluetooth.service

# get status of bluetooth service
$ sudo systemctl status bluetooth.service

2nd issue

The 2nd issue to fix is that the user pi should be added to the group bluetooth. Otherwise, only user root can use the bluetoothctl command properly.

Example of 2nd issue

pi@raspberrypi:~ $ bluetoothctl 
[bluetooth]# power on
No default controller available

[bluetooth]# agent on
Agent registration enabled

[bluetooth]# default-agent 
No agent is registered

Solution of 2nd issue

# add user pi to group bluetooth
$ sudo usermod -G bluetooth -a pi

# verify group (optional)
$ sudo cat /etc/group | grep bluetooth

# reboot (optional)
$ sudo reboot

Note: this step is not needed if you use a GUI (Raspberry Pi OS with desktop)!

3rd issue

As there is no (GUI) player, you need some tools to play media files (e.g. MP3 files) from command line.

Solution of 3rd issue

Note: This step is only for Pulseaudio needed, for BlueALSA you should use aplay!

# install package
$ sudo apt install -y sox libsox-fmt-all

# download a mp3 file (optional)
$ curl "https://cdnm.meln.top/mr/Bob%20Marley%20-%20Roots%20rock%20reagge.mp3?session_key=64881459a074106afb3bdf36670fb2f2&hash=b82631d7822e065e953767c362efd167" -o test.mp3

# play mp3 from command line (optional)
$ play test.mp3

Note: in case you like to use VLC, you can do $ nvlc test.mp3.

Pulseaudio

The first approach is to use Pulseaudio, in case you already tried and don't like it - jump over to second approach BlueALSA.

# install package
$ sudo apt install -y --no-install-recommends pulseaudio pulseaudio-module-bluetooth

The pulse user needs permission to use Bluetooth.

# modify file
$ sudo vim /etc/dbus-1/system.d/pulseaudio-system.conf

Add the following content to file /etc/dbus-1/system.d/pulseaudio-system.conf.

<busconfig>
    <policy user="pulse">
        <allow own="org.pulseaudio.Server"/>
        <allow send_destination="org.bluez"/>
    </policy>
</busconfig>

For Bluetooth, you need also to load some driver modules. This can be done via file /etc/pulse/system.pa.

# modify file
$ sudo vim /etc/pulse/system.pa

Add following content at the end and save.

### Load driver modules for Bluetooth  
.ifexists module-bluetooth-policy.so  
load-module module-bluetooth-policy  
.endif  
 
.ifexists module-bluetooth-discover.so  
load-module module-bluetooth-discover  
.endif

Add user pi should be added to the groups pulse-access and audio.

# add user pi to group pulse-access
$ sudo usermod -G pulse-access,audio -a pi

# verify group (optional)
$ sudo cat /etc/group | grep pulse-access
$ sudo cat /etc/group | grep audio

# reboot (recommended)
$ sudo reboot

Connect the speaker, select audio output (sink) and play music.

# trust a device
[bluetooth]# trust [mac address]

# pair a device
[bluetooth]# pair [mac address]

# connect to device
[bluetooth]# connect [mac address]

Note: Please also note the necessary steps from tutorial Bluetooth Basics & Analysis!

# start pulseaudio
$ pulseaudio --start

# list available cards (optional)
$ pactl list cards short

# list available audio sinks
$ pactl list sinks short

# select bluetooth sink
$ pactl set-default-sink 1

# set volume (optional)
$ pactl set-sink-volume 1 60%

# play sound
$ play test.mp3

Note: This process is very unstable and can be super annoying. I do use 2 terminals (1x for bluetoothctl and 1x for pactl).

BlueALSA

# install package
$ sudo apt install -y bluealsa

# start service
$ sudo systemctl start bluealsa.service

# get status (optional)
$ sudo systemctl status bluealsa.service

Same as above - connect speaker and play sound.

# trust a device
[bluetooth]# trust [mac address]

# pair a device
[bluetooth]# pair [mac address]

# connect to device
[bluetooth]# connect [mac address]

# play sound (A2DP)
$ aplay -D bluealsa:HCI=hci0,DEV=[mac address],PROFILE=a2dp test.mp3

# play sound (SCO)
$ aplay -D bluealsa:HCI=hci0,DEV=[mac address],PROFILE=sco test.mp3

We come to the somewhat surprising possibility. You can (depending on the target device) also spy on what victims are talking about.

# record sound (SCO)
$ arecord -D bluealsa:HCI=hci0,DEV=[mac address],PROFILE=sco spy.wav

Since many people take for example, AirPods with them to the office, it becomes critical at this point. These Bluetooth devices mostly use BLE (AKA Bluetooth Smart) and do not offer/need a pairing PIN option. ... What a world!

Go Back