Skip to content

Instantly share code, notes, and snippets.

@hu553in
Last active November 7, 2024 00:51
Show Gist options
  • Save hu553in/7abb5e0ac4ad43864b1146e9a346160a to your computer and use it in GitHub Desktop.
Save hu553in/7abb5e0ac4ad43864b1146e9a346160a to your computer and use it in GitHub Desktop.
Bash script to re-connect Bluetooth headphones on Linux via bluetoothctl and PulseAudio
hp() {
~/headphones.sh
}
#!/bin/bash
# device MAC
DEVICE_MAC="41:42:DB:A9:E6:8D"
DEVICE_MAC_UNDERSCORE="${DEVICE_MAC//:/_}"
TIMEOUT=60
# colors
GREEN="\033[0;32m"
RED="\033[0;31m"
CYAN="\033[0;36m"
NO_COLOR="\033[0m"
untrust_device() {
echo -e "${CYAN}Untrusting device $DEVICE_MAC...${NO_COLOR}"
bluetoothctl untrust $DEVICE_MAC
if [ $? -eq 0 ]; then
echo -e "${GREEN}Untrusted device $DEVICE_MAC${NO_COLOR}"
else
echo -e "${RED}Failed to untrust device $DEVICE_MAC, but it's probably OK${NO_COLOR}"
fi
}
forget_device() {
echo -e "${CYAN}Forgetting device $DEVICE_MAC...${NO_COLOR}"
bluetoothctl remove $DEVICE_MAC
if [ $? -eq 0 ]; then
echo -e "${GREEN}Forgot device $DEVICE_MAC${NO_COLOR}"
else
echo -e "${RED}Failed to forget device $DEVICE_MAC, but it's probably OK${NO_COLOR}"
fi
}
restart_bluetooth() {
echo -e "${CYAN}Restarting Bluetooth service...${NO_COLOR}"
sudo systemctl restart bluetooth \
&& sleep 3
if [ $? -eq 0 ]; then
echo -e "${GREEN}Bluetooth service restarted successfully${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to restart Bluetooth service${NO_COLOR}"
return 1
fi
}
scan_for_devices() {
echo -e "${CYAN}Starting scan for devices for $TIMEOUT seconds...${NO_COLOR}"
bluetoothctl scan on &
SCAN_PID=$!
START_TIME=$(date +%s)
while true; do
if bluetoothctl devices | grep -q "$DEVICE_MAC"; then
echo -e "${GREEN}Device $DEVICE_MAC found${NO_COLOR}"
kill $SCAN_PID
return 0
fi
CURRENT_TIME=$(date +%s)
if [ $((CURRENT_TIME - START_TIME)) -ge $TIMEOUT ]; then
echo -e "${RED}Timeout: device $DEVICE_MAC not found within $TIMEOUT seconds${NO_COLOR}"
kill $SCAN_PID
return 1
fi
sleep 1
done
}
pair_device() {
echo -e "${CYAN}Pairing with device $DEVICE_MAC...${NO_COLOR}"
bluetoothctl pair $DEVICE_MAC
if [ $? -eq 0 ]; then
echo -e "${GREEN}Paired with device $DEVICE_MAC${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to pair with device $DEVICE_MAC${NO_COLOR}"
return 1
fi
}
connect_device() {
echo -e "${CYAN}Connecting to device $DEVICE_MAC...${NO_COLOR}"
bluetoothctl connect $DEVICE_MAC
if [ $? -eq 0 ]; then
echo -e "${GREEN}Connected to device $DEVICE_MAC${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to connect to device $DEVICE_MAC${NO_COLOR}"
return 1
fi
}
trust_device() {
echo -e "${CYAN}Trusting device $DEVICE_MAC...${NO_COLOR}"
bluetoothctl trust $DEVICE_MAC
if [ $? -eq 0 ]; then
echo -e "${GREEN}Trusted device $DEVICE_MAC${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to trust device $DEVICE_MAC${NO_COLOR}"
return 1
fi
}
set_default_output() {
echo -e "${CYAN}Setting default output...${NO_COLOR}"
SINK_INDEX=$(pacmd list-sinks | grep -B 1 "name:.*$DEVICE_MAC_UNDERSCORE" | grep index | awk '{print $3}')
echo "Sink index: $SINK_INDEX"
pacmd set-default-sink $SINK_INDEX
if [ $? -eq 0 ]; then
echo -e "${GREEN}Set default output successfully${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to set default output${NO_COLOR}"
return 1
fi
}
set_default_input() {
echo -e "${CYAN}Setting default input...${NO_COLOR}"
SOURCE_INDEX=$(pacmd list-sources | grep -B 1 "name:.*$DEVICE_MAC_UNDERSCORE" | grep index | awk '{print $3}')
echo "Source index: $SINK_INDEX"
pacmd set-default-source $SOURCE_INDEX
if [ $? -eq 0 ]; then
echo -e "${GREEN}Set default input successfully${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to set default input${NO_COLOR}"
return 1
fi
}
set_output() {
echo -e "${CYAN}Setting output...${NO_COLOR}"
SINK_INDEX=$(pacmd list-sinks | grep -B 1 "name:.*$DEVICE_MAC_UNDERSCORE" | grep index | awk '{print $3}')
echo "Sink index: $SINK_INDEX"
pacmd list-sink-inputs | grep index | while read line
do
INPUT_INDEX=$(echo $line | cut -f2 -d' ')
echo "Input index: $INPUT_INDEX"
pacmd move-sink-input $INPUT_INDEX $SINK_INDEX
done
if [ $? -eq 0 ]; then
echo -e "${GREEN}Set output successfully${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to set output${NO_COLOR}"
return 1
fi
}
set_input() {
echo -e "${CYAN}Setting input...${NO_COLOR}"
SOURCE_INDEX=$(pacmd list-sources | grep -B 1 "name:.*$DEVICE_MAC_UNDERSCORE" | grep index | awk '{print $3}')
echo "Source index: $SINK_INDEX"
pacmd list-source-outputs | grep index | while read line
do
OUTPUT_INDEX=$(echo $line | cut -f2 -d' ')
echo "Output index: $OUTPUT_INDEX"
pacmd move-source-output $OUTPUT_INDEX $SOURCE_INDEX
done
if [ $? -eq 0 ]; then
echo -e "${GREEN}Set input successfully${NO_COLOR}"
return 0
else
echo -e "${RED}Failed to set input${NO_COLOR}"
return 1
fi
}
# remove device
untrust_device
forget_device
# re-add device
restart_bluetooth \
&& scan_for_devices \
&& pair_device \
&& connect_device \
&& trust_device \
&& set_default_output \
&& set_default_input \
&& set_output \
&& set_input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment