[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

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: