python 中的 dbus 发送版本

dbus-send version in python

我有一个有效的 dbus-send 调用:

#                                   OBJECT          INTERFACE        .MEMBER  CONTENT
dbus-send --system --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.SetMode string:discoverable

现在我正在尝试在 python 中做同样的事情,但是由于文档少得可怜,尽管我尝试了所有可以想到的排列,但我得到的只是 last 步骤中的错误.

import dbus
bus = dbus.SystemBus()
hci0 = bus.get_object('org.bluez', '/org/bluez/hci0')
# everything good so far

# v1
hci0_setmode = hci0.get_dbus_method('SetMode', 'org.bluez.Adapter')
hci0_setmode('discoverable')

# v2
iface = dbus.Interface(hci0, 'org.bluez.Adapter')
iface.SetMode('discoverable')

# v3
iface = dbus.Interface(hci0, 'org.bluez.Adapter')
hci0_setmode =iface.get_dbus_method('SetMode', 'org.bluez.Adapter')
hci0_setmode('discoverable')

无论我做什么,错误是:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method "SetMode" with signature "s" on interface "org.bluez.Adapter" doesn't exist

我还没有找到一种方法来告诉我存在什么方法和什么签名,此外这个错误消息似乎与初始 dbus-send 调用相矛盾,这证明 "org.bluez.Adapter.SetMode(s)" 存在。

我通过查看 api 找到了解决方案:

dbus-send --system --dest=org.bluez --type=method_call --print-reply /org/bluez/hci0 org.freedesktop.DBus.Introspectable.Introspect

这里是 python 代码:

import dbus
bus = dbus.SystemBus()
hci0 = bus.get_object('org.bluez', '/org/bluez/hci0')
props = dbus.Interface(hci0, 'org.freedesktop.DBus.Properties')
props.Set('org.bluez.Adapter1', 'Discoverable', True)

我仍然不确定为什么初始的 dbus-send 命令甚至可以工作。我在其他地方可以找到的唯一对 SetMode 的引用是在这里:http://svn.openmoko.org/developers/erin_yueh/bt/bt_adapter.py.