Guest User

bluetooth rofi script

a guest
Jul 21st, 2022
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.54 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. bluetooth_on=""
  4. bluetooth_off=""
  5.  
  6. rofi_command="rofi -dmenu -no-fixed-num-lines \
  7.                   -yoffset -100 -i -p"
  8.  
  9. divider="━━━━━━━━━━━━━━━━━━━━━━━"
  10.  
  11. power_p() {
  12.     if bluetoothctl show | grep -q "Powered: yes"; then
  13.         return 0
  14.     else
  15.         return 1
  16.     fi
  17. }
  18.  
  19. scan_p() {
  20.     if bluetoothctl show | grep -q "Discovering: yes"; then
  21.         echo "Scan: on"
  22.         return 0
  23.     else
  24.         echo "Scan: off"
  25.         return 1
  26.     fi
  27. }
  28.  
  29. pairable_p() {
  30.     if bluetoothctl show | grep -q "Pairable: yes"; then
  31.         echo "Pairable: on"
  32.         return 0
  33.     else
  34.         echo "Pairable: off"
  35.         return 1
  36.     fi
  37. }
  38.  
  39. discoverable_p() {
  40.     if bluetoothctl show | grep -q "Discoverable: yes"; then
  41.         echo "Discoverable: on"
  42.         return 0
  43.     else
  44.         echo "Discoverable: off"
  45.         return 1
  46.     fi
  47. }
  48.  
  49. connected_p() {
  50.     local device_info=$(bluetoothctl info "$1")
  51.     if echo "$device_info" | grep -q "Connected: yes"; then
  52.         return 0
  53.     else
  54.         return 1
  55.     fi
  56. }
  57.  
  58. paired_p() {
  59.     local device_info=$(bluetoothctl info "$1")
  60.     if echo "$device_info" | grep -q "Paired: yes"; then
  61.         echo "Paired: yes"
  62.         return 0
  63.     else
  64.         echo "Paired: no"
  65.         return 1
  66.     fi
  67. }
  68.  
  69. trusted_p() {
  70.     local device_info=$(bluetoothctl info "$1")
  71.     if echo "$device_info" | grep -q "Trusted: yes"; then
  72.         echo "Trusted: yes"
  73.         return 0
  74.     else
  75.         echo "Trusted: no"
  76.         return 1
  77.     fi
  78. }
  79.  
  80. power_toggle() {
  81.     if power_p; then
  82.         bluetoothctl power off
  83.     else
  84.         if rfkill list bluetooth | grep -q 'blocked: yes'; then
  85.             rfkill unblock bluetooth && sleep 3
  86.         fi
  87.         bluetoothctl power on
  88.     fi
  89. }
  90.  
  91. scan_toggle() {
  92.     if scan_p; then
  93.         kill $(pgrep -f "bluetoothctl scan on")
  94.         bluetoothctl scan off
  95.     else
  96.         bluetoothctl scan on &
  97.         echo "Scanning..."
  98.         sleep 5
  99.     fi
  100.  
  101.     main_menu
  102. }
  103.  
  104. pairable_toggle() {
  105.     if pairable_p; then
  106.         bluetoothctl pairable off
  107.     else
  108.         bluetoothctl pairable on
  109.     fi
  110.    
  111.     main_menu
  112. }
  113.  
  114. discoverable_toggle() {
  115.     if discoverable_p; then
  116.         bluetoothctl discoverable off
  117.     else
  118.         bluetoothctl discoverable on
  119.     fi
  120.  
  121.     main_menu
  122. }
  123.  
  124. connection_toggle() {
  125.     if connected_p $1; then
  126.         bluetoothctl disconnect $1
  127.     else
  128.         bluetoothctl connect $1
  129.     fi
  130.  
  131.     device_menu "$2"
  132. }
  133.  
  134. paired_toggle() {
  135.     if paired_p $1; then
  136.         bluetoothctl remove $1
  137.     else
  138.         bluetoothctl pair $1
  139.     fi
  140.  
  141.     device_menu "$2"
  142. }
  143.  
  144. trust_toggle() {
  145.     if trusted_p $1; then
  146.         bluetoothctl untrust $1
  147.     else
  148.         bluetoothctl trust $1
  149.     fi
  150.  
  151.     device_menu "$2"
  152. }
  153.  
  154. device_menu() {
  155.  
  156.     local device_name=$(echo $1 | cut -d ' ' -f 3-)
  157.     local mac=$(echo $1 | cut -d ' ' -f 2)
  158.  
  159.     if connected_p $mac; then
  160.         local connected="Connected: yes"
  161.     else
  162.         local connected="Connected: no"
  163.     fi
  164.     local paired=$(paired_p $mac)
  165.     local trusted=$(trusted_p $mac)
  166.     local options="$connected\n$paired\n$trusted\n$divider\nBack\nExit"
  167.  
  168.     local chosen="$(echo -e "$options" | $rofi_command "$device_name")"
  169.  
  170.     case $chosen in
  171.         "")
  172.             echo "No option chosen."
  173.             ;;
  174.         $divider)
  175.             device_menu "$1"
  176.             ;;
  177.         $connected)
  178.             connection_toggle $mac "$1"
  179.             ;;
  180.         $paired)
  181.             paired_toggle $mac "$1"
  182.             ;;
  183.         $trusted)
  184.             trust_toggle $mac "$1"
  185.             ;;
  186.         "Back")
  187.             main_menu
  188.             ;;
  189.     esac
  190. }
  191.  
  192. main_menu() {
  193.  
  194.     if power_p; then
  195.         local power="Power: on"
  196.  
  197.         local devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-)
  198.  
  199.         local scan=$(scan_p)
  200.         local pairable=$(pairable_p)
  201.         local discoverable=$(discoverable_p)
  202.         local options="$devices\n$divider\n$power\n$scan\n$pairable\n$discoverable\nExit"
  203.     else
  204.         local power="Power: off"
  205.         local options="$power\nExit"
  206.     fi
  207.  
  208.     local chosen="$(echo -e "$options" | $rofi_command "Bluetooth")"
  209.  
  210.     case $chosen in
  211.         "")
  212.             echo "No option chosen."
  213.             ;;
  214.         "Exit")
  215.             exit 0
  216.             ;;
  217.         $divider)
  218.             main_menu
  219.             ;;
  220.         $power)
  221.             power_toggle
  222.             main_menu
  223.             ;;
  224.         $scan)
  225.             scan_toggle
  226.             ;;
  227.         $pairable)
  228.             pairable_toggle
  229.             ;;
  230.         $discoverable)
  231.             discoverable_toggle
  232.             ;;
  233.         *)
  234.             local device=$(bluetoothctl devices | grep "$chosen")
  235.  
  236.             if [[ $device ]] ; then device_menu "$device"; fi
  237.             ;;
  238.     esac
  239. }
  240.  
  241. print_status() {
  242.     if power_p; then
  243.         mapfile -t paired_devices < <(bluetoothctl paired-devices | grep Device | cut -d ' ' -f 2)
  244.        
  245.         local counter=0
  246.         for device in "${paired_devices[@]}"; do
  247.             if connected_p $device; then
  248.                 device_alias=$(bluetoothctl info $device | grep "Alias" | cut -d ' ' -f 2-)
  249.                 ((counter++))
  250.             fi
  251.         done
  252.         printf "$bluetooth_on $counter"
  253.     else
  254.         printf "$bluetooth_off"
  255.     fi
  256. }
  257.  
  258. case "$1" in
  259.     --status)
  260.         print_status
  261.         ;;
  262.     --toggle)
  263.         power_toggle
  264.         ;;
  265.     *)
  266.         main_menu
  267.         ;;
  268. esac
  269.  
Advertisement
Add Comment
Please, Sign In to add comment