bluez/test/simple-agent

121 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/python
import gobject
2008-06-08 21:23:35 +08:00
import sys
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"
class Agent(dbus.service.Object):
exit_on_release = True
def set_exit_on_release(self, exit_on_release):
self.exit_on_release = exit_on_release
@dbus.service.method("org.bluez.Agent",
2008-03-15 07:44:23 +08:00
in_signature="", out_signature="")
def Release(self):
2008-03-15 07:44:23 +08:00
print "Release"
if self.exit_on_release:
mainloop.quit()
@dbus.service.method("org.bluez.Agent",
in_signature="os", out_signature="")
def Authorize(self, device, uuid):
print "Authorize (%s, %s)" % (device, uuid)
authorize = raw_input("Authorize connection (yes/no): ")
if (authorize == "yes"):
return
raise Rejected("Connection rejected by user")
@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):
print "RequestPinCode (%s)" % (device)
return raw_input("Enter PIN Code: ")
@dbus.service.method("org.bluez.Agent",
in_signature="o", out_signature="u")
2008-06-04 16:07:24 +08:00
def RequestPasskey(self, device):
print "RequestPasskey (%s)" % (device)
2008-06-09 12:06:15 +08:00
passkey = raw_input("Enter passkey: ")
return dbus.UInt32(passkey)
@dbus.service.method("org.bluez.Agent",
in_signature="ou", out_signature="")
2008-06-04 16:07:24 +08:00
def DisplayPasskey(self, device, passkey):
print "DisplayPasskey (%s, %d)" % (device, passkey)
@dbus.service.method("org.bluez.Agent",
in_signature="ou", out_signature="")
2008-06-04 16:07:24 +08:00
def RequestConfirmation(self, device, passkey):
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")
@dbus.service.method("org.bluez.Agent",
2008-03-15 07:44:23 +08:00
in_signature="s", out_signature="")
def ConfirmModeChange(self, mode):
print "ConfirmModeChange (%s)" % (mode)
authorize = raw_input("Authorize mode change (yes/no): ")
if (authorize == "yes"):
return
raise Rejected("Mode change by user")
@dbus.service.method("org.bluez.Agent",
2008-03-15 07:44:23 +08:00
in_signature="", out_signature="")
def Cancel(self):
2008-03-15 07:44:23 +08:00
print "Cancel"
2008-06-11 16:44:38 +08:00
def create_device_reply(device):
print "New device (%s)" % (device)
mainloop.quit()
2008-06-11 16:44:38 +08:00
def create_device_error(error):
print "Creating device failed: %s" % (error)
mainloop.quit()
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")
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 07:44:23 +08:00
path = "/test/agent"
agent = Agent(bus, path)
mainloop = gobject.MainLoop()
if len(sys.argv) > 2:
if len(sys.argv) > 3:
device = adapter.FindDevice(sys.argv[2])
adapter.RemoveDevice(device)
agent.set_exit_on_release(False)
adapter.CreatePairedDevice(sys.argv[2], path, "DisplayYesNo",
reply_handler=create_device_reply,
2008-06-11 16:44:38 +08:00
error_handler=create_device_error)
else:
adapter.RegisterAgent(path, "DisplayYesNo")
print "Agent registered"
mainloop.run()
#adapter.UnregisterAgent(path)
2008-03-15 07:44:23 +08:00
#print "Agent unregistered"