Skip to content

Instantly share code, notes, and snippets.

@kenanpelit
Forked from crigertg/toggle_headset.sh
Created September 28, 2023 09:20
Show Gist options
  • Save kenanpelit/4c8ab47baf0451f4d8a81a820eb59924 to your computer and use it in GitHub Desktop.
Save kenanpelit/4c8ab47baf0451f4d8a81a820eb59924 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}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment