Joho the Blog » Toggling a bluetooth device from the command line
Everyday Chaos
Too Big to Know
Too Big to Know
Cluetrain 10th Anniversary edition
Cluetrain 10th Anniversary
Everything Is Miscellaneous
Everything Is Miscellaneous
Small Pieces cover
Small Pieces Loosely Joined
Cluetrain cover
Cluetrain Manifesto
My face
Speaker info
Who am I? (Blog Disclosure Form) Copy this link as RSS address Atom Feed

Toggling a bluetooth device from the command line

I move my laptop from my home office to other rooms of the house after I realize, as I do at least twice a day, that my office looks like a hoarder’s storage locker. Rather than cleaning it up, I decided to make it slightly easier for me to make those moves.

The problem is that if I move my MacBookPro (2019) out of bluetooth range of my Apple MagicMouse, the trackpad doesn’t accept input. And that means I have to trudge the entire 15 feet back to my office to get my mouse. That obviously is simply not acceptable, so I instead spent several hours writing a shell script that I can run in a terminal that will toggle my mouse (or any other bluetooth device) on or off.

It requires you to install blueutil, an open source set of command line tools for managing your bluetooth connections. (Thank you, Ivan Kuchin). To install blueutil, in a terminal type “brew install blueutil”. If you don’t have Homebrew installed, you’ll get an error message, in which case install Homebrew. It’s very useful.

You also will need to know the bluetooth ID of the device you want to control. You can find that by going to your Mac’s Settings and clicking on the bluetooth icon, or by running blueutil in a terminal with the parameter “–paired.”

To use this script, copy and paste it into a text editor and save it with a “.sh” extension. To let your Mac know that this is a text file that should be run, not just read, you need to go to a terminal and change its permissions to 755. (For instructions on how to do that and how to run it from a terminal, check here.) (I have bound the crl-alt-com B key to running it, thanks to KeyboardMaestro.)

Note that I am a bad writer of shell scripts. I had to look up just about everything except how to write a comment. Have some compassion, people! And if not that, how about some pity?

# Toggles a device's bluetooth activity on and off.
# Useful for when you move out of range of your bluetooth
#   mouse and your Mac's trackpad becomes 
#   unresponsive. For example.
# Requires installing Blueutil:
# I post this as Creative Commons Zero, i.e., public domain. 
# David Weinberger, Sept. 24, 2020 [email protected]

# Add your device's bluetooth ID here.
# If you don't know it, run "blueutil --paired"
# in a terminal window to find out.
ID="04-4b-ed-d2-ef-23" # Just an example

echo "Toggles bluetooth device. Requires blueutils be installed.";
echo "USAGE: bluetoggle. No parameters";
echo "Device ID: ${ID}"
echo "--------";

# is the device connected?
# Run blueutil and returns the text.
STATUS=`blueutil --paired`

# is the ID mentioned in the status?
# If not, then it hasn't been connected, so there's
# nothing for this  script to do
if [[ $STATUS != *"${ID}"* ]]
    echo "Device not found. Edit to add device's bluetooth ID. Exiting."

# Look for the ID followed by ", not connected"
if [[ $STATUS == *"${ID}, not"* ]]
    echo "${ID} is not connected. Connecting..."
    CL=`blueutil --connect ${ID}` # Create command string
    NEWSTATUS=`${CL}` # Run the command
    echo "Disconnecting device..."
    CL=`blueutil --disconnect ${ID}`


Previous: « || Next: »

Leave a Reply

Comments (RSS).  RSS icon