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 6.11 is out! See what's new in the release blog

Qt versus Android Java

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 910 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.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote on last edited by mrdebug
    #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

    raven-worxR 1 Reply Last reply
    0
    • mrdebugM mrdebug

      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);
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #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
      • mrdebugM Offline
        mrdebugM Offline
        mrdebug
        wrote on 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

        • Login

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