Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233522
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: Android平台

2016-04-08 09:02:34


点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <jni.h>
  5. #include <assert.h>
  6. #include "YN020_Function.h"


  7. # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))

  8. #define JNIREG_CLASS "com/example/jnitestfun/MainActivity"


  9. #include <android/log.h>


  10. #define TAG "Jniface"

  11. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__)

  12. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)

  13. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)



  14. JNIEXPORT jstring JNICALL
  15. stringFromJni(JNIEnv *env, jobject clazz)
  16. {
  17.  
  18.     jstring str = (*env)->NewStringUTF(env, "Hello From Jni");
  19.     return str;
  20. }



  21. JNIEXPORT void JNICALL
  22. stringToJni(JNIEnv *env, jobject clazz, jstring val)
  23. {

  24.     char *str = (char *)(*env)->GetStringUTFChars(env, val, JNI_FALSE);
  25.     LOGD("%s str=%s\n", __func__, str);
  26. }


  27. JNIEXPORT jfloat JNICALL
  28. floatFromJni(JNIEnv *env, jobject clazz)
  29. {
  30.     return (jfloat)1.34;
  31. }


  32. JNIEXPORT void JNICALL
  33. floatToJni(JNIEnv *env, jobject clazz, jfloat val)
  34. {
  35.     float f = (float)val;
  36.     LOGD("%s f=%f\n", __func__, f);
  37. }



  38. JNIEXPORT jint JNICALL
  39. intFromJni(JNIEnv *env, jobject clazz)
  40. {
  41.     return (jint)25;
  42. }


  43. JNIEXPORT void JNICALL
  44. intToJni(JNIEnv *env, jobject clazz, jint val)
  45. {

  46.     int i = (int)val;
  47.     LOGD("%s i=%d\n", __func__, i);
  48. }



  49. JNIEXPORT jint JNICALL
  50. SendCmdToJni(JNIEnv *env, jobject clazz,jint p_wCmdCode,jint p_nParam1, jint p_nParam2, jint p_nParam3, jint p_nParam4)
  51. {
  52.     char ret;

  53.     ret = CommandProcess((UINT16) p_wCmdCode, (UINT32) p_nParam1, (UINT32) p_nParam2, (UINT32) p_nParam3, (UINT32) p_nParam4);
  54.     LOGD("%s p_wCmdCode=%d\n", __func__, p_wCmdCode);
  55.     LOGD("%s p_nParam1=%d\n", __func__, p_nParam1);
  56.     LOGD("%s p_nParam2=%d\n", __func__, p_nParam2);
  57.     LOGD("%s p_nParam3=%d\n", __func__, p_nParam3);
  58.     LOGD("%s p_nParam4=%d\n", __func__, p_nParam4);
  59.     LOGD("%s ret=%d\n", __func__, ret);
  60.     return (jint)ret;
  61. }



  62. JNIEXPORT jint JNICALL
  63. JNI_Init_Device(JNIEnv *env, jobject clazz)
  64. {
  65.     char ret;

  66.     ret = Init_Device(UART_COMMUNICATION,"/dev/ttyUSB0",38400);
  67.     if(ret != 0)
  68.     {
  69.         LOGD("%s Init Device fail ret=%d\n", __func__, ret);
  70.         return -1;
  71.     }
  72.     LOGD("%s ret=%d\n", __func__, ret);
  73.     return (jint)ret;
  74. }



  75. static JNINativeMethod method_table[] = {
  76.     { "intFromJni" , "()I" , (void*)intFromJni },
  77.     { "intToJni" , "(I)V" , (void*)intToJni },
  78.     { "floatFromJni" , "()F" , (void*)floatFromJni },
  79.     { "floatToJni" , "(F)V" , (void*)floatToJni },
  80.     { "stringFromJni" , "()Ljava/lang/String;" , (void*)stringFromJni },
  81.     { "stringToJni" , "(Ljava/lang/String;)V", (void*)stringToJni },
  82.     { "SendCmdToJni" , "(IIIII)I", (void*)SendCmdToJni },
  83.     { "JNI_Init_Device" , "()I", (void*)JNI_Init_Device },
  84. };




  85. static int registerNativeMethods(JNIEnv* env, const char* className,
  86.         JNINativeMethod* gMethods, int numMethods)
  87. {
  88.     jclass clazz;
  89.     clazz = (*env)->FindClass(env, className);
  90.     if (clazz == NULL) {
  91.         return JNI_FALSE;
  92.     }
  93.     if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
  94.         return JNI_FALSE;
  95.     }

  96.     return JNI_TRUE;
  97. }

  98. int register_basic_ndk_param(JNIEnv *env)
  99. {

  100.     return registerNativeMethods(env, JNIREG_CLASS,
  101.             method_table, NELEM(method_table));
  102. }


  103. JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
  104. {
  105.     JNIEnv* env = NULL;
  106.     jint result = -1;

  107.     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
  108.         return result;
  109.     }

  110.     register_basic_ndk_param(env);

  111.  
  112.     return JNI_VERSION_1_4;
  113. }
写完jni后要编写makefile文件也就是Android.mk

点击(此处)折叠或打开

  1. LOCAL_PATH := $(call my-dir)

  2. include $(CLEAR_VARS)

  3. LOCAL_MODULE := Jniface
  4. LOCAL_SRC_FILES := Jniface.c YN020_Function.c CommBase.c Encryption.c md5.c uart.c usb_scsi.c


  5. LOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -llog


  6. include $(BUILD_SHARED_LIBRARY)

  7. LOCAL_PATH := $(call my-dir)


对应的安卓java代码如下:

点击(此处)折叠或打开

  1. package com.example.jnitestfun;

  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.app.Activity;


  7. public class MainActivity extends Activity {

  8.     private static final String TAG = "Nguhyw--Jnitest";
  9.     private int ival;
  10.     private float fval;
  11.     private String str;
  12.     private int ret;
  13.     
  14.     EditText TextCmd;
  15.     EditText TextParm1;
  16.     EditText TextParm2;
  17.     EditText TextParm3;
  18.     EditText TextParm4;
  19.     
  20.     
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         
  26.         Log.d(TAG,"onCreate");
  27.         
  28.         ival = intFromJni();
  29.         Log.d(TAG,"ival="+ival);
  30.         
  31.         intToJni((int)123);
  32.         
  33.         fval = floatFromJni();
  34.         Log.d(TAG,"fval="+fval);
  35.         
  36.         floatToJni((float)456.78);
  37.         
  38.         str = stringFromJni();
  39.         Log.d(TAG,"str="+str);
  40.         stringToJni("Hello From Java");
  41.         TextCmd = (EditText) findViewById(R.id.editText1);
  42.         TextParm1 = (EditText) findViewById(R.id.editText2);
  43.         TextParm2 = (EditText) findViewById(R.id.editText3);
  44.         TextParm3 = (EditText) findViewById(R.id.editText4);
  45.         TextParm4 = (EditText) findViewById(R.id.editText5);
  46.     }
  47.     
  48.     
  49.     public void InitDevClick(View v){
  50.          int ret = JNI_Init_Device();
  51.          Log.d(TAG,"JNI_Init_Device ret="+ret);
  52.     }
  53.     
  54.     public void ResetDevClick(View v){
  55.         int ret = SendCmdToJni(161,0,0,0,0);
  56.         Log.d(TAG,"SendCmdToJni ret="+ret);
  57.     }
  58.     
  59.     public void SendCmdClick(View v){
  60.         int ret = SendCmdToJni(0,0,0,0,0);
  61.         Log.d(TAG,"SendCmdToJni ret="+ret);
  62.     }
  63.     
  64.     public void SendPram(View v){
  65.         String Strcmd = "0";

  66.         Strcmd = TextCmd.getText().toString();
  67.         int NumCmd = Integer.valueOf(Strcmd).intValue();
  68.         Log.d(TAG,"NumCmd = "+NumCmd);     
  69.         
  70.         Strcmd = TextParm1.getText().toString();
  71.         int NumParm1 = Integer.valueOf(Strcmd).intValue();
  72.         Log.d(TAG,"NumParm1 = "+NumParm1);
  73.         
  74.         Strcmd = TextParm2.getText().toString();
  75.         int NumParm2 = Integer.valueOf(Strcmd).intValue();
  76.         Log.d(TAG,"NumParm2 = "+NumParm2);
  77.         
  78.         Strcmd = TextParm3.getText().toString();
  79.         int NumParm3 = Integer.valueOf(Strcmd).intValue();
  80.         Log.d(TAG,"NumParm3 = "+NumParm3);
  81.         
  82.         Strcmd = TextParm4.getText().toString();
  83.         int NumParm4 = Integer.valueOf(Strcmd).intValue();
  84.         Log.d(TAG,"NumParm4 = "+NumParm4);
  85.         
  86.         int ret = SendCmdToJni(NumCmd,NumParm1, NumParm2, NumParm3, NumParm4);
  87.         Log.d(TAG,"SendCmdToJni NumCmd="+NumCmd+" NumParm1="+NumParm1+" NumParm2"+NumParm2+" NumParm3="+NumParm3+" NumParm4"+NumParm4+" ret="+ret);
  88.     }
  89.     
  90.     private native int JNI_Init_Device();
  91.     private native int SendCmdToJni(int p_wCmdCode,int p_nParam1, int p_nParam2, int p_nParam3, int p_nParam4);
  92.     private native int intFromJni();
  93.     private native void intToJni(int val);
  94.     private native float floatFromJni();
  95.     private native void floatToJni(float val);
  96.     private native String stringFromJni();
  97.     private native void stringToJni(String str);
  98.     
  99.     static {
  100.         System.loadLibrary("Jniface");
  101.     }
  102.     
  103. }

阅读(1436) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~