Consider this java method:
public int write(byte[] src, int timeoutMillis) throws IOException {
System.out.println("Write function started.");
System.out.println("buffer = " + src.length);
System.out.println("buffer = " + src[0]);
return 1;
}
I tried to use this method from c++ using Qandroid JNI. Here is the code I've written in c++:
jbyteArray buffer = env->NewByteArray(3);
jbyte *bytes = env->GetByteArrayElements( buffer, 0);
bytes[0]=1;
bytes[1]=2;
bytes[2]=3;
env->SetByteArrayRegion(buffer,0,3,bytes);
jniObject.callMethod<jint>("write","([Ljava/lang/byte;I)I",buffer,1000);
According to the signature of Java code. Do I use the callMethod correctly? Why it does not run at all? It prints nothing in the console! No exception no error!
For your information, I'm trying to use JNI to open a usb serial device. I successfully open the device, get the UsbDeviceConnection object. The seria port is successfully opened and everything is working nice. But here the write function does not work. It seems it does not enter to the function at all. I receive no error. I think the problem is the signature string or the buffer object that I have provided for the method? What's your opinion about it? Thanks for any help.
The signature for a type of jbyteArray
is [B
. So the method call should be like :
jniObject.callMethod<jint>("write","([BI)I",buffer,1000);