我一直在使用复制、粘贴和魔法通过Python注册蓝牙代理,这对hci0非常有用,但我无法理解如何让该代理为其他蓝牙控制器(即hci1 )工作。我已经尝试选择控制器,并将其设置为bluetoothctl和其他侧通道中的默认设置。
有人能告诉我代理与控制器的关联在哪里吗?这太神奇了。我也找不到代理人或任何东西与D-脚-我应该如何或可以找到它吗?
一个愚蠢的玩具例子是:
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)发布于 2022-01-29 13:44:55
当蓝牙需要代理时,它会查看/org/bluez D总线对象路径上的/org/bluez接口,以查找已注册的代理的位置。然后,它在注册的对象路径上调用org.bluez.Agent1接口上的相关方法(在您的示例中是/org/bluez/anAgent)。需要为org.bluez.Agent1接口实现的方法记录在:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/agent-api.txt
不管您的系统上有多少适配器,只有一个代理。
问题是,正在配对的设备与“错误”适配器相关吗?
设备API具有一个Adapter属性,该属性跟踪设备所属适配器的对象路径。
要找到您创建的代理,使用busctl是很有用的。使用busctl list将列出所有可用的SystemBus服务。由于您没有为代理声明总线名称,所以它将是匿名的,所以是:x.xxx格式的。这可能是一个很长的列表,所以busctl list | grep python是我通常用来缩小列表范围的方法。
反思一下,你会做这样的事情:
$ busctl introspect :1.174 /org/bluez/anAgent要测试已发布的代理服务,请执行以下操作:
$ busctl call :1.174 /org/bluez/anAgent org.bluez.Agent1 AuthorizeService os /org/bluez/hci1/dev_12_34_56_78 1234这个服务需要是异步的,所以您需要启动MainLoop事件循环。
下面是使用pydbus D总线绑定创建代理的完整示例.
提到适配器的唯一地方是开始发现的指令。如果您有另一个设备试图使用代理来发现您的设备,那么它将取决于可发现的适配器和/或其他设备试图连接到的适配器。
import threading
import pydbus
from gi.repository import GLib
BUS_NAME = 'org.bluez'
AGENT_IFACE = 'org.bluez.Agent1'
AGNT_MNGR_IFACE = 'org.bluez.AgentManager1'
ADAPTER_IFACE = 'org.bluez.Adapter1'
AGENT_PATH = '/org/bluez/anAgent'
AGNT_MNGR_PATH = '/org/bluez'
DEVICE_IFACE = 'org.bluez.Device1'
CAPABILITY = 'KeyboardDisplay'
bus = pydbus.SystemBus()
class Agent:
"""
<node>
<interface name="org.bluez.Agent1">
<method name="Release" />
<method name="RequestPinCode">
<arg name="device" direction="in" type="o" />
<arg name="pincode" direction="out" type="s" />
</method>
<method name="DisplayPinCode">
<arg name="device" direction="in" type="o" />
<arg name="pincode" direction="in" type="s" />
</method>
<method name="RequestPasskey">
<arg name="device" direction="in" type="o" />
<arg name="passkey" direction="out" type="u" />
</method>
<method name="DisplayPasskey">
<arg name="device" direction="in" type="o" />
<arg name="passkey" direction="in" type="u" />
<arg name="entered" direction="in" type="q" />
</method>
<method name="RequestConfirmation">
<arg name="device" direction="in" type="o" />
<arg name="passkey" direction="in" type="u" />
</method>
<method name="RequestAuthorization">
<arg name="device" direction="in" type="o" />
</method>
<method name="AuthorizeService">
<arg name="device" direction="in" type="o" />
<arg name="uuid" direction="in" type="s" />
</method>
<method name="Cancel" />
</interface>
</node>
"""
def Release(self):
print('Release')
def RequestPinCode(self, device):
print('RequestPinCode', device)
return '1234'
def DisplayPinCode(self, device, pincode):
print('DisplayPinCode', device, pincode)
def RequestPasskey(self, device):
print('RequestPasskey', device)
return 1234
def DisplayPasskey(self, device, passkey, entered):
print('DisplayPasskey', device, passkey, entered)
def RequestConfirmation(self, device, passkey):
print('RequestConfirmation', device, passkey)
def RequestAuthorization(self, device):
print('RequestAuthorization', device)
def AuthorizeService(self, device, uuid):
print('AuthorizeService', device, uuid)
def Cancel(self):
return
def pair_reply(*args):
print('reply', args)
def pair_error(*args):
print('error', args)
def dbus_path_up(dbus_obj):
return '/'.join(dbus_obj.split('/')[:-1])
def device_found(dbus_obj, properties):
adapter = bus.get(BUS_NAME, dbus_path_up(dbus_obj))
device = bus.get(BUS_NAME, dbus_obj)
print('Stopping discovery')
adapter.StopDiscovery()
if device.Paired:
device.Connect()
else:
print('Pairing procedure starting...')
device.Pair()
def interface_added(path, ifaces):
if DEVICE_IFACE in ifaces.keys():
dev_name = ifaces[DEVICE_IFACE].get('Name')
print('Device found:', dev_name)
if dev_name == 'HC-06':
device_found(path, ifaces[DEVICE_IFACE])
def publish_agent():
bus.register_object(AGENT_PATH, Agent(), None)
aloop = GLib.MainLoop()
aloop.run()
print('Agent Registered')
def create_agent():
thread = threading.Thread(target=publish_agent, daemon=True)
thread.start()
print('Agent running...')
def my_app(hci_idx=0):
adapter_path = f'/org/bluez/hci{hci_idx}'
mngr = bus.get(BUS_NAME, '/')
mngr.onInterfacesAdded = interface_added
create_agent()
agnt_mngr = bus.get(BUS_NAME, AGNT_MNGR_PATH)[AGNT_MNGR_IFACE]
agnt_mngr.RegisterAgent(AGENT_PATH, CAPABILITY)
print('Agent registered...')
adapter = bus.get(BUS_NAME, adapter_path)
# adapter.StartDiscovery()
mainloop = GLib.MainLoop()
try:
mainloop.run()
except KeyboardInterrupt:
mainloop.quit()
adapter.StopDiscovery()
if __name__ == '__main__':
my_app()https://stackoverflow.com/questions/70903233
复制相似问题