Tuesday, March 16, 2010

Getting N900 Battery Level via Python & D-Bus

After experimenting with Python and Qt (PyQt) on the Nokia N900, I need to access some device capabilities.

The simplest is getting the battery level. I use D-Bus to access battery charge percentage level with Python. It's not Maemo specific, but a cross platform API that works on any Linux / FreeDesktop-compliant environment. So the code below should work on your Linux desktop too. I've tried with my Ubuntu 9.10 desktop but doesn't work, so I guess I should learn more about it.

Similar API is available through System Information API in Qt Mobility. But I'm not sure how to access Qt Mobility through Python at the moment.

#!/usr/bin/python
import dbus

bus = dbus.SystemBus()
hal_obj = bus.get_object('org.freedesktop.Hal',
  '/org/freedesktop/Hal/Manager')
hal = dbus.Interface(hal_obj,
  'org.freedesktop.Hal.Manager')
uids = hal.FindDeviceByCapability('battery')
dev_obj = bus.get_object('org.freedesktop.Hal', uids[0])

print 'battery percent', \
  dev_obj.GetProperty('battery.charge_level.percentage')

No comments:

Post a Comment

Be the first to comment!