Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/zsh
- Bluetooth_device_menu () {
- info=("${(ws(|))ROFI_INFO}")
- device_mac=$info[1]
- btcommand=$info[2]
- device_name=${"$(bluetoothctl info $device_mac | grep 'Name: ')"##*: }
- case "$btcommand" in
- "Connect")
- notify-send "Connecting to $device_name..."
- bluetoothctl connect $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
- notify-send "Connection successful!"
- ;;
- "Disconnect")
- notify-send "Disconnecting to $device_name..."
- bluetoothctl disconnect $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
- notify-send "Disconnection successful!"
- ;;
- "Pair")
- bluetoothctl pair $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
- ;;
- "Unpair")
- bluetoothctl remove $device_mac 0>/dev/null 1>/dev/null 2>/dev/null #Todo: add confirmaton
- ;;
- "Trust")
- bluetoothctl trust $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
- ;;
- "Untrust")
- bluetoothctl untrust $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
- ;;
- *)
- ;;
- esac
- connect_verb="$(bluetoothctl info $device_mac | grep -q 'Connected: yes' && echo 'Disconnect' || echo 'Connect')"
- pair_verb="$(bluetoothctl info $device_mac | grep -q 'Paired: yes' && echo 'Unpair' || echo 'Pair')"
- trust_verb="$(bluetoothctl info $device_mac | grep -q 'Trusted: yes' && echo 'Untrust' || echo 'Trust')"
- echo "$connect_verb\0info\x1f$device_mac|$connect_verb"
- echo "$pair_verb\0info\x1f$device_mac|$pair_verb"
- echo "$trust_verb\0info\x1f$device_mac|$trust_verb"
- echo "------\0info\x1f$device_mac"
- echo "Back"
- echo "Exit"
- }
- Bluetooth () {
- prompted=0 # was the user prompted to pick a device ($@ is either
- # asking to pair a new device or to pick a device)
- is_powered="$(bluetoothctl show | grep 'Powered: yes')"
- if [[ ( $is_powered ) ]]; then
- if [[ $ROFI_INFO == "BTscan" ]]; then
- bluetoothctl scan on
- print "Scaning..."
- sleep 5
- exit 0
- elif [[ -v ROFI_INFO ]]; then
- #because rofi info is defined and not BTscan, then we can hope that this means we have a valid mac address
- #and can continue with per-device logic
- Bluetooth_device_menu $ROFI_INFO
- elif [[ ${#State} -lt 2 ]]; then #Either state is empty (this is the first call) or has 1 entry (Bluetooth)
- Devices_string="$(bluetoothctl devices)"
- Devices=("${(@f)Devices_string}")
- for device in $Devices; do
- device_name=${${device##*:}[4,100]}
- device_mac=${device[8,24]}
- echo "${device_name}\0info\x1f${device_mac}\n" #if bluetooth is powered, then display known devices
- done
- echo "Pair new device...\0info\x1fBTscan\n" #Or we could pair a new one
- echo "Exit" #Or we could leave
- fi
- else
- echo "Powered: False"
- fi
- }
- Wifi() {
- notify-send "Not Implemented"
- }
- echo "\0no-custom\x1f"
- if [[ "$#" -eq 0 ]]; then
- echo "Bluetooth"
- echo "WiFi"
- echo "Exit"
- elif [[ $1 == "Exit" ]]; then
- return 0
- elif [[ -v ROFI_DATA ]]; then
- State=("${(@f)ROFI_DATA}")
- case $State[1] in
- "Bluetooth")
- Bluetooth
- ;;
- "Wifi")
- Wifi
- ;;
- esac
- else
- case "$1" in
- "Bluetooth")
- echo -en "\0data\x1fBluetooth\n"
- selected_device="None"
- Bluetooth
- ;;
- "WiFi")
- Wifi
- ;;
- "Exit")
- exit 0
- esac
- fi
Advertisement
Add Comment
Please, Sign In to add comment