Browse Source

1. 优化串口管理器
2. 更新gradle版本

王育民 5 years ago
parent
commit
69703fdbae

+ 1 - 2
app/src/main/java/cn/minbb/serial/activities/MainActivity.java

@@ -36,7 +36,7 @@ public class MainActivity extends AppCompatActivity {
     TextView textView;
 
     private String TAG = "MainActivity";
-    private SerialPortManager serialPortManager;
+    private SerialPortManager serialPortManager = SerialPortManager.getInstance();
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -48,7 +48,6 @@ public class MainActivity extends AppCompatActivity {
         sendString.setEnabled(false);
         sendFile.setEnabled(false);
 
-        serialPortManager = new SerialPortManager();
         serialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
             @Override
             public void onSuccess(File device) {

+ 1 - 1
build.gradle

@@ -8,7 +8,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.5.3'
+        classpath 'com.android.tools.build:gradle:3.6.2'
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files

+ 2 - 2
gradle/wrapper/gradle-wrapper.properties

@@ -1,6 +1,6 @@
-#Tue Jan 14 14:44:43 CST 2020
+#Thu Apr 09 09:44:20 CST 2020
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

+ 41 - 2
serialport/src/main/java/cn/minbb/serialport/SerialPortManager.java

@@ -11,6 +11,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.Serializable;
 import java.util.Timer;
 import java.util.TimerTask;
 
@@ -26,9 +27,9 @@ import cn.minbb.serialport.listener.OnSerialPortDataListener;
 import cn.minbb.serialport.thread.SerialPortReadThread;
 
 /**
- * SerialPortManager
+ * 串口管理器 - 单例模式  防止 反射 克隆 序列化 破坏
  */
-public class SerialPortManager extends SerialPort {
+public class SerialPortManager extends SerialPort implements Serializable, Cloneable {
 
     private static final String TAG = SerialPortManager.class.getSimpleName();
     private FileInputStream mFileInputStream;
@@ -42,6 +43,32 @@ public class SerialPortManager extends SerialPort {
     private Handler mSendingHandler;
     private SerialPortReadThread mSerialPortReadThread;
 
+    // 默认是第一次创建
+    private static volatile boolean isCreate = false;
+    // 单例对象
+    private static SerialPortManager serialPortManager = null;
+
+    private SerialPortManager() {
+        if (isCreate) {
+            throw new RuntimeException("已然被实例化一次,不能在实例化");
+        }
+        isCreate = true;
+    }
+
+    /**
+     * 线程安全 双重检查加锁
+     */
+    public static SerialPortManager getInstance() {
+        if (null == serialPortManager) {
+            synchronized (SerialPortManager.class) {
+                if (null == serialPortManager) {
+                    serialPortManager = new SerialPortManager();
+                }
+            }
+        }
+        return serialPortManager;
+    }
+
     /**
      * 打开串口
      * <p>
@@ -318,4 +345,16 @@ public class SerialPortManager extends SerialPort {
         }
         throw new RuntimeException("数据流为空");
     }
+
+    @Override
+    protected Object clone() throws CloneNotSupportedException {
+        return serialPortManager;
+    }
+
+    /**
+     * 防止序列化破坏
+     */
+    private Object readResolve() {
+        return serialPortManager;
+    }
 }