This gist will show how to setup Raspbian Stretch as a headless Bluetooth A2DP audio sink. This will allow your phone, laptop or other Bluetooth device to play audio wirelessly through a Rasperry Pi.
A quick search will turn up a plethora of tutorials on setting up A2DP on the Raspberry Pi. However, I felt this gist was necessary beacuse this solution is:
- Automatic & Headless - Once setup, the system is entirely automatic. No user iteration is required to pair, connect or start playback. Therefore the Raspberry Pi can be run headless.
- Simple - This solution has few dependencies, readily available packages and minimal configuration.
- Up to date - As of December 2017. Written for Raspbian Stretch & Bluez 5.43
- Raspberry Pi - I used the Raspberry Pi 3. The Pi 3 has integrated Bluetooth, however there is a known bug when the WiFi is used simultaneously.
- Raspbian Stretch Lite - See the offical guide on how to install the latest Raspbian OS: https://www.raspberrypi.org/learning/software-guide/quickstart/
If you are using a separate USB Bluetooth dongle, disable the integrated Bluetooth to prevent conflicts.
To disable the integrated Bluetooth add the following
# Disable onboard Bluetooth
dtoverlay=pi3-disable-bt
to /boot/config.txt and execute the following command
sudo systemctl disable hciuart.service
First make sure the system is up to date using the following commands.
sudo apt-get update
sudo apt-get upgrade
Then reboot the Pi to ensure the latest kernel is loaded.
Now install the per-requisite packages.
sudo apt-get install bluealsa python-dbus
Normally a Bluetooth device is only discoverable for a limited amount of time. Since this is a headless setup we want the device to always be discoverable.
- Set the DiscoverableTimeout in
/etc/bluetooth/main.confto 0
# How long to stay in discoverable mode before going back to non-discoverable
# The value is in seconds. Default is 180, i.e. 3 minutes.
# 0 = disable timer, i.e. stay discoverable forever
DiscoverableTimeout = 0
- Enable discovery on the Bluetooth controller
sudo bluetoothctl
power on
discoverable on
exit
A Bluetooth agent is a piece of software that handles pairing and authorization of Bluetooth devices. The following agent allows the Raspberry Pi to automatically pair and accept A2DP connection from Bluetooth devices. All other Bluetooth services are rejected.
Copy the included file a2dp-agent to /usr/local/bin and make the file executable with
sudo chmod +x /usr/local/bin/a2dp-agent
Before continuing, we will verify that the agent is functional, and that the Raspberry Pi is discoverable and recognized as an audio device.
- Manually run the agent by executing
sudo /usr/local/bin/a2dp-agent
- Attempt to pair and connect with the Raspberry Pi using your phone or computer.
- The agent should output the accepted and rejected Bluetooth UUIDs
A2DP Agent Registered
AuthorizeService (/org/bluez/hci0/dev_94_01_C2_47_01_AA, 0000111E-0000-1000-8000-00805F9B34FB)
Rejecting non-A2DP Service
AuthorizeService (/org/bluez/hci0/dev_94_01_C2_47_01_AA, 0000110d-0000-1000-8000-00805f9b34fb)
Authorized A2DP Service
AuthorizeService (/org/bluez/hci0/dev_94_01_C2_47_01_AA, 0000111E-0000-1000-8000-00805F9B34FB)
Rejecting non-A2DP Service
If the Raspberry Pi is not recognized as a audio device, ensure that the bluealsa package was installed as documented in the Initial Setup heading.
To make the A2Dp Agent run on boot copy the included file bt-agent-a2dp.service to /etc/systemd/system.
Now run the following command to enable the A2DP Agent service
sudo systemctl enable bt-agent-a2dp.service
Bluetooth devices should now be able to discover, pair and connect to the Raspberry Pi without any user intervention.
Written with StackEdit.