Switch bluetooth setting from command line on macOS

Before proceed reading, there is one thing to mention.. The daemon name blued (at least until macOS 10.11 El Capitan), has been changed to bluetoothd.

So based on the version of the macOS, you need to change the daemon name in the below command(s).

Another thing to note is that, unload then load the daemon’s plist(instead of stop then start or sending HUP signal) may not work due to the SIP(System Integrity Protection) introduced in El Capitan. But it should work when you disabled the SIP or on macOS before El Capitan.

There are several options to do the task,

defaults

If you have root privilege, you can do,

  • By using defaults command, edit bluetooth daemon setting
  • Then restart the daemon
# Let bluetooth be on $ sudo defaults write /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 1

 
# let bluetooth be off $ sudo defaults write /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 0

 

# Then reload the daemon$ sudo killall -HUP bluetoothd # or blued based on macOS version

# On a macOS system which has proctools installed, you can replace `killall` to `pkill`

# Or if you'd like to use launchctl to restart the daemon
$ sudo launchctl stop com.apple.bluetoothd # or blued based on macOS version
$ sudo launchctl start com.apple.bluetoothd

Blueutil

  • Install blueutil using HomeBrew
  • Use blueutil command to switch the bluetooth controller
$ brew install blueutil

$ blueutil --power 1 # to power on$ blueutil --power 0 # to power off

Blueutil is cool stuff, but it is using some private APIs of IOBluetooth.framework, so it may not work on the future version of macOS.

m-cli

  • Install m-cli using HomeBrew
  • Use m command to switch the bluetooth controller
$ brew install m-cli

$ m bluetooth enable # to power on

$ m bluetooth disable # to power off



    usage: m bluetooth [ status | enable | disable | help ]


    Examples:      m bluetooth status    # bluetooth status
      m bluetooth enable    # turn on bluetooth
      m bluetooth disable   # turn off bluetooth

m does not support macOS Sierra or later(as of 2018-07-24).  https://github.com/rgcr/m-cli/issues/127

JXA

Above 3 options require root privilege, but using JXA for switching bluetooth power does not require it(but you need to give your terminal or Script editor(or Automator) to access to Accessibility API before or upon the execution of the JXA script.

The following is a sample JXA script which does either one of the followings,

  • Click Bluetooth power on/off menu item in the System Menu bar
  • Click Bluetooth power on/off radio button in the System Preferences
// Preparation 
se = Application("System Events");sp = Application("System Preferences");app = Application.currentApplication();app.includeStandardAdditions = true;



// CLick System UI Menu bar and its inside item// The number '7' below, depends on User's system menu bar setting, so you need to change the number based on your settingse.processes["SystemUIServer"].menuBars[0].menuBarItems[7].click() ; delay(0.2); se.processes["SystemUIServer"].menuBars[0].menuBarItems[7].menus[0].menuItems[1].click()



// Click System Preference > Bluetooth radio button

// To power it on

se.processes["System Preferences"].windows["Bluetooth"].buttons['Turn Bluetooth on'].click()

// To power it off

se.processes["System Preferences"].windows["Bluetooth"].buttons['Turn Bluetooth off'].click()

Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s