This gist will show how to setup a generic SBC Debian / Ubuntu install 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.
This is forked from another gist specific to the Raspberry Pi on Stretch. A required package isn't in Ubuntu's repos, so in this gist we build it from scratch.
Tested to be working on Armbian/Ubuntu/Debian images of the Orange Pi Zero, ODROID XU4, ODROID N2, and Atomic Pi.
- A linux SBC running Debian or Ubuntu
- Bluetooth Dongle or integrated Bluetooth.
- Sound card (internal or external) that has been set up to work with Alsa.
curl https://gist.githubusercontent.com/qcasey/5bd4943b27320e65a033948fafb86d19/raw/2a3be1bc78e9ac30b744c2fdd332d035ac13d77d/bt-a2dp-autoinstall.sh | sudo bash
cURL-ing to bash can be dangerous, in the event of a hack. Please ensure what you're downloading first.
First make sure the system is up to date using the following commands.
sudo apt-get update
sudo apt-get upgrade
Then reboot the device to ensure the latest kernel is loaded.
Now install the required packages.
sudo apt-get install alsa git gcc make autoconf libtool blueman bluez bluetooth libbluetooth-dev libfdk-aac-dev libsbc-dev libasound2-dev libdbus-1-dev python-dbus glib-2.0-dev libperl-dev libgtk2.0-dev
Change over to your /tmp directory, and clone then build another requirement.
cd /tmp
git clone https://github.com/Arkq/bluez-alsa
cd bluez-alsa/
autoreconf --install
mkdir build && cd build
../configure --enable-aac --enable-ofono --enable-debug
make && sudo make install
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.conf
to 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 connections 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
A Python 3 version has been generously provided by @abelmatser in another gist
Before continuing, verify that the agent is functional. The Raspberry Pi should be discoverable, pairable and recognized as an audio device.
Note: At this point the device will not output any audio. This step is only to verify the Bluetooth is discoverable and bindable.
- 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
To make the A2DP Bluetooth 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
Thanks to @matthijskooijman for fixing up some issues in the Bluetooth Agent service.
Bluetooth devices should now be able to discover, pair and connect to the Raspberry Pi without any user intervention.
Now that Bluetooth devices can pair and connect with the Raspberry Pi we can test the audio playback.
The tool bluealsa-aplay
is used to forward audio from the Bluetooth device to the ALSA output device (sound card).
Execute the following command to accept A2DP audio from any connected Bluetooth device.
bluealsa-aplay -vv 00:00:00:00:00:00
Play a song on the Bluetooth device and the Raspberry Pi should output audio on either the headphone jack or the HDMI port. See this guide for configuring the audio output device of the Raspberry Pi.
To make the audio playback run on boot copy the included file a2dp-playback.service to /etc/systemd/system
.
Now run the following command to enable A2DP Playback service
sudo systemctl enable a2dp-playback.service
We also need to install bluealsa as a service, copy that to /etc/systemd/system
as well and enable it.
sudo systemctl enable bluealsa.service
Reboot and enjoy!
If you are experiencing low volume output, run alsamixer
and increase the volume of the Pi's soundcard.