@steve.penrod After a week of grappling with bluez5 and pulseaudio here’s what I’ve learned.
TL;DR Speaker probably works on bluetooth, microphone may or may not. Overall not really consistent enough for general use.
Here are the steps to connect
- Install pulseaudio-module-bluetooth:
sudo apt-get install pulseaudio-module-bluetooth
- Remove the audio device assignment in
mycroft.conf
:
- Change
"play_wav_cmdline": "aplay -Dhw:0,0 %1",
to "play_wav_cmdline": "aplay %1",
- Change
"play_mp3_cmdline": "mpg123 -a hw:0,0 %1",
to "play_mp3_cmdline": "mpg123 %1",
- To make the last connect audio device be the active one type
pactl load-module module-switch-on-connect
(this module can be added to /etc/pulse/default.pa
so that it is automatically loaded)
- I initially thought the file
/etc/bluetooth/audio.conf
had to be created with the following contents but it still worked for me after I removed the file.
[General]
Enable=Source,Sink,Headset,Gateway,Control,Media,Socket
- Pair/trust/connect bluetooth speaker:
- Type
bluetoothctl
- Put bluetooth speaker in pairing mode and type
scan on
.
- When the mac address (
<mac>
) of the speaker is found type:
pair <mac>
trust <mac>
connect <mac>
exit
Sound should now come from the bluetooth speaker. This is where it gets more complicated. There are two main profiles a speaker can have: a2dp_sink and headset_head_unit. a2dp_sink provides high fidelity stereo playback (speaker only) with a sample rate of around 44 kHz whereas headset_head_unit provides bidirectional mono audio (ie. both microphone and speaker which are referred to as source and sink, respectively) with a sample rate of only 8 kHz each. The available cards and their profiles can be viewed with the command pactl list cards | grep -edescrip -eHSP -eA2DP -eActive -eCard
and set with the command pactl set-card-profile <card> <profile>
.
The headset_head_unit profile is split into two sub-profiles: headset profile (HSP) and hands-free profile (HFP). With pulseaudio 10 HSP is natively supported but for HFP ofono must be installed. The features available for the connected bluetooth device can be found with the command info <mac>
executed within bluetoothctl
.
Here arises the problem I have been contending with: the available features of the device are not always seen as available by pactl
. For some devices the combination of a2dp_sink and headset_head_unit availability is random and for others the availability is dependant on whether the device was connected automatically or manually with bluetoothctl
. A profile can only be set when pactl
thinks it’s available. When a device is reconnected and the profile it was previously connected with is not available it will be set to off and must be manually set to an available profile.
I have two bluetooth devices I’ve been testing with: a headset (Philips SHB7000) and a speaker (Sephia A2). The available profiles for the headset are random but when they both show as available both profiles work fine. The speaker however always has a2dp_sink and only has headset_head_unit when it’s connected manually. When I finally found the “manual connection” trick to set the speaker profile as headset_head_unit I thought I finally found how to get the speaker’s microphone working. Unfortunately this was not the case and when I use the speaker in the headset_head_unit profile I get nothing from the speaker or microphone - I can only hear the background hum on the speaker. I have not found a solution to this so even if the headset_head_unit profile is available it might not work. In my opinion, not being able to use the microphone drastically reduces the usefulness of using a bluetooth speaker with Mycroft.
A separate issue is that when headset_head_unit does work it sounds absolutely terrible! This is not due to the low sample rate but cause by a bad bit of code in pulseaudio 10. It has been fixed in later versions but for it to work with picroft I had to patch version 10 as described here. This produced libbluez5-util.so which can replace the original in /usr/lib/pulse-10.0/modules/
. With this patch the audio sounds as it should with a sample rate of 8 kHz and mycroft can then recognise commands. It works for both rpi3b and rpi3b+ models.
I focussed more on HSP rather than HFP in my research because the added dependency of ofono for HFP seemed to add yet another layer of complexity and because the most recent version of pusleaudio concentrates more on HSP. I may try to look more into HFP to see if it’s any better or reliable.