Android串口操作方法實例
1.首先下載一個libserial_port.so,新建目錄libs/armeabi,將so文件放到該目錄下。
2.定義串口類,在類的構(gòu)建函數(shù)中修改權(quán)限,打開設(shè)備,創(chuàng)建輸入流和輸出流,通過native接口訪問串口打開關(guān)閉函數(shù)
public class SerialPort {
/*Do not remove or rename the field mFd: it is used by native method close();*/
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException, InvalidParameterException{
//如果串口權(quán)限不夠,改變權(quán)限
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
mFd = open(device.getAbsolutePath(), baudrate, flags);//打開串口
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);//串口輸入流
mFileOutputStream = new FileOutputStream(mFd);//串口輸出流
}
// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
// JNI
private native static FileDescriptor open(String path, int baudrate, int flags);//c文件中的串口open()函數(shù)
public native void close();//c文件中的串口close()函數(shù)
static {
System.loadLibrary("serial_port");//加載串口庫
}
}
}
3.定義抽象類ServerData
public abstract class ServerData {
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private class ReadThread extends Thread {
@Override
//在線程中讀取數(shù)據(jù)并處理數(shù)據(jù)
public void run() {
super.run();
byte[] buffer = new byte[128];
int size;
while(true) {
try {
if (mInputStream == null) return;
size = mInputStream.read(buffer);//讀取數(shù)據(jù)
if (size > 0) {
onDataReceived(buffer, size);//處理數(shù)據(jù)
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
4.實例化串口類,輸出流和輸入流,實例化讀取線程并開始執(zhí)行該線程
[code]
public ServerData(String path, int baudrate){
try {
mSerialPort = new SerialPort(new File(path), baudrate, 0);
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();
/* Create a receiving thread */
mReadThread = new ReadThread();
mReadThread.start();
} catch (SecurityException e) {
} catch (IOException e) {
} catch (InvalidParameterException e) {
}
}
protected abstract void onDataReceived(final byte[] buffer, final int size);
}
[/code]
5.然后再新建一個類,在新建的類中實現(xiàn)上面的抽象函數(shù),并寫一個函數(shù)返回讀取到的數(shù)據(jù)。
package View;
//導(dǎo)入R類,所在包不同,不能直接飲用,需要導(dǎo)入才可以使用
import android_serialport_api.sample.R;
/* EtcView類,Etc界面管理 */
public class SerialView {
private Activity context = null;
private Serial mEtcServer = null;
/* Etc界面構(gòu)造函數(shù) */
public SerialView(Activity context) {
this.context = context;
}
public void EtcInitView() {
//這樣才可以找到android_serialport_api.sample包下的id
TextView mytext=(TextView)context.findViewById(R.id.mytext);
mEtcServer = new Serial("/dev/s3c2410_serial3", 9600);
}
public void EtcRefresh() {
//返回串口線程讀取的數(shù)據(jù)
byte[] buffer = mEtcServer.getdata();
String recString=new String(buffer);//將byte[]的數(shù)組轉(zhuǎn)換成字符串string
mytext.setText(recString);//設(shè)置字符文本
buffer = null;
}
}
- Android串口通信apk源碼詳解(附完整源碼)
- Android串口通信之串口讀寫實例
- Android串口開發(fā)之使用JNI實現(xiàn)ANDROID和串口通信詳解
- android串口開發(fā)入門之搭建ndk開發(fā)環(huán)境及第一個jni調(diào)用程序
- Android串口通信封裝之OkUSB的示例代碼
- 詳解Android USB轉(zhuǎn)串口通信開發(fā)基本流程
- Android USB轉(zhuǎn)串口通信開發(fā)實例詳解
- Android 串口通信編程及串口協(xié)議分析
- Android開發(fā)之串口編程原理和實現(xiàn)方式
- Android實現(xiàn)藍牙串口通訊
相關(guān)文章
Android使用GridView實現(xiàn)日歷的方法
本篇文章主要介紹了Android使用GridView實現(xiàn)日歷的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Android適配器Adapter與ListView和RecycleView的簡單使用
本文將為大家介紹Android開發(fā)中常用的適配器(Adapter)概念,以及ListView和RecycleView的簡單使用方法,需要的朋友可以參考下2023-05-05
Android實現(xiàn)CoverFlow效果控件的實例代碼
這篇文章主要介紹了Android實現(xiàn)CoverFlow效果控件的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android?WebView緩存機制優(yōu)化加載慢問題
我知道你一定在煩惱Android?Webview的性能問題,特別突出的是-加載速度慢、消耗流量,針對Android?Webview的性能問題,提出一些有效解決方案2023-02-02
flutter BottomAppBar實現(xiàn)不規(guī)則底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter BottomAppBar實現(xiàn)不規(guī)則底部導(dǎo)航欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10
使用android studio導(dǎo)入模塊的兩種方法(超詳細(xì))
這篇文章主要介紹了使用android studio導(dǎo)入模塊的兩種方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09

