Home | History | Annotate | Download | only in test
      1 #!/usr/bin/python
      2 
      3 import gobject
      4 
      5 import dbus.mainloop.glib
      6 
      7 def create_device_reply(device):
      8 	print "Pairing succeed!"
      9 	mainloop.quit()
     10 
     11 def create_device_error(error):
     12 	print "Pairing failed."
     13 	mainloop.quit()
     14 
     15 if __name__ == '__main__':
     16 	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
     17 
     18 	mainloop = gobject.MainLoop()
     19 
     20 	bus = dbus.SystemBus()
     21 	manager = dbus.Interface(bus.get_object("org.bluez", "/"),
     22 							"org.bluez.Manager")
     23 
     24 	adapter0_path = manager.FindAdapter("hci0")
     25 	adapter1_path = manager.FindAdapter("hci1")
     26 
     27 	adapter0 = dbus.Interface(bus.get_object("org.bluez", adapter0_path),
     28 							"org.bluez.Adapter")
     29 	adapter1 = dbus.Interface(bus.get_object("org.bluez", adapter1_path),
     30 							"org.bluez.Adapter")
     31 
     32 	adapter0_address = adapter0.GetProperties()["Address"]
     33 	adapter1_address = adapter1.GetProperties()["Address"]
     34 	print "Adapters:"
     35 	print "    hci0: " + adapter0_address
     36 	print "    hci1: " + adapter1_address
     37 	print
     38 
     39 	print "Removing any existing bond..."
     40 
     41 	try:
     42 		device = adapter0.FindDevice(adapter1_address)
     43 		adapter0.RemoveDevice(device)
     44 	except:
     45 		pass
     46 
     47 	try:
     48 		device = adapter1.FindDevice(adapter0_address)
     49 		adapter1.RemoveDevice(device)
     50 	except:
     51 		pass
     52 
     53 	print "Done."
     54 	print
     55 	print "Reading local Out of Band data..."
     56 
     57 	oob_adapter0 = dbus.Interface(bus.get_object("org.bluez",
     58 					adapter0_path), "org.bluez.OutOfBand")
     59 	oob_adapter1 = dbus.Interface(bus.get_object("org.bluez",
     60 					adapter1_path), "org.bluez.OutOfBand")
     61 
     62 	oob0 = oob_adapter0.ReadLocalData()
     63 	oob1 = oob_adapter1.ReadLocalData()
     64 
     65 	print "Done."
     66 	print
     67 	print "Exchanging Out of Band data..."
     68 
     69 	oob_adapter0.AddRemoteData(adapter1_address, oob1[0], oob1[1])
     70 	oob_adapter1.AddRemoteData(adapter0_address, oob0[0], oob0[1])
     71 
     72 	print "Done."
     73 	print
     74 	print "Starting to pair."
     75 	adapter1.CreatePairedDevice(adapter0_address, "/test/agent_oob",
     76 					"DisplayYesNo",
     77 					reply_handler=create_device_reply,
     78 					error_handler=create_device_error)
     79 
     80 	mainloop.run()
     81