I have been using copy, paste, and magic to register a Bluetooth agent via Python DBUs which works great for hci0
, but I cannot for the life of me see how I can get this agent to work for other Bluetooth controllers, i.e hci1
. I have tried selecting the controller and setting it as default in bluetoothctl
and other side channels.
Can someone please show me where the agent is associated with the controller. This is all too magical. I am also unable to find the agent or anything on it with D-Feet - How should or could I find it please?
A dumb toy example is as:
import dbus
import dbus.service
AGENT_PATH = "/org/bluez/anAgent"
class Agent(dbus.service.Object):
@dbus.service.method(AGENT_INTERFACE,
in_signature="os", out_signature="")
def AuthorizeService(self, device, uuid):
print("Some stuff and things")
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
capability = "DisplayYesNo"
agent = Agent(bus, AGENT_PATH)
obj = bus.get_object("org.bluez", "/org/bluez")
# Create the agent manager
manager = dbus.Interface(obj, "org.bluez.AgentManager1")
manager.RegisterAgent(AGENT_PATH, capability)
manager.RequestDefaultAgent(AGENT_PATH)
When Bluetooth needs an agent it looks at the
org.bluez.AgentManager1
interface at the/org/bluez
D-Bus object path for the location of the agent that has been registered. It then calls the relevant method on theorg.bluez.Agent1
interface at that registered object path (/org/bluez/anAgent
in your example). The methods that need to be implemented for theorg.bluez.Agent1
interface are documented at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/agent-api.txtThere is only one agent irrespective of how many adapters there are on your system.
Could the issue be that the device being paired is associated with the "wrong" adapter?
The Device API has an
Adapter
property which tracks the object path of the adapter the device belongs to.To find the agent that you have created it can be useful to use
busctl
. Usingbusctl list
will list all the availableSystemBus
services. As you are not claiming a bus name for the agent it will be anonymous so in the format of:x.xxx
. This can be a long list sobusctl list | grep python
is what I usually do to narrow the list down.To introspect what was there you would do something like:
To test the agent service that has been published:
This service will need to be asynchronous so you will need to start the
MainLoop
event loop.Below is a full example of creating an Agent using
pydbus
D-Bus bindings.The only place that the adapter is mentioned is the instruction to start discovery. If you had another device trying to discover your device with the agent, then it will depend on which adapter is discoverable and/or the other device tries to connect to.