random/bluetooth-toggle

29 lines
771 B
Plaintext
Raw Normal View History

#!/bin/bash
2022-01-02 22:35:07 -06:00
2021-04-19 13:54:49 -05:00
# Toggles bluetooth on a Linux system. Make sure that you have bluetoothctl installed.
# I use it with a shortcut.
2024-09-22 21:42:25 -05:00
DONGLE_MAC=$(bluetoothctl list | grep dongle | awk '{print $2}')
INTERNAL_MAC=$(bluetoothctl list | grep internal | awk '{print $2}')
if [ -z $DONGLE_MAC ]; then
DEVICE_MAC=$INTERNAL_MAC
2021-04-19 13:54:49 -05:00
else
2024-09-22 21:42:25 -05:00
DEVICE_MAC=$DONGLE_MAC
fi
2024-09-22 21:42:25 -05:00
if [ $(bluetoothctl show $DEVICE_MAC | grep Powered | grep -c yes) == 1 ]; then
echo -e "select $DEVICE_MAC\npower off" | bluetoothctl
else
if [ $(rfkill --output-all | grep bluetooth | grep -c blocked) != 0 ]; then
2021-12-08 19:35:57 -06:00
rfkill unblock bluetooth
fi
2024-09-22 21:42:25 -05:00
echo -e "select $DEVICE_MAC\npower on" | bluetoothctl
fi
2024-09-24 17:55:44 -05:00
if ! [ -z $DONGLE_MAC ]; then
sleep 0.5
echo -e "select $INTERNAL_MAC\npower off" | bluetoothctl
fi