2017-01-07 49 views
1

我想用我的覆盆子pi爲我的車設置藍牙音頻。我從我的手機播放音樂,並設置了2個GPIO按鈕,它們使用DBus消息向前或向後移動曲目。我想要一個顯示當前歌曲播放的屏幕,但我遇到了麻煩。是否有可能「超載」dbus獲取屬性命令?

使用DBUS我可以發出此命令:

的dbus-發送--system --type = method_call --print回覆--dest = org.bluez /組織/ bluez的/ hci0/dev_DC_41_5F_17_4C_79/player0 org.freedesktop.DBus.Properties.Get字符串:org.bluez.MediaPlayer1字符串:跟蹤

它返回這個 「變異體」

variant  array [ 
    dict entry(
     string "Item" 
     variant    object path "/org/bluez/hci0/dev_DC_41_5F_17_4C_79/player0/NowPlaying/item751498629074736430" 
    ) 
    dict entry(
     string "Album" 
     variant    string "Horse Of A Different Color" 
    ) 
    dict entry(
     string "TrackNumber" 
     variant    uint32 1 
    ) 
    dict entry(
     string "Genre" 
     variant    string "Country" 
    ) 
    dict entry(
     string "Duration" 
     variant    uint32 173061 
    ) 
    dict entry(
     string "NumberOfTracks" 
     variant    uint32 50 
    ) 
    dict entry(
     string "Title" 
     variant    string "Drinkin' 'Bout You" 
    ) 
    dict entry(
     string "Artist" 
     variant    string "Big & Rich" 
    ) 
    ] 

我想什麼做的是隻歌曲的標題將被退回。我嘗試輸入「標題」而不是「軌道」,還在命令'string:Title'的末尾添加了另一個運算符,希望它能縮小信息範圍。但我沒有運氣。

任何人都可以闡明我如何去顯示標題嗎? 謝謝

回答

0

我不確定有一種方法可以讀取程序外的變體。如果沒有,你需要建立一個小程序來實現你想要做的事情。

變體是一個容器,你尋求的信息是在這個變體中。你的變體的類型是{sv},這意味着它是一個字典{鍵,值},其中鍵是字符串,值是變體(v)。

下面的代碼將解析變種(使用GLib的GDBus API):

/* Call the method that will return your variant dictionary */ 
GVariant *returnValue = MethodCallThatWillReturnTheDictionary(); 

/* This will be used to iterate through the dict */ 
GVariantIter *iter_dict; 

/* These two will be used to store the key and value pair in the dict */ 
const gchar *key; 
GVariant value; 

/* Init the iterator */ 
g_variant_get(returnValue, "a{sv}", &iter_dict); 

/* Iterate through the dict */ 
while(g_variant_iter_loop(iter_dict, "{&sv}", &key, &value)){ 

    /* Each time we iterate, check if the key is "Title" */ 
    if(! strcmp(key, "Title")){ 

     /* You now know that the Title is inside the "value" variant 
      We still have to extract it */ 

     int title_length /* This will receive the Title length */ 
     const gchar *yourTitle = g_variant_get_string(value, &title_length); 

    } 
} 
g_variant_iter_free(iter_dict); /* We don't need it anymore */ 

你能得到關於變種這裏的更多信息:

https://developer.gnome.org/glib/stable/glib-GVariant.html

這裏:

https://developer.gnome.org/glib/stable/gvariant-format-strings.html

如果您不確定DBus和GDBus(DBus的GLib綁定),可以在下面的鏈接中閱讀更多內容,查找低級別和高級別D-Bus支持。在你的情況下,你需要GDBusConnection和GDBusProxy:

創建到總線的連接,然後使用您在dbus-send中使用的名稱,路徑和接口名稱構建代理。然後使用我提供的代碼示例來提取標題。

https://developer.gnome.org/gio/stable/

相關問題