import dbus import dbus.service import dbus.mainloop.glib from gi.repository import GLib import logging as log class Agent(dbus.service.Object): @dbus.service.method("org.bluez.Agent1", in_signature="", out_signature="") def Cancel(self): log.debug("Agent.Cancel") class Profile(dbus.service.Object): @dbus.service.method("org.bluez.Profile1", in_signature="", out_signature="") def Cancel(self): print("Profile.Cancel") def agent_loop(target_path): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) loop = GLib.MainLoop() bus = dbus.SystemBus() path = "/test/agent" agent = Agent(bus, path) agent.target_path = target_path obj = bus.get_object("org.bluez", "/org/bluez") manager = dbus.Interface(obj, "org.bluez.AgentManager1") manager.RegisterAgent(path, "NoInputNoOutput") manager.RequestDefaultAgent(path) log.debug("'NoInputNoOutput' pairing-agent is running") loop.run() def register_hid_profile(iface, addr): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = dbus.SystemBus() get_obj = lambda path, iface: dbus.Interface(bus.get_object("org.bluez", path), iface) addr_str = addr.replace(":", "_") path = "/org/bluez/%s/dev_%s" % (iface, addr_str) manager = get_obj("/org/bluez", "org.bluez.ProfileManager1") profile_path = "/test/profile" profile = Profile(bus, profile_path) hid_uuid = "00001124-0000-1000-8000-00805F9B34FB" # Hardcoded XML content xml_content = """ """ opts = {"ServiceRecord": xml_content} log.debug("calling RegisterProfile") manager.RegisterProfile(profile, hid_uuid, opts) loop = GLib.MainLoop() try: log.debug("running dbus loop") loop.run() except KeyboardInterrupt: log.debug("calling UnregisterProfile") manager.UnregisterProfile(profile)