Turning off Bluetooth when computer sleeps

I finally got myself a pair of wireless headphones at the end of last year, which means I started using Bluetooth much more in the past months.

Because my 6+ years old Macbook Pro’s battery isn’t that great anymore and also to avoid automatically connecting my headphones to a sleeping computer (they’re not that smart), I wanted to figure out a way to programmatically turn the Bluetooth off when I’m not using it.

I found a quite old but great article about automating tasks on MacOS and even a ready-to-use solution but decided to implement it myself and learn something new. Documenting the process ended up becoming my first attempt to learn in public.

I started by using homebrew to install Sleepwatcher, a command line tool for macOS that monitors sleep, wakeup and idleness of a Mac and can be used to execute a command when one of those things happen.

brew install sleepwatcher

I also installed blueutil, which is a command line utility that allows you to control Bluetooth in macOS.

brew install blueutil

The next step was writing a sleep script.

sleepwatcher looks for user scripts in ~/.sleep and ~/.wakeup to know what to do, so I created a .sleep file and added the following lines to it:

#!/bin/bash
/usr/local/bin/blueutil --power 0

This will tell sleepwatcher to run the command /usr/local/bin/blueutil --power 0 when the computer sleeps and the command will then turn the Bluetooth off.

You could put any command here that you would like to run when the computer sleeps (or wakes up, in the respective file). I added AppleScript to my list of things to investigate so I can figure out what the possibilities are.

After saving this file, I needed to give it permission to execute by running:

chmod 700 ~/.sleep

chmod is a Unix command used to change file permissions and 700 means users (first digit) can read, write and execute (7), but groups and others can’t do anything (0).

To test that the script worked, I ran the following command in the terminal:

/usr/local/sbin/sleepwatcher --verbose --sleep ~/.sleep

At first it looked like nothing happened and that the terminal was just hanging, but I made sure I had Bluetooth on and then put my Macbook to sleep. After a moment, I opened the lid again and confirmed that Bluetooth was off.

If anything didn’t work as expected, you should see the error in the terminal. If it works, you can exit sleepwatcher with control + C.

To make sure this is happening in the background without having to manually run the command everytime, I ran:

brew services start sleepwatcher

Now, whenever I close my computer, Bluetooth is automatically turned off. Whoop!

PS: Because I don’t really need it to always be on when I’m using my personal Macbook, I didn’t implement the wakeup script, but the process would be the same for adding the ~/.wakeup file and then changing --power 0 to --power 1 in the blueutil command.

PS2: To turn sleepwatcher off, run brew services stop sleepwatcher.

Written on a Friday in January, 2021