1#!/usr/bin/python 2 3import gobject 4 5import dbus 6import dbus.mainloop.glib 7from optparse import OptionParser, make_option 8 9def device_found(address, properties): 10 print "[ " + address + " ]" 11 12 for key in properties.keys(): 13 value = properties[key] 14 if (key == "Class"): 15 print " %s = 0x%06x" % (key, value) 16 else: 17 print " %s = %s" % (key, value) 18 19def property_changed(name, value): 20 if (name == "Discovering" and not value): 21 mainloop.quit() 22 23if __name__ == '__main__': 24 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 25 26 bus = dbus.SystemBus() 27 manager = dbus.Interface(bus.get_object("org.bluez", "/"), 28 "org.bluez.Manager") 29 30 option_list = [ 31 make_option("-i", "--device", action="store", 32 type="string", dest="dev_id"), 33 ] 34 parser = OptionParser(option_list=option_list) 35 36 (options, args) = parser.parse_args() 37 38 if options.dev_id: 39 adapter_path = manager.FindAdapter(options.dev_id) 40 else: 41 adapter_path = manager.DefaultAdapter() 42 43 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), 44 "org.bluez.Adapter") 45 46 bus.add_signal_receiver(device_found, 47 dbus_interface = "org.bluez.Adapter", 48 signal_name = "DeviceFound") 49 50 bus.add_signal_receiver(property_changed, 51 dbus_interface = "org.bluez.Adapter", 52 signal_name = "PropertyChanged") 53 54 adapter.StartDiscovery() 55 56 mainloop = gobject.MainLoop() 57 mainloop.run() 58