58 lines
2.1 KiB
Bash
Executable File
58 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# 1. Notify that we are scanning for new devices
|
|
notify-send " Bluetooth" "Scanning for devices..." -t 1500
|
|
|
|
# 2. Get list of all devices (Connected, Paired, and New)
|
|
# We run a brief scan to find new devices nearby
|
|
timeout 2s bluetoothctl scan on > /dev/null 2>&1
|
|
|
|
# Get all known/nearby devices
|
|
devices=$(bluetoothctl devices | cut -d' ' -f2)
|
|
menu_list=""
|
|
|
|
for mac in $devices; do
|
|
info=$(bluetoothctl info "$mac")
|
|
name=$(echo "$info" | grep "Name:" | cut -d' ' -f2-)
|
|
paired=$(echo "$info" | grep "Paired:" | awk '{print $2}')
|
|
connected=$(echo "$info" | grep "Connected:" | awk '{print $2}')
|
|
batt=$(echo "$info" | grep "Battery Percentage" | awk -F '[()]' '{print $2}')
|
|
|
|
# Set Icons and Status
|
|
if [ "$connected" == "yes" ]; then
|
|
icon="" # Connected Icon
|
|
status="[Connected]"
|
|
[ -n "$batt" ] && status="[$batt%]"
|
|
elif [ "$paired" == "yes" ]; then
|
|
icon="" # Paired but disconnected
|
|
status="[Paired]"
|
|
else
|
|
icon="" # New / Unpaired
|
|
status="[New]"
|
|
fi
|
|
|
|
# Format line for Rofi: Icon - Name - Status
|
|
menu_list+="$icon $name $status\n"
|
|
done
|
|
|
|
# 3. Show Rofi Menu
|
|
selected=$(echo -e "$menu_list" | sort -r | rofi -dmenu -i -p "Bluetooth" -config "$HOME/.config/rofi/bluetooth.rasi")
|
|
|
|
[ -z "$selected" ] && exit
|
|
|
|
# 4. Action Logic
|
|
# Extract the name by removing the icon and the status bracket
|
|
device_name=$(echo "$selected" | sed -E 's/^[] //' | sed -E 's/ \[.*\]$//')
|
|
device_mac=$(bluetoothctl devices | grep "$device_name" | awk '{print $2}')
|
|
|
|
if [[ "$selected" == *"[Connected]"* ]] || [[ "$selected" == *"%"* ]]; then
|
|
notify-send "Bluetooth" "Disconnecting $device_name..."
|
|
bluetoothctl disconnect "$device_mac"
|
|
elif [[ "$selected" == *"[Paired]"* ]]; then
|
|
notify-send "Bluetooth" "Connecting to $device_name..."
|
|
bluetoothctl connect "$device_mac"
|
|
elif [[ "$selected" == *"[New]"* ]]; then
|
|
notify-send "Bluetooth" "Pairing with $device_name..."
|
|
bluetoothctl pair "$device_mac" && bluetoothctl trust "$device_mac" && bluetoothctl connect "$device_mac"
|
|
fi
|