Skip to content

Instantly share code, notes, and snippets.

@ukBaz
Last active September 12, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukBaz/81020da3c8f16624c20305a9616637fb to your computer and use it in GitHub Desktop.
Save ukBaz/81020da3c8f16624c20305a9616637fb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
mainloop = GLib.MainLoop()
def interfaces_added(path, interfaces):
if 'org.bluez.Device1' in interfaces:
print('Device added at {}'.format(path))
Device(path)
def get_iface_prop(iface, prop):
response = {}
bus = dbus.SystemBus()
mng = dbus.Interface(bus.get_object('org.bluez', '/'),
'org.freedesktop.DBus.ObjectManager')
bus.add_signal_receiver(interfaces_added,
dbus_interface='org.freedesktop.DBus.ObjectManager',
signal_name='InterfacesAdded')
for path, ifaces in mng.GetManagedObjects().items():
if iface in ifaces:
response[path] = ifaces[iface][prop]
return response
class Device:
def __init__(self, device_path):
self.bus = dbus.SystemBus()
self.device_path = device_path
self.device_obj = self.bus.get_object('org.bluez', device_path)
device_props = dbus.Interface(self.device_obj, dbus.PROPERTIES_IFACE)
device_props.connect_to_signal('PropertiesChanged', self.on_prop_changed)
def on_prop_changed(self, iface, changed_props, invalidated_props):
if 'org.bluez.Device1' in iface:
if 'Connected' in changed_props:
print('{0} is now {1}'.format(self.device_path, changed_props['Connected']))
if __name__ == '__main__':
for k, v in get_iface_prop('org.bluez.Device1', 'Connected').items():
print('{0} is {1}'.format(k, v))
Device(k)
mainloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment