Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Qt versus Android Java

    Mobile and Embedded
    2
    3
    452
    Loading More Posts
    • 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.
    • mrdebug
      mrdebug last edited by 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);
      

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

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @mrdebug last edited by raven-worx

        @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 Reply Quote 4
        • mrdebug
          mrdebug last edited by

          It works perfectly!

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

          1 Reply Last reply Reply Quote 1
          • First post
            Last post