我正在try 从dbus读取树莓Pi的蓝牙地址.我可以从命令行使用busctl来完成这项工作,如下所述,但是看起来应该运行相同过程的Python代码失败了.从dbus读取其他属性时,我也会遇到同样的失败.我可以通过将hciconfig作为子进程运行的不令人满意的方法获得地址,但是Python代码有什么问题吗?

1. List dbus bluez with busctl 

  busctl introspect org.bluez /org/bluez/hci0
  produces this (edited) result:

  NAME                                TYPE      SIGNATURE RESULT/VALUE           FLAGS
  org.bluez.Adapter1                  interface -         -                      -
  .GetDiscoveryFilters                method    -         as                     -
  .Address                            property  s         "DC:A6:32:04:DB:56"    emits-change
  .AddressType                        property  s         "public"               emits-change
  org.freedesktop.DBus.Introspectable interface -         -                      -
  .Introspect                         method    -         s                      -
  org.freedesktop.DBus.Properties     interface -         -                      -
  .Get                                method    ss        v                      -
  .GetAll                             method    s         a{sv}                  -
  .Set                                method    ssv       -                      -
  .PropertiesChanged                  signal    sa{sv}as  -                      -


2. Read Address with busctl

  busctl get-property org.bluez /org/bluez/hci0 org.bluez.Adapter1 Address

  produces this correct result:
    s "DC:A6:32:04:DB:56"

  this is from dbus-monitor:
  
  method call time=1653300359.259826 sender=:1.43 ->
       destination=org.bluez serial=2 
       path=/org/bluez/hci0;
       interface=org.freedesktop.DBus.Properties;
       member=Get
          string "org.bluez.Adapter1"
          string "Address"
  method return time=1653300359.259940 sender=:1.8 ->
     destination=:1.43 serial=85 reply_serial=2
     variant       string "DC:A6:32:04:DB:56"



3. Python3 code to do the same thing as follows:

  bus = dbus.SystemBus() 
  obj = bus.get_object("org.bluez","/org/bluez/hci0") 
  interface = dbus.Interface(obj,"org.freedesktop.Dbus.Properties")
  address = interface.Get("org.bluez.Adapter1","Address")

  produces this result:
  
  bus = runs OK
  obj = runs OK
  interface = runs OK
  address = fails with following error:  
    Method "Get" with signature "ss" on interface "org.freedesktop.DBus.Properties"  doesn't exist

  BUT IT DOES! and it works for busctl 

  address = interface.Get()
  address = interface.Get("org.bluez.Adapter1")
  address = interface.GetAll()
  address = interface.GetAll("org.bluez.Adapter1")
  address = obj.Get("org.bluez.Adapter1","Address",dbus_interface="org.freedesktop.Dbus.Properties")
    also fail with the same error 

推荐答案

这一行代码:

interface = dbus.Interface(obj,"org.freedesktop.Dbus.Properties")

实际上应该是

interface = dbus.Interface(obj,"org.freedesktop.DBus.Properties")

Python相关问答推荐

处理具有多个独立头的CSV文件

ruamel.yaml dump:如何阻止map标量值被移动到一个新的缩进行?

导入错误:无法导入名称';操作';

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

Odoo16:模板中使用的docs变量在哪里定义?

应用指定的规则构建数组

你能把函数的返回类型用作其他地方的类型吗?'

使用SQLAlchemy从多线程Python应用程序在postgr中插入多行的最佳方法是什么?'

在第一次调用时使用不同行为的re. sub的最佳方式

极柱内丢失类型信息""

在任何要保留的字段中添加引号的文件,就像在Pandas 中一样

关于数字S种子序列内部工作原理的困惑

用LAKEF划分实木地板AWS Wrangler

pyspark where子句可以在不存在的列上工作

在行数据为向量的DataFrame上计算逐行更改

为什么Python多处理.Process()传递队列参数并且读取比函数传递队列参数和读取更快?

极点中的链接表达式不起作用

修复如何使用python排序方法对列表中的元素进行排序

如何添加脊椎箭头和偏移脊椎

Python字符串包含不能与方括号一起使用