[How-to] set up Bluetooth audio

[Updated November 2021]

Bluez-alsa allows you to stream audio both to and from your OSMC device. We have now integrated bluez-alsa into OSMC as a replacement for our a2dp package which is deprecated. Our Kodi v19 builds include bluez-alsa. If you are still on v18.9, you can install it on Vero 4k or RPi 2/3 with:

sudo apt-get install armv7-bluezalsa-osmc

and re-boot to get the bluealsa service started.

If you have previously installed one of our older BT audio packages, you will need to remove it/them (see below).

Streaming to your OSMC device

Just use My OSMC → Networking → Bluetooth to pair and trust your source (eg a smartphone). You should now be able to choose your OSMC device in the list of BT devices on your smartphone.

The stream goes to your default audio device which is HDMI. Make sure Kodi isn’t trying to use HDMI audio at the same time - turn off “Keep audio device alive” in Settings->System->Audio. See below for streaming to other audio devices.

Streaming from your OSMC device

Since our November 2021 update with Kodi 19.3, this works without having to write ALSA config files. Just pair and connect your BT audio device using My OSMC or bluetoothctl and an item ‘Bluetooth Audio’ will appear in Settings->System->Audio->Audio output device whenever a BT device is connected.

If you have more than one BT audio device and want each device name to show up in Kodi’s device selection dialogue you must make a custom ALSA configuration file. See below. Otherwise, do not use a .asoundrc file for Bluetooth.

Connecting using bluetoothctl

If you can’t connect using My OSMC, try using the command bluetoothctl. Start it from the command line, make your BT device discoverable, then type

agent on
scan on

Wait for your headphones/speaker to show up, then, using its MAC instead of 01:23:45:67:89:ab (bluetoothctl supports tab completion so type, eg 01 Tab):

pair 01:23:45:67:89:ab
trust 01:23:45:67:89:ab
connect 01:23:45:67:89:ab
scan off
agent off
quit

Writing a custom ALSA config file

If the default setup doesn’t work for your device or you have special requirements you will need to write a configuration file for ALSA. You need to be familiar with the commandline and a text editor (eg nano).

You need to make a file called .asoundrc in your home directory (/home/osmc). Your .asoundrc file should have entries like this

pcm.mybtspeaker {
        type bluealsa
        device "01:23:45:67:89:AB"
        profile "a2dp"
        hint { show on description "My BT Speaker"}
}

mybtspeaker can be any name you like, as long as it’s unique.

01:23:45:67:89:AB is a bluetooth MAC address. You need to substitute the MAC address of your speaker/headphones. You may see this when you connect it using My OSMC. If not, use bluetoothctl devices to find it.

My BT Speaker can also be anything you like and it will show up in Kodi’s list of audio devices.

Make a separate entry in .asoundrc for each of your BT audio devices.

After saving the .asoundrc file, re-start Kodi (Power->Exit) or disconnect and re-connect your BT device so that Kodi knows it’s available.

Streaming from BT to a device other than HDMI

The streaming function is provided by bluealsa-aplay which is started by systemd. To stream from your phone to, say, a USB DAC you can edit the systemd unit file sudo nano /lib/systemd/system/bluealsa-aplay.service like this (ie add the -D option to the ExecStart line):

[Unit]
Description=A2DP Playback
After=bluealsa.service syslog.service
Requires=bluealsa.service
StopWhenUnneeded=true

[Service]
ExecStartPre=/bin/sleep 3
ExecStart=/usr/bin/bluealsa-aplay -D front:CARD=Audio --profile-a2dp 00:00:00:00:00:00
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=A2DP-Playback
Restart=on-failure

[Install]
WantedBy=bluetooth.target

then
sudo systemctl daemon-reload
sudo systemctl restart bluealsa-aplay

This assumes your DAC is reported like this by aplay -L

default:CARD=Audio
    USB2.0 High-Speed True HD Audio, USB Audio
    Default Audio Device

USB cards are usually called ‘Audio’. If yours is different, or for other audio cards like Pi HATs, substitute what aplay -L tells you after CARD=.

You may have to install alsa-utils which provides aplay:

sudo apt-get update
sudo apt-get install alsa-utils

Removing older BT audio packages

OSMC used to ship a package called a2dp-app-osmc which used a customised version of pulseaudio. This needs to be removed as we no longer support pulse. Also remove any previous version of this bluez-alsa based package.

sudo dpkg -r armv7-bluez-alsa-app-osmc
sudo dpkg -r armv7-bluez-alsa-osmc
sudo apt-get --fix-broken install
sudo apt-get remove --purge a2dp-app-osmc
sudo apt-get remove --purge pulseaudio-module-bluetooth
sudo apt-get remove --purge pulseaudio

Building from source

We do not build bluez-alsa with codecs that are subject to licencing restrictions. To support extra codecs, you will need to obtain the libraries and build the bluez-alsa package from source. You can build the package on your OSMC device as follows:

  • set up a build environment as described here
  • in the OSMC git tree you have cloned, navigate to osmc/package/bluezalsa-osmc and edit build.sh to add the codecs you need. The codecs that bluez-alsa supports are described on the bluez-alsa wiki.

As an example, these are the changes you need to add aptX and LDAC support.

diff --git a/package/bluezalsa-osmc/build.sh b/package/bluezalsa-osmc/build.sh
index dfb0837df..dcca4a39c 100644
--- a/package/bluezalsa-osmc/build.sh
+++ b/package/bluezalsa-osmc/build.sh
@@ -22,6 +22,9 @@ then
        handle_dep "libsbc-dev"
        handle_dep "libglib2.0-dev"
        handle_dep "libfdk-aac-dev"
+       handle_dep "libopenaptx-dev"
+       handle_dep "libldacbt-enc-dev"
+       handle_dep "libldacbt-abr-dev"
        handle_dep "automake"
        handle_dep "libtool"
        handle_dep "pkg-config"
@@ -29,7 +32,7 @@ then
        echo "Package: ${1}-bluezalsa-osmc" >> files/DEBIAN/control
        pushd src/bluez-alsa*
        autoreconf -vif .
-       ./configure --enable-aac --with-alsaplugindir=/usr/lib/arm-linux-gnueabihf/alsa-lib/ --with-alsaconfdir=/usr/share/alsa/alsa.conf.d
+       ./configure --enable-aac --enable-aptx --with-libopenaptx --enable-ldac --with-alsaplugindir=/usr/lib/arm-linux-gnueabihf/alsa-lib/ --with-alsaconfdir=/usr/share/alsa/alsa.conf.d
        if [ $? != 0 ]; then echo -e "Configure failed!" && umount /proc/ > /dev/null 2>&1 && exit 1; fi
        $BUILD
        if [ $? != 0 ]; then echo -e "Build failed!" && exit 1; fi

Run the build script by typing make armv7. This will produce a .deb that you can install with
sudo dpkg -i armv7-bluezalsa-osmc.deb. This package will be overwritten the next time bluezalsa-osmc is updated so you will have to repeat the custom build if that happens.

9 Likes
Bluetooth don't worki on Rpi3B+
Bluetooth headphones
My Bluetooth speakers won't connect either!
OSMC RPi 3B+ headphones not foud
Bluetooth keyboard stopped working after Nov.2020 Beta update
Connecting A Bluetooth Device
Issues connecting JBL Flip 5
Config a2dp bluetooth audio after august update
[TESTING] Kodi v19 builds for Raspberry Pi 2/3/4
Redirect A2DP sink
Vero 4k+ stream audio to bluetooth
No Audio after update to matrix
Kodi v19.1 update - error install
Stream audio to OSMC via bluetooth
Vero 4k+ Kodi 19.1 update error installing bluezalsa
Vero 4k+ bluetooth and usb dac
Bluetooth connection not working
Vero 4K: No HDMI audio after 2022-03-1 update
No Pulseaudio in Kodi, alsa sink in mono only after Buster upgrade
Vero 4k - Sad Face Loop after Kodi 19 Upgrade
Bluetooth audio not working after reinstallation
[Deprecated] [TESTING] Bluetooth audio streaming (A2DP)
Connecting to bluetooth audio on Raspberry 3
Audio connection problems Vero 4K to soundbar
Cannot pair Bluetooth keyboard with Vero 4K (APLIC 303324
Bluetoooth pairing fails when connecting to phone
Bluetoooth pairing fails when connecting to phone
Enabling bluetooth
Bluetooth audio is choppy
Raspi 3 & Bluetooth & USB DAC [RESOLVED]
Show bluetooth audio devices in settings
No Pulseaudio in Kodi, alsa sink in mono only after Buster upgrade
V19 upgrade: sad face loop while installing a package
Bluetooth transfer
Silent audio in certain cases after update to v19
Receive songs from bluetooth
No 3.5mm analogue audio output option, since latest update, Pi 3B
Kodi 19 update error: An error occurred while installing the following package: (unknown package)
Lost audio on upgrade to v19
Bluetooth Audio Stream
CActiveAESink error after Kodi Matrix upgrade
CActiveAESink error after Kodi Matrix upgrade

Marvellous grahamh - many thanks for the heads up.

I spent half a day trying to work around why a2dp no longer worked on the Oct version of OSMC. I was thinking I might have to just revert to the Jun version (BT fine(ish) for internal Pi3 BT).

But no need - your helped worked like a dream! Am now listening happily to carols through Bluealsa service and my Bose.

pgt44

Glad it works. This is very much a temporary implementation (WIP).

Hi Graham,
Just repeating the installation of bluez-alsa using your notes and find I can no longer find libfdk-aac1 as in your post

We have compiled bluez-alsa for Vero and Pi2/3. Download armv7-bluez-alsa-osmc.deb from here https://collab.osmc.tv/s/hxG3JX4m3rY2XiM and put it onto your Vero/Pi. Run

sudo apt-get install wget libbluetooth3 libsbc1 libfdk-aac1
sudo dpkg -i armv7-bluez-alsa-osmc.deb

Any idea how I can find this library item?
Regards,
Budgie

What do you see when you type

sudo apt-get install libfdk-aac1 ?

This is what I have:-

abr@rpi-sound:~ $ sudo apt-get install libfdk-aac1
Reading package lists… Done
Building dependency tree
Reading state information… Done
Package libfdk-aac1 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package ‘libfdk-aac1’ has no installation candidate
abr@rpi-sound:~ $

BTW as suggested by others I have now moved to Raspberrypi OS Lite so although I had been working with your earlier posts I notice that I was using an osmc repo so it is possible I have a crossed wire in my head.

As you may recall I had everything working well with your advice on bluez-alsa except for getting the playing using mpd and connecting to BT device.
Moving to the OS Lite I have gone backwards with the new OS card installed. I can easily switch back but I couldn’t get the mpd player working. If you are able and have the time I can swap SD card and resume from where I had reached. Let me know. I am on my own because of Covid and rest of family abroad so happy to press on if you are.
Otherwise again, Happy Christmas.
Budgie

Thanks. So I was able to connect to my bluetooth speaker but no audio is coming through. In the GUI in OSMC it shows as connected with the blue logo. I enabled gui sounds and I can hear them if I plug in my headphones plugged into the raspberry pi but not the speakers. Is this a audio settings issue?

If I shouldn’t be selecting HDMI as output device in audio settings, what should I select?
I only have these options:

ALSA: DEFAULT( bcm2835 ALSA bcm2835 ALSA)
ALSA: BCM2835 alsa, BCM2835 ALSA
PI: HDMI
PI: Analogue
PI: HDMI and Analogue

bluetooth speaker definitely works. I can play via my mac.

THANKS!!
I also turned Keep Audio Device Alive off

Seems you are missing your Bluetooth device.
Did you create an appropriate .asoundrc?

Ok, stupid mistake on the file. Yeah it’s working now!! THANK YOU . This is amazing

Hi grahamh,
I am stuck with this libfdk-aac1 and libfdk-aac-dev. Can you help please?

If you are compiling bluez-alsa on RaspberryPiOS then the right place to ask would be Raspberry Pi forums. Otherwise try the bluez-alsa guys on github. Note RasberryPiOS now uses pulseaudio instead of bluez-alsa so that may provide a solution for you.

If you are using mpd on OSMC I’ve no experience of that. OSMC supports Kodi and has no need of other players.

Hi Graham and many thanks for your reply. Your help with bluez-alsa is much appreciated and my RPi3 with OSMC installed and your bluetooth advice worked a treat, as did upnpdcli but I couldn’t get the mpd part to work and play to BT device.

Rather than unpick the Kodi setup I thought I would try a Raspberry Pi OS lite and just install what I needed and you are right to point me to RPi forum.

I tripped up on one of the dependencies, libfdk-aac1 which was not in the Raspbian version of Buster. I think it is in the Debian non-free repo but have hesitated to try and fetch it from there because I do not know if apt will sort out the fact that I need a 32 bit arm version. But shall continue through RPi Forum. Just wanted to say thanks again.
best wishes for the New Year.
Budgie

1 Like

This works, nice! Only when connecting through command line though. Always fails connecting to speakers in MyOSMC.

I’ve followed the instruction to the letter and managed to connect to the ENACFIRE Future headphones and stream audio over it.
Previously I couldn’t even pair RPI 3b bluetooth and the headphones, so thank you @grahamh.

I have a small issue though. The streaming is not continuous, it drops pretty frequently and comes back almost immediately - quite annoying :slight_smile:

Is there anything I can do about it?

We might be able to see something in debug logs if you could grab those. I can’t recall if there’s an option for logging of bluetooth but if there is, turn it on.

I’ve done the following:

  1. Enabled debug and event logging

  2. Connected the headphones

  3. Started a movie

  4. Got a few drops every few seconds

  5. Stopped the movie

  6. Grabbed the logs (grab-logs -A)

Hopefully this will provide useful information.

Can you try turning off DD capable receiver and changing audio to Best match?

Do you mean Output configuration -> Best match?
Where can I find DD capable receiver option?

My bad - you have passthrough turned off anyway. Dolby Digital capable receiver shows up when Settings level is Advanced or Expert only when passthrough is enabled.

Any luck with Best match?

Unfortunately not :frowning: