Setting up a USB audio device as a Bluetooth speaker under Ubuntu - arkq/bluez-alsa GitHub Wiki

Setting up a USB audio device as a Bluetooth speaker under Ubuntu

This guide explains how to configure a USB audio (or onboard audio) device attached to a Ubuntu 22.04 machine as a Bluetooth speaker. It presumes you are NOT running Pulseaudio, JACK or Pipewire. Pulseaudio is installed by default by most Ubuntu-based desktop distros so this guide is best suited to Ubuntu Server and the Ubuntu Server versions of Armbian that don't include Pulseaudio by default.

Install required packages

Ubuntu 22.04 users can install bluez-alsa-utils and the bluez Bluetooth configuration tools by running:

$ sudo apt install bluez bluez-alsa-utils

Note that Ubuntu 22.04 is the first release of Ubuntu to have the bluez-alsa-utils package in its repos and at least one bug is known to be fixed in the latest bluez-alsa code compared to the version in the 22.04 repos - automatic resampling of a2dp streams but this can be worked around with the ALSA configuration below if required.

Check your Bluetooth adapter is working

Before we go any further, lets check Ubuntu recognises your machines Bluetooth adapter:

$ bluetoothctl show

This command will show the name of your Bluetooth controller if it has been recognised by the Linux kernel.

Configure the bluez-alsa.service systemd service

Edit /etc/default/bluez-alsa and modify the OPTIONS line so that it reads:

OPTIONS="-p a2dp-sink"

This enables the bluez-alsa Bluetooth audio receiver profile.

Configure ALSA

Unless you tell it to do otherwise, ALSA will default to using your computers onboard audio device, if it is supported by ALSA. In this guide we want ALSA to use an external USB audio device so we must first find out the ALSA device name of the audio device we wish to use to playback the Bluetooth audio stream with. You may not need to create an ALSA config file if you are using the default audio device or a newer distro than Ubuntu 22.04.

You can list all of the audio devices currently attached and supported by ALSA by running:

$ aplay -L

There is a bug in the version of bluez-alsa-utils used in the Ubuntu 22.04 repos (the package is based upon bluez-alsa 3.0 whilst version 4.0 is current at the time of writing) that causes errors when resampling a2dp streams so under 22.04 we must use create a new ALSA pcm device that uses the ALSA plug plugin to fix this resampling issue.

aplay -L told me that my USB audio device is called front:CARD=UACDemoV10,DEV=0 so I created a /etc/asound.conf ALSA config file like this:

pcm.front-plug {
    type plug
    slave.pcm "front:CARD=UACDemoV10,DEV=0"
}

This code creates a new ALSA pcm device called front-plug that uses my USB audio device, front:CARD=UACDemoV10,DEV=0 to output the audio and uses the ALSA plug plugin to resample the stream so that it will play without errors. You will need to adjust the audio device name used by slave.pcm but you can call this new device what you wish instead of front-plug.

At this point you may want to either restart bluez-alsa and ALSA or just reboot your machine.

Connect your Bluetooth device

Enter the bluetoothctl console by running bluetoothctl with no extra options then run scan on. Enable the Bluetooth connection on your device and you should see it recognised with its address listed in the bluetoothctl console within a few seconds.

Next, pair your bluetooth device by running pair XX:XX:XX:XX:XX:XX followed by trust XX:XX:XX:XX:XX:XX then connect XX:XX:XX:XX:XX:XX replacing the X's with your devices Bluetooth MAC address. After you have paired your device you can quit the bluetoothctl console by typing exit or quit. You usually only need to perform these steps the first time you pair a device.

Play the Bluetooth audio stream using bluealsa-aplay

If your device says it is connected to your Ubuntu machine under its Bluetooth settings, you should now be ready to playback the a2dp steam (ie whatever audio is playing on your connected BT device) on your Ubuntu box by running:

$ bluealsa-aplay -D front-plug

Replace front-plug with the name of the ALSA pcm device you want to use for playback.

It is recommended that you create a systemd service to start bluealsa-aplay at boot instead of manually running it. See the instructions for that here.