Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register dbus object using dbus-java?

Tags:

java

linux

dbus

I'm trying to register object on dbus using dbus-java from org.freedesktop. According to documentation such operation requires: creating DBusConnection, creating object and calling method exportObject on DBusConnection.

My Code:

DatabaseAccessImpl dbAccess = new DatabaseAccessImpl();

    System.out.println("Object created.");



    System.out.println("Trying to connect session bus");
    try {
        conn = DBusConnection.getConnection(DBusConnection.SESSION);
    } catch(DBusException ex) {
        ex.printStackTrace();
    }

    System.out.println("Connected to session bus.");

    System.out.println("Trying to register dbus object");

    try {
        conn.exportObject("/obj/path", new DatabaseAccessImpl());
    } catch (DBusException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

where:

DatabaseAccessImpl is implementation of interface created automatically by CreateInterface (xml worked fine while using with c++ (qt)).

dbAccess will send on dbus some data (mostly ints) from database. It all work good in Qt but I have to reimplement it in java (not my language).

For using dbus-java you have to install libmatthew-debug and execute program with

-Djava.library.path=/usr/lib/jni

I'm working on Linux Mint.

like image 470
user2140451 Avatar asked Jul 25 '26 17:07

user2140451


1 Answers

Two things:

  1. You need to register a bus name first, i.e. conn.requestBustName("com.example")
  2. DatabaseAccessImpl needs to implement an interface which extends DBusInterface.

Here's a tutorial that I made a while ago with code that may help you understand what's going on.

like image 88
rm5248 Avatar answered Jul 27 '26 07:07

rm5248



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!