Android串口开发

1.开发工具:Android Studio
2.所用资源:谷歌开源 serialPort api (下载地址:https://download.csdn.net/download/qq_30297763/10900944 )

开发:

  1. 将文件copy到相应位置:(注意:因为用的谷歌原生so库,所以SerialPort类的包名一定要是android_serialport_api,如果想修改这个包名,就需要重新生成对应的so库

Android串口开发_第1张图片

2.Android串口开发_第2张图片

  buildTypes {
        sourceSets {
            main {
                jni.srcDirs = []
            }
        }
    }

3.串口操作类:

import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android_serialport_api.SerialPort;

public class SerialPortUtils {
    private final String TAG = "SerialPortUtils";
    private String path = "/dev/ttyS4";  // 可根据设备不同修改
    private int baudrate = 9600; //波率
    public boolean serialPortStatus = false; //是否打开串口标志
    public boolean threadStatus; //线程状态,为了安全终止线程

    public SerialPort serialPort = null;
    public InputStream inputStream = null;
    public OutputStream outputStream = null;


    /**
     * 打开串口
     *
     * @return serialPort串口对象
     */
    public SerialPort openSerialPort() {
        try {
            serialPort = new SerialPort(new File(path), baudrate, 0);
            this.serialPortStatus = true;
            threadStatus = false; //线程状态

            //获取打开的串口中的输入输出流,以便于串口数据的收发
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();

            new ReadThread().start(); //开始线程监控是否有数据要接收
        } catch (IOException e) {
            Log.e(TAG, "openSerialPort: 打开串口异常:" + e.toString());
            return serialPort;
        }
        Log.d(TAG, "openSerialPort: 打开串口");
        return serialPort;
    }

    /**
     * 关闭串口
     */
    public void closeSerialPort() {
        try {
            inputStream.close();
            outputStream.close();

            this.serialPortStatus = false;
            this.threadStatus = true; //线程状态
            serialPort.close();
        } catch (IOException e) {
            Log.e(TAG, "closeSerialPort: 关闭串口异常:" + e.toString());
            return;
        }
        Log.d(TAG, "closeSerialPort: 关闭串口成功");
    }

    /**
     * 发送串口指令(字符串)
     * @param data String数据指令
     */
    public void sendSerialPort(String data) {
        if (!data.isEmpty()){
            Log.d(TAG, "sendSerialPort: 发送数据");
            data = data.toUpperCase();
            int len = data.length()/2;
            byte[] sendData = new byte[len];
            char[] hc = data.toCharArray();
            for (int i=0; i 0) {
                        Log.d(TAG, "run: 接收到了数据大小:" + String.valueOf(size));
                        onDataReceiveListener.onDataReceive(buffer, size);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "run: 数据读取异常:" + e.toString());
                }
            }
        }
    }

    //监听接收数据
    public OnDataReceiveListener onDataReceiveListener = null;

    public static interface OnDataReceiveListener {
        public void onDataReceive(byte[] buffer, int size);
    }

    public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) {
        onDataReceiveListener = dataReceiveListener;
    }

    /**
     * 字符转换为字节
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}

简单运用:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private SerialPortUtils serialPortUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serialPortUtils = new SerialPortUtils();
        serialPortUtils.openSerialPort();  //打开串口
        serialPortUtils.sendSerialPort("01010101");//发送指令

        //串口数据监听事件
        serialPortUtils.setOnDataReceiveListener(new SerialPortUtils.OnDataReceiveListener() {
            @Override
            public void onDataReceive(byte[] buffer, int size) {
                Log.d("SerialPort", "监听到的数据: " + new String(buffer));
            }
        });

        serialPortUtils.closeSerialPort();//关闭串口
    }

}

简单demo: ( https://download.csdn.net/download/qq_30297763/10901105 )

你可能感兴趣的:(Android)