Android通過JNI操作串口 1. 本地類TtyNativeControl package com.notioni.uart.manager;
import java.lang.ref.WeakReference;
import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log;
/** * 本地方法類 */ public class TtyNativeControl { private final static String TAG = "TtyNativeControl"; static{ System.loadLibrary("uart_ctl"); } private static final int TTY_MSG_RECEIVE = 1; private static final int TTY_CLOSE_DEVICE = TTY_MSG_RECEIVE+1;
private EventHandler mEventHandler; private ReceiveCallback mReceiveCallBack;
TtyNativeControl(){ mReceiveCallBack = null;
Looper looper; if((looper = Looper.myLooper()) != null){ mEventHandler = new EventHandler(this, looper); }else if((looper = Looper.getMainLooper()) != null){ mEventHandler = new EventHandler(this, looper); }else{ mEventHandler = null; } native_setup(new WeakReference<TtyNativeControl>(this)); } /** * 打開驅動 * @return 是否打開成功 */ public int openTty(){ return _openTty(); } /** * 關閉驅動,,需要一定時間,所以采用Handler機制 */ public int closeTty(){ // mEventHandler.obtainMessage(TTY_CLOSE_DEVICE).sendToTarget(); // return 1; return _closeTty(); } /** * 發(fā)送數(shù)據(jù) * @param data * @return */ public int sendMsgToTty(byte[] data){ return _sendMsgToTty(data); }
/** * 接收數(shù)據(jù) * @param callback */ public final void receiveMsgFromTty(ReceiveCallback callback){ mReceiveCallBack = callback; _receiveMsgFromTty(); } /** * 設置串口數(shù)據(jù)位,校驗位,速率,,停止位 * @param databits 數(shù)據(jù)位 取值 位7或8 * @param event 校驗類型 取值N ,E, O, * @param speed 速率 取值 2400,4800,9600,115200 * @param stopBit 停止位 取值1 或者 2 */ public int configTty(int databits,char event,int speed,int stopBit){ return _configTty(databits, event, speed, stopBit); }
/** * @param mode 是否使用原始模式(Raw Mode)方式來通訊 取值0,1,2 說明:0=nothing,1=Raw mode,2=no raw mode * @param showLog 打印出串口信息Log 取值1,0 */ public int setMode(int mode ,int showLog){ return _setMode(mode, showLog); }
/** * 接收數(shù)據(jù)回調接口 */ public interface ReceiveCallback{ void onReceiveData(byte[] data,TtyNativeControl tty); }
/**************************************************************** * 本地方法 */ private native final void native_setup(Object tty_this); private native int _openTty(); private native int _closeTty(); private native int _sendMsgToTty(byte[] data); private native void _receiveMsgFromTty(); private native int _configTty(int databits,char event,int speed,int stopBit); private native int _setMode(int mode,int showLog);
/* * 實現(xiàn)底層回調 */ private static void postEventFromNative(Object tty_ref,int what ,int arg1,int arg2,Object obj){ Log.i(TAG, "[postEventFromNative] what:"+what); TtyNativeControl t = (TtyNativeControl)((WeakReference)tty_ref).get(); if(t == null)return; if(t.mEventHandler != null){ Message m = t.mEventHandler.obtainMessage(what, arg1, arg2,obj); t.mEventHandler.sendMessage(m); } }
private class EventHandler extends Handler{ private TtyNativeControl mTty; public EventHandler(TtyNativeControl t,Looper looper){ super(looper); mTty = t; } @Override public void handleMessage(Message msg) { switch(msg.what){ case TTY_MSG_RECEIVE://底層接收數(shù)據(jù)回調上來的事件 if(mReceiveCallBack != null){ mReceiveCallBack.onReceiveData((byte[])msg.obj,mTty); } return; case TTY_CLOSE_DEVICE://關閉驅動 _closeTty(); break; } } } }
2. JNI類頭文件 #include <jni.h> #ifndef _Included_com_notioni_uart_manager_TtyNativeControl #define _Included_com_notioni_uart_manager_TtyNativeControl #ifdef __cplusplus extern "C"{ #endif
/** * Class com_notioni_uart_TtyNativeControl * Method */ JNIEXPORT static void JNICALL com_notioni_uart_manager_TtyNativeControl_native_setup(JNIEnv *env,jobject clazz,jobject weak_this); JNIEXPORT static int JNICALL com_notioni_uart_manager_TtyNativeControl__openTty(JNIEnv *env,jobject clazz); JNIEXPORT static int JNICALL com_notioni_uart_manager_TtyNativeControl__closeTty(JNIEnv *env,jobject clazz);
JNIEXPORT static int JNICALL com_notioni_uart_manager_TtyNativeControl__sendMsgToTty(JNIEnv *env,jobject clazz,jbyteArray data);
JNIEXPORT static void JNICALL com_notioni_uart_manager_TtyNativeControl__receiveMsgFromTty(JNIEnv *env,jobject clazz);
JNIEXPORT static int JNICALL com_notioni_uart_manager_TtyNativeControl__configTty(JNIEnv *env,jobject clazz,int nBits,jchar nEvent,int nSpeed,int nStop);
JNIEXPORT static int JNICALL com_notioni_uart_manager_TtyNativeControl__setMode(JNIEnv *env,jobject clazz,int nMode,int showLog);
//JNIEXPORT int JNICALL com_notioni_uart_manager_TtyNativeControl__setSpeed(JNIEnv *env,jobject clazz,int speed); //JNIEXPORT int JNICALL com_notioni_uart_manager_TtyNativeControl__setParity(JNIEnv *env,jobject clazz,int databits,int stopbits,int parity);
#ifdef __cplusplus } #endif #endif
未完下接 Android通過JNI操作串口《二》 |
|
來自: 昵稱13517980 > 《Android_JNI_串口》