2008-03-15 03:56:30 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import gobject
|
|
|
|
|
2008-06-08 21:23:35 +08:00
|
|
|
import sys
|
2008-03-15 03:56:30 +08:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import dbus.mainloop.glib
|
|
|
|
|
2008-06-09 22:10:51 +08:00
|
|
|
class Rejected(dbus.DBusException):
|
2008-06-09 22:09:15 +08:00
|
|
|
_dbus_error_name = "org.bluez.Error.Rejected"
|
|
|
|
|
2008-03-15 03:56:30 +08:00
|
|
|
class Agent(dbus.service.Object):
|
2008-06-12 14:19:37 +08:00
|
|
|
exit_on_release = True
|
|
|
|
|
|
|
|
def set_exit_on_release(self, exit_on_release):
|
|
|
|
self.exit_on_release = exit_on_release
|
|
|
|
|
2008-03-15 03:56:30 +08:00
|
|
|
@dbus.service.method("org.bluez.Agent",
|
2008-03-15 07:44:23 +08:00
|
|
|
in_signature="", out_signature="")
|
2008-03-15 03:56:30 +08:00
|
|
|
def Release(self):
|
2008-03-15 07:44:23 +08:00
|
|
|
print "Release"
|
2008-06-12 14:19:37 +08:00
|
|
|
if self.exit_on_release:
|
|
|
|
mainloop.quit()
|
2008-03-15 03:56:30 +08:00
|
|
|
|
2008-04-11 04:52:49 +08:00
|
|
|
@dbus.service.method("org.bluez.Agent",
|
|
|
|
in_signature="os", out_signature="")
|
|
|
|
def Authorize(self, device, uuid):
|
2008-06-08 21:00:44 +08:00
|
|
|
print "Authorize (%s, %s)" % (device, uuid)
|
2009-06-17 04:36:30 +08:00
|
|
|
authorize = raw_input("Authorize connection (yes/no): ")
|
|
|
|
if (authorize == "yes"):
|
|
|
|
return
|
|
|
|
raise Rejected("Connection rejected by user")
|
2008-04-11 04:52:49 +08:00
|
|
|
|
2008-03-15 03:56:30 +08:00
|
|
|
@dbus.service.method("org.bluez.Agent",
|
2008-03-15 07:44:23 +08:00
|
|
|
in_signature="o", out_signature="s")
|
2008-05-30 22:17:42 +08:00
|
|
|
def RequestPinCode(self, device):
|
2008-06-08 21:00:44 +08:00
|
|
|
print "RequestPinCode (%s)" % (device)
|
2008-06-12 01:32:53 +08:00
|
|
|
return raw_input("Enter PIN Code: ")
|
2008-03-15 03:56:30 +08:00
|
|
|
|
2008-06-03 22:09:45 +08:00
|
|
|
@dbus.service.method("org.bluez.Agent",
|
|
|
|
in_signature="o", out_signature="u")
|
2008-06-04 16:07:24 +08:00
|
|
|
def RequestPasskey(self, device):
|
2008-06-08 21:00:44 +08:00
|
|
|
print "RequestPasskey (%s)" % (device)
|
2008-06-09 12:06:15 +08:00
|
|
|
passkey = raw_input("Enter passkey: ")
|
|
|
|
return dbus.UInt32(passkey)
|
2008-06-03 22:09:45 +08:00
|
|
|
|
|
|
|
@dbus.service.method("org.bluez.Agent",
|
|
|
|
in_signature="ou", out_signature="")
|
2008-06-04 16:07:24 +08:00
|
|
|
def DisplayPasskey(self, device, passkey):
|
2008-06-08 21:00:44 +08:00
|
|
|
print "DisplayPasskey (%s, %d)" % (device, passkey)
|
2008-06-03 22:09:45 +08:00
|
|
|
|
|
|
|
@dbus.service.method("org.bluez.Agent",
|
|
|
|
in_signature="ou", out_signature="")
|
2008-06-04 16:07:24 +08:00
|
|
|
def RequestConfirmation(self, device, passkey):
|
2008-06-08 21:00:44 +08:00
|
|
|
print "RequestConfirmation (%s, %d)" % (device, passkey)
|
2008-06-09 22:09:15 +08:00
|
|
|
confirm = raw_input("Confirm passkey (yes/no): ")
|
|
|
|
if (confirm == "yes"):
|
|
|
|
return
|
|
|
|
raise Rejected("Passkey doesn't match")
|
2008-06-03 22:09:45 +08:00
|
|
|
|
2008-03-15 05:12:25 +08:00
|
|
|
@dbus.service.method("org.bluez.Agent",
|
2008-03-15 07:44:23 +08:00
|
|
|
in_signature="s", out_signature="")
|
2008-03-15 05:12:25 +08:00
|
|
|
def ConfirmModeChange(self, mode):
|
2008-06-08 21:00:44 +08:00
|
|
|
print "ConfirmModeChange (%s)" % (mode)
|
2010-09-02 18:44:05 +08:00
|
|
|
authorize = raw_input("Authorize mode change (yes/no): ")
|
|
|
|
if (authorize == "yes"):
|
|
|
|
return
|
|
|
|
raise Rejected("Mode change by user")
|
2008-03-15 05:12:25 +08:00
|
|
|
|
2008-03-15 03:56:30 +08:00
|
|
|
@dbus.service.method("org.bluez.Agent",
|
2008-03-15 07:44:23 +08:00
|
|
|
in_signature="", out_signature="")
|
2008-03-15 03:56:30 +08:00
|
|
|
def Cancel(self):
|
2008-03-15 07:44:23 +08:00
|
|
|
print "Cancel"
|
2008-03-15 03:56:30 +08:00
|
|
|
|
2008-06-11 16:44:38 +08:00
|
|
|
def create_device_reply(device):
|
|
|
|
print "New device (%s)" % (device)
|
2008-06-12 14:19:37 +08:00
|
|
|
mainloop.quit()
|
2008-06-11 16:44:38 +08:00
|
|
|
|
|
|
|
def create_device_error(error):
|
|
|
|
print "Creating device failed: %s" % (error)
|
2008-06-12 14:19:37 +08:00
|
|
|
mainloop.quit()
|
2008-06-04 16:16:20 +08:00
|
|
|
|
2008-03-15 03:56:30 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
|
|
|
|
bus = dbus.SystemBus()
|
2008-03-15 07:44:23 +08:00
|
|
|
manager = dbus.Interface(bus.get_object("org.bluez", "/"),
|
|
|
|
"org.bluez.Manager")
|
|
|
|
|
2008-06-08 21:00:44 +08:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
path = manager.FindAdapter(sys.argv[1])
|
|
|
|
else:
|
|
|
|
path = manager.DefaultAdapter()
|
|
|
|
|
2008-03-15 07:44:23 +08:00
|
|
|
adapter = dbus.Interface(bus.get_object("org.bluez", path),
|
|
|
|
"org.bluez.Adapter")
|
2008-03-15 03:56:30 +08:00
|
|
|
|
2008-03-15 07:44:23 +08:00
|
|
|
path = "/test/agent"
|
2008-06-12 14:19:37 +08:00
|
|
|
agent = Agent(bus, path)
|
2008-03-15 03:56:30 +08:00
|
|
|
|
2008-06-08 21:00:44 +08:00
|
|
|
mainloop = gobject.MainLoop()
|
|
|
|
|
|
|
|
if len(sys.argv) > 2:
|
|
|
|
if len(sys.argv) > 3:
|
|
|
|
device = adapter.FindDevice(sys.argv[2])
|
|
|
|
adapter.RemoveDevice(device)
|
|
|
|
|
2008-06-12 14:19:37 +08:00
|
|
|
agent.set_exit_on_release(False)
|
2008-06-12 01:32:53 +08:00
|
|
|
adapter.CreatePairedDevice(sys.argv[2], path, "DisplayYesNo",
|
2008-06-04 16:16:20 +08:00
|
|
|
reply_handler=create_device_reply,
|
2008-06-11 16:44:38 +08:00
|
|
|
error_handler=create_device_error)
|
2008-06-09 20:46:25 +08:00
|
|
|
else:
|
2008-06-12 01:32:53 +08:00
|
|
|
adapter.RegisterAgent(path, "DisplayYesNo")
|
2008-06-09 20:46:25 +08:00
|
|
|
print "Agent registered"
|
2008-06-04 16:16:20 +08:00
|
|
|
|
2008-03-15 03:56:30 +08:00
|
|
|
mainloop.run()
|
|
|
|
|
|
|
|
#adapter.UnregisterAgent(path)
|
2008-03-15 07:44:23 +08:00
|
|
|
#print "Agent unregistered"
|