Widgets, my love: Bluezwitch
Another of my single-button widgets for the Nokia N900 got into Maemo Extras this week: Bluezwitch, a simple widget to turn Bluetooth on/off (saving you 3 clicks compared to the original way). Apparently, this widget is a great thing for many users, as it already got 12′000 downloads in just six days!
For me, the main work in this was to find out on how to programmatically enable/disable Bluetooth with Bluez 4.x using D-Bus - all the examples I found online only talked about the older Bluez 3.x versions. After digging through the Bluez Docs and some experimenting, here is how it works in Python:
- Find the default Bluetooth adapter.
- Modify the “Powered” property of the adapter.
The following snippet of code toggles the adapter on/off on each run:
self.bus = dbus.SystemBus();
self.root = self.bus.get_object('org.bluez', '/')
self.manager = dbus.Interface(self.root, 'org.bluez.Manager')
self.defaultAdapter = self.manager.DefaultAdapter()
print "Default adapter is at", self.defaultAdapter
self.obj = self.bus.get_object('org.bluez', self.defaultAdapter)
self.adapter = dbus.Interface(self.obj, 'org.bluez.Adapter')
props = self.adapter.GetProperties()
powered = props['Powered']
Accidentally, “Bluezwitch” (Bluez-Switch :-) was rather a byproduct on my quest to another widget I’m currently working on: “Carmode” is a widget that toggles a number of things when entering the car. But more about that in another post of my “series”.
And BTW: From now on, I’m changing the button icon size on all my widgets from “Finger” (48px) to the “Thumb” (64px) size, as apparently the smaller icons were just too small for many people (dragging these icons became a mini-game in itself ;-). Bluezwitch has been updated today to version 1.0.0 to accommodate this change - it will show up in your favourite repository soon.