What I want to do is declare a buffer, pass it to the native code to modify, then use the changed buffer to update the app. This is an Android project that in the end will use another native library to do the actual work so this seems to be the best route to me.
In the main activity
which calls the native
If I leave in the ReleaseByteArrayElements() when it returns from native to write the buffer in a list the buffer seems to reset to all 0's.
If I comment out the ReleaseByteArrayElements() the buffer keeps the random numbers.
Is this right or did I miss something?
In the end instead of filling it with random numbers it will call a read function of another library and return that data.
In the main activity
Code:
byte the_buffer[];
the_buffer = new byte[1024];
int bytes_read = myLibrary.ReadMessage(the_id,the_buffer);
...
writes the bytes out in a list to scroll through
Code:
JNIEXPORT jint JNICALL Java_com_example_myapp_mylibrary_ReadMessage(JNIEnv *pEnv, jobject pThis, jint nexiq_id, jbyteArray buffer) {
jboolean is_copy;
jbyte *buf = (char*)pEnv->GetByteArrayElements(buffer,&is_copy);
char char_buf[1024] = {0};
// test
int bytes_read = rand();
while(bytes_read > 1024) bytes_read -= 1024;
int i;
for (i = 0; i < bytes_read; i++) {
char_buf[i] = (char)(rand() & 0xff);
}
pEnv->SetByteArrayRegion(buffer,0,i,(jbyte*)char_buf);
pEnv->ReleaseByteArrayElements(buffer, buf, 0);
return bytes_read;
}
If I comment out the ReleaseByteArrayElements() the buffer keeps the random numbers.
Is this right or did I miss something?
In the end instead of filling it with random numbers it will call a read function of another library and return that data.