MacBook lid closing to automatically disable Bluetooth toggle switch

Auto-Toggle Bluetooth on MacBook Lid Open/Close

👁81views

Automating Bluetooth toggling on a MacBook when the lid opens or closes requires a small shell script combined with a launchd sleep/wake hook or a tool like SleepWatcher installed via Homebrew. The script runs a blueutil command to disable Bluetooth on sleep and re-enable it on wake, preventing unwanted headset connections while the lid is closed.

CloudScale AI SEO - Article Summary
  • 1.
    What it is
    A simple script that automatically turns off Bluetooth when you close your MacBook lid and turns it back on when you open it.
  • 2.
    Why it matters
    This prevents Bluetooth devices like headphones from connecting to your closed MacBook, avoiding frustrating interruptions when you're trying to use those devices with other equipment.
  • 3.
    Key takeaway
    You can eliminate the annoyance of Bluetooth devices connecting to your sleeping MacBook with one simple automation script.
~1 min read

If you’re like me, little things bother you. When I turn on my bluetooth headset and it connects to my Macbook when its closed/sleeping, I get very frustrated. So I wrote a simple script to fix this behaviour. After running the script below, when you close the lid on your Macbook it will automatically turn bluetooth off. When you open you Macbook it will automatically re-enable bluetooth. Simple 🤓

If you need to install brew/homebrew on your mac then run this:

## Install homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
## IMPORTANT: Once the install finishes run the two commands displayed in the terminal window
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> $HOME/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

Script to automatically enable/disable bluetooth:

## Install the bluetooth util and sleepwatcher
brew install sleepwatcher blueutil
## This creates a file which switches bluetooth off when the macbook lid is closed
echo "$(which blueutil) -p 0" > ~/.sleep
## This creates a file which switches on bluetooth when the lid is open
echo "$(which blueutil) -p 1" > ~/.wakeup
## This makes both the files runable
chmod 755 ~/.sleep ~/.wakeup
## Finally restart the sleepwatcher service (to pickup the new files)
brew services restart sleepwatcher