-
#include <stdlib.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <jni.h>
-
#include <assert.h>
-
#include "YN020_Function.h"
-
-
-
# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
-
-
#define JNIREG_CLASS "com/example/jnitestfun/MainActivity"
-
-
-
#include <android/log.h>
-
-
-
#define TAG "Jniface"
-
-
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__)
-
-
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
-
-
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
-
-
-
-
JNIEXPORT jstring JNICALL
-
stringFromJni(JNIEnv *env, jobject clazz)
-
{
-
-
jstring str = (*env)->NewStringUTF(env, "Hello From Jni");
-
return str;
-
}
-
-
-
-
JNIEXPORT void JNICALL
-
stringToJni(JNIEnv *env, jobject clazz, jstring val)
-
{
-
-
char *str = (char *)(*env)->GetStringUTFChars(env, val, JNI_FALSE);
-
LOGD("%s str=%s\n", __func__, str);
-
}
-
-
-
JNIEXPORT jfloat JNICALL
-
floatFromJni(JNIEnv *env, jobject clazz)
-
{
-
return (jfloat)1.34;
-
}
-
-
-
JNIEXPORT void JNICALL
-
floatToJni(JNIEnv *env, jobject clazz, jfloat val)
-
{
-
float f = (float)val;
-
LOGD("%s f=%f\n", __func__, f);
-
}
-
-
-
-
JNIEXPORT jint JNICALL
-
intFromJni(JNIEnv *env, jobject clazz)
-
{
-
return (jint)25;
-
}
-
-
-
JNIEXPORT void JNICALL
-
intToJni(JNIEnv *env, jobject clazz, jint val)
-
{
-
-
int i = (int)val;
-
LOGD("%s i=%d\n", __func__, i);
-
}
-
-
-
-
JNIEXPORT jint JNICALL
-
SendCmdToJni(JNIEnv *env, jobject clazz,jint p_wCmdCode,jint p_nParam1, jint p_nParam2, jint p_nParam3, jint p_nParam4)
-
{
-
char ret;
-
-
ret = CommandProcess((UINT16) p_wCmdCode, (UINT32) p_nParam1, (UINT32) p_nParam2, (UINT32) p_nParam3, (UINT32) p_nParam4);
-
LOGD("%s p_wCmdCode=%d\n", __func__, p_wCmdCode);
-
LOGD("%s p_nParam1=%d\n", __func__, p_nParam1);
-
LOGD("%s p_nParam2=%d\n", __func__, p_nParam2);
-
LOGD("%s p_nParam3=%d\n", __func__, p_nParam3);
-
LOGD("%s p_nParam4=%d\n", __func__, p_nParam4);
-
LOGD("%s ret=%d\n", __func__, ret);
-
return (jint)ret;
-
}
-
-
-
-
JNIEXPORT jint JNICALL
-
JNI_Init_Device(JNIEnv *env, jobject clazz)
-
{
-
char ret;
-
-
ret = Init_Device(UART_COMMUNICATION,"/dev/ttyUSB0",38400);
-
if(ret != 0)
-
{
-
LOGD("%s Init Device fail ret=%d\n", __func__, ret);
-
return -1;
-
}
-
LOGD("%s ret=%d\n", __func__, ret);
-
return (jint)ret;
-
}
-
-
-
-
static JNINativeMethod method_table[] = {
-
{ "intFromJni" , "()I" , (void*)intFromJni },
-
{ "intToJni" , "(I)V" , (void*)intToJni },
-
{ "floatFromJni" , "()F" , (void*)floatFromJni },
-
{ "floatToJni" , "(F)V" , (void*)floatToJni },
-
{ "stringFromJni" , "()Ljava/lang/String;" , (void*)stringFromJni },
-
{ "stringToJni" , "(Ljava/lang/String;)V", (void*)stringToJni },
-
{ "SendCmdToJni" , "(IIIII)I", (void*)SendCmdToJni },
-
{ "JNI_Init_Device" , "()I", (void*)JNI_Init_Device },
-
};
-
-
-
-
-
static int registerNativeMethods(JNIEnv* env, const char* className,
-
JNINativeMethod* gMethods, int numMethods)
-
{
-
jclass clazz;
-
clazz = (*env)->FindClass(env, className);
-
if (clazz == NULL) {
-
return JNI_FALSE;
-
}
-
if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
-
return JNI_FALSE;
-
}
-
-
return JNI_TRUE;
-
}
-
-
int register_basic_ndk_param(JNIEnv *env)
-
{
-
-
return registerNativeMethods(env, JNIREG_CLASS,
-
method_table, NELEM(method_table));
-
}
-
-
-
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
-
{
-
JNIEnv* env = NULL;
-
jint result = -1;
-
-
if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
-
return result;
-
}
-
-
register_basic_ndk_param(env);
-
-
-
return JNI_VERSION_1_4;
-
}
写完jni后要编写makefile文件也就是Android.mk
-
LOCAL_PATH := $(call my-dir)
-
-
include $(CLEAR_VARS)
-
-
LOCAL_MODULE := Jniface
-
LOCAL_SRC_FILES := Jniface.c YN020_Function.c CommBase.c Encryption.c md5.c uart.c usb_scsi.c
-
-
-
LOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -llog
-
-
-
include $(BUILD_SHARED_LIBRARY)
-
-
LOCAL_PATH := $(call my-dir)
对应的安卓java代码如下:
-
package com.example.jnitestfun;
-
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.EditText;
-
import android.app.Activity;
-
-
-
public class MainActivity extends Activity {
-
-
private static final String TAG = "Nguhyw--Jnitest";
-
private int ival;
-
private float fval;
-
private String str;
-
private int ret;
-
-
EditText TextCmd;
-
EditText TextParm1;
-
EditText TextParm2;
-
EditText TextParm3;
-
EditText TextParm4;
-
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
Log.d(TAG,"onCreate");
-
-
ival = intFromJni();
-
Log.d(TAG,"ival="+ival);
-
-
intToJni((int)123);
-
-
fval = floatFromJni();
-
Log.d(TAG,"fval="+fval);
-
-
floatToJni((float)456.78);
-
-
str = stringFromJni();
-
Log.d(TAG,"str="+str);
-
stringToJni("Hello From Java");
-
TextCmd = (EditText) findViewById(R.id.editText1);
-
TextParm1 = (EditText) findViewById(R.id.editText2);
-
TextParm2 = (EditText) findViewById(R.id.editText3);
-
TextParm3 = (EditText) findViewById(R.id.editText4);
-
TextParm4 = (EditText) findViewById(R.id.editText5);
-
}
-
-
-
public void InitDevClick(View v){
-
int ret = JNI_Init_Device();
-
Log.d(TAG,"JNI_Init_Device ret="+ret);
-
}
-
-
public void ResetDevClick(View v){
-
int ret = SendCmdToJni(161,0,0,0,0);
-
Log.d(TAG,"SendCmdToJni ret="+ret);
-
}
-
-
public void SendCmdClick(View v){
-
int ret = SendCmdToJni(0,0,0,0,0);
-
Log.d(TAG,"SendCmdToJni ret="+ret);
-
}
-
-
public void SendPram(View v){
-
String Strcmd = "0";
-
-
Strcmd = TextCmd.getText().toString();
-
int NumCmd = Integer.valueOf(Strcmd).intValue();
-
Log.d(TAG,"NumCmd = "+NumCmd);
-
-
Strcmd = TextParm1.getText().toString();
-
int NumParm1 = Integer.valueOf(Strcmd).intValue();
-
Log.d(TAG,"NumParm1 = "+NumParm1);
-
-
Strcmd = TextParm2.getText().toString();
-
int NumParm2 = Integer.valueOf(Strcmd).intValue();
-
Log.d(TAG,"NumParm2 = "+NumParm2);
-
-
Strcmd = TextParm3.getText().toString();
-
int NumParm3 = Integer.valueOf(Strcmd).intValue();
-
Log.d(TAG,"NumParm3 = "+NumParm3);
-
-
Strcmd = TextParm4.getText().toString();
-
int NumParm4 = Integer.valueOf(Strcmd).intValue();
-
Log.d(TAG,"NumParm4 = "+NumParm4);
-
-
int ret = SendCmdToJni(NumCmd,NumParm1, NumParm2, NumParm3, NumParm4);
-
Log.d(TAG,"SendCmdToJni NumCmd="+NumCmd+" NumParm1="+NumParm1+" NumParm2"+NumParm2+" NumParm3="+NumParm3+" NumParm4"+NumParm4+" ret="+ret);
-
}
-
-
private native int JNI_Init_Device();
-
private native int SendCmdToJni(int p_wCmdCode,int p_nParam1, int p_nParam2, int p_nParam3, int p_nParam4);
-
private native int intFromJni();
-
private native void intToJni(int val);
-
private native float floatFromJni();
-
private native void floatToJni(float val);
-
private native String stringFromJni();
-
private native void stringToJni(String str);
-
-
static {
-
System.loadLibrary("Jniface");
-
}
-
-
}
阅读(1436) | 评论(0) | 转发(0) |