2017-08-02 153 views
2

我在Ubuntu 16.04上並使用Python Dbus。我想將DBus上的字典列表返回給我的客戶端,但似乎只能返回一個字符串數組。如果我將我的dbus簽名裝飾器更改爲「as {v}」,則會發生異常:「ValueError:損壞類型簽名」。如何返回DBus上的字典列表?dbus-python如何返回字典數組

@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

我想通了,答案就在這裏:

@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