36

This question is almost the same as my previous question, but with one major difference; I want to turn on/off Bluetooth from the command line (bash or similar), NOT from AppleScript - the answers for this I saw on superuser all used AppleScript.

I would guess that networksetup might be useful but have no idea what interface(s) to interact with - I would like an answer including a complete command I can run (and understand) - if device model matters, it's Snow Leopard on MacBook2,1 to MacBook7,1, except MacBook5,1, and if you can only do for 6,1 and 7,1 that's fine.

4 Answers4

59

Blueutil is a neat little command line tool to do this. It's free and comes with the source code. If you have homebrew installed, you can install it via brew install blueutil.

Usage:

Print bluetooth status
blueutil

Switch bluetooth on
blueutil --power 1 or blueutil -p 1

Switch bluetooth off
blueutil --power 0 or blueutil -p 0

Works just fine on a Macbook Pro running Lion (10.7.3) and Mac mini running Snow Leopard (10.6.8). You will get some errors if you switch off the bluetooth whilst a magic mouse is connected, it still works though :)

binarybob
  • 9,875
  • Thank you. I'll accept this soon but don't like to accept within 6 hours after posting (if you can do this without downloading anything, just using built-in OS X that would also be very nice so I'm waiting to see if someone suggests that). – Andrew Wonnacott Apr 03 '12 at 21:41
  • 3
    @Andrew The other way you could do this without downloading anything is by killing (and restarting) the bluetooth daemon process (blued) as follows. However, I would recommend not to use this method as it plays havoc with the bluetooth preferences pane and the bluetooth status icon in the menubar, as they now will not be able to update themselves any more. To stop the bluetooth daemon: sudo launchctl unload /System/Library/LaunchDaemons/com.apple.blued.plist. To restart the bluetooth daemon: sudo launchctl load /System/Library/LaunchDaemons/com.apple.blued.plist – binarybob Apr 03 '12 at 22:23
  • @Andrew Ah, I just noticed in an earlier comment you said you couldn't "admin on this computer". If I've understood that correctly then my previous comment won't work as you need to type an admin password to sudo – binarybob Apr 03 '12 at 22:34
  • It's OK, blueutil worked fine. – Andrew Wonnacott Apr 04 '12 at 14:09
  • blueutil has some questionable code included. It attempts to use an osax called XMail. XMail is a free osax for Mac OS X allowing you to send e-mail from AppleScript. Be careful of software you give admin privileges to. Here's a message I got in Terminal when I ran the installer but before I authorized admin privileges. Error loading /Library/ScriptingAdditions/XMail.osax/Contents/MacOS/XMail: dlopen(/Library/ScriptingAdditions/XMail.osax/Contents/MacOS/XMail, 262): no suitable image found. –  Nov 24 '12 at 05:28
  • 1
    I didn't give it an admin privilege. I just copied out the binary. – Andrew Wonnacott Nov 25 '12 at 04:03
  • 1
    Yeah, just copied the binary to /usr/local/bin. But where's that questionable code? Took a look at the included source and it looked legit, didn't build it though. – Erika Mar 08 '13 at 18:59
  • 5
    This was a great answer and BTW, you can brew install blueutil to easily install it. They seem to have changed the command line flags, use blueutil power 1 to turn on bluetooth. – Ivan -Oats- Storck Jun 16 '14 at 03:58
  • And blueutil power 0 to turn off. – Douglas Ludlow Apr 28 '15 at 15:32
  • Mostly expected an old Mac answer to fail on newer Intel-based machines, but it works for macOS High Sierra 10.13.6 too! Was a charm since my Bluetooth service was not picking up my devices from login this morning again. This was able to toggle the service when the Pref Pane was not! I also like shorter commands the community supports in some cases, like opposed to the other launchctl answer below. blueutil -p <\d> worked for me like in this answer, unlike the comments above. – Pysis Jan 22 '19 at 14:38
11

The following worked for me (OSX 10.7.5) to turn bluetooth ON from the command line (using commands found here):

sudo defaults write /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 1

sudo launchctl unload /System/Library/LaunchDaemons/com.apple.blued.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.blued.plist

(I ran into the problem that I disabled bluetooth and then on the next system start I could not use the wireless keyboard anymore... so no logging in from the screen but I could log in via ssh. And it was not clear to me how to install software such as blueutil from the command line as suggested by @binarybob )

  • I can't sudo on this computer, as per one of my other comments, but this is still nice to know. I unpacked blueutil graphically and then ran the executable from the command line, so I had to already have set it up. – Andrew Wonnacott Sep 01 '13 at 17:35
  • As binarybob previously said in his comment, this can confuse the GUI – Greenonline Feb 08 '16 at 15:07
1

This opensource mac command line tool supposedly does that, and a whole lot more:

https://github.com/guarinogabriel/Mac-CLI

The ultimate tool to manage your Mac. It provides a huge set of command line commands that automatize the usage of your OS X system.

Brad Parks
  • 2,393
1

Solution for macOS 13 Ventura which does not require 3rd party utility:

/usr/bin/defaults write /private/var/root/Library/Preferences/com.apple.BTServer.plist defaultPoweredState off

/usr/bin/killall -HUP bluetoothd

qzyphus
  • 396