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 classTtyNativeControl {

    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;

      

       Looperlooper;

       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(newWeakReference(this));

    }

    /**

     * 打开驱动

     * @return 是否打开成功

     */

    public int openTty(){

       return _openTty();

    }

    /**

     * 关闭驱动,需要一定时间,所以采用Handler机制

     */

    public int closeTty(){

//     mEventHandler.obtainMessage(TTY_CLOSE_DEVICE).sendToTarget();

//     return1;

       return _closeTty();

    }

    /**

     * 发送数据

     * @param data

     * @return

     */

    public  int sendMsgToTty(byte[] data){

       return _sendMsgToTty(data);

    }

   

    /**

     * 接收数据

     * @param callback

     */

    public final voidreceiveMsgFromTty(ReceiveCallback callback){

       mReceiveCallBack = callback;

       _receiveMsgFromTty();

    }

    /**

     * 设置串口数据位,校验位,速率,停止位

     * @param databits   数据位     取值 78

     * @param event      校验类型 取值N ,E, O,

     * @param speed      速率      取值 2400,4800,9600,115200

     * @param stopBit    停止位     取值1 或者 2

     */

    public  int configTty(intdatabits,charevent,intspeed,intstopBit){

       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);

    }

   

    /**

     * 接收数据回调接口

     */

    public interface ReceiveCallback{

       void onReceiveData(byte[] data,TtyNativeControltty);

    }

   

    /****************************************************************

     * 本地方法

     */

    private native final void native_setup(Objecttty_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);

   

    /*

     * 实现底层回调

     */

    private static voidpostEventFromNative(Object tty_ref,int what ,intarg1,intarg2,Object obj){

       Log.i(TAG, "[postEventFromNative] what:"+what);

       TtyNativeControlt = (TtyNativeControl)((WeakReference)tty_ref).get();

       if(t == null)return;

       if(t.mEventHandler != null){

           Messagem = t.mEventHandler.obtainMessage(what,arg1, arg2,obj);

           t.mEventHandler.sendMessage(m);

       }

    }

   

    private class EventHandler extends Handler{

       private TtyNativeControl mTty;

       publicEventHandler(TtyNativeControl t,Looper looper){

           super(looper);

           mTty = t;

       }

       @Override

       public void handleMessage(Messagemsg) {

           switch(msg.what){

           case TTY_MSG_RECEIVE://底层接收数据回调上来的事件

              if(mReceiveCallBack != null){

                  mReceiveCallBack.onReceiveData((byte[])msg.obj,mTty);

              }

              return;

           case TTY_CLOSE_DEVICE://关闭驱动

              _closeTty();

              break;

           }

       }

    }

}

 

 

2.     JNI类头文件

#include

#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


更多相关文章

  1. Android - Intent - 传递数据
  2. android webview web里面的数据透传到java以及java的数据透传到w
  3. Android和Java本地数据库新选择
  4. 在Android中查看和管理sqlite数据库
  5. Android图表控件MPAndroidChart——曲线图LineChart(多条曲线)动态
  6. Android联系人数据库全解析(1)
  7. [转」android中的数据库操作
  8. 数据存储和界面展示一
  9. android中的数据库操作ZZ

随机推荐

  1. Android(安卓)不要把android做成ios!
  2. Android单线程模型相关概念详解
  3. 微软的 Android(安卓)计划:邪恶的天才计划
  4. Android真的很火吗?
  5. 有关Android线程的学习
  6. Android(安卓)程式开发:(一)详解Activity —
  7. Android基础备忘(android中的分享功能)
  8. 《程序人生 —— Android实现录音、播音
  9. 让Android(安卓)变身回一台真正的Linux系
  10. Android进程与线程基本知识一