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. Way to pass byte[] as char* from Android to C++
Forum Updated to NodeBB v4.3 + New Features

Way to pass byte[] as char* from Android to C++

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 2 Posters 1.5k 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.
  • K Offline
    K Offline
    Kyeiv
    wrote on last edited by Kyeiv
    #1

    I am developing an Android app using Qt. The app is communicating with a FTDI device via Android's USB libraries. The device sends me hex data which is stored in byte[] array on the android side. The approaches i stumbled upon require converting it to String before passing it to c++. Unfortunately this conversion ruins my hex data due to using UTF-8. So i am wandering if there is a way that could let me pass byte[] from Java and it to appear as char* in the c++ side?

    Going from:

    private static native void getData(String message);
    
        JNINativeMethod methods[] {
            {"getData", "(Ljava/lang/String;)V", reinterpret_cast<void *>(getData)}
        };
    
    static void getData(JNIEnv *env, jobject /*thiz*/, jstring value)
    

    to something like:

    private static native void getData(byte[] message);
    
        JNINativeMethod methods[] {
            {"getData", "([B)V", reinterpret_cast<void *>(getData)}
        };
    
    static void getData(JNIEnv *env, jobject /*thiz*/, jbyteArray value)
    

    Can anyone prompt me with some ideas?

    KroMignonK 1 Reply Last reply
    0
    • K Kyeiv

      @KroMignon i corrected it, but can you answer the question? how to cature this jbyteArray as char*?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #4

      @Kyeiv said in Way to pass byte[] as char* from Android to C++:

      i corrected it, but can you answer the question? how to cature this jbyteArray as char*

      Yes I can ;)

      void getData(JNIEnv *env, jobject /*thiz*/, jbyteArray value)
      {
          // get array pointer and size
          jbyte *bytes = env->GetByteArrayElements(value, false);
          int arrayLength = env->GetArrayLength(value);
      
          // copy to local variable, for example a QByteArray 
          QByteArray b = QByteArray::fromRawData((const char*)bytes , arrayLength);
      
          // release memory
          env->ReleaseByteArrayElements(value, bytes, JNI_ABORT);
      
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • K Kyeiv

        I am developing an Android app using Qt. The app is communicating with a FTDI device via Android's USB libraries. The device sends me hex data which is stored in byte[] array on the android side. The approaches i stumbled upon require converting it to String before passing it to c++. Unfortunately this conversion ruins my hex data due to using UTF-8. So i am wandering if there is a way that could let me pass byte[] from Java and it to appear as char* in the c++ side?

        Going from:

        private static native void getData(String message);
        
            JNINativeMethod methods[] {
                {"getData", "(Ljava/lang/String;)V", reinterpret_cast<void *>(getData)}
            };
        
        static void getData(JNIEnv *env, jobject /*thiz*/, jstring value)
        

        to something like:

        private static native void getData(byte[] message);
        
            JNINativeMethod methods[] {
                {"getData", "([B)V", reinterpret_cast<void *>(getData)}
            };
        
        static void getData(JNIEnv *env, jobject /*thiz*/, jbyteArray value)
        

        Can anyone prompt me with some ideas?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @Kyeiv said in Way to pass byte[] as char* from Android to C++:

        Can anyone prompt me with some ideas?

        You function has wrong signature:

        static void getData(JNIEnv *env, jobject /*thiz*/, jbyte value)
        

        should be:

        static void getData(JNIEnv *env, jobject /*thiz*/, jbyteArray value)
        

        cf. https://doc.qt.io/qt-5/qandroidjniobject.html#object-types

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        K 1 Reply Last reply
        1
        • KroMignonK KroMignon

          @Kyeiv said in Way to pass byte[] as char* from Android to C++:

          Can anyone prompt me with some ideas?

          You function has wrong signature:

          static void getData(JNIEnv *env, jobject /*thiz*/, jbyte value)
          

          should be:

          static void getData(JNIEnv *env, jobject /*thiz*/, jbyteArray value)
          

          cf. https://doc.qt.io/qt-5/qandroidjniobject.html#object-types

          K Offline
          K Offline
          Kyeiv
          wrote on last edited by
          #3

          @KroMignon i corrected it, but can you answer the question? how to cature this jbyteArray as char*?

          KroMignonK 1 Reply Last reply
          0
          • K Kyeiv

            @KroMignon i corrected it, but can you answer the question? how to cature this jbyteArray as char*?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #4

            @Kyeiv said in Way to pass byte[] as char* from Android to C++:

            i corrected it, but can you answer the question? how to cature this jbyteArray as char*

            Yes I can ;)

            void getData(JNIEnv *env, jobject /*thiz*/, jbyteArray value)
            {
                // get array pointer and size
                jbyte *bytes = env->GetByteArrayElements(value, false);
                int arrayLength = env->GetArrayLength(value);
            
                // copy to local variable, for example a QByteArray 
                QByteArray b = QByteArray::fromRawData((const char*)bytes , arrayLength);
            
                // release memory
                env->ReleaseByteArrayElements(value, bytes, JNI_ABORT);
            
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            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