Bootstrap

.net thread操作串口_USB串口CH340芯片在Android中的使用

此实例需要用到ch340 驱动libs包,下载地址:http://www.wch.cn/downloads/CH341SER_ANDROID_ZIP.html

1、引入jar包:

a84fc649e026cd0fc7e424b88a7b2466.png

2、声明权限

<uses-permission android:name="android.permission.hardware.usb.host"/>

3、详细代码

import android.annotation.SuppressLint;import android.content.Context;import android.content.Intent;import android.hardware.usb.UsbManager;import android.os.Handler;import android.os.Message;import java.io.IOException;import java.util.Arrays;import java.util.concurrent.LinkedBlockingQueue;import cn.wch.ch34xuartdriver.CH34xUARTDriver;import es.dmoral.toasty.Toasty;/** * ch340串口工具 */public class Ch340xSerialPortUtils {
        public static Ch340xSerialPortUtils instance;    private static final String ACTION_USB_PERMISSION = "cn.wch.wchusbdriver.USB_PERMISSION";    private static CH34xUARTDriver driver;    private OnSerialPortListener onSerialPortListener;    private  boolean isOpen = false;    public int readLength = 1024; //读取长度    private byte[] receiveData = new byte[]{};//接收数据缓冲区    //是否读取数据    private boolean bReadThreadGoing = false;    private Ch340xSerialPortUtils.ReadThread read_thread;    private boolean isSendBroadCast = true;    private Thread writeThread;    public static Ch340xSerialPortUtils getInstance( Context context ) {
            if (instance == null) {
                synchronized (Ch340
;