Created
January 31, 2022 01:23
-
-
Save manojkarthick/289ab431727fa367c4c04ba66e320fb0 to your computer and use it in GitHub Desktop.
Simple utility script to connect/disconnect bluetooth headphones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
SONY_ID="sony-wh-1000-xm4" | |
SONY_MAC="f8-4e-17-38-53-18" | |
PLT_ID="plantronics-bb-pro-2" | |
PLT_MAC="bc-f2-92-d4-11-25" | |
declare -A headphones | |
headphones["$SONY_ID"]="$SONY_MAC" | |
headphones["$PLT_ID"]="$PLT_MAC" | |
if [[ "$(/usr/bin/arch)" =~ "arm64" ]]; then | |
CMD="/opt/homebrew/bin/bluetoothconnector" | |
else | |
CMD="$HOME/bin/bluetoothconnector" | |
fi | |
if [[ "$#" -ne 2 || ("$1" != "toggle" && "$1" != "status" && "$1" != "connect" && "$1" != "disconnect") || ("$2" != "$SONY_ID" && "$2" != "$PLT_ID") ]]; then | |
echo "Usage: headphones [connect/disconnect/toggle/status]" | |
exit 1 | |
fi | |
TARGET_MAC="${headphones[$2]}" | |
OUTPUT="$($CMD --status "$TARGET_MAC")" | |
case $1 in | |
status) | |
if [[ "$OUTPUT" =~ "Disconnected" ]]; then | |
echo "$2 is not connected" | |
else | |
echo "$2 is connected" | |
fi | |
;; | |
toggle) | |
if [[ "$OUTPUT" =~ "Disconnected" ]]; then | |
echo "Connecting $2 with mac address $TARGET_MAC." | |
$CMD --connect "$TARGET_MAC" --notify >/dev/null 2>&1 & | |
else | |
echo "Disconnecting $2 with mac address $TARGET_MAC." | |
$CMD --disconnect "$TARGET_MAC" --notify >/dev/null 2>&1 & | |
fi | |
;; | |
disconnect) | |
if [[ "$OUTPUT" =~ "Disconnected" ]]; then | |
echo "Nothing to do." | |
else | |
echo "Disconnecting $2 with mac address $TARGET_MAC." | |
$CMD --disconnect "$TARGET_MAC" --notify >/dev/null 2>&1 & | |
fi | |
;; | |
connect) | |
if [[ "$OUTPUT" =~ "Disconnected" ]]; then | |
echo "Connecting $2 with mac address $TARGET_MAC." | |
$CMD --connect "$TARGET_MAC" --notify >/dev/null 2>&1 & | |
else | |
echo "Nothing to do." | |
fi | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment