Преглед изворни кода

修改了断开重新连接的问题

robbin пре 4 година
родитељ
комит
674307fb34

+ 86 - 94
app/src/main/java/com/example/administrator/wingcool_gt9_apk/GT9MainActivity.java

@@ -408,102 +408,83 @@ public class GT9MainActivity extends AppCompatActivity
                         @Override
 
                         public void run() {
-
-                            try {
-
-
-                                // server将一直等待连接的到来
-                                System.out.println("server将一直等待连接的到来");
-
-                                socket = server.accept();
-                                // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
-                                inputStream = socket.getInputStream();
-                                outputStream = socket.getOutputStream();
-                                byte[] headbytes = new byte[8];
-                                byte[] databytes = new byte[1024*6];
-                                int len;
-                                int datalen = 0;
-                                int framelen = 0;
-                                int frameid = 0;
-                                StringBuilder sb = new StringBuilder();
-
-                                //for(int i = 0; i < 128; i++)
-                                //{
-                                //    mBytes[i] = 0 ;
-                                //}
-
-                                //只有当客户端关闭它的输出流的时候,服务端才能取得结尾的-1
-                                while ((len = inputStream.read(headbytes, 0,  8)) != -1) {
-
-                                    //先判断头部字节是否正确
-                                    if ( headbytes[0] == (byte)0x8d
-                                            && headbytes[1] == 0x7c
-                                            && headbytes[2] == 0x6b
-                                            && headbytes[3] == 0x5a )
-                                    {
-
-                                        framelen = (headbytes[4] & 0xff) + (int) ((headbytes[5] & 0xff) * 256);  //低位在前
-                                        frameid = (headbytes[6] & 0xff) + (int) ((headbytes[7] & 0xff) * 256);
-
-                                        int offset = 0;
-
-                                        do{
-                                           // byte[] bytes = new byte[framelen];
-                                            len = inputStream.read(mBytes, offset, framelen - offset);
-                                            if(len==-1)
-                                            {
-                                                break;
+                            while(true) {
+                                try {
+                                    // server将一直等待连接的到来
+
+                                    System.out.println("等待客户端连接");
+                                    socket = server.accept();
+                                    System.out.println("客户端已连接");
+                                    // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
+                                    inputStream = socket.getInputStream();
+                                    outputStream = socket.getOutputStream();
+                                    byte[] headbytes = new byte[8];
+                                    byte[] databytes = new byte[1024 * 6];
+                                    int len;
+                                    int datalen = 0;
+                                    int framelen = 0;
+                                    int frameid = 0;
+                                    StringBuilder sb = new StringBuilder();
+
+                                    //只有当客户端关闭它的输出流的时候,服务端才能取得结尾的-1
+                                    while ((len = inputStream.read(headbytes, 0, 8)) != -1) {
+
+                                        //先判断头部字节是否正确
+                                        if (headbytes[0] == (byte) 0x8d
+                                                && headbytes[1] == 0x7c
+                                                && headbytes[2] == 0x6b
+                                                && headbytes[3] == 0x5a) {
+
+                                            framelen = (headbytes[4] & 0xff) + (int) ((headbytes[5] & 0xff) * 256);  //低位在前
+                                            frameid = (headbytes[6] & 0xff) + (int) ((headbytes[7] & 0xff) * 256);
+
+                                            int offset = 0;
+
+                                            do {
+                                                // byte[] bytes = new byte[framelen];
+                                                len = inputStream.read(mBytes, offset, framelen - offset);
+                                                if (len == -1) {
+                                                    break;
+                                                }
+                                                offset += len;
+                                            } while (offset < framelen);
+
+                                            if (offset != framelen) {
+                                                continue;
                                             }
-                                            offset+=len;
-                                        } while(offset < framelen);
-
-                                        if(offset!=framelen)
-                                        {
-                                            continue;
-                                        }
 
-                                        datalen = (mBytes[5] & 0xFF) + (int)(mBytes[4] * 256);  //高位在前
-
-                                        if(mBytes[0] == 1){
-                                            SetUSBData(mBytes, datalen);
-                                        }
-                                        else{
-                                            if(GetUSBData(mBytes,databytes,datalen)) {//根据命令去获取USB数据
-                                                databytes[0] = (byte)0x8d;
-                                                databytes[1] = 0x7c;
-                                                databytes[2] = 0x6b;
-                                                databytes[3] = 0x5a;
-                                                databytes[4] = (byte)datalen;
-                                                databytes[5] = (byte)(datalen>>8);
-                                                databytes[6] = (byte)frameid;
-                                                databytes[7] = (byte)(frameid>>8);
-                                                outputStream.write(databytes,0, datalen + 8);
+                                            datalen = (mBytes[5] & 0xFF) + (int) (mBytes[4] * 256);  //高位在前
+
+                                            if (mBytes[0] == 1) {
+                                                SetUSBData(mBytes, datalen);
+                                            } else {
+                                                if (GetUSBData(mBytes, databytes, datalen)) {//根据命令去获取USB数据
+                                                    databytes[0] = (byte) 0x8d;
+                                                    databytes[1] = 0x7c;
+                                                    databytes[2] = 0x6b;
+                                                    databytes[3] = 0x5a;
+                                                    databytes[4] = (byte) datalen;
+                                                    databytes[5] = (byte) (datalen >> 8);
+                                                    databytes[6] = (byte) frameid;
+                                                    databytes[7] = (byte) (frameid >> 8);
+                                                    outputStream.write(databytes, 0, datalen + 8);
+                                                }
                                             }
                                         }
                                     }
-                                    // 注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-
-                                    /*
-                                    for(int i=0; i<len; i++)
-                                    {
-                                        sb.append(Integer.toHexString(bytes[i]));
-                                        sb.append(',');
-                                    }
-                                    sb.append('\n');
-                                    System.out.println(sb);
-                                    */
-                                }
 
-                                //outputStream = socket.getOutputStream();
+                                    inputStream.close();
+                                    outputStream.close();
+                                    socket.close();
+                                    System.out.println("客户端已断开");
 
-                                inputStream.close();
-                                outputStream.close();
-                                socket.close();
-                                server.close();
+                                    //server.close();
 
-                            } catch (IOException e) {
-                                e.printStackTrace();
-                                Log.i(TAG, "连接失败!!!" + e.toString());
+                                } catch (IOException e) {
+                                    e.printStackTrace();
+                                    Log.i(TAG, "连接失败!!!" + e.toString());
 
+                                }
                             }
                         }
 
@@ -515,10 +496,10 @@ public class GT9MainActivity extends AppCompatActivity
                     //bt_tcpstart.setBackgroundColor(getResources().getColor(R.color.colorAccent2));
 
                     try {
-                        //inputStream.close();
-                        //outputStream.close();
-                        //socket.close();
-                        server.close();
+                        inputStream.close();
+                        outputStream.close();
+                        socket.close();
+                        //server.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
@@ -682,17 +663,17 @@ public class GT9MainActivity extends AppCompatActivity
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        /*
+
         try {
 
-            socket.close();
+            server.close();
 
         } catch (IOException e) {
 
             e.printStackTrace();
 
         }
-        */
+
         if (mUsbDeviceConnection != null) {
             mUsbDeviceConnection.releaseInterface(mUsbInterface);
             mUsbDeviceConnection.close();
@@ -709,6 +690,17 @@ public class GT9MainActivity extends AppCompatActivity
 
     public void exit()
     {
+
+        try {
+
+            server.close();
+
+        } catch (IOException e) {
+
+            e.printStackTrace();
+
+        }
+
         for(Activity act:activityList)
         {
             act.finish();

+ 1 - 1
app/src/main/res/values-en/strings.xml

@@ -79,7 +79,7 @@
     <string name="companyaddr">Address:Room 2008, Chuangxingda Business Building, No.36 Liuxian 3rd Road, Baoan District, Shenzhen</string>
     <string name="telephonenum">Telephone:186 8896 1937</string>
     <string name="emailaddr">Email:max@wingcool.cn</string>
-    <string name="version">APK Version:v0.0.0.1_20210408-Internal Version</string>
+    <string name="version">APK Version:v0.0.0.2_20210430-Internal Version</string>
 
     <string name="app_name">WingCool(GT9)</string>
     <string name="input_num_character">0123456789ABCDEFabcdef</string>

+ 1 - 1
app/src/main/res/values/strings.xml

@@ -78,7 +78,7 @@
     <string name="companyaddr">地址:深圳市宝安区留仙三路 36 号创兴达商务大厦 2008 室</string>
     <string name="telephonenum">电话:186 8896 1937</string>
     <string name="emailaddr">邮箱:max@wingcool.cn</string>
-    <string name="version">APK版本:v0.0.0.1_20210408-内部版本</string>
+    <string name="version">APK版本:v0.0.0.2_20210430-内部版本</string>
 
     <string name="app_name">WingCool(GT9)</string>
     <string name="input_num_character">0123456789ABCDEFabcdef</string>