android jni 串口收发,Android通过JNI操作串口《一》

Android通过JNI操作串口

1.本地类TtyNativeControl

packagecom.notioni.uart.manager;

importjava.lang.ref.WeakReference;

importandroid.os.Handler;

importandroid.os.Looper;

importandroid.os.Message;

importandroid.util.Log;

/**

*本地方法类

*/

publicclassTtyNativeControl {

privatefinalstaticStringTAG="TtyNativeControl";

static{

System.loadLibrary("uart_ctl");

}

privatestaticfinalintTTY_MSG_RECEIVE= 1;

privatestaticfinalintTTY_CLOSE_DEVICE=TTY_MSG_RECEIVE+1;

privateEventHandlermEventHandler;

privateReceiveCallbackmReceiveCallBack;

TtyNativeControl(){

mReceiveCallBack=null;

Looper

looper;

if((looper = Looper.myLooper())

!=null){

mEventHandler=newEventHandler(this, looper);

}elseif((looper = Looper.getMainLooper())

!=null){

mEventHandler=newEventHandler(this, looper);

}else{

mEventHandler=null;

}

native_setup(newWeakReference(this));

}

/**

*打开驱动

*@return是否打开成功

*/

publicintopenTty(){

return_openTty();

}

/**

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

*/

publicintcloseTty(){

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

//     return

1;

return_closeTty();

}

/**

*发送数据

*@paramdata

*@return

*/

publicintsendMsgToTty(byte[] data){

return_sendMsgToTty(data);

}

/**

*接收数据

*@paramcallback

*/

publicfinalvoidreceiveMsgFromTty(ReceiveCallback callback){

mReceiveCallBack= callback;

_receiveMsgFromTty();

}

/**

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

*@paramdatabits数据位取值位7或8

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

*@paramspeed速率取值2400,4800,9600,115200

*@paramstopBit停止位取值1或者2

*/

publicintconfigTty(intdatabits,charevent,intspeed,intstopBit){

return_configTty(databits,

event, speed, stopBit);

}

/**

*@parammode是否使用原始模式(Raw Mode)方式来通讯取值0,1,2说明:0=nothing,1=Raw mode,2=no raw mode

*@paramshowLog打印出串口信息Log取值1,0

*/

publicintsetMode(intmode ,intshowLog){

return_setMode(mode,

showLog);

}

/**

*接收数据回调接口

*/

publicinterfaceReceiveCallback{

voidonReceiveData(byte[] data,TtyNativeControl

tty);

}

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

*本地方法

*/

privatenativefinalvoidnative_setup(Object

tty_this);

privatenativeint_openTty();

privatenativeint_closeTty();

privatenativeint_sendMsgToTty(byte[] data);

privatenativevoid_receiveMsgFromTty();

privatenativeint_configTty(intdatabits,charevent,intspeed,intstopBit);

privatenativeint_setMode(intmode,intshowLog);

/*

*实现底层回调

*/

privatestaticvoidpostEventFromNative(Object tty_ref,intwhat ,intarg1,intarg2,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);

}

}

privateclassEventHandlerextendsHandler{

privateTtyNativeControlmTty;

publicEventHandler(TtyNativeControl t,Looper looper){

super(looper);

mTty= t;

}

@Override

publicvoidhandleMessage(Message

msg) {

switch(msg.what){

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

if(mReceiveCallBack!=null){

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

}

return;

caseTTY_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

*/

JNIEXPORTstaticvoidJNICALL com_notioni_uart_manager_TtyNativeControl_native_setup(JNIEnv *env,jobject clazz,jobject weak_this);

JNIEXPORTstaticintJNICALL com_notioni_uart_manager_TtyNativeControl__openTty(JNIEnv *env,jobject clazz);

JNIEXPORTstaticintJNICALL com_notioni_uart_manager_TtyNativeControl__closeTty(JNIEnv *env,jobject clazz);

JNIEXPORTstaticintJNICALL com_notioni_uart_manager_TtyNativeControl__sendMsgToTty(JNIEnv *env,jobject clazz,jbyteArray data);

JNIEXPORTstaticvoidJNICALL com_notioni_uart_manager_TtyNativeControl__receiveMsgFromTty(JNIEnv *env,jobject clazz);

JNIEXPORTstaticintJNICALL com_notioni_uart_manager_TtyNativeControl__configTty(JNIEnv *env,jobject clazz,intnBits,jchar nEvent,intnSpeed,intnStop);

JNIEXPORTstaticintJNICALL com_notioni_uart_manager_TtyNativeControl__setMode(JNIEnv *env,jobject clazz,intnMode,intshowLog);

//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操作串口《二》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值