Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Toggle Bluetooth setting based on power source Laptop Macs
One of my biggest complaints is not having the ability to toggle Bluetooth on and off based on my power source. I use my Macbook in clamshell mode, so turning Bluetooth back on with my Bluetooth keyboard and mouse is impossible. I have to wait for my Macbook to wake up, open the lid, and reach over to the trackpad and toggle Bluetooth on.

With some help, I was able to make the following script to toggle Bluetooth on when the machine is plugged in, and off when running on the battery. This script is smart in that it will not fight you if you turn Bluetooth on while on battery power. You can unplug the power, the script will turn Bluetooth off, and you can turn it back on and it will stay that way.

There is one requirement to use this application which is blueutil. (I have it mirrored on my site where I originally posted this; you can see my full guide there as well). Here's the script (remember to make it executable):
#! /bin/bash
PREVIOUS_SOURCE=$(pmset -g ps | perl -ne '/(w+) Power/ && print $1');

while [ 1 ]; do
  CURRENT_SOURCE=$(pmset -g ps | perl -ne '/(w+) Power/ && print $1');

  if [ $CURRENT_SOURCE != $PREVIOUS_SOURCE ]; then
    PREVIOUS_SOURCE=$CURRENT_SOURCE
    if [ $CURRENT_SOURCE = "AC" ]; then
      blueutil on
    else
      blueutil off
    fi
  fi
  sleep 5
done
Next you simply need to run the script at startup, which is easy enough to do with the free app Lingon.

I have not noticed any memory leaks, and this only uses about 600KB of RAM to run with almost no CPU usage to call on pmset or blueutil. You can obviously change the sleep setting (which determines how long before the script runs again) to whatever you like.

[robg adds: I haven't tested this one.]
    •    
  • Currently 1.60 / 5
  You rated: 5 / 5 (10 votes cast)
 
[8,061 views]  

Toggle Bluetooth setting based on power source | 3 comments | Create New Account
Click here to return to the 'Toggle Bluetooth setting based on power source' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Toggle Bluetooth setting based on power source
Authored by: maclaxguy on Aug 04, '09 08:50:29AM
Nice script, thanks! It looks like the escapes on the word characters were removed from your post , i.e. should be:
PREVIOUS_SOURCE=$(pmset -g ps | perl -ne '/(\w+) Power/ && print $1');
and
CURRENT_SOURCE=$(pmset -g ps | perl -ne '/(\w+) Power/ && print $1');

[ Reply to This | # ]
What about MarcoPolo?
Authored by: Tlalox on Aug 04, '09 09:06:08AM
You might want to use http://www.symonds.id.au/marcopolo/ for this. It's far more powerful and you can also disabled bluetooth based on the power source.

[ Reply to This | # ]
Toggle Bluetooth setting based on power source
Authored by: simonpie on Aug 05, '09 04:32:25AM

You could also use launchctl to load and unload blued instead of using blueutil.



[ Reply to This | # ]