2011-04-08 17:37:54 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
2011-04-08 17:37:54 +08:00
|
|
|
import gobject
|
|
|
|
|
|
|
|
import dbus.mainloop.glib
|
|
|
|
|
|
|
|
def create_device_reply(device):
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Pairing succeed!")
|
2011-04-08 17:37:54 +08:00
|
|
|
mainloop.quit()
|
|
|
|
|
|
|
|
def create_device_error(error):
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Pairing failed.")
|
2011-04-08 17:37:54 +08:00
|
|
|
mainloop.quit()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
|
|
|
|
mainloop = gobject.MainLoop()
|
|
|
|
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
manager = dbus.Interface(bus.get_object("org.bluez", "/"),
|
|
|
|
"org.bluez.Manager")
|
|
|
|
|
|
|
|
adapter0_path = manager.FindAdapter("hci0")
|
|
|
|
adapter1_path = manager.FindAdapter("hci1")
|
|
|
|
|
|
|
|
adapter0 = dbus.Interface(bus.get_object("org.bluez", adapter0_path),
|
|
|
|
"org.bluez.Adapter")
|
|
|
|
adapter1 = dbus.Interface(bus.get_object("org.bluez", adapter1_path),
|
|
|
|
"org.bluez.Adapter")
|
|
|
|
|
|
|
|
adapter0_address = adapter0.GetProperties()["Address"]
|
|
|
|
adapter1_address = adapter1.GetProperties()["Address"]
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Adapters:")
|
|
|
|
print(" hci0: " + adapter0_address)
|
|
|
|
print(" hci1: " + adapter1_address)
|
|
|
|
print()
|
2011-04-08 17:37:54 +08:00
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Removing any existing bond...")
|
2011-04-08 17:37:54 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
device = adapter0.FindDevice(adapter1_address)
|
|
|
|
adapter0.RemoveDevice(device)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
device = adapter1.FindDevice(adapter0_address)
|
|
|
|
adapter1.RemoveDevice(device)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Done.")
|
|
|
|
print()
|
|
|
|
print("Reading local Out of Band data...")
|
2011-04-08 17:37:54 +08:00
|
|
|
|
|
|
|
oob_adapter0 = dbus.Interface(bus.get_object("org.bluez",
|
|
|
|
adapter0_path), "org.bluez.OutOfBand")
|
|
|
|
oob_adapter1 = dbus.Interface(bus.get_object("org.bluez",
|
|
|
|
adapter1_path), "org.bluez.OutOfBand")
|
|
|
|
|
|
|
|
oob0 = oob_adapter0.ReadLocalData()
|
|
|
|
oob1 = oob_adapter1.ReadLocalData()
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Done.")
|
|
|
|
print()
|
|
|
|
print("Exchanging Out of Band data...")
|
2011-04-08 17:37:54 +08:00
|
|
|
|
2012-08-13 18:46:40 +08:00
|
|
|
oob_adapter0.AddRemoteData(adapter1_address, oob1)
|
|
|
|
oob_adapter1.AddRemoteData(adapter0_address, oob0)
|
2011-04-08 17:37:54 +08:00
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Done.")
|
|
|
|
print()
|
|
|
|
print("Starting to pair.")
|
2011-04-08 17:37:54 +08:00
|
|
|
adapter1.CreatePairedDevice(adapter0_address, "/test/agent_oob",
|
|
|
|
"DisplayYesNo",
|
|
|
|
reply_handler=create_device_reply,
|
|
|
|
error_handler=create_device_error)
|
|
|
|
|
|
|
|
mainloop.run()
|