monitor-bluetooth 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/python
  2. # SPDX-License-Identifier: LGPL-2.1-or-later
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import dbus
  5. import dbus.mainloop.glib
  6. try:
  7. from gi.repository import GObject
  8. except ImportError:
  9. import gobject as GObject
  10. relevant_ifaces = [ "org.bluez.Adapter1", "org.bluez.Device1" ]
  11. def property_changed(interface, changed, invalidated, path):
  12. iface = interface[interface.rfind(".") + 1:]
  13. for name, value in changed.iteritems():
  14. val = str(value)
  15. print("{%s.PropertyChanged} [%s] %s = %s" % (iface, path, name,
  16. val))
  17. def interfaces_added(path, interfaces):
  18. for iface, props in interfaces.iteritems():
  19. if not(iface in relevant_ifaces):
  20. continue
  21. print("{Added %s} [%s]" % (iface, path))
  22. for name, value in props.iteritems():
  23. print(" %s = %s" % (name, value))
  24. def interfaces_removed(path, interfaces):
  25. for iface in interfaces:
  26. if not(iface in relevant_ifaces):
  27. continue
  28. print("{Removed %s} [%s]" % (iface, path))
  29. if __name__ == '__main__':
  30. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  31. bus = dbus.SystemBus()
  32. bus.add_signal_receiver(property_changed, bus_name="org.bluez",
  33. dbus_interface="org.freedesktop.DBus.Properties",
  34. signal_name="PropertiesChanged",
  35. path_keyword="path")
  36. bus.add_signal_receiver(interfaces_added, bus_name="org.bluez",
  37. dbus_interface="org.freedesktop.DBus.ObjectManager",
  38. signal_name="InterfacesAdded")
  39. bus.add_signal_receiver(interfaces_removed, bus_name="org.bluez",
  40. dbus_interface="org.freedesktop.DBus.ObjectManager",
  41. signal_name="InterfacesRemoved")
  42. mainloop = GObject.MainLoop()
  43. mainloop.run()