簡體   English   中英

Python + D-Bus + BlueZ 5:無法讀取對象的屬性?

[英]Python+D-Bus+BlueZ 5: can't read property of object?

這是在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? 我怎樣才能得到這個屬性的價值呢?

以下是如何通過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"))

通過對Get方法的dbus方法調用間接訪問DBus屬性。 看到這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM