| #!/usr/bin/python |
| |
| import sys |
| import dbus |
| import dbus.mainloop.glib |
| |
| |
| def SetUnplug(path): |
| bus = dbus.SystemBus() |
| props = dbus.Interface(bus.get_object('org.bluez', path), |
| 'org.freedesktop.DBus.Properties') |
| value = dbus.Boolean(1) |
| props.Set('org.bluez.Device1', 'Unplug', value) |
| |
| |
| def getAllDevices(): |
| bus = dbus.SystemBus() |
| om = dbus.Interface(bus.get_object('org.bluez', '/'), |
| 'org.freedesktop.DBus.ObjectManager') |
| objects = om.GetManagedObjects() |
| devices = {} |
| for path, interfaces in objects.iteritems(): |
| if 'org.bluez.Device1' in interfaces: |
| devices[path] = interfaces['org.bluez.Device1'] |
| return devices |
| |
| |
| def GetAllRemotes(): |
| devices = getAllDevices() |
| remotes = [] |
| for (key, value) in devices.iteritems(): |
| name = value.get('Name', '') |
| if name == 'GFRM100': |
| remotes.append(key) |
| return remotes |
| |
| |
| def GetDevicesByAddresses(addresses): |
| devices = getAllDevices() |
| compare = [a.lower() for a in addresses] |
| remotes = [] |
| for (key, value) in devices.iteritems(): |
| a = value.get('Address', '').lower() |
| if a in compare: |
| remotes.append(key) |
| return remotes |
| |
| |
| def usage(): |
| print 'usage: %s [addr1 addr2 | all]' % sys.argv[0] |
| print ' where addr is a Bluetooth device BDADDR' |
| print ' "all" will unplug all paired GFRM100 devices.' |
| sys.exit(1) |
| |
| |
| if __name__ == '__main__': |
| dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| |
| if len(sys.argv) == 1: |
| usage() |
| if len(sys.argv) > 2 and sys.argv[1] == 'all': |
| usage() |
| |
| if sys.argv[1] == 'all': |
| remotes = GetAllRemotes() |
| else: |
| remotes = GetDevicesByAddresses(sys.argv[1:]) |
| |
| for remote in remotes: |
| SetUnplug(remote) |