To my knowledge there is not a simple one-click solution that would give us a fast switching from internal audio to Bluetooth A2DP audio profile, and Bluetooth HSP headset profile.
However we can change the sound output from command line that gives us the opportunity to create a script to switch the audio output. The commands we need are the following:
pacmd list-cards
will list the cards available. As soon as a headset is connected we will have access to the Bluetooth audio properties. In addition we can now find out the pulseaudio card index, and possible profiles for the following commands.
pacmd set-card-profile <index> <profile>
changes the profile for a card number <index>
. For a Bluetooth card the profile in question will be a2dp
for audio profile, and hsp
for headset telephony profile.
pacmd set-default-sink
and pacmd set-default-source
will change the output resp. input used by pulseaudio. This can not be done during playback. See this answer for details on how to do that.
Below there is an example script that may be used to switch the sound output for a Bluetooth headset:
#!/bin/bash
# CLI options: `a2dp': Audio Profile
# `hsp': Telephony Profile
# <Index> Default Sink (try `0' or `1')
SINK=$( pacmd list-cards | grep -B 1 bluez )
INDEX=${SINK:10:2}
SINK=$( pacmd list-cards | grep bluez )
MAC=${SINK:19:17}
BT_SINK="bluez_sink.$MAC"
BT_SOURCE="bluez_source.$MAC"
if [ $1 = a2dp ]; then
echo Setting A2DP audio sink $BT_SINK
pacmd set-card-profile $INDEX a2dp
pacmd set-default-sink $BT_SINK
elif [ $1 = hsp ]; then
echo Setting HSP headset sink $BT_SOURCE
pacmd set-card-profile $INDEX hsp
pacmd set-default-sink $BT_SINK
pacmd set-default-source $BT_SOURCE
else
echo Resetting to internal audio
pacmd set-default-sink $1
pacmd set-default-source $1
fi
Copy the script, save it as switch-sink
, and grant it executable permission. In a terminal change to the script directory and issue the script with the following options:
switch-sink a2dp # switches to audio profile
switch-sink hsp # switches to headset/telephony profile
switch-sink <index> # switches to sink <index>, e.g. 0 depending on our default
Give the full path to the script when executing from a keyboard shortcut or from a .desktop file (for desktop or launcher).
Additional note on sound card INDEX:
In case there are more than 99 sound cards registered with pulseaudio this script here will not work. This may become an issue as the card INDEX will be increased with every power ON or reconnect of a Bluetooth device. As a workaround we may change the last else
condition to reset the pulseaudio server (which will then also load our default audio sink):
else
pulseaudio -k
fi