0

Im using ubuntu with cinnamon and im tired of not having a bluetooth switch, and having to use rfkill to turn BT off. For now, i use blueman to turn BT on and manage devices and connections, and I use rfkill on my terminal to turn it back off after im done using it. I wanted to create a clickable macro (ie : as a cinnamon applet that would display on my taskbar) that rfkills on and off BT.

My question is : is there a command of rfkill that lets you simply invert a device's state (that is unblock if blocked and blocked if unblocked) ?

Bruh
  • 317

1 Answers1

1

There is no invert flag in rfkill and there is no alternative cli tool that can do this. You can write a small script like the one below for it and have a keybinding initiate the script.

Note : You will need to add your user to sudoers file to run rfkill without password.

#!/bin/bash
BTDEVNO=1
currentState=$(rfkill list $BTDEVNO)
if [[ $currentState =~ ": no" ]]; then
    echo "Unblocked, going to block";
    sudo rfkill block $BTDEVNO;
else
    echo "Blocked, going to unblock";
    sudo rfkill unblock $BTDEVNO;
fi

Edit - About sudoers (made by Bruh) :

To add rfkill to the sudoers list, type sudo visudo and then add the following at the end of the file, under the %sudo line :

user_name : ALL=(ALL) NOPASSWD : /usr/sbin/rfkill

This will allow rfkill to run without the need of the sudo password.

Edit 2 - Slight change to the script (Made by Bruh) :

If the scipt seems to be always seeing BT blocked despite a manual rfkill list showing it unblocked, try replacing the if statement by the following :

if echo $currentState | grep 'Bluetooth Soft blocked: no'; then
Bruh
  • 317
  • Thanks you ! This should do the trick ! – Bruh Oct 22 '20 at 11:53
  • Hi so i have a slight problem with your script, i get the following error : ./BT_switch.sh: 3: [[: not found. 3 is indeed the BT id i need to switch on and off, but idk what could be wrong here. The if statement doesnt seem to be ever satisfied thus it can only unblock, even tho "$(rfkill list $BTDEVNO)"does seem to return a string that contains your regex. Any idea ? – Bruh Oct 23 '20 at 20:23
  • @Bruh, do you have the shebang line for bash in the script? – MidwayNomad Oct 24 '20 at 12:57
  • not sure about the shebang, im new to scripts. It seems to be working now, but still requires sudo identification despite adding rfkill to the sudoers file under /etc/sudoers using username ALL=NOPASSWD: /usr/sbin/rfkill – Bruh Oct 26 '20 at 10:31
  • just so that I understand, did you add the shebang line to the script for it to start working? Also, can you please try the following within sudoers. <bruhs uname> ALL=(ALL) NOPASSWD:<output_of_which rfkill>

    Please make sure you edit sudoers file using visudo

    – MidwayNomad Oct 27 '20 at 12:37
  • I had to change the pattern matching condition to a $currentState | grep 'Bluetooth Soft blocked: no' instead of what you provided. I also added th e shebang in case. Adding the provided line in sudoers didnt do anything, i still seem to be needing sudo. IE : launching the shortcut in terminal mode opens a terminal on sudo password prompt. Launching the shortcut normally doesnt do anything – Bruh Oct 28 '20 at 13:00
  • Can you please share the output of the following commands : id & which rfkill. Also, please share the sudoers entry you created verbatim. – MidwayNomad Oct 30 '20 at 05:58
  • sure : uid=1000(iamgron) gid=1000(iamgron) groups=1000(iamgron),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare) is returned by id and /usr/sbin/rfkill is returned by which rfkill Finally, this is what i have in my sudoers : iamgron ALL=(ALL) NOPASSWD: /usr/sbin/rfkill – Bruh Oct 30 '20 at 18:52
  • @Bruh, does this help : https://askubuntu.com/questions/100051/why-is-sudoers-nopasswd-option-not-working ? – MidwayNomad Oct 31 '20 at 11:43
  • worked a charm, thank you so much ! I'll edit your answer to summurize everything. – Bruh Nov 03 '20 at 14:09