How to pass an Android class back to C++



  • I have managed to get callbacks to C++ with this help https://www.kdab.com/qt-android-episode-5/
    This is using a simple int therefore it is not very challenging.

    With some java lines I am even able to pass back an object array. The object array is generated from a java callback object. However, when getting a java callback object what do I have to do in order to put into a callback to C++ and access it under C++?


  • Moderators

    @koahnig said in How to pass an Android class back to C++:

    what do I have to do in order to put into a callback to C++ and access it under C++?

    what exactly do you mean with "put into a callback to C++"?

    If i understood you correct, you want to pass a JAVA object to C++ an call a method on it, right?

    // JNI signature: "(Ljava/lang/Object;)V" - also could be "(Lmy/custom/Class;)V"
    void fibonaciResult(JNIEnv */*env*/, jobject /*obj*/, jobject param)
    {
        QAndroidJniObject callbackObj( param );
        callbackObj.callMethod( ... );
    }
    


  • hi @koahnig

    may be this two post will be helpful....

    1. https://forum.qt.io/topic/96836/native-android-code-with-qt
    2. https://forum.qt.io/topic/97028/to-connect-android-broadcastreceiver-onreceive-method-with-cpp/2
      1st post is for calling a java function from cpp (Cpp to java)
      2nd is for broadcasting (observer pattern).(java to cpp)


  • @raven-worx @ashajg

    Thanks guys. I guess the problem sits between keyboard and chair. :(
    It is the lack of enough understanding of java and the interface to cpp.

    I have managed to get within java callbacks of location module. The callback can be read for basic pieces int, long, double and stuff. I am able to read and send those through a callback to C++, where I am more comfortable with. I am also able to create an array of objects in java and pass this C++.

    I am digging deeper with the information you provided. Thanks for the leads.



  • The callMethod is working. I missed the forest for the trees.

    There is one remaining issue for a byte array.
    This is the Android description:
    0_1549400978595_a4bcd11e-0123-457c-96d0-a41fa5821ca7-image.png

    That is my implementation where the compiler is complaining

        jbyteArray byteArray = nObject.callMethod<jbyteArray>("getData", "()[B");
    
    ...ing.cpp:761: error: undefined reference to '_jbyteArray* QAndroidJniObject::callMethod<_jbyteArray*>(char const*, char const*, ...) const'
    

  • Moderators

    @koahnig
    try:

    QAndroidJniObject arrObj = nObject.callObjectMethod<jbyteArray>("getData", "()[B");
    jbyteArray arr = arrObj.object<jbyteArray>()
    

    (jbyteArray is of type jarray, which is of type jobject)


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.