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編程實現(xiàn)通過反射獲取資源Id的方法
這篇文章主要介紹了Android編程實現(xiàn)通過反射獲取資源Id的方法,結(jié)合實例形式分析了Android反射機制操作資源的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-01-01Android貝塞爾曲線初步學(xué)習(xí)第二課 仿QQ未讀消息氣泡拖拽黏連效果
這篇文章主要為大家詳細介紹了Android貝塞爾曲線初步學(xué)習(xí)的第二課,仿QQ未讀消息氣泡拖拽黏連效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03activity全屏實現(xiàn)沉浸式效果,并且單獨觸摸不會彈出虛擬按鍵的方法
今天小編就為大家分享一篇activity全屏實現(xiàn)沉浸式效果,并且單獨觸摸不會彈出虛擬按鍵的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android實戰(zhàn)打飛機游戲之怪物(敵機)類的實現(xiàn)(4)
這篇文章主要為大家詳細介紹了Android實戰(zhàn)打飛機游戲之怪物(敵機)類的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07Android?中TextureView和SurfaceView的屬性方法及示例說明
這篇文章主要介紹了Android?中TextureView和SurfaceView的屬性方法及示例說明,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06