Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to pass an Android class back to C++
Forum Updated to NodeBB v4.3 + New Features

How to pass an Android class back to C++

Scheduled Pinned Locked Moved Solved Mobile and Embedded
androidandroidcallbackobject callback
6 Posts 3 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by koahnig
    #1

    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++?

    Vote the answer(s) that helped you to solve your issue(s)

    raven-worxR 1 Reply Last reply
    0
    • K koahnig

      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++?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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( ... );
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • ashajgA Offline
        ashajgA Offline
        ashajg
        wrote on last edited by
        #3

        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)
        1 Reply Last reply
        2
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by koahnig
            #5

            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'
            

            Vote the answer(s) that helped you to solve your issue(s)

            raven-worxR 1 Reply Last reply
            0
            • K koahnig

              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'
              
              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @koahnig
              try:

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

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

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved