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,
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
- 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.
- 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 ]
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
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 setting se.processes["SystemUIServer"].menuBars.menuBarItems.click() ; delay(0.2); se.processes["SystemUIServer"].menuBars.menuBarItems.menus.menuItems.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()
- m Swiss Army Knife for macOS !: