【发布时间】:2020-03-08 05:08:12
【问题描述】:
正如标题所示。
我很难为这个找到一个像样的 python dbus 模块。路上有很多烟雾、镜子和陷阱。
首先,让我们看看我做了什么。
第一个陷阱是dbus-python。根本无法让它工作。连例子都坏掉了。
好像已经过时了,换成pydbus
(两者的版权所有者/作者似乎相同)
那么让我们看看 pydbus。此外,这些示例也没有那么好用:
from pydbus import SystemBus
bus = SystemBus()
dev = bus.get('.NetworkManager', 'Devices/0')
给予
KeyError: 'object does not export any interfaces; you might need to pass object path as the 2nd argument for get()'
但是,如果我们尝试与该错误消息完全相反的事情..
dev = bus.get('.NetworkManager')
我们得到了一个带有很多方法的漂亮对象:
dir(dev)
['ActivateConnection',
'ActivatingConnection',
'ActiveConnections',
'AddAndActivateConnection',
'AllDevices',
'Capabilities',
'CheckConnectivity',
'CheckPermissions',
'CheckpointCreate',
'CheckpointDestroy',
'CheckpointRollback',
'Connectivity',
....
]
到目前为止一切顺利。让我们看看我们是否可以通过那个蓝牙获得我们的手:
dev = bus.get('org.bluez')
dir(dev)
['Introspect',
'RegisterAgent',
'RegisterProfile',
'RequestDefaultAgent',
'UnregisterAgent',
'UnregisterProfile'
]
所以,又是一个带有一些方法的好对象。然而,大部分东西都不见了。让我们看一下这个例子:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
无法使用 pydbus.. 获得上述“连接”方法?
请不要建议我将 subprocess.Popen 与 bluetoothctl 一起使用 - 那是多么蹩脚。另外,我想学dbus。
问题:
- 如何访问所有蓝牙方法?
- 什么是正确的 dbus python 绑定使用(实际有效)?
- 任何工作示例都将受到高度赞赏。
- 我想用python实现bt配对
一些参考资料:
- 另一个 SO 问题:Bluez Programming
- Dbus 规格:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
- 现代 debian linux 发行版中著名的
bluetoothctl程序的源代码:https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client/main.c - 不错的 dbus 文章:https://medium.com/cesar-update/exposing-a-d-bus-interface-in-linux-part-2-90374a9e381b
编辑:
当然,还有 python bluez 库。但这是 GPL 许可的。而且没有办法将设备与 bluez 配对,对吧?
【问题讨论】: