Android通过JNI操作串口《一》
来源:互联网 发布:windows自动更新有用吗 编辑:程序博客网 时间:2024/05/20 06:23
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;
/**
*本地方法类
*/
publicclassTtyNativeControl {
privatefinalstatic StringTAG ="TtyNativeControl";
static{
System.loadLibrary("uart_ctl");
}
privatestaticfinalintTTY_MSG_RECEIVE= 1;
privatestaticfinalintTTY_CLOSE_DEVICE=TTY_MSG_RECEIVE+1;
private EventHandlermEventHandler;
private ReceiveCallbackmReceiveCallBack;
TtyNativeControl(){
mReceiveCallBack =null;
Looperlooper;
if((looper = Looper.myLooper())!=null){
mEventHandler =new EventHandler(this, looper);
}elseif((looper = Looper.getMainLooper())!=null){
mEventHandler =new EventHandler(this, looper);
}else{
mEventHandler =null;
}
native_setup(newWeakReference<TtyNativeControl>(this));
}
/**
*打开驱动
*@return是否打开成功
*/
publicint openTty(){
return _openTty();
}
/**
*关闭驱动,需要一定时间,所以采用Handler机制
*/
publicint closeTty(){
// mEventHandler.obtainMessage(TTY_CLOSE_DEVICE).sendToTarget();
// return1;
return _closeTty();
}
/**
*发送数据
*@param data
*@return
*/
public int sendMsgToTty(byte[] data){
return _sendMsgToTty(data);
}
/**
*接收数据
*@param callback
*/
publicfinalvoidreceiveMsgFromTty(ReceiveCallback callback){
mReceiveCallBack = callback;
_receiveMsgFromTty();
}
/**
*设置串口数据位,校验位,速率,停止位
*@param databits 数据位 取值位7或8
*@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
*/
publicint setMode(int mode ,int showLog){
return _setMode(mode,showLog);
}
/**
*接收数据回调接口
*/
publicinterface ReceiveCallback{
void onReceiveData(byte[] data,TtyNativeControltty);
}
/****************************************************************
*本地方法
*/
privatenativefinalvoid native_setup(Objecttty_this);
privatenativeint _openTty();
privatenativeint _closeTty();
privatenativeint _sendMsgToTty(byte[] data);
privatenativevoid _receiveMsgFromTty();
privatenativeint _configTty(int databits,char event,int speed,int stopBit);
privatenativeint _setMode(int mode,int showLog);
/*
*实现底层回调
*/
privatestaticvoidpostEventFromNative(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);
}
}
privateclass EventHandlerextends Handler{
private TtyNativeControlmTty;
publicEventHandler(TtyNativeControl t,Looper looper){
super(looper);
mTty = t;
}
@Override
publicvoid handleMessage(Messagemsg) {
switch(msg.what){
caseTTY_MSG_RECEIVE://底层接收数据回调上来的事件
if(mReceiveCallBack != null){
mReceiveCallBack.onReceiveData((byte[])msg.obj,mTty);
}
return;
caseTTY_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
*/
JNIEXPORTstaticvoid JNICALL com_notioni_uart_manager_TtyNativeControl_native_setup(JNIEnv *env,jobject clazz,jobject weak_this);
JNIEXPORTstaticint JNICALL com_notioni_uart_manager_TtyNativeControl__openTty(JNIEnv *env,jobject clazz);
JNIEXPORTstaticint JNICALL com_notioni_uart_manager_TtyNativeControl__closeTty(JNIEnv *env,jobject clazz);
JNIEXPORTstaticint JNICALL com_notioni_uart_manager_TtyNativeControl__sendMsgToTty(JNIEnv *env,jobject clazz,jbyteArray data);
JNIEXPORTstatic void JNICALL com_notioni_uart_manager_TtyNativeControl__receiveMsgFromTty(JNIEnv *env,jobject clazz);
JNIEXPORTstaticint JNICALL com_notioni_uart_manager_TtyNativeControl__configTty(JNIEnv *env,jobject clazz,int nBits,jchar nEvent,int nSpeed,int nStop);
JNIEXPORTstaticint 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操作串口《一》
- Android通过JNI操作串口
- Android通过JNI操作串口
- Android通过JNI操作串口《二》
- Android通过JNI操作串口《三》
- Android通过JNI操作串口《四》
- Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块
- Android - JNI 串口通信
- android 串口通讯 JNI
- Android Jni 多线程 蓝牙串口收发 实例 一
- 浅析android下如何通过jni监控wifi(一)
- 移植soundtouch 到android 通过JNI来调用(一)
- android 串口jni 开发流程
- Android Studio 串口jni开发
- Android开发之jni串口
- Android开发之jni串口
- JNI基础实验一:调用.so文件--友善之臂Tiny210 android 串口/pwm/ADC/LED
- Android JNI操作指南
- 如何往Images.xcassets加入图片
- ARC指南1 - strong和weak指针
- 2014-04-08
- 值传递引用误区
- ARC指南3 - @property
- Android通过JNI操作串口《一》
- 多线程编程1 - NSThread
- opencv 消除相机透镜畸变和视角变换
- 重学c语言系列三---变量在内存中的布局
- Android通过JNI操作串口《二》
- java各类异常
- 支付宝公众服务接口和微信公众平台接口的区别
- 2014-04-03
- python 文件操作