Turn Bluetooth On and Off from Command Line on macOS

By  on  
bluetooth

Bluetooth has been a revelation in wireless technology: wireless mice, headphones, streaming devices, and a variety of home and office environments.  It goes without saying that wireless peripherals are so much easier to manage than wired counterparts, especially mice, that I usually have my MacBook's bluetooth turned on.

There are times, however, that I move my laptop away from the mouse (OK, I admit:  the kitchen) where the bluetooth connection is still in range but with the mouse still connected, I can't use my laptop's touchpad, leaving me to only use my keyboard which is an annoyance.  That led me to finding blueutil, a command line utility for macOS that lets me turn Bluetooth on and off with one command!

Start by installing blueutil with HomeBrew:

brew install blueutil

The -p flag is a switch for turning bluetooth on and off:

# Turn bluetooth off
blueutil -p 0

# Turn bluetooth on
blueutil -p 1

This utility will be so helpful as I move around my house.  Since I can't use my touchpad during these situations, I can't turn Bluetooth off from the command bar, so a simple command like this is exactly what I need!

Recent Features

  • By
    Create a Sheen Logo Effect with CSS

    I was inspired when I first saw Addy Osmani's original ShineTime blog post.  The hover sheen effect is simple but awesome.  When I started my blog redesign, I really wanted to use a sheen effect with my logo.  Using two HTML elements and...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    Create Your Own Dijit CSS Theme with LESS CSS

    The Dojo Toolkit seems to just get better and better.  One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes.  The move to using LESS is a brilliant one because it makes creating your own Dijit theme...

  • By
    CSS Fixed Positioning

    When you want to keep an element in the same spot in the viewport no matter where on the page the user is, CSS's fixed-positioning functionality is what you need. The CSS Above we set our element 2% from both the top and right hand side of the...

Discussion

  1. Patrick K

    Thanks for sharing this! I was trying to find an easier way to toggle Bluetooth and I kept finding articles about AppleScript, but this is way easier for people familiar with the command line.

  2. Ahmed Hossein

    Thank you! I just came across this and it was a life-saver.

    I created a small “bluetoggle.sh” to toggle the switch: ON if it was OFF, OFF if it was ON. I then used iCanHazShortcut to map the script to [^B] for ease of access.

    #!/bin/zsh
    
    if [ blueutil -p |grep 1 = "1" 2>/dev/null 2>/dev/null ]; then blueutil -p 0; else blueutil -p 1; fi
    
  3. Ahmed Hossein

    The code above was messed up by the interpreter (symbol ` is not accepted it seems). I put here the final code of my 1-line bluetoggle.sh

    https://pastebin.com/YssdgD5Z

  4. Ahmed Hossein

    This can be turned into a toggle command using:

    #!/bin/bash
    blueutil -p | grep 1 && blueutil -p 0 || blueutil -p 1
    
  5. Sean Beeg

    Thanks for this. Bluetooth has been crashing on my mac every so often, so a quick brew install and 2 aliases and I’m in business.

  6. Matt D

    Here is how I control my bluetooth from ZSH.

    bt () {
    	if [ -n $1 ]
    	then
    		echo $1
    		export btArg=$(echo $1 | tr '[:upper:]' '[:lower:]') 
    		echo $btArg
    		case $btArg in
    			(on) echo "Turning on BlueTooth" >> /tmp/bt.log
    				blueutil --power 1 | tee /tmp/bt.log ;;
    			(off) echo "Turning off BlueTooth" >> /tmp/bt.log
    				blueutil --power 0 | tee /tmp/bt.log ;;
    			(stat*) echo "Checking BlueTooth state" >> /tmp/bt.log
    				export BT=$(blueutil -p | tee /tmp/bt.log) 
    				case $BT in
    					(0) echo "BlueTooth is powered off"
    						blueutil --connected ;;
    					(1) echo "BlueTooth is powered on"
    						blueutil --connected ;;
    					(*) echo "BlueTooth in unknown state"
    						blueutil --connected ;;
    				esac ;;
    			(*) echo -n "Unknown input" ;;
    		esac
    	else
    		echo -n "Need input"
    	fi
    }
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!