hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

Getting started #161

Closed mcookAmazon closed 2 years ago

mcookAmazon commented 2 years ago

Hi -

I'm having a tough time figuring out the parameters required to call getRemoteObject. I'm trying to write some java code to interface with ncspot. I was able to get the code below figured out, which calls my player using the low level API, and I can code up my app to call my player this way, but I'd really like to learn how to do it the right way. I read the docs, but it's not 100% clear yet the process I'm to follow.

Could you provide an example of what I need to do?

Thank you! Mike

This code works and is able to call PlayPause on my ncspot app.

        m = new MethodCall("org.mpris.MediaPlayer2.ncspot", "/org/mpris/MediaPlayer2",  
                    "org.mpris.MediaPlayer2.Player", "PlayPause", (byte) 0,  
                    "su", "org.testname", 0);  

This code does not work:

`

        connection = DBusConnection.getConnection(DBusConnection.SESSION);

            Player mp2 = connection.getRemoteObject("org.mpris.MediaPlayer2", "/org/mpris/MediaPlayer2", Player.class);

        mp2.PlayPause();

` It produces this exception:

org.freedesktop.DBus$Error$ServiceUnknown: The name org.mpris.MediaPlayer2 was not provided by any .service files at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at org.freedesktop.dbus.Error.getException(Error.java:110) at org.freedesktop.dbus.Error.throwException(Error.java:140) at org.freedesktop.dbus.RemoteInvocationHandler.executeRemoteMethod(RemoteInvocationHandler.java:136) at org.freedesktop.dbus.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:188) at jdk.proxy1/jdk.proxy1.$Proxy1.PlayPause(Unknown Source) at TestProject.MyTest.main(MyTest.java:32)

Attached is information about the interfaces I pulled through introspection.

hypfvieh commented 2 years ago

Please use the InterfaceCodeGenerator to create proper Java interfaces for the application you want to talk to (see here: https://hypfvieh.github.io/dbus-java/code-generation.html). After creating the interfaces you can use them in addition to dbus-java to establish a connection to DBus and exchange messages, signals etc with the desired program. Examples can be found here: https://github.com/hypfvieh/sandbox/tree/master/src/main/java/com/github/hypfvieh/sandbox/dbus (e.g. take a look at the NetworkManager example)

mcookAmazon commented 2 years ago

Thank you. Very cool. I was able to get it to work using the XML file I created using the dbus-send/introspection.

I had to fool around a bit with the parameters. The command below worked for me. In the example, from the link you provided, what is the %classpath for? I had to omit it to get it to work.

Tonight, I'll try to get it working.

` mvn exec:java \

-Dexec.mainClass="org.freedesktop.dbus.utils.generator.InterfaceCodeGenerator" \

-Dexec.executable="java" \

-Dexec.args=" \

--inputFile ./interfaces.xml \

--outputDir ./output ' ' '/org/mpris/MediaPlayer2'" `

hypfvieh commented 2 years ago

%classpath is usually replaced with the classpath generated by maven when running mvn exec:java

mcookAmazon commented 2 years ago

Thank you. I was likely doing something incorrectly. With the %classpath parameter, when using the following command:

mvn exec:java \ -Dexec.mainClass="org.freedesktop.dbus.utils.generator.InterfaceCodeGenerator" \ -Dexec.executable="java" \ -Dexec.args="%classpath \ --inputFile ./interfaces.xml \ --outputDir ./output ' ' '/org/mpris/MediaPlayer2'" I get this error (I added debugs to see the command line arguments) -

`These are the args Arg: 0 value: %classpath Arg: 1 value: --inputFile Arg: 2 value: ./interfaces.xml Arg: 3 value: --outputDir Arg: 4 value: ./output Arg: 5 value:
Arg: 6 value: /org/mpris/MediaPlayer2 Syntax: [busname object] [object path] Options: --system | -y Use SYSTEM DBus --session | -s Use SESSION DBus --outputDir

| -o Use as output directory for all generated files --inputFile | -i Use as XML introspection input file instead of querying DBus

    --enable-dtd-validation          Enable DTD validation of introspection XML
    --version                        Show version information
    --help                           Show this help

If --inputFile is given busname object argument can be skipped (or can be used), that will force the util to extract all interfaces found in the given file. If busname (not empty, blank and not '') is given, then only interfaces starting with the given busname will be extracted. `

mcookAmazon commented 2 years ago

Thanks again. I have it working. I wonder if I could ask you one more favor? Could you point me to a good example of registering for DBusSignals? I have the interface that was created by the code generation, and I have a high level understanding of what I need to do, but I am missing some steps.

mcookAmazon commented 2 years ago

I got the signals working. Thanks again for your help. It was much appreciated.