2

我在 Ubuntu 16.04 上并使用 Python Dbus。我想通过 DBus 向我的客户返回一个字典列表,但似乎只能返回一个字符串数组。如果我将 dbus 签名装饰器更改为“as{v}”,则会出现异常:“ValueError: Corrupt type signature”。如何通过 DBus 返回字典列表?

   @dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='as')
   def getScanList(self):
      btMsg("Starting BT Scan List...")
      # Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
      self.discoveredDevs = self.getScannedDevices()
      returnList = []
      for dev in self.discoveredDevs:
          returnList.append(dev["name"])
      return returnList

编辑:这也不起作用:

   @dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='a{sv}')
   def getScanList(self):
      btMsg("Starting BT Scan List...")
      # Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
      self.discoveredDevs = self.getScannedDevices()
      returnList = dbus.Array()
      for dev in self.discoveredDevs:
          btMsg(dev)
          returnList.append(dbus.Dictionary(dev, signature='sv'))
      return returnList
4

1 回答 1

1

我想通了,答案在这里:

   @dbus.service.method("com.example.service.BtPairedList", in_signature='', out_signature='aa{ss}')
   def getPairedList(self):
      btMsg("Starting BT Paired List...")
      # Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
      self.pairedDevs = self.getPairedDevices()
      returnList = dbus.Array()
      for dev in self.pairedDevs:
          btMsg(dev)
          returnList.append(dbus.Dictionary(dev, signature='sv'))
      return returnList
于 2017-08-02T12:48:57.573 回答