dotfiles/.local/bin/bluetooth

47 lines
No EOL
1.1 KiB
Bash
Executable file

#!/bin/sh -x
checkstate() {
if [ "$(bluetoothctl show | grep "Powered: yes")" ]; then
echo "󰂯 ON"
else
echo "󰂲 OFF"
fi
}
switchstate() {
case "$(checkstate | fuzzel -d)" in
"󰂯 ON")
bluetoothctl power off
notify-send "Bluetooth" "Bluetooth is disabled"
;;
"󰂲 OFF")
echo "Bluetooth is enabled"
bluetoothctl power on
notify-send "Bluetooth" "Bluetooth is enabled"
;;
*) exit ;;
esac
}
scandevices() {
bluetoothctl --timeout 5 scan on
devices=$(bluetoothctl devices | awk '{$1=$2=""; print substr($0,3)}')
if [ -z "$devices" ]; then
notify-send "Bluetooth" "Error: No devices were found"
exit 1
fi
selection="$(printf "%s\n" "$devices" | fuzzel -d -p "Available devices: ")"
if [ -n "$selection" ]; then
mac=$(bluetoothctl devices | grep "$selection" | awk '{print $2}')
bluetoothctl connect "$mac"
notify-send "Bluetooth" "Connecting to $selection"
fi
}
case $(printf "%s\n" "󰂯 Connect Device" "󰂳 Change State" | fuzzel -d) in
"󰂯 Connect Device")
scandevices ;;
"󰂳 Change State") switchstate ;;
*) exit ;;
esac