Qt versus Android Java
Solved
Mobile and Embedded
-
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);
-
@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!