bluez/test/monitor-bluetooth
Petri Gynther 7adf8d09b4 test: Python import cleanup
Some test scripts use "from gi.repository import GObject" whereas others
use "import gobject". gi.repository is not always available on embedded
systems, so convert all instances to this format:

try:
  from gi.repository import GObject
except ImportError:
  import gobject as GObject

Also, sort the imports in this order: system, dbus, gobject, bluezutils
2014-01-18 00:38:39 -08:00

55 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
import dbus
import dbus.mainloop.glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject
relevant_ifaces = [ "org.bluez.Adapter1", "org.bluez.Device1" ]
def property_changed(interface, changed, invalidated, path):
iface = interface[interface.rfind(".") + 1:]
for name, value in changed.iteritems():
val = str(value)
print("{%s.PropertyChanged} [%s] %s = %s" % (iface, path, name,
val))
def interfaces_added(path, interfaces):
for iface, props in interfaces.iteritems():
if not(iface in relevant_ifaces):
continue
print("{Added %s} [%s]" % (iface, path))
for name, value in props.iteritems():
print(" %s = %s" % (name, value))
def interfaces_removed(path, interfaces):
for iface in interfaces:
if not(iface in relevant_ifaces):
continue
print("{Removed %s} [%s]" % (iface, path))
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(property_changed, bus_name="org.bluez",
dbus_interface="org.freedesktop.DBus.Properties",
signal_name="PropertiesChanged",
path_keyword="path")
bus.add_signal_receiver(interfaces_added, bus_name="org.bluez",
dbus_interface="org.freedesktop.DBus.ObjectManager",
signal_name="InterfacesAdded")
bus.add_signal_receiver(interfaces_removed, bus_name="org.bluez",
dbus_interface="org.freedesktop.DBus.ObjectManager",
signal_name="InterfacesRemoved")
mainloop = GObject.MainLoop()
mainloop.run()