How to control Bluetooth wireless radio from the command line?

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.


Solution 1:

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 :)

Solution 2:

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 )

Solution 3:

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.