-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbluetooth-menu.sh
executable file
·105 lines (90 loc) · 3.15 KB
/
bluetooth-menu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
# Author: Jesse Mirabel (@sejjy)
# GitHub: https://github.com/sejjy/mechabar
# Rofi config
config="$HOME/.config/rofi/bluetooth-menu.rasi"
# Rofi window override
override_disabled="mainbox { children: [ listview ]; } listview { lines: 1; padding: 6px; }"
get_device_icon() {
local device_mac=$1
device_info=$(bluetoothctl info "$device_mac")
device_icon=$(echo "$device_info" | grep "Icon:" | awk '{print $2}')
case "$device_icon" in
"audio-headphones" | "audio-headset") echo " " ;; # Headphones
"video-display" | "computer") echo " " ;; # Monitor
"audio-input-microphone") echo " " ;; # Microphone
"input-keyboard") echo " " ;; # Keyboard
"audio-speakers") echo " " ;; # Speakers
"input-mouse") echo " " ;; # Mouse
"phone") echo " " ;; # Phone
*)
echo " " # Default
;;
esac
}
while true; do
# Get list of paired devices
bluetooth_devices=$(bluetoothctl devices | while read -r line; do
device_mac=$(echo "$line" | awk '{print $2}')
device_name=$(echo "$line" | awk '{$1=$2=""; print substr($0, 3)}')
icon=$(get_device_icon "$device_mac")
echo "$icon $device_name"
done)
options=$(
echo "Scan for devices "
echo "Disable Bluetooth"
echo "$bluetooth_devices"
)
option="Enable Bluetooth"
# Get Bluetooth status
bluetooth_status=$(bluetoothctl show | grep "Powered:" | awk '{print $2}')
if [[ "$bluetooth_status" == "yes" ]]; then
selected_option=$(echo -e "$options" | rofi -dmenu -i -selected-row 1 -config "${config}" -p " " || pkill -x rofi)
else
selected_option=$(echo -e "$option" | rofi -dmenu -i -selected-row 1 -config "${config}" -theme-str "${override_disabled}" -p " " || pkill -x rofi)
fi
# Exit if no option is selected
if [ -z "$selected_option" ]; then
exit
fi
# Actions based on selected option
case "$selected_option" in
"Enable Bluetooth")
notify-send "Bluetooth Enabled"
rfkill unblock bluetooth
bluetoothctl power on
sleep 1
;;
"Disable Bluetooth")
notify-send "Bluetooth Disabled"
rfkill block bluetooth
bluetoothctl power off
exit
;;
"Scan for devices"*)
notify-send "Press '?' to show help."
kitty --title ' Bluetooth TUI' bash -c "bluetui" # Launch bluetui
;;
*)
# Extract device name
device_name="${selected_option#* }"
device_name="${device_name## }"
if [[ -n "$device_name" ]]; then
# Get MAC address
device_mac=$(bluetoothctl devices | grep "$device_name" | awk '{print $2}')
# Trust and pair device
bluetoothctl trust "$device_mac" >/dev/null 2>&1
bluetoothctl pair "$device_mac" >/dev/null 2>&1
# Connect to device
bluetoothctl connect "$device_mac" &
sleep 3
connection_status=$(bluetoothctl info "$device_mac" | grep "Connected:" | awk '{print $2}')
if [[ "$connection_status" == "yes" ]]; then
notify-send "Connected to \"$device_name\"."
else
notify-send "Failed to connect to \"$device_name\"."
fi
fi
;;
esac
done