Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dreampiggy
Forked from ralph089/.wakeup
Created August 11, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreampiggy/1d6b462243e83236d39b3121c9006427 to your computer and use it in GitHub Desktop.
Save dreampiggy/1d6b462243e83236d39b3121c9006427 to your computer and use it in GitHub Desktop.
Restarts Bluetooth Module on Mac OS X El Capitan. You can use the script as shortcut to restart Bluetooth on demand or you can use it with "SleepWatcher" to automatically restart Bluetooth on wakeup (See README.md). I created it, because my Logitech Bluetooth Mouse doesn't stay connected after sleep-mode, so i had to manually re-pair my mouse.
#!/bin/bash
#
# Restart Bluetooth Module on Mac OS X
#
# Requires Blueutil to be installed: http://brewformulas.org/blueutil
BT="/usr/local/bin/blueutil"
log() {
echo "$@"
logger -p notice -t bt_restarter "$@"
}
err() {
echo "$@" >&2
logger -p error -t bt_restarter "$@"
}
if [ -f "$BT" ]; then
if [[ $("$BT" power) == *1 ]];
then
echo "Bluetooth on, restarting ..."
($("$BT" power 0) &> /dev/null && echo "Bluetooth Module stopped") || (err "Couldn't stop Bluetooth Module" && exit 1)
($("$BT" power 1) &> /dev/null && echo "Bluetooth Module started") || (err "Couldn't start Bluetooth Module" && exit 1)
log "Successfully restarted Bluetooth" && exit 0
else
echo "Bluetooth is off, nothing to do ..."
fi
else
err "Couldn't find blueutil, please install http://brewformulas.org/blueutil" && exit 1
fi

There is a program called [SleepWatcher] (http://www.bernhard-baehr.de/) that monitors sleep, wakeup and idleness of a Mac. To restart Bluetooth everytime your Mac comes up from sleep, you have to tell SleepWatcher to execute the script from above automatically. Restarting Bluetooth takes approximately 2s, so it doesn't attract negative attention.

The script makes use of BlueUtil, so you have to install it too. (I'm assuming you have HomeBrew installed)

brew install sleepwatcher
brew install blueutil

To make use of SleepWatcher, you have to put the gist to ~/.wakeup (For more information about sleepwatcher open terminal and insert: man sleepwatcher)

After you put your script to ~/.wakeup do the following:

chmod +x ~/.wakeup

Now you can test the script. Add the following command to your terminal:

/usr/local/sbin/sleepwatcher --verbose -w ~/.wakeup

Now you have to enter sleep mode and wakeup your Mac again. After that, you should see the following console output:

Bluetooth on, restarting ...
Bluetooth Module stopped
Bluetooth Module started
Successfully restarted Bluetooth
sleepwatcher: wakeup: /Users/ralph/.wakeup: 0

If it works, you can set up the user LaunchAgent by simlinking the example config files (more explanation can be found here: from https://www.kodiakskorner.com/log/258):

ln -sfv /usr/local/Cellar/sleepwatcher/2.2/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist ~/Library/LaunchAgents/

Then we tell launchd to load the configuration:

launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist

For debugging purposes, you can enter Console App and search for "bt_restarter". Maybe there are some messages, indicating a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment