bluez/test/test-proximity
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

71 lines
1.8 KiB
Python
Executable File

#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
'''
Proximity Monitor test script
'''
from optparse import OptionParser, make_option
import sys
import dbus
import dbus.mainloop.glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject
import bluezutils
BUS_NAME = 'org.bluez'
PROXIMITY_MONITOR_INTERFACE = 'org.bluez.ProximityMonitor1'
def properties_changed(interface, changed, invalidated):
if interface != PROXIMITY_MONITOR_INTERFACE:
return
for name, value in changed.iteritems():
print("Property %s changed: %s" % (name, str(value)))
if __name__ == "__main__":
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
option_list = [
make_option("-i", "--adapter", action="store",
type="string", dest="dev_id"),
make_option("-b", "--device", action="store",
type="string", dest="address"),
]
parser = OptionParser(option_list=option_list)
(options, args) = parser.parse_args()
if (len(args) < 1):
print("Usage: %s <command>" % (sys.argv[0]))
print("")
print(" -b MAC LinkLossAlertLevel <none|mild|high>")
print(" -b MAC ImmediateAlertLevel <none|mild|high>")
sys.exit(1)
device = bluezutils.find_device(options.address, options.dev_id)
device_path = device.object_path
bus.add_signal_receiver(properties_changed, bus_name=BUS_NAME,
path=device_path,
dbus_interface="org.freedesktop.DBus.Properties",
signal_name="PropertiesChanged")
proximity = dbus.Interface(bus.get_object(BUS_NAME, device_path),
PROXIMITY_MONITOR_INTERFACE)
device_prop = dbus.Interface(bus.get_object(BUS_NAME, device_path),
"org.freedesktop.DBus.Properties")
print("Proximity SetProperty('%s', '%s')" % (args[0], args[1]))
device_prop.Set(PROXIMITY_MONITOR_INTERFACE, args[0], args[1])
mainloop = GObject.MainLoop()
mainloop.run()