Guest User

Bluetoothhelp

a guest
Dec 1st, 2026
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.33 KB | Fixit | 0 0
  1. #!/bin/zsh
  2.  
  3. Bluetooth_device_menu () {
  4.   info=("${(ws(|))ROFI_INFO}")
  5.   device_mac=$info[1]
  6.   btcommand=$info[2]
  7.   device_name=${"$(bluetoothctl info $device_mac | grep 'Name: ')"##*: }
  8.   case "$btcommand" in
  9.     "Connect")
  10.       notify-send "Connecting to $device_name..."
  11.       bluetoothctl connect $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
  12.       notify-send "Connection successful!"
  13.     ;;
  14.     "Disconnect")
  15.       notify-send "Disconnecting to $device_name..."
  16.       bluetoothctl disconnect $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
  17.       notify-send "Disconnection successful!"
  18.     ;;
  19.     "Pair")
  20.       bluetoothctl pair $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
  21.     ;;
  22.     "Unpair")
  23.       bluetoothctl remove $device_mac 0>/dev/null 1>/dev/null 2>/dev/null #Todo: add confirmaton
  24.     ;;
  25.     "Trust")
  26.       bluetoothctl trust $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
  27.     ;;
  28.     "Untrust")
  29.       bluetoothctl untrust $device_mac 0>/dev/null 1>/dev/null 2>/dev/null
  30.     ;;
  31.     *)
  32.     ;;
  33.   esac
  34.   connect_verb="$(bluetoothctl info $device_mac | grep -q 'Connected: yes' && echo 'Disconnect' ||  echo 'Connect')"
  35.   pair_verb="$(bluetoothctl info $device_mac | grep -q 'Paired: yes' && echo 'Unpair' ||  echo 'Pair')"
  36.   trust_verb="$(bluetoothctl info $device_mac | grep -q 'Trusted: yes' && echo 'Untrust' ||  echo 'Trust')"
  37.   echo "$connect_verb\0info\x1f$device_mac|$connect_verb"
  38.   echo "$pair_verb\0info\x1f$device_mac|$pair_verb"
  39.   echo "$trust_verb\0info\x1f$device_mac|$trust_verb"
  40.   echo "------\0info\x1f$device_mac"
  41.   echo "Back"
  42.   echo "Exit"
  43. }
  44. Bluetooth () {
  45.   prompted=0 # was the user prompted to pick a device ($@ is either
  46.              # asking to pair a new device or to pick a device)
  47.   is_powered="$(bluetoothctl show | grep 'Powered: yes')"
  48.   if [[ ( $is_powered ) ]]; then
  49.     if [[ $ROFI_INFO == "BTscan" ]]; then
  50.       bluetoothctl scan on
  51.       print "Scaning..."
  52.       sleep 5
  53.      exit 0
  54.     elif [[ -v ROFI_INFO ]]; then
  55.       #because rofi info is defined and not BTscan, then we can hope that this means we have a valid mac address
  56.       #and can continue with per-device logic
  57.       Bluetooth_device_menu $ROFI_INFO
  58.     elif [[ ${#State} -lt 2 ]]; then #Either state is empty (this is the first call) or has 1 entry (Bluetooth)
  59.       Devices_string="$(bluetoothctl devices)"
  60.       Devices=("${(@f)Devices_string}")
  61.       for device in $Devices; do
  62.         device_name=${${device##*:}[4,100]}
  63.         device_mac=${device[8,24]}
  64.         echo  "${device_name}\0info\x1f${device_mac}\n" #if bluetooth is powered, then display known devices
  65.         done
  66.       echo "Pair new device...\0info\x1fBTscan\n" #Or we could pair a new one
  67.       echo "Exit" #Or we could leave
  68.     fi
  69.   else
  70.     echo "Powered: False"
  71.   fi
  72. }
  73. Wifi() {
  74.   notify-send "Not Implemented"
  75. }
  76. echo "\0no-custom\x1f"
  77. if [[ "$#" -eq 0 ]]; then
  78.   echo "Bluetooth"
  79.   echo "WiFi"
  80.   echo "Exit"
  81. elif [[ $1 == "Exit" ]]; then
  82.   return 0
  83. elif [[ -v ROFI_DATA ]]; then
  84.   State=("${(@f)ROFI_DATA}")
  85.  
  86.   case $State[1] in
  87.     "Bluetooth")
  88.       Bluetooth
  89.     ;;
  90.     "Wifi")
  91.       Wifi
  92.     ;;
  93.   esac
  94. else
  95.   case "$1" in
  96.     "Bluetooth")
  97.       echo -en "\0data\x1fBluetooth\n"
  98.       selected_device="None"
  99.       Bluetooth
  100.     ;;
  101.     "WiFi")
  102.       Wifi
  103.     ;;
  104.     "Exit")
  105.       exit 0
  106.   esac
  107. fi
  108.  
Advertisement
Add Comment
Please, Sign In to add comment