Android Java JNI and C Char array Can't recognize the value
I tried to pass the char array from java to c. I have googled the way to implement it. But when I tried to acc开发者_开发百科ess the value of the array, the value was not correct.
In fact, I tried to give the array a character. Just Like buffer[i]='z'
, and the result is correct in android.
Btw, the value I wanted was the value sent by the rs232. The value from the rs232 was ok.
Someone told me to use the bytearray. Would it work? I was afraid of that the value from rs232 could not be store into the bytearray....
Java
public native int OpenPort(int portnum,int brates);
public native int ClosePort(int portnum);
public native int READ(char[] databuffer);
public char[] buffer=new char[40];
int i;
for(i=0;i<40;i++)
buffer[i]='s'; //initialization
OpenPort(16,9600); // A function to open the rs232 port
while (i<1000)
{
READ(buffer);
i++;
}
JNI.c
JNIEXPORT jint JNICALL Java_com_example_ndk_Vehicles_READ(JNIEnv *env, jclass
cls,jcharArray databuffer)
{
char tmp[40];
jchar *buffer=(*env)->GetCharArrayElements(env,databuffer,0);
memset(tmp,0,sizeof(tmp));
PollComport(16,tmp,40); //will the problem here ? This function need a unsigned char[]
//,but in the c program I wrote , char[] was ok for this.
memcpy(buffer,tmp,40);
(*env)->ReleaseCharArrayElements(env, databuffer, buffer, 0);
return 0;
}
Here's a quick example
public native int play(String filename_main, String filename_prev, int main_x, int main_y, int main_width, int main_height, int preview_x, int preview_y, int preview_width, int preview_height);
maps to
int play(char* filename_main, char* filename_preview, int main_x, int main_y, int main_width, int main_height, int preview_x, int preview_y, int preview_width, int preview_height)
using this JNI call.
JNIEXPORT jint JNICALL Java_com_rtrk_demo_PELib_play(JNIEnv *env, jobject obj, jstring main_video, jstring prev_video, jint main_x, jint main_y, jint main_width, jint main_height, jint prev_x, jint prev_y, jint prev_width, jint prev_height)
{
LOGI("JNICALL - Java_com_rtrk_demo_PELib_play(filename)\n");
const char* utf_main_video = env->GetStringUTFChars(main_video, 0);
const char* utf_prev_video = env->GetStringUTFChars(prev_video, 0);
char* file_main_video = (char*) malloc(strlen(utf_main_video) * sizeof(char) +1);
char* file_prev_video = (char*) malloc(strlen(utf_prev_video) * sizeof(char) +1);
strcpy(file_main_video, utf_main_video);
strcpy(file_prev_video, utf_prev_video);
LOGI("JNICALL - filenames: %s\t%s\n", file_main_video, file_prev_video);
env->ReleaseStringUTFChars(main_video, utf_main_video);
env->ReleaseStringUTFChars(prev_video, utf_prev_video);
LOGI("###JNICALL RET - Java_com_rtrk_demo_PELib_play(%s, %s)\n", file_main_video, file_prev_video);
int retVal = play(file_main_video, file_prev_video, (int)main_x, (int)main_y, (int)main_width, (int)main_height, (int)prev_x, (int)prev_y, (int)prev_width, (int)prev_height);
free(file_main_video);
free(file_prev_video);
return retVal;
}
Because GetCharArrayElements() is allowed to make a copy of the Java array and hand you a pointer to the copy instead. In your code, you are passing in null for the pointer-to-boolean argument that would tell you if a copy was made:
jchar *buffer=(*env)->GetCharArrayElements(env,databuffer,0);
So you are likely operating on a copy which is discarded when you are done. Instead of GetCharArrayElements(), operate on your local array in C++ and use SetCharArrayRegion() to copy them.
精彩评论