[HowTo] Use OSMC as a Bluetooth Speaker (a2dp sink)

Background

So, I have a HifiBerry to get a supreme audio experience on my Kodi / raspberry. I wanted to use the same speakers for my TV, so I used an AUX-cable to attach my TV to Kodi. That worked, until I bought a new TV which didn’t have an AUX out. I tried to use SPDIF using an external USB soundcard, but it appeared Linux doesn’t properly the used audiochip. So I couldn’t get that to work.

So, why not configure OSMC as a A2DP receiver?

My thoughts exactly. After ruling out buying a dedicated receiver as what any sane person would do. So I went to google. And found a lot of different, conflicting tutorials on how to configure Linux as a Bluetooth Speaker. I finally got something working, so I thought I’d place my steps here to document it to help my future self and perhaps you as well.

Pre-requisites

  • Raspberry Pi3b or Pi3B+ or a bluetooth dongle
  • Not the requirement to use WiFi, as this reportedly interferes with Bluetooth reportedly has been fixed on the pi (see comment below)
  • Not hate PulseAudio with every fibre of your being
  • OSMC, I used latest kodi 18 nightlies (this should work on any debian-based distro though)

Step 1: install required software

> sudo apt-get update 
> sudo apt-get install bluetooth bluez pulseaudio pulseaudio-module-bluetooth rfkill

Step 2: Enable bluetooth, disable wifi using rfkill

> sudo rfkill block 0
> sudo rfkill unblock 1
> sudo rfkill list
0: phy0: Wireless LAN
        Soft blocked: yes
        Hard blocked: no
1: hci0: Bluetooth
        Soft blocked: no
        Hard blocked: no

Step 3: Configure Bluetooth as a Bluetooth loudspeaker

> sudo mkdir /etc/bluetooth
> sudo nano /etc/bluetooth/main.conf
[General]
Class = 0x240414
> sudo systemctl restart bluetooth
> sudo systemctl status bluetooth
( there should be no errors )
> sudo hciconfig -a
hci0:   Type: Primary  Bus: UART
(,,,,,,,)
        Name: 'osmc'
        Class: 0x000414
        Service Classes: Unspecified
        Device Class: Audio/Video, Loudspeaker
(,,,,,,,,,,,,)

Look for: Device Class: Audio/Video, Loudspeaker. If you don’t do this, some devices (e.g. my TV) will not offer to pair.

Configure pulseaudio

Change /etc/pulse/system.pa (when pa runs as system) and/or /etc/pulse/default.pa (normally) and add:

load-module module-bluetooth-policy
load-module module-bluetooth-discover

Step 4: Configure Kodi

We have to configure Kodi to use Pulseaudio. Kodi should start the pulseaudio daemon for you upon starting osmc.

  1. Go to settings → system → Audio
  2. Audio output device: ALSA: Playback/recording through the PulseAudio sound server
  3. Restart kodi, verify pulseaudio is running:
> sudo systemctl restart mediacenter
> sudo ps aux | grep pulse
osmc       434  9.8  1.3 118812  9948 ?        Sl   21:55   0:17 /usr/bin/pulseaudio --start --log-target=syslog
osmc       974  0.0  0.0   2168   584 pts/0    S+   21:58   0:00 grep pulse

(Note: You might have to make Kodi generate some audio first before Kodi starts PA)
If PulseAudio is running, make sure your current shell can find it by running as osmc user. You need to do this for step 5 and future steps. If you don’t do this you cannot talk to the running PA instance using pacmd:

export PULSE_RUNTIME_PATH="` echo /home/osmc/.config/pulse/*-runtime/ | cut -d ' ' -f 2`"

Step 5: Configure pulseaudio

  1. Change settings of pulseaudio daemon
> sudo nano /etc/pulse/daemon.conf
(append at the end)
high-priority = yes
nice-level = -11
exit-idle-time = -1
flat-volumes = no
default-sample-rate = 48000
resample-method = speex-float-5

The exit-idle-time = -1 is the most important line, as this prevents PA from stopping. If PA is not running, a2dp will not work. You want Kodi to start PA, or you might end up with two running PA’s.

  1. Set default audio output device (sink) for PA
> sudo nano /etc/pulse/default.pa
(append at the end)
set-default-sink alsa_output.platform-soc_sound.analog-stereo

replace the set-default-sink with the sink you want to use! Use pacmd list-sinks to view available sinks. If you get No PulseAudio daemon running, or not running as session daemon. then look again at the end of Step 4. If you have no Kodi, start PA with pulseaudio -D instead.

Step 6: Reboot and verify

We changed quite some settings, so it’s good to see everything survives a reboot.

  1. Restart Kodi
  2. Verify the following items:
    2.1 Pulseaudio is running after a reboot
 sudo ps aux | grep pulse
osmc       434 18.9  1.3 119040  9980 ?        Sl   21:55   4:55 /usr/bin/pulseaudio --start --log-target=syslog
root      1449  0.0  0.0   2168   596 pts/0    S+   22:21   0:00 grep pulse

2.2 Bluetooth is still presenting itself as a speaker

> sudo hciconfig -a
Device Class: Audio/Video, Loudspeaker

If not, go back and retrace the appropriate steps.

Step 7: Let’s do some bluetooth pairing!

  1. Enable Bluetooth and make it visible:
> sudo bluetoothctl 
># power on
Changing power on succeeded
># discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:##:##:## Discoverable: yes
># pairable on
Changing pairable on succeeded

2 .Try to pair with your phone first.

[NEW] Device A0:DE:AD:BE:EF:FE YourPhoneNameHere
(..............)
[CHG] Device A0:DE:AD:BE:EF:FE ServicesResolved: yes
[CHG] Device A0:DE:AD:BE:EF:FE Paired: yes
[CHG] Device A0:DE:AD:BE:EF:FE ServicesResolved: no
[CHG] Device A0:DE:AD:BE:EF:FE Connected: no

Note the paired: yes in the console.
3. Next, you need to trust your device so it can automatically connect in the future

This is a bit tricky, your device needs to connect and when it is connected you need to type in trust in the console. The next time it connects, it should connect automatically.
It looks a bit like this:

[CHG] Device A0:DE:AD:BE:EF:FE Connected: yes
[YourPhoneNameHere]# trust
[CHG] Device A0:DE:AD:BE:EF:FE Trusted: yes
Changing  trust succeeded
[CHG] Device A0:DE:AD:BE:EF:FE Connected: no
[CHG] Device A0:DE:AD:BE:EF:FE Connected: yes
(.........)
[YourPhoneNameHere]#

Your phone should now be able to use Kodi as a bluetooth speaker. Now try to do the steps again but use your TV instead!

Can I ask if you tested and had issues with [Deprecated] [TESTING] Bluetooth audio streaming (A2DP)?

I am just about to include the former as part of OSMC by default.

Sam

This has since been fixed on Pi.

Hi, no i did not test.I saw the last reply,stating "The thread is now closed until we have a new solution

Sam" and assumed there was no new solution yet :slight_smile:

Because you asked, I just had a look at a2dp-app-osmc and it seems it is mostly the same as my tutorial above, with the exception of using system-wide pulseaudio and an extra kodi addon so you can manage whatever is playing through Bluetooth on Kodi. If that works, it’s great and definitely an improvement on just being able to listen through the speakers !

EDIT: What I do differently in my tutorial is manually setting the default output device of PA to my Hifiberry. I didn’t see that happening in a2dp-app-osmc and thus I believe Pulseaudio could possibly choose the wrong audio output device. If this happens the user can fix this by following step 5 of the above tutorial.

Maybe it would make sense that you adapt your tutorial to use a2dp and just describe your configuration for manually setting the default output device.

Hi, yeah I am definitely considering something like that. I want to make this a2dp-guide into two parts: 1) the easy way with a2dp-app-osmc plus some troubleshooting tips, and 2) an advanced part for power users, custom setups and just a general a2dp-app-osmc didn’t-work-for-some-reason-guide. For that guide I will use my own input and suggestions from a2dp-app-osmc.

I just bought a new rpi3b+, so I can easily use the old sdcard for testing a new install after I have successfully migrated everything from the old pi. I’ll set an calendar item for that on this Friday.

Sounds promising

Is this the issue with onboard Bluetooth cutting out with onboard WiFi enabled?

Will you to pushing the fix to next update cheers

This was fixed some time ago.

Sam

Thanks wasn’t sure it was fixed as you mentioned it still had issues a few months back (assuming its the same problem). Will give it a try cheers

so i gave it a try on a 100% fresh install of kodiv18 beta rpi3b w/ hifiberry and it didn’t work. Haven’t had time yet to figure out why, maybe tomorrow