DEV Community

Cover image for How to Automatically Disable Bluetooth and WiFi on MacBook Sleep
Shawon Saha
Shawon Saha

Posted on

How to Automatically Disable Bluetooth and WiFi on MacBook Sleep

Are you tired of your MacBook maintaining Bluetooth and WiFi connections when it's supposed to be sleeping? This can lead to unnecessary battery drain and potential security risks. In this guide, we'll walk you through a simple yet effective solution using SleepWatcher to automatically disable both Bluetooth and WiFi when your MacBook goes to sleep.


Why Disable Bluetooth and WiFi on Sleep?

When your MacBook goes to sleep, it often keeps Bluetooth and WiFi active. While this is convenient for quick wake-ups or maintaining network connections, it can:

  • Drain your battery unnecessarily.
  • Keep your Mac connected to devices or networks when it's not in use.
  • Pose potential security risks by leaving your wireless interfaces active.

By disabling both Bluetooth and WiFi during sleep, you can conserve battery life and improve security.


Step 1: Install SleepWatcher and Blueutil

To get started, you'll need two tools: SleepWatcher (to run scripts when your Mac sleeps or wakes) and Blueutil (to control Bluetooth via the command line).

  1. Open Terminal.
  2. Install both tools using Homebrew:
   brew install sleepwatcher blueutil
Enter fullscreen mode Exit fullscreen mode

This will install SleepWatcher and Blueutil into the /opt/homebrew directory.


Step 2: Create Sleep and Wake Scripts

SleepWatcher works by running custom scripts when your Mac sleeps or wakes. You'll create two scripts: one for sleep (to disable Bluetooth and WiFi) and one for wake (to re-enable them).

  1. Create the sleep script using Terminal in ~home directory:
echo "/opt/homebrew/bin/blueutil -p 0" > ~/.sleep
echo "networksetup -setairportpower Wi-Fi off" >> ~/.sleep
Enter fullscreen mode Exit fullscreen mode
  1. Create the wake script using Terminal in ~home directory:
echo "/opt/homebrew/bin/blueutil -p 1" > ~/.wakeup
echo "networksetup -setairportpower Wi-Fi on" >> ~/.wakeup
Enter fullscreen mode Exit fullscreen mode
  1. Make these scripts executable:
chmod +x ~/.sleep ~/.wakeup
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure SleepWatcher

Start the SleepWatcher service:

brew services start sleepwatcher
Enter fullscreen mode Exit fullscreen mode

Step 4: Make SleepWatcher Start at Login

To ensure SleepWatcher runs automatically whenever you log in, follow these steps:

Option 1: Use Login Items & Extensions (GUI Method)

  1. Open System Settings on your Mac.
  2. Go to General > Login Items & Extensions.
  3. Under Login Items, click the + button.
  4. Navigate to /opt/homebrew/Cellar/sleepwatcher/[version]/sbin/ (replace [version] with the installed version of SleepWatcher, e.g., 2.2.1).
  5. Select sleepwatcher and click Add.

This will add SleepWatcher as a login item so it starts automatically whenever you log in.


Testing Your Setup

To ensure everything is working correctly:

  1. Close your MacBook lid
  2. Wait a few seconds
  3. Open the lid

Both Bluetooth and WiFi should turn off when the lid is closed and turn back on when opened.


Troubleshooting

If you encounter any issues:

  • Make sure SleepWatcher is running: brew services list
  • Check your script permissions: ls -l ~/.sleep ~/.wakeup
  • Review system logs for any error messages

Conclusion

By following this guide, you've successfully automated the process of disabling Bluetooth and WiFi whenever your MacBook goes to sleep—and re-enabling them upon waking up! With SleepWatcher configured to start at login, you can enjoy improved battery life, enhanced security, and peace of mind knowing that your wireless interfaces are inactive when not in use.

Remember, you can always modify the sleep and wake scripts to include additional actions or fine-tune the behavior to suit your specific needs.

Happy automating! 😊

Top comments (0)