Skip to content

Instantly share code, notes, and snippets.

View pgsamila's full-sized avatar
:octocat:
W I P

Amila Sampath pgsamila

:octocat:
W I P
View GitHub Profile
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#define DEVICENAME "hello_world"
#define READINGDATA "Hello World Reads!\n"
extern "C"
JNIEXPORT void JNICALL
Java_com_example_androidndk_1passing_1complex_1data_NativeLibrary_callTheCallBackMethod(JNIEnv *env,
jobject instance) {
// Get jclass of the instance
jclass jClassInstance = env->GetObjectClass(instance);
// Get java callback method
jmethodID callBackJava = env->GetMethodID(jClassInstance, "callBack", "(ILjava/lang/String;)V");
public native void callTheCallBackMethod();
private void callBack(int data, String stringValue) {
callBackInterface.callBackEvent(stringValue + String.valueOf(data));
}
/**
* On Call Back From JNI Button Clicked Event
*
* @param view
*/
public void onCallBackFromJNIClicked(View view) {
// Call JNI callback method
nativeLib.callTheCallBackMethod();
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_example_androidndk_1passing_1complex_1data_NativeLibrary_getObjectFromJNI(JNIEnv *env,
jobject instance) {
// Create a jclass from actual Java object class path
jclass sampleObjClass = env->FindClass(
"com/example/androidndk_passing_complex_data/SampleDataObj");
jmethodID methodId = env->GetMethodID(sampleObjClass, "<init>", "()V");
jobject sampleObj = env->NewObject(sampleObjClass, methodId);
/**
* On Get Object From JNI Button Clicked Event
*
* @param view
*/
public void onGetObjectFromJNI(View view) {
TextView textView = findViewById(R.id.textViewGetObjectFromJNI);
SampleDataObj dataObj = nativeLib.getObjectFromJNI();
textView.setText(dataObj.getSampleString());
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_androidndk_1passing_1complex_1data_NativeLibrary_passObjectToJNI(JNIEnv *env,
jobject instance,
jobject sampleDataObj) {
// Get jclass of the object
jclass sampleDataOBJClass = env->GetObjectClass(sampleDataObj);
// Get data field IDs of the object
/**
* On Pass Object To JNI Button Clicked Event
*
* @param view
*/
public void onPassObjectToJNI(View view) {
TextView textView = findViewById(R.id.textViewPassObjectToJNI);
//Create Sample Data Object
SampleDataObj sampleDataObj = new SampleDataObj();
sampleDataObj.setSampleInt(0);
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_androidndk_1passing_1complex_1data_NativeLibrary_passingDataToJni(JNIEnv *env,
jobject instance,
jdoubleArray tmpArray_,
jint tmpInt,
jfloat tmpFloat) {
jdouble *tmpArray = env->GetDoubleArrayElements(tmpArray_, NULL);
int ret = 0;
/**
* On Pass Data To JNI Button Clicked Event
*
* @param view
*/
public void onPassDataToJNIButtonClicked(View view) {
TextView textView = findViewById(R.id.textViewPassDataToJNI);
// Temp data which passing to JNI
double[] tmpArray = {1, 2, 3};
int tmpInt = 1;