基于ndk jni技术实现串口收发的方法

步骤:
1、写java jni类;

2、javah生成.h,实现对应.c文件;

3、写java activity类测试。

参考代码:

//*******************java jni类*************************//
package com.example.hellojni;


public class JniInterface {

	static {
       System.loadLibrary("hello-jni");//可装载java.library.path包含路径下的so
       // System.load("/sdcard/mylib/libhello-jni.so");//可装载任何绝对路径的so
    }

    //串口初始化
    public static native void CommInit();  
   
    //串口收发
    public static native int ComSendRecv(byte[] inBuf,int nInLen,byte[] outBuf);
    
    public static native void nativeInit();

    //下面两个是测试jni里面调用java方法
    public static native void nativeExecution();

    private  static void DispInfo(int value)
   {
    	System.out.println("this is called by c value:"+value);
		//Toast.makeText(mainContext, "this is from c:"+value,Toast.LENGTH_LONG).show();
    } 
}
//*********************javah生成.h****************************//
/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_example_hellojni_JniInterface */

#ifndef _Included_com_example_hellojni_JniInterface
#define _Included_com_example_hellojni_JniInterface
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    CommInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_hellojni_JniInterface_CommInit
  (JNIEnv *, jobject);

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    ComSendRecv
 * Signature: ([BI[B)I
 */
JNIEXPORT jint JNICALL Java_com_example_hellojni_JniInterface_ComSendRecv
  (JNIEnv *, jobject, jbyteArray, jint, jbyteArray);

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    nativeInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_hellojni_JniInterface_nativeInit
  (JNIEnv *, jobject);

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    nativeExecution
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_hellojni_JniInterface_nativeExecution
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
//*********************实现对应.c****************************//
#include"com_example_hellojni_JniInterface.h"

void Java_com_example_hellojni_JniInterface_CommInit(JNIEnv* env,jobject thiz)
{
	Comm_Init();//这里面就是linux c串口初始化代码,如打开串口,设置波特率,阻塞等
}

int _ComSendRecvPack(unsigned char *psInBuf,unsigned int nInLen,unsigned char *psOutBuf)
{
	unsigned char ucmRetCmdNo[2];
	int i=0;
	int nTagCount=0;//tag个数
		
	//发送
	m_SendBufLen = nInLen;
	memset(m_SendBuf,0,sizeof(m_SendBuf));
	memcpy(m_SendBuf,psInBuf,nInLen);

	ComSend();//里面是串口write

	//接收	
	ComRecv();//里面是串口read
	memcpy(psOutBuf,m_RecvBuf,m_RecvBufLen);
	
	return m_RecvBufLen;

}
//串口封装给JNI
int Java_com_example_hellojni_JniInterface_ComSendRecv(JNIEnv* env,jobject thiz,jbyteArray psInBuf,int nInLen,jbyteArray psOutBuf)
{	
	jbyte native_Inbuf[1024];
	jbyte native_Outbuf[1024];
	jint nlen = 0;

	//类型转换java byte -> c buf
	(*env)->GetByteArrayRegion(env,psInBuf,0,nInLen,native_Inbuf);
	
	nlen = _ComSendRecvPack(native_Inbuf,nInLen,native_Outbuf);

	//c buf -> java byte	
	(*env)->SetByteArrayRegion(env,psOutBuf,0,nlen,native_Outbuf);
	
	return nlen;
}
jmethodID gOnNativeID;
JNIEnv* genv;
jobject mObject;
jclass mClass;
JavaVM* gs_jvm;

//native初始化函数
void Java_com_example_hellojni_JniInterface_nativeInit(JNIEnv* env,jobject thiz)
{

	printf("CalledForJni_nativeInit................");
	
	//获取类
	jclass clazz = (*env)->FindClass(env,"com/example/hellojni/JniInterface");
	if(clazz==NULL)
	{
		printf("clazz IS NULL................");
		return;
	}
	//mClass = (jclass)(*env)->NewGlobalRef(env,clazz);//永久保存mClass

	mObject = (jobject)(*env)->NewGlobalRef(env,thiz);
	//获取方法ID
	gOnNativeID = (*env)->GetStaticMethodID(env,clazz,"DispInfo","(I)V");	
	if(gOnNativeID==NULL)
	{
		printf("gOnNativeID IS NULL................");
		return;
	}
	//得到JAVA VM,为了在其他没有传env参数的函数获取env调用java方法
	(*env)->GetJavaVM(env,&gs_jvm); 
	printf("CalledForJni_nativeInit end................");

}
void Java_com_example_hellojni_JniInterface_nativeExecution(JNIEnv* env,jobject thiz)
{
	printf("nativeExecution................");
	(*env)->CallStaticVoidMethod(env,mObject,gOnNativeID);
	(*env)->DeleteGlobalRef(env,mObject);
	return;
}

//*********************activity部分****************************//
JniInterface.CommInit();

serialtestbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				//Log.i(HelloJni.ACTIVITY_TAG,"serialtestbutton clicked================");
				
				
				Log.i(HelloJni.ACTIVITY_TAG,"inBuf:"+nInLen+":"+ByteTool.getHexString(inBuf));
				
				Log.i(HelloJni.ACTIVITY_TAG,"before send");				
				nOutLen = JniInterface.ComSendRecv(inBuf, nInLen, outBuf);
				Log.i(HelloJni.ACTIVITY_TAG,"after recv");
				
				byte[] temp = new byte[nOutLen];				
				System.arraycopy(outBuf, 0, temp, 0, nOutLen);
				Log.i(HelloJni.ACTIVITY_TAG,"outBuf:"+nOutLen+":"+ByteTool.getHexString(temp));
			}
			});

你可能感兴趣的:(ndk,c)