Browse Source

1. 使用策略模式处理发送数据事件
2. 完善Demo工程

王育民 5 years ago
parent
commit
96fc1e39ce

+ 2 - 0
app/build.gradle

@@ -48,6 +48,8 @@ dependencies {
     testImplementation 'junit:junit:4.13'
     androidTestImplementation 'com.android.support.test:runner:1.0.2'
     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
+    implementation 'com.jakewharton:butterknife:8.8.1'
+    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
     // implementation(name: 'serialport-release', ext: 'aar')
     implementation project(path: ':serialport')
 }

+ 54 - 38
app/src/main/java/cn/minbb/serial/activities/MainActivity.java

@@ -9,32 +9,44 @@ import android.widget.TextView;
 import android.widget.Toast;
 
 import java.io.File;
-import java.nio.charset.Charset;
+import java.util.Arrays;
 
+import butterknife.BindView;
+import butterknife.ButterKnife;
 import cn.minbb.serial.R;
 import cn.minbb.serialport.SerialPortManager;
+import cn.minbb.serialport.listener.OnDataProgressListener;
 import cn.minbb.serialport.listener.OnOpenSerialPortListener;
 import cn.minbb.serialport.listener.OnSerialPortDataListener;
 import cn.minbb.serialport.utils.ByteUtil;
 
 public class MainActivity extends AppCompatActivity {
 
+    @BindView(R.id.open_serial_port)
+    Button openSerial;
+    @BindView(R.id.close_serial_port)
+    Button closeSerial;
+    @BindView(R.id.send_string)
+    Button sendString;
+    @BindView(R.id.send_file)
+    Button sendFile;
+    @BindView(R.id.edit_text)
+    EditText editText;
+    @BindView(R.id.text_view)
+    TextView textView;
+
     private String TAG = "MainActivity";
-    private Button openSerial, closeSerial;
-    private EditText editText;
-    private TextView textView;
     private SerialPortManager serialPortManager;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
+        ButterKnife.bind(this);
 
-        openSerial = findViewById(R.id.open_serial_port);
-        closeSerial = findViewById(R.id.close_serial_port);
         closeSerial.setEnabled(false);
-        editText = findViewById(R.id.edit_text);
-        textView = findViewById(R.id.text_view);
+        sendString.setEnabled(false);
+        sendFile.setEnabled(false);
 
         serialPortManager = new SerialPortManager();
         serialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
@@ -45,7 +57,7 @@ public class MainActivity extends AppCompatActivity {
 
             @Override
             public void onFail(File device, Status status) {
-                Toast.makeText(getApplicationContext(), "串口打开失败", Toast.LENGTH_SHORT).show();
+                Toast.makeText(getApplicationContext(), "串口打开失败" + status.getDescription(), Toast.LENGTH_SHORT).show();
             }
         });
         serialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
@@ -53,17 +65,18 @@ public class MainActivity extends AppCompatActivity {
             public void onDataReceived(byte[] bytes, String data) {
                 runOnUiThread(() -> {
                     byte[] bytes1 = ByteUtil.addBytes(data.getBytes(), new byte[64]);
-                    setText(data, data.length());
-                    try {
-                        if (serialPortManager.sendBytes(bytes1)) {
-                            Toast.makeText(getApplicationContext(), "发送成功 = " + data, Toast.LENGTH_SHORT).show();
-                        } else {
-                            Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_SHORT).show();
-                        }
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        Toast.makeText(getApplicationContext(), "发送失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
-                    }
+//                    setText(data, data.length());
+                    setText(Arrays.toString(bytes), bytes.length);
+//                    try {
+//                        if (serialPortManager.sendBytes(bytes1)) {
+//                            Toast.makeText(getApplicationContext(), "发送成功 = " + data, Toast.LENGTH_SHORT).show();
+//                        } else {
+//                            Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_SHORT).show();
+//                        }
+//                    } catch (Exception e) {
+//                        e.printStackTrace();
+//                        Toast.makeText(getApplicationContext(), "发送失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
+//                    }
                 });
             }
 
@@ -85,6 +98,8 @@ public class MainActivity extends AppCompatActivity {
                     Toast.makeText(getApplicationContext(), "串口打开成功", Toast.LENGTH_SHORT).show();
                     openSerial.setEnabled(false);
                     closeSerial.setEnabled(true);
+                    sendString.setEnabled(true);
+                    sendFile.setEnabled(true);
                 } else {
                     Toast.makeText(getApplicationContext(), "串口打开失败", Toast.LENGTH_SHORT).show();
                 }
@@ -94,29 +109,30 @@ public class MainActivity extends AppCompatActivity {
                 serialPortManager.closeSerialPort();
                 openSerial.setEnabled(true);
                 closeSerial.setEnabled(false);
+                sendString.setEnabled(false);
+                sendFile.setEnabled(false);
                 break;
-            case R.id.send:
-                // byte[] reportID = {0x0};
-                // byte[] data = ByteUtil.addBytes(reportID, editText.getText().toString().getBytes());
-                boolean sendBytes = false;
-                try {
-                    sendBytes = serialPortManager.sendString(editText.getText().toString());
-                    if (sendBytes) {
-                        Toast.makeText(getApplicationContext(), "数据发送成功", Toast.LENGTH_SHORT).show();
-                    } else {
-                        Toast.makeText(getApplicationContext(), "数据发送失败", Toast.LENGTH_SHORT).show();
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                    Toast.makeText(getApplicationContext(), "数据发送失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
-                }
-                break;
-            case R.id.clearSend:
+            case R.id.clear_send:
                 editText.setText("");
                 break;
-            case R.id.clearText:
+            case R.id.clear_text:
                 textView.setText("");
                 break;
+            case R.id.send_string:
+                // byte[] reportID = {0x0};
+                // byte[] data = ByteUtil.addBytes(reportID, editText.getText().toString().getBytes());
+                String s = editText.getText().toString();
+                serialPortManager.sendString(s, (progress, index, total, bytes) -> log(Arrays.toString(bytes)));
+                log("发送字符串:" + s);
+                Toast.makeText(getApplicationContext(), "数据已发送", Toast.LENGTH_SHORT).show();
+                break;
+            case R.id.send_file:
+                break;
         }
     }
+
+    private void log(String log) {
+        StringBuilder stringBuilder = new StringBuilder(textView.getText());
+        textView.setText(stringBuilder.append("\n").append(log));
+    }
 }

+ 31 - 22
app/src/main/res/layout/activity_main.xml

@@ -9,7 +9,7 @@
 
     <Button
         android:id="@+id/open_serial_port"
-        android:layout_width="match_parent"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="onClick"
         android:text="打开串口"
@@ -18,12 +18,30 @@
 
     <Button
         android:id="@+id/close_serial_port"
-        android:layout_width="match_parent"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="onClick"
         android:text="关闭串口"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/open_serial_port" />
+        app:layout_constraintStart_toEndOf="@+id/open_serial_port"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <Button
+        android:id="@+id/clear_send"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:onClick="onClick"
+        android:text="清除发送区"
+        app:layout_constraintEnd_toStartOf="@id/clear_text"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <Button
+        android:id="@+id/clear_text"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:onClick="onClick"
+        android:text="清除接收区"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
 
     <EditText
         android:id="@+id/edit_text"
@@ -34,38 +52,29 @@
         app:layout_constraintTop_toBottomOf="@id/close_serial_port" />
 
     <Button
-        android:id="@+id/send"
-        android:layout_width="0dp"
+        android:id="@+id/send_string"
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="onClick"
-        android:text="发送"
+        android:text="发送字符串"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@id/edit_text" />
 
     <Button
-        android:id="@+id/clearSend"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:onClick="onClick"
-        android:text="清除发送区"
-        app:layout_constraintEnd_toStartOf="@id/clearText"
-        app:layout_constraintTop_toBottomOf="@id/edit_text" />
-
-    <Button
-        android:id="@+id/clearText"
-        android:layout_width="0dp"
+        android:id="@+id/send_file"
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="onClick"
-        android:text="清除接收区"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/edit_text" />
+        android:text="发送文件"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/send_string" />
 
     <ScrollView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="16dp"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@id/send">
+        app:layout_constraintTop_toBottomOf="@id/send_file">
 
         <TextView
             android:id="@+id/text_view"

+ 2 - 2
serialport/src/main/cpp/SerialPort.h

@@ -13,7 +13,7 @@ extern "C" {
  * Method:    open
  * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
  */
-JNIEXPORT jobject JNICALL Java_com_kongqw_serialportlibrary_SerialPort_open
+JNIEXPORT jobject JNICALL Java_cn_minbb_serialport_SerialPort_open
   (JNIEnv *, jclass, jstring, jint, jint);
 
 /*
@@ -21,7 +21,7 @@ JNIEXPORT jobject JNICALL Java_com_kongqw_serialportlibrary_SerialPort_open
  * Method:    close
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_com_kongqw_serialportlibrary_SerialPort_close
+JNIEXPORT void JNICALL Java_cn_minbb_serialport_SerialPort_close
   (JNIEnv *, jobject);
 
 #ifdef __cplusplus

+ 78 - 24
serialport/src/main/java/cn/minbb/serialport/SerialPortManager.java

@@ -8,12 +8,19 @@ import android.util.Log;
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.nio.charset.Charset;
 import java.util.Timer;
 import java.util.TimerTask;
 
+import cn.minbb.serialport.app.Data;
+import cn.minbb.serialport.app.DataType;
+import cn.minbb.serialport.handler.SendDataContext;
+import cn.minbb.serialport.impl.SendDataBytesImpl;
+import cn.minbb.serialport.impl.SendDataFileImpl;
+import cn.minbb.serialport.impl.SendDataStringImpl;
+import cn.minbb.serialport.listener.OnDataProgressListener;
 import cn.minbb.serialport.listener.OnOpenSerialPortListener;
 import cn.minbb.serialport.listener.OnSerialPortDataListener;
 import cn.minbb.serialport.thread.SerialPortReadThread;
@@ -29,6 +36,7 @@ public class SerialPortManager extends SerialPort {
     private FileDescriptor mFd;
     private OnOpenSerialPortListener mOnOpenSerialPortListener;
     private OnSerialPortDataListener mOnSerialPortDataListener;
+    private OnDataProgressListener onDataProgressListener;
 
     private HandlerThread mSendingHandlerThread;
     private Handler mSendingHandler;
@@ -150,17 +158,40 @@ public class SerialPortManager extends SerialPort {
         mSendingHandler = new Handler(mSendingHandlerThread.getLooper()) {
             @Override
             public void handleMessage(Message msg) {
-                byte[] sendBytes = (byte[]) msg.obj;
-
-                if (null != mFileOutputStream && null != sendBytes && 0 < sendBytes.length) {
-                    try {
-                        mFileOutputStream.write(sendBytes);
-                        if (null != mOnSerialPortDataListener) {
-                            mOnSerialPortDataListener.onDataSent(sendBytes);
-                        }
-                    } catch (IOException e) {
-                        e.printStackTrace();
+                try {
+//                    byte[] sendBytes = new byte[64];
+                    Data data = (Data) msg.obj;
+                    switch (data.getDataType()) {
+                        case BYTES:
+                            SendDataBytesImpl sendDataBytes = new SendDataBytesImpl();
+                            SendDataContext<byte[]> contextBytes = new SendDataContext<>(mFileInputStream, mFileOutputStream, onDataProgressListener, sendDataBytes);
+                            contextBytes.doSend((byte[]) data.getData());
+                            break;
+                        case STRING:
+                            SendDataStringImpl sendDataString = new SendDataStringImpl();
+                            SendDataContext<String> contextString = new SendDataContext<>(mFileInputStream, mFileOutputStream, onDataProgressListener, sendDataString);
+                            contextString.doSend((String) data.getData());
+                            break;
+                        case FILE:
+                            SendDataFileImpl sendDataFile = new SendDataFileImpl();
+                            SendDataContext<File> contextFile = new SendDataContext<>(mFileInputStream, mFileOutputStream, onDataProgressListener, sendDataFile);
+                            contextFile.doSend((File) data.getData());
+                            break;
                     }
+//                    if (null != mFileOutputStream && null != sendBytes && 0 < sendBytes.length) {
+//                        try {
+//                            mFileOutputStream.write(sendBytes);
+//                            if (null != mOnSerialPortDataListener) {
+//                                mOnSerialPortDataListener.onDataSent(sendBytes);
+//                            }
+//                        } catch (IOException e) {
+//                            e.printStackTrace();
+//                        }
+//                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                } catch (Exception e) {
+                    e.printStackTrace();
                 }
             }
         };
@@ -241,27 +272,50 @@ public class SerialPortManager extends SerialPort {
      * 发送字符数组数据
      *
      * @param sendBytes 字符数组
-     * @return 发送是否成功
      */
-    public boolean sendBytes(byte[] sendBytes) throws Exception {
-        if (null != mFd && null != mFileInputStream && null != mFileOutputStream) {
-            if (null != mSendingHandler) {
-                Message message = Message.obtain();
-                message.obj = sendBytes;
-                return mSendingHandler.sendMessage(message);
-            }
-            throw new RuntimeException("缺失发送处理器");
-        }
-        throw new RuntimeException("数据流为空");
+    public void sendBytes(byte[] sendBytes, OnDataProgressListener onDataProgressListener) {
+        this.onDataProgressListener = onDataProgressListener;
+        sendData(new Data(sendBytes, DataType.BYTES));
     }
 
     /**
      * 发送字符串数据(UTF-8编码)
      *
      * @param string 字符串
+     */
+    public void sendString(String string, OnDataProgressListener onDataProgressListener) {
+        Data data = new Data(string, DataType.STRING);
+        this.onDataProgressListener = onDataProgressListener;
+        sendData(data);
+    }
+
+    /**
+     * 发送文件数据
+     *
+     * @param file 文件
+     */
+    public void sendFile(File file, OnDataProgressListener onDataProgressListener) throws Exception {
+        if (!file.exists()) throw new FileNotFoundException("文件不存在");
+        if (file.isDirectory()) throw new RuntimeException("文件夹不可以发送");
+        this.onDataProgressListener = onDataProgressListener;
+        sendData(new Data(file, DataType.FILE));
+    }
+
+    /**
+     * 发送数据
+     *
+     * @param data 数据
      * @return 是否发送成功
      */
-    public boolean sendString(String string) throws Exception {
-        return !string.isEmpty() && sendBytes(string.getBytes(Charset.forName("UTF-8")));
+    private boolean sendData(Data data) {
+        if (null != mFd && null != mFileInputStream && null != mFileOutputStream) {
+            if (null != mSendingHandler) {
+                Message message = Message.obtain();
+                message.obj = data;
+                return mSendingHandler.sendMessage(message);
+            }
+            throw new RuntimeException("缺失发送处理器");
+        }
+        throw new RuntimeException("数据流为空");
     }
 }

+ 8 - 0
serialport/src/main/java/cn/minbb/serialport/app/Const.java

@@ -0,0 +1,8 @@
+package cn.minbb.serialport.app;
+
+public class Const {
+    public interface ErrorCode {
+        // 传输中断异常
+        String TRANSMISSION_INTERRUPTION_EXCEPTION = "-1000";
+    }
+}

+ 28 - 0
serialport/src/main/java/cn/minbb/serialport/app/Data.java

@@ -0,0 +1,28 @@
+package cn.minbb.serialport.app;
+
+public class Data {
+
+    private Object data;
+    private DataType dataType;
+
+    public Data(Object data, DataType dataType) {
+        this.data = data;
+        this.dataType = dataType;
+    }
+
+    public Object getData() {
+        return data;
+    }
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+
+    public DataType getDataType() {
+        return dataType;
+    }
+
+    public void setDataType(DataType dataType) {
+        this.dataType = dataType;
+    }
+}

+ 10 - 0
serialport/src/main/java/cn/minbb/serialport/app/DataType.java

@@ -0,0 +1,10 @@
+package cn.minbb.serialport.app;
+
+public enum DataType {
+    // 数据位
+    BYTES,
+    // 字符串
+    STRING,
+    // 文件
+    FILE
+}

+ 44 - 0
serialport/src/main/java/cn/minbb/serialport/handler/SendDataContext.java

@@ -0,0 +1,44 @@
+package cn.minbb.serialport.handler;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+import cn.minbb.serialport.listener.OnDataProgressListener;
+
+public class SendDataContext<T> {
+
+    private FileInputStream fileInputStream;
+    private FileOutputStream fileOutputStream;
+    private OnDataProgressListener onDataProgressListener;
+    private SendDataHandler<T> sendDataHandler;
+
+    public SendDataContext(FileInputStream fileInputStream, FileOutputStream fileOutputStream, OnDataProgressListener onDataProgressListener, SendDataHandler<T> sendDataHandler) {
+        this.fileInputStream = fileInputStream;
+        this.fileOutputStream = fileOutputStream;
+        this.onDataProgressListener = onDataProgressListener;
+        this.sendDataHandler = sendDataHandler;
+    }
+
+    public void doSend(T t) throws Exception {
+        if (null == fileInputStream) throw new RuntimeException("文件输入流为空");
+        if (null == fileOutputStream) throw new RuntimeException("文件输出流为空");
+        if (null == sendDataHandler) throw new RuntimeException("未设置发送数据处理器");
+        sendDataHandler.send(fileInputStream, fileOutputStream, onDataProgressListener, t);
+    }
+
+    public void setFileInputStream(FileInputStream fileInputStream) {
+        this.fileInputStream = fileInputStream;
+    }
+
+    public void setFileOutputStream(FileOutputStream fileOutputStream) {
+        this.fileOutputStream = fileOutputStream;
+    }
+
+    public void setOnDataProgressListener(OnDataProgressListener onDataProgressListener) {
+        this.onDataProgressListener = onDataProgressListener;
+    }
+
+    public void setSendDataHandler(SendDataHandler<T> sendDataHandler) {
+        this.sendDataHandler = sendDataHandler;
+    }
+}

+ 11 - 0
serialport/src/main/java/cn/minbb/serialport/handler/SendDataHandler.java

@@ -0,0 +1,11 @@
+package cn.minbb.serialport.handler;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import cn.minbb.serialport.listener.OnDataProgressListener;
+
+public interface SendDataHandler<T> {
+    void send(FileInputStream fileInputStream, FileOutputStream fileOutputStream, OnDataProgressListener onDataProgressListener, T t) throws IOException;
+}

+ 16 - 0
serialport/src/main/java/cn/minbb/serialport/impl/SendDataBytesImpl.java

@@ -0,0 +1,16 @@
+package cn.minbb.serialport.impl;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import cn.minbb.serialport.handler.SendDataHandler;
+import cn.minbb.serialport.listener.OnDataProgressListener;
+
+public class SendDataBytesImpl implements SendDataHandler<byte[]> {
+
+    @Override
+    public void send(FileInputStream fileInputStream, FileOutputStream fileOutputStream, OnDataProgressListener onDataProgressListener, byte[] bytes) throws IOException {
+        fileOutputStream.write(bytes);
+    }
+}

+ 27 - 0
serialport/src/main/java/cn/minbb/serialport/impl/SendDataFileImpl.java

@@ -0,0 +1,27 @@
+package cn.minbb.serialport.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import cn.minbb.serialport.handler.SendDataHandler;
+import cn.minbb.serialport.listener.OnDataProgressListener;
+
+public class SendDataFileImpl implements SendDataHandler<File> {
+
+    @Override
+    public void send(FileInputStream fileInputStream, FileOutputStream fileOutputStream, OnDataProgressListener onDataProgressListener, File file) throws IOException {
+        FileInputStream fis = new FileInputStream(file);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        int len;
+        byte[] buffer = new byte[1024];
+        while ((len = fis.read(buffer)) != -1) {
+            baos.write(buffer, 0, len);
+        }
+        byte[] byteArray = baos.toByteArray();
+        fis.close();
+        baos.close();
+    }
+}

+ 26 - 0
serialport/src/main/java/cn/minbb/serialport/impl/SendDataStringImpl.java

@@ -0,0 +1,26 @@
+package cn.minbb.serialport.impl;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import cn.minbb.serialport.handler.SendDataHandler;
+import cn.minbb.serialport.listener.OnDataProgressListener;
+
+public class SendDataStringImpl implements SendDataHandler<String> {
+
+    @Override
+    public void send(FileInputStream fileInputStream, FileOutputStream fileOutputStream, OnDataProgressListener onDataProgressListener, String s) throws IOException {
+        byte[] dataBytes = s.getBytes(Charset.forName("UTF-8"));
+        fileOutputStream.write(dataBytes);
+        byte[] buffer = new byte[64];
+        int length;
+        while ((length = fileInputStream.read(buffer)) != -1) {
+            System.out.println(new String(buffer, 0, length));
+            if (null != onDataProgressListener) {
+                onDataProgressListener.onProgress(0, 0, 0, buffer);
+            }
+        }
+    }
+}

+ 15 - 0
serialport/src/main/java/cn/minbb/serialport/listener/OnDataProgressListener.java

@@ -0,0 +1,15 @@
+package cn.minbb.serialport.listener;
+
+/**
+ * 数据发送进度监听器
+ */
+public interface OnDataProgressListener {
+    /**
+     * 进度回调
+     *
+     * @param progress 当前进度 计算方法为 index / total 最大值为 1
+     * @param index    当前发送的数据包索引 从 1 开始
+     * @param total    总数据包数量
+     */
+    void onProgress(double progress, int index, int total, byte[] bytes);
+}

+ 12 - 2
serialport/src/main/java/cn/minbb/serialport/listener/OnOpenSerialPortListener.java

@@ -12,7 +12,17 @@ public interface OnOpenSerialPortListener {
     void onFail(File device, Status status);
 
     enum Status {
-        NO_READ_WRITE_PERMISSION,
-        OPEN_FAIL
+        NO_READ_WRITE_PERMISSION("没有读写权限"),
+        OPEN_FAIL("打开串口失败");
+
+        private String description;
+
+        Status(String description) {
+            this.description = description;
+        }
+
+        public String getDescription() {
+            return description;
+        }
     }
 }