Python + D-Bus + BlueZ 5:无法读取对象的属性?

时间:2014-05-05 19:39:12

标签: python linux dbus bluez

这是在Gentoo Linux上。

我正试图让BlueZ 5告诉我我的蓝牙耳机是否已连接。

我已阅读并重新阅读freedesktop.org上的D-Bus«文档»,但它似乎已经过时(或不完整,或两者兼而有之)。我也试图了解bluez.org上的微薄信息,但没有太多运气。

我尝试了以下内容:

Python 2.7.6 (default, Apr 26 2014, 11:38:54) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
>>> bus = dbus.SystemBus()
>>> obj = bus.get_object( "org.bluez", '/org/bluez/hci0/dev_00_18_91_D0_7A_24' )
>>> iface = dbus.Interface( obj, "org.bluez.Device1" )
>>> print iface.Connected
<dbus.proxies._DeferredMethod instance at 0x236e2d8>

我认为Connected是设备的属性,但它是_DeferredMethod?我怎样才能得到这个属性的值呢?

2 个答案:

答案 0 :(得分:1)

通过对Get方法的dbus方法调用间接访问DBus属性。见this

答案 1 :(得分:1)

以下是如何通过D-Bus API从蓝牙设备读取属性:

#!/usr/bin/env python3

import dbus

bus = dbus.SystemBus()
adapter_object = bus.get_object('org.bluez', '/org/bluez/hci0')
adapter = dbus.Interface(adapter_object, 'org.bluez.Adapter1')

device_object = bus.get_object("org.bluez", "/org/bluez/hci0/dev_FC_52_6E_8E_87_06")
device = dbus.Interface(device_object, "org.bluez.Device1")

device_properties = dbus.Interface(device, "org.freedesktop.DBus.Properties")
print(device_properties.Get("org.bluez.Device1", "Name"))
print(device_properties.Get("org.bluez.Device1", "Connected"))