Skip to content

Instantly share code, notes, and snippets.

@crigertg
Last active September 28, 2023 09:21
Show Gist options
  • Save crigertg/b0a745cc5d3f09ced0f65239bdd76d62 to your computer and use it in GitHub Desktop.
Save crigertg/b0a745cc5d3f09ced0f65239bdd76d62 to your computer and use it in GitHub Desktop.
Use this script to automatically connect to a bluetooth headset and switch audio profiles.
#!/bin/bash
# Use this script to automatically connect to a bluetooth headset and switch audio profiles.
# By default the script toggles the headset connection on/off and selects the handsfree profile
# when a connection is established.
# There is the possibility to switch to the ALT_MODE which ensures a connection and
# when executed again it switches between the a2dp profile for music and handsfree for using the microphone.
# The MAC address of your Bluetooth headset
# Use `bluetoothctl devices` to list your devices
HEADSET_MAC="<YOUR_HEADSET_MAC>"
# Set to true if you want to switch audio profiles instead of connect/disconnect functionality.
ALT_MODE=false
# Set to true if you want notifications on your desktop. Requires libnotify-bin
SHOW_NOTIFICATIONS=true
# Connect to the Bluetooth device
connect_device () {
notify "Connecting to ${HEADSET_MAC}..."
bluetoothctl connect ${HEADSET_MAC}
}
# Disconnect from the Bluetooth device
disconnect_device () {
notify "Disconnecting from ${HEADSET_MAC}..."
bluetoothctl disconnect ${HEADSET_MAC}
}
notify () {
if [ "${SHOW_NOTIFICATIONS}" == "true" ]; then notify-send "$@"; fi
echo "$@"
}
if bluetoothctl info ${HEADSET_MAC} | grep -q "Connected: yes"; then
if [ "${ALT_MODE}" = false ]; then
disconnect_device
exit 0
fi
else
connect_device
sleep 3
fi
# Get the current profile for the Bluetooth headset
profile=$(pacmd list-cards | grep -A 21 bluez_card."$(echo $HEADSET_MAC | tr : _)" | grep "active profile" | cut -d " " -f 3 | tr -d \< | tr -d \>)
# If the current profile is "a2dp_sink", switch to "headset_head_unit" profile
if [[ "$profile" == "a2dp_sink" || "$profile" == "off" ]]; then
pactl set-card-profile bluez_card."$(echo $HEADSET_MAC | tr : _)" handsfree_head_unit
echo "Switched to HSP/HFP profile"
sleep 3
source_name=$(pacmd list-sources | grep "bluez_sink.$(echo $HEADSET_MAC | tr : _)" | awk '{print $2}'| tr -d \< | tr -d \>)
# Set the Bluetooth headset as the default audio input device
pacmd set-default-source "${source_name}"
notify "Switched Bluetooth Headset to handsfree mode"
# If the current profile is "headset_head_unit", switch to "a2dp_sink" profile
elif [ "$profile" == "handsfree_head_unit" ] && [ "${ALT_MODE}" = true ]; then
pactl set-card-profile bluez_card."$(echo $HEADSET_MAC | tr : _)" a2dp_sink
notify "Switched to A2DP profile"
sleep 3
else
# If the Bluetooth headset is not connected, print an error message
notify "Bluetooth headset is not connected"
fi
sink_name=$(pacmd list-sinks | grep "bluez_sink.$(echo $HEADSET_MAC | tr : _)" | awk '{print $2}'| tr -d \< | tr -d \>)
# Set the Bluetooth headset as the default audio output device
pacmd set-default-sink "${sink_name}"
@fheinle-mak
Copy link

suggestion: To show desktop notfications - useful when running from hotkeys:

--- toggle_headset.sh_claus	2023-04-05 08:34:07.817050479 +0200
+++ toggle_headset.sh	2023-04-05 08:26:42.362044169 +0200
@@ -1,18 +1,21 @@
 #!/bin/bash
 # Use this script to automatically connect to a bluetooth headset and switch audio profiles.
 # By default the script toggles the headset connection on/off and selects the handsfree profile
 # when a connection is established. 
 # There is the possibility to switch to the ALT_MODE which ensures a connection and
 # when executed again it switches between the a2dp profile for music and handsfree for using the microphone.
 
 
 # The MAC address of your Bluetooth headset
 # Use `bluetoothctl devices` to list your devices
 HEADSET_MAC="<YOUR_HEADSET_MAC_HERE>"
 
 # Set to true if you want to switch audio profiles instead of connect/disconnect functionality.
 ALT_MODE=false
 
+# Set to true if you want notifications on your desktop. Requires libnotify-bin
+SHOW_NOTIFICATIONS=true
+
 
 # Connect to the Bluetooth device
 connect_device () {
@@ -26,14 +29,19 @@
     bluetoothctl disconnect ${HEADSET_MAC}
 }
 
+notify () {
+    if [ "${SHOW_NOTIFICATIONS}" == "true" ]; then notify-send "$@"; fi
+    echo "$@"
+}
+
 if bluetoothctl info ${HEADSET_MAC} | grep -q "Connected: yes"; then
     if [ "${ALT_MODE}" = false ]; then
	    disconnect_device
	    exit 0
     fi
 else
@@ -47,14 +55,15 @@
     source_name=$(pacmd list-sources | grep "bluez_sink.$(echo $HEADSET_MAC | tr : _)" | awk '{print $2}'| tr -d \< | tr -d \>)
     # Set the Bluetooth headset as the default audio input device
     pacmd set-default-source "${source_name}"
+    notify "Switched Bluetooth Headset to handsfree mode"
 # If the current profile is "headset_head_unit", switch to "a2dp_sink" profile
 elif [ "$profile" == "handsfree_head_unit" ] && [ "${ALT_MODE}" = true ]; then
     pactl set-card-profile bluez_card."$(echo $HEADSET_MAC | tr : _)" a2dp_sink
-    echo "Switched to A2DP profile"
+    notify "Switched to A2DP profile"
     sleep 3
 else
     # If the Bluetooth headset is not connected, print an error message
-    echo "Bluetooth headset is not connected"
+    notify "Bluetooth headset is not connected"
 fi
 
 sink_name=$(pacmd list-sinks | grep "bluez_sink.$(echo $HEADSET_MAC | tr : _)" | awk '{print $2}'| tr -d \< | tr -d \>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment