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. Qt versus Android Java

Qt versus Android Java

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 552 Views
  • 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.
  • M Offline
    M Offline
    mrdebug
    wrote on 5 Jul 2018, 07:34 last edited by mrdebug 7 May 2018, 13:46
    #1

    Hi. I need to pass a char array to a java method.
    Which is the right way?
    This id the Java method:

    public static int Write(char[] data, int maxSize)
    

    and this is my cpp line of code (that does not work)

    const char *data, int maxSize;
    jint Result= QAndroidJniObject::callStaticMethod<jint>("it/labcsp/rfportal/rfportaltouch/BluetoothWrapper", "Write", "([CI)I", data, maxSize);
    

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    R 1 Reply Last reply 5 Jul 2018, 11:55
    0
    • M mrdebug
      5 Jul 2018, 07:34

      Hi. I need to pass a char array to a java method.
      Which is the right way?
      This id the Java method:

      public static int Write(char[] data, int maxSize)
      

      and this is my cpp line of code (that does not work)

      const char *data, int maxSize;
      jint Result= QAndroidJniObject::callStaticMethod<jint>("it/labcsp/rfportal/rfportaltouch/BluetoothWrapper", "Write", "([CI)I", data, maxSize);
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 5 Jul 2018, 11:55 last edited by raven-worx 7 May 2018, 12:04
      #2

      @mrdebug
      there is not direct conversion from C++ char* to Java char[]!
      Actually in JNI an array always needs to be explicitly created (e.g. using env->NewObjectArray() etc....)

      So it would be easier if you can change the Java method signature to take a String (if possible):

      QString str(data);
      jstring jStr = QAndroidJniObject::fromString(str).object<jstring>();
      jint Result= QAndroidJniObject::callStaticMethod<jint>("it/labcsp/rfportal/rfportaltouch/BluetoothWrapper", "Write", "(Ljava/lang/String;I)I", jStr, maxSize);
      

      Or use a byte[] instead:

      jbyteArray jBuff = env->NewByteArray( length );
      env->SetByteArrayRegion(jBuff, 0, length, (jbyte*)buff);
      
      jint Result= QAndroidJniObject::callStaticMethod<jint>("it/labcsp/rfportal/rfportaltouch/BluetoothWrapper", "Write", "([BI)I", jBuff, maxSize);
      
      env->DeleteLocalRef(jBuff); // IMPORTANT TO AVOID MEMORY LEAK!
      

      --- 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
      4
      • M Offline
        M Offline
        mrdebug
        wrote on 5 Jul 2018, 13:45 last edited by
        #3

        It works perfectly!

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        1

        1/3

        5 Jul 2018, 07:34

        • Login

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