dbus-python如何返回字典数组

时间:2017-08-02 12:22:53

标签: python linux python-2.7 dbus dbus-python

我在Ubuntu 16.04上使用Python Dbus。我想将DBus上的字典列表返回给我的客户端,但似乎只能返回一个字符串数组。如果我将我的dbus签名装饰器改为'作为{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

1 个答案:

答案 0 :(得分: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