android framework层本来提供了SerialPort和SerialManager两个类来操作串口,本文提供的是一种利用jni操作串口的方法,app层也可以使用。言归正传,下面来详细说下过程:
一:在frameworks/base/serivces/core/java/com/android/server 新建文件夹tnport,里面分别有
1:ComBean
package com.android.server.tnport; import java.text.SimpleDateFormat; /** * Author: ZHY * Email: 2892478492@qq.com * Time: 2017/10/12. 13:49 * Package: com.android.server.tnport * FileName: ComBean */ public class ComBean { public byte[] bRec = null; public String sRecTime=""; public String sComPort=""; public ComBean(String sPort,byte[] buffer,int size) { sComPort = sPort; bRec = new byte[size]; for (int i = 0; i < size; i++) { bRec[i] = buffer[i]; } SimpleDateFormat sDateFormat = new SimpleDateFormat("hh:mm:ss"); sRecTime = sDateFormat.format(new java.util.Date()); } }
2:MyFunc
package com.android.server.tnport; public class MyFunc { //------------------------------------------------------- static public int isOdd(int num) { return num & 0x1; } //------------------------------------------------------- static public int HexToInt(String inHex)// { return Integer.parseInt(inHex, 16); } //------------------------------------------------------- static public byte HexToByte(String inHex)// { return (byte)Integer.parseInt(inHex,16); } //------------------------------------------------------- static public String Byte2Hex(Byte inByte) { return String.format("%02x", inByte).toUpperCase(); } //------------------------------------------------------- static public String ByteArrToHex(byte[] inBytArr) { StringBuilder strBuilder=new StringBuilder(); int j=inBytArr.length; for (int i = 0; i < j; i++) { strBuilder.append(Byte2Hex(inBytArr[i])); strBuilder.append(" "); } return strBuilder.toString(); } //------------------------------------------------------- static public String ByteArrToHex(byte[] inBytArr,int offset,int byteCount) { StringBuilder strBuilder=new StringBuilder(); int j=byteCount; for (int i = offset; i < j; i++) { strBuilder.append(Byte2Hex(inBytArr[i])); } return strBuilder.toString(); } static public byte[] HexToByteArr(String inHex) { int hexlen = inHex.length(); byte[] result; if (isOdd(hexlen)==1) { hexlen++; result = new byte[(hexlen/2)]; inHex="0"+inHex; }else { result = new byte[(hexlen/2)]; } int j=0; for (int i = 0; i < hexlen; i+=2) { result[j]=HexToByte(inHex.substring(i,i+2)); j++; } return result; } }
3:SerialHelper
package com.android.server.tnport; import android.util.Slog; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidParameterException; public abstract class SerialHelper { private static final String TAG = "SerialHelper"; private SerialPort mSerialPort; private OutputStream mOutputStream; private InputStream mInputStream; private ReadThread mReadThread; private SendThread mSendThread; private String sPort = "/dev/ttyS4"; private int iBaudRate = 9600; private boolean _isOpen = false; private byte[] _bLoopData = new byte[]{ 0x30}; private int iDelay = 500; //---------------------------------------------------- public SerialHelper(String sPort, int iBaudRate) { this.sPort = sPort; this.iBaudRate = iBaudRate; } public SerialHelper() { this("/dev/ttyS4", 9600); } public SerialHelper(String sPort) { this(sPort, 9600); } public SerialHelper(String sPort, String sBaudRate) { this