• 标记当前串口状态(true:打开,false:关闭)
**/
public static boolean isFlagSerial = false;
public static SerialPort serialPort = null;
public static InputStream inputStream = null;
public static OutputStream outputStream = null;
public static Thread receiveThread = null;
public static String strData = “”;
public static Handler mHandler;
/**
• 打开串口
*/
public static boolean open() {
boolean isopen = false;
if(isFlagSerial){
LogUtils.e(TAG,“串口已经打开,打开失败”);
return false;
}
try {
serialPort = new SerialPort(new File(“/dev/ttyS3”), 115200, 0);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
receive();
isopen = true;
isFlagSerial = true;
} catch (IOException e) {
e.printStackTrace();
isopen = false;
}
return isopen;
}
/**
• 关闭串口
*/
public static boolean close() {
if(isFlagSerial){
LogUtils.e(TAG,“串口关闭失败”);
return false;
}
boolean isClose = false;
LogUtils.e(TAG, “关闭串口”);
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
isClose = true;
isFlagSerial = false;//关闭串口时,连接状态标记为false
} catch (IOException e) {
e.printStackTrace();
isClose = false;
}
return isClose;
}
/**
• 发送串口指令
*/
public static void sendString(String data, Handler handler) {
mHandler = handler;
if (!isFlagSerial) {
LogUtils.e(TAG, “串口未打开,发送失败” + data);
return;
}
try {
outputStream.write(ByteUtil.hex2byte(data));
outputStream.flush();
LogUtils.e(TAG, “sendSerialData:” + data);
} catch (IOException e) {
e.printStackTrace();
LogUtils.e(TAG, “发送指令出现异常”);
}
}
/**
• 接收串口数据的方法
*/
public static void receive() {
if (receiveThread != null && !isFlagSerial) {
return;
}
receiveThread = new Thread() {
@Override
public void run() {
while (isFlagSerial) {
try {
byte[] readData = new byte[32];
if (inputStream == null) {
return;
}
int size = inputStream.read(readData);
if (size > 0 && isFlagSerial) {
strData = ByteUtil.byteToStr(readData, size);
LogUtils.e(TAG, “readSerialData:” + strData);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
receiveThread.start();
}
}

这个类就比较重要了,打开串口、关闭串口、读写操作,都在这个类里面写了详细的注释,另外下面在贴一个工具类出来

package com.sqy.scancode.util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
/**
• Created by Administrator on 2018/6/15.
*/
public class ByteUtil {
/**
• 字符串转化成为16进制字符串
• 
• @param s
• @return
*/
public static String strTo16(String s) {
String str = “”;
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
/**
• 16进制转换成为string类型字符串
• 
• @param s
• @return
*/
public static String hexStringToString(String s) {
if (s == null || s.equals(“”)) {
return null;
}
s = s.replace(" ", “”);
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, “UTF-8”);
new String();
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
/**
• 向串口发送数据转为字节数组
*/
public static byte[] hex2byte(String hex) {
String digital = “0123456789ABCDEF”;
String hex1 = hex.replace(" ", “”);
char[] hex2char = hex1.toCharArray();
byte[] bytes = new byte[hex1.length() / 2];
byte temp;
for (int p = 0; p < bytes.length; p++) {
temp = (byte) (digital.indexOf(hex2char[2 * p]) * 16);
temp += digital.indexOf(hex2char[2 * p + 1]);
bytes[p] = (byte) (temp & 0xff);
}
return bytes;
}
/**
• 接收到的字节数组转换16进制字符串
*/
public static String bytes2HexString(byte[] b, int size) {
String ret = “”;
for (int i = 0; i < size; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = ‘0’ + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder(“”);
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
• 接收到的字节数组转换16进制字符串
*/
public static String byteToStr(byte[] b, int size) {
String ret = “”;
for (int i = 0; i < size; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = ‘0’ + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
/**
• BASE64码解密成图片
*/
public static Bitmap Base64ToImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
BASE64Decoder decoder = new BASE64Decoder();
Bitmap bitmap = null;
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
bitmap = BitmapFactory.decodeByteArray(b,0,b.length);
return bitmap;
} catch (Exception e) {
LogUtils.e(“TAG”,“解析异常”);
return bitmap;
}
}
/**
• 将图片转换为base64加密数据
*/
public static String ImageToBase64(String imgFile) {
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
LogUtils.e(“TAG”,“加密异常”);
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
• 计算CRC16校验码
• 逐个求和
• 
• @param bytes 字节数组
• @return {@link String} 校验码
• @since 1.0
*/
public static String getCRC_16(byte[] bytes) {
int CRC = 0x0000ffff;
int POLYNOMIAL = 0x0000a001;
int i, j;
for (i = 0; i < bytes.length; i++) {
CRC ^= ((int) bytes[i] & 0x000000ff);
for (j = 0; j < 8; j++) {
if ((CRC & 0x00000001) != 0) {
CRC >>= 1;
CRC ^= POLYNOMIAL;
} else {
CRC >>= 1;
}
}
}
if (Integer.toHexString(CRC).toUpperCase().length() == 2) {
return byteToStr(bytes, bytes.length) + “00” + Integer.toHexString(CRC).toUpperCase();
} else if (Integer.toHexString(CRC).toUpperCase().length() == 3) {
return byteToStr(bytes, bytes.length) + “0” + Integer.toHexString(CRC).toUpperCase();
}
return byteToStr(bytes, bytes.length) + Integer.toHexString(CRC).toUpperCase();
}
/**
• 指令校验和,并取出后两位字节
• */
public static String getSum16(byte[] msg, int length) {
long mSum = 0;
byte[] mByte = new byte[length];
/** 逐Byte添加位数和 */
for (byte byteMsg : msg) {
long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256);
mSum += mNum;
} /** end of for (byte byteMsg : msg) */
/** 位数和转化为Byte数组 */
for (int liv_Count = 0; liv_Count < length; liv_Count++) {
mByte[length - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);
} /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */
return byteToStr(msg, length) + byteToStr(mByte, mByte.length).substring(byteToStr(mByte, mByte.length).length() - 4, byteToStr(mByte, mByte.length).length());
}
}

4、demo下载地址 : https://github.com/z-jc/ScanCode   里面可能还会有一些别的功能,需要的话自行下载

5、另外再提供一个自己封装好的module,https://github.com/z-jc/SerialProject-master,app导入module

android 串口数据并包 android操作串口_android 串口数据并包

然后在activity内直接这样调用

最后

只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。

sdn.net/20180721091700752?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMyMTM2ODI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

然后在activity内直接这样调用