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 Bluetoothreportedly 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.
- Go to settings → system → Audio
- Audio output device: ALSA: Playback/recording through the PulseAudio sound server
- 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
- 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.
- 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.
- Restart Kodi
- 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!
- 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!