Search code examples
javadbus

How to send a{sv} with java through DBus?


My goal is to send a dictionary of key - valuees on DBus, receive it and process it.

I created the dictionary and added some values:

        Map<String, Variant<?>> argumentsToSend = new HashMap<String, Variant<?>>();        
        argumentsToSend.put("arg1", new Variant<Integer>(111));
        argumentsToSend.put("arg2", new Variant<Integer>(222));

        proxy.getObject().useTheseArgs(argumentsToSend);

The dictionary is sent but on the receiving side it is seen as {sv} not as a{sv}.

(process:10144): GLib-CRITICAL **: the GVariant format string 'a{sv}' has a type of 'a{sv}' but the given value has a type of '{sv}'

What am I missing ?


Solution

  • To determine the type of the message that you're sending, you need to decode the signature(the a{sv} that you have). Here's a quick breakdown:

    a = array(list)

    {sv} = a dictionary of strings to variants

    So since we have a{sv} as our signature, and dbus-java converts java.util.list to a DBus array, all you should have to do is to change your code to look something like the following:

        List<Map<String, Variant<?>>> argumentsToSend = new ArrayList<>();
        argumentsToSend.add( new HashMap<String, Variant<?>>() );
        argumentsToSend.add( new HashMap<String, Variant<?>>() );
        argumentsToSend.get(0).put("arg1", new Variant<Integer>(111));
        argumentsToSend.get(1).put("arg2", new Variant<Integer>(222));
    
        proxy.getObject().useTheseArgs(argumentsToSend);
    

    More information on types(which also includes an a{sv} example): https://rm5248.com/d-bus-tutorial/