Skip to content

Instantly share code, notes, and snippets.

@konchunas
Created February 5, 2019 16:18
Show Gist options
  • Save konchunas/265adbc4974c0559a374f0e7d3cbe17f to your computer and use it in GitHub Desktop.
Save konchunas/265adbc4974c0559a374f0e7d3cbe17f to your computer and use it in GitHub Desktop.
#!/bin/sh
ADAPTER="Adapter1"
ADDRESS="hci0"
IS_BLUETOOTH_ON=$(dbus-send --system --dest=org.bluez --print-reply /org/bluez/${ADDRESS} org.freedesktop.DBus.Properties.Get string:org.bluez.${ADAPTER} string:Powered )
DESIRED_STATE="false"
case $IS_BLUETOOTH_ON in
*false*)
DESIRED_STATE="true"
;;
esac
dbus-send --system --dest=org.bluez --print-reply /org/bluez/${ADDRESS} org.freedesktop.DBus.Properties.Set string:org.bluez.${ADAPTER} string:Powered variant:boolean:${DESIRED_STATE}
exit 0
@pacharanero
Copy link

pacharanero commented Jun 9, 2020

Thanks for this, I've been looking for something similar for Linux Mint/Ubuntu
Unfortunately this didn't work for me, the lines relating to reading of state worked, but the setting of state reoprted:
"Error org.bluez.Error.Blocked: Blocked through rfkill"

More research going to be required for me I think.....

@pacharanero
Copy link

pacharanero commented Jun 9, 2020

update:

here is a version which works on Linux Mint

#!/bin/sh

IS_BLUETOOTH_ON=$(dbus-send --system --dest=org.bluez --print-reply /org/bluez/hci0 org.freedesktop.DBus.Properties.Get string:org.bluez.Adapter1 string:Powered )

case $IS_BLUETOOTH_ON in 
  *false*)
    rfkill unblock 0
    ;;
  *true*)
    rfkill block 0
    ;;
esac

exit 0

I saved this to a file (in my case in ~/code/scripts/bluetooth-toggle.sh) and made it executable with chmod +x ~/code/scripts/bluetooth-toggle.sh. Then I assigned this to run via a keyboard shortcut using the GUI keyboard management tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment