From aecc5aa11fd2cbdea193db53adfc7c7920f43b26 Mon Sep 17 00:00:00 2001
From: whyclj <1525436766@qq.com>
Date: 星期一, 21 十月 2019 13:27:32 +0800
Subject: [PATCH] 修复bug

---
 platforms/android/app/src/main/java/com/socket/MySocketClientThread.java |   52 ++++++++++++++++++++++++++--------------------------
 1 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/platforms/android/app/src/main/java/com/socket/MySocketClientThread.java b/platforms/android/app/src/main/java/com/socket/MySocketClientThread.java
index 94bdbc5..3f3b09d 100644
--- a/platforms/android/app/src/main/java/com/socket/MySocketClientThread.java
+++ b/platforms/android/app/src/main/java/com/socket/MySocketClientThread.java
@@ -44,9 +44,10 @@
 
     private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
     public Handler mHandler = null;
-    public static Socket socket;
+    public static Socket socket = null;
     private Date lastConTime;                       //涓婁竴娆¢�氳鎴愬姛鐨勬椂闂�
     private boolean thread_run_flag = true;
+    public boolean isRuning = false;
 
     ServiceModel returnmodel = new ServiceModel();
 
@@ -55,12 +56,7 @@
         battData = new BattRTData();
         testParam = new BattTestParam();
         testRtData = new BattTestRtData();
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                reConnectDevice();
-            }
-        }).start();
+
     }
 
     /*********************************************************************************************/
@@ -69,7 +65,7 @@
 
         //鍒濆鍖栨秷鎭惊鐜槦鍒楋紝闇�瑕佸湪Handler鍒涘缓涔嬪墠
         Looper.prepare();
-
+        isRuning = true;
         mHandler = new Handler()
         {
             public void handleMessage(Message msg)
@@ -112,7 +108,15 @@
         Log.e(TAG, "Loop start at "+ Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms) );
         //鍚姩瀛愮嚎绋嬫秷鎭惊鐜槦鍒�
         Looper.loop();
+        isRuning = false;
         Log.e(TAG, "Loop stop at "+ Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms) );
+        if(socket != null){
+            try {
+                socket.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
     }
 
     /**
@@ -130,6 +134,10 @@
 //            socket = new Socket();
 //            SocketAddress socAddress = new InetSocketAddress(server_ip, server_port);
 //            socket.connect(socAddress, 2000);
+            if(socket == null || socket.isClosed()){
+                Log.e(TAG, "SocketComm:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " );
+                reConnectDevice();
+            }
             if(this.socket != null)
             {
                 in = this.socket.getInputStream();
@@ -233,6 +241,7 @@
                     res_t = false;
                     model.code = 0;
                 }
+                thread_run_flag = true;
             } else {
                 //System.out.println(this.getName() + "-------socket  寮傚父鍏抽棴-------" + Com.getNowTimeWithAt());
                 thread_run_flag = false;
@@ -240,20 +249,6 @@
         } catch (IOException | InterruptedException e) {
             thread_run_flag = false;
         } finally {
-            if(in != null) {
-                try {
-                    in.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if(out != null) {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
             if(!thread_run_flag){
                 reConnectDevice();
             }
@@ -387,8 +382,9 @@
             }
             //---------------------  鑾峰彇鏀剧數鍙傛暟    ----------------------------------------------
             else if(FBS_ComBase.CMD_GETDISCHARGEPARAM == m_FBS_Cmd.CMD) {
+                Log.e(TAG, "getDataFromCommBuf: 鑾峰彇鏀剧數鍙傛暟" );
                 if(testParam.putByteBuffer(bf)) {
-                    //System.out.println("鑾峰彇鏀剧數鍙傛暟鎴愬姛");
+                    System.out.println("鑾峰彇鏀剧數鍙傛暟鎴愬姛");
                     //System.out.println(testParam);
                     isSuccess = true;
                     model.setData(testParam);
@@ -483,9 +479,13 @@
 
     public boolean setUpDeviceIp(String server_ip){
         boolean flag  = false;
-        synchronized (socket){
-            if (server_ip != null && server_ip.length() > 0){
-                flag = reConnectDevice();
+        if(this.socket == null){
+            flag = reConnectDevice();
+        }else{
+            synchronized (socket){
+                if (this.server_ip != null && server_ip.length() > 0){
+                    flag = reConnectDevice();
+                }
             }
         }
         return false;

--
Gitblit v1.9.1