DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Chmouel has posted 1 posts at DZone. View Full User Profile

Kill Switch For Bluetooth Device

11.17.2009
| 3792 views |
  • submit to reddit
        Ripped from http://blog.homac.de/?p=153 to make a cli enable/disable/get client.

Adjust your HAL_PATH to your device....

#!/usr/bin/python
import dbus
HAL_PATH = "/org/freedesktop/Hal/devices/dell_bluetooth_switch"

# Copied from http://blog.homac.de/?p=153
class KSADBus:
    def __init__(self):
        from dbus.mainloop.glib import DBusGMainLoop

        dbus_loop = DBusGMainLoop()

        self.bus = dbus.SystemBus(mainloop=dbus_loop)

    def get_object(self, dest, udi):
        return self.bus.get_object(dest, udi)
    
    def listen_to_signal(self, cb, signal, iface, destination, udi):
        self.bus.add_signal_receiver(cb,
                                     signal,
                                     iface,
                                     destination,
                                     udi, path_keyword="path")
class Hal(KSADBus):
    def __init__(self):
        KSADBus.__init__(self)

        self.hal_manager = self.bus.get_object('org.freedesktop.Hal',
                                               '/org/freedesktop/Hal/Manager')

        self.hal_manager_iface = dbus.Interface(self.hal_manager,
                                                dbus_interface='org.freedesktop.Hal.Manager')

    def get_property(self, udi, key):
        manager = self.bus.get_object('org.freedesktop.Hal',
                                      udi)
        iface = dbus.Interface(manager,
                               dbus_interface='org.freedesktop.Hal.Device')
        if not iface.PropertyExists(key):
            return False
        return iface.GetProperty(key)

    def get_kswitch_devices(self):
        return self.hal_manager_iface.FindDeviceByCapability("killswitch")

    def get_state(self, udi):
        manager = self.bus.get_object('org.freedesktop.Hal',
                                      udi)
        manager_interface = dbus.Interface(manager,
                                           dbus_interface='org.freedesktop.Hal.Device.KillSwitch')
        return manager_interface.GetPower()

    def set_state(self, udi, state):
        manager = self.bus.get_object('org.freedesktop.Hal',
                                      udi)
        manager_interface = dbus.Interface(manager,
                                           dbus_interface='org.freedesktop.Hal.Device.KillSwitch')
        manager_interface.SetPower(state)        

if __name__ == '__main__':
    import sys
    if len(sys.argv) != 2 or sys.argv[1] not in ("0", "1", "get"):
        print "Usage: bluetooth-switch 0 / 1 / get"
        sys.exit(1)

    arg = sys.argv[1]
    bus = Hal()

    if arg in ("0", "1"):
        bus.set_state(HAL_PATH, int(arg))
    elif arg in ("get"):
        print bus.get_state(HAL_PATH) and "enabled" or "disabled"
        
    #main()