How do I talk to Apple's Bluetooth driver?
April 22, 2006 10:43 AM   Subscribe

I want to script the "Turn Bluetooth (On|Off)" action in OSX. I don't know how. There's just a little bit...

I normally leave Bluetooth disabled on my machine in order to conserve battery power. However, I'd like to automate some actions that begin with turning the Bluetooth device on. Clearly this is possible from user space, as there's the perfectly normal vendor-supplied menuextra that allows me to do it in the GUI. However, I'd like to avoid having to remember to perform a GUI action before running these scripts.

I have gone prodding through the Darwin man pages. I have poked around in the Applescript documentation. I have S'ed TFW. I have gazed upon Apple's Bluetooth Framework docs on the assumption that I'll have to write a little utility to do the actual toggling. No joy.

So where, exactly, is the API that lets me turn this device on and off? Is there even an undocumented or unsupported way to talk to the Bluetooth driver? Am I missing something absurdly obvious like loading and unloading the kext? I'm willing to write C code, Applescript, or even delve into something obscure if it'll let me do this. Basically I'm up for anything shy of having to write my own kext from scratch, which pushes my current knowledge (which currently doesn't include more than a cursory understanding of Mach or Apple's driver architecture) and the appropriateness of the solution a bit too far.
posted by majick to Computers & Internet (6 answers total)
 
Looks like there's info on how to do it at the apple.com forums.
posted by jedrek at 11:03 AM on April 22, 2006


I'm sincerely interested to know what your results have been with keeping it turned off vs. sporadic use vs. always on. Do you have any data?
posted by kcm at 11:16 AM on April 22, 2006


Also, if you end up wanting to do this via your own program, it doesn't look like you need to go down so deep as kernel extensions. The daemon "blued" runs when Bluetooth is turned on, and it's started by the "configd" daemon -- man configd for more details. Theoretically you should be able to use "scutil" to tell configd what to do (it looks like via the key "com.apple.BluetoothMenuExtraStatus"?).
posted by xil at 11:20 AM on April 22, 2006


Or write some code using the System Configuration API.
posted by xil at 11:26 AM on April 22, 2006


Thanks, folks! The forum solution -- emitting GUI events at the existing menuextra -- is a ghastly, evil kludge that gives me hives to think about, but a hideous hack is acceptable in this case if it works where other stuff doesn't.

Sorry, kcm, I haven't done a controlled test of Bluetooth battery consumption. Seeing how Bluetooth devours cell phone batteries at two or three times normal rate was enough impetus for me to keep it largely turned off for 99% of my uptime.

I'll be investigating turning blued on and off, too, if it's blued that deals with the hardware state. Unfortunately there's zero documentation on blued itself, apart from a man page suggesting to stay the hell away from it. I have no idea if it's just the userspace interface or if it deals with power state.
posted by majick at 6:50 AM on April 23, 2006


Partial followup: The Applescript GUI event hack is fairly fragile and has the occasional timing issue, but it does -- usually -- work.

I'm still digging around in scutil to see if I can make it do things.
posted by majick at 9:56 PM on April 24, 2006


« Older Pay as you go webhosting   |   Creating an e-mail list from volunteer form... Newer »
This thread is closed to new comments.