parse tk data.
authorTrillGates <williamtrillgates@gmail.com>
Wed, 31 Mar 2021 07:48:30 +0000 (15:48 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Wed, 31 Mar 2021 07:48:30 +0000 (15:48 +0800)
app/src/main/java/com/enqualcomm/support/utils/Util.java
app/src/main/java/com/ic/protocols/protocol_fzd.java

index 2e32584..b73bac5 100644 (file)
@@ -1013,8 +1013,7 @@ public class Util {
         }
         System.out.println("]");
         System.out.println("-----before------");
-        List<Byte> list = new ArrayList<>();
-        byte a = 0X7D;
+        byte[] result = new byte[source.length];
         if (isSend) {
             //往外发的数据
             // 0X7D --> 0X7D 0X01
@@ -1022,66 +1021,63 @@ public class Util {
             // 0X5D --> 0X7D 0X03
             // 0X2C --> 0X7D 0X04
             // 0X2A --> 0X7D 0X05
-            for (byte value : source) {
-                if (value == 0x7d) {
-                    list.add(a);
-                    byte b = 0X01;
-                    list.add(b);
-                } else if (value == 0x5b) {
-                    list.add(a);
-                    byte b = 0X02;
-                    list.add(b);
-                } else if (value == 0x5d) {
-                    list.add(a);
-                    byte b = 0X03;
-                    list.add(b);
-                } else if (value == 0x2c) {
-                    list.add(a);
-                    byte b = 0X04;
-                    list.add(b);
-                } else if (value == 0x2a) {
-                    list.add(a);
-                    byte b = 0X05;
-                    list.add(b);
-                } else {
-                    list.add(value);
-                }
-            }
+            //TODO:
         } else {
             // 0X7D 0X01 --> 0X7D
             // 0X7D 0X02 --> 0X5B
             // 0X7D 0X03 --> 0X5D
             // 0X7D 0X04 --> 0X2C
             // 0X7D 0X05 --> 0X2A
-            for (int i = 0; i < source.length - 1; i += 2) {
-                //如果i是最后一位,那么直接添加完事
-                if (i == source.length - 1) {
-                    list.add(source[i]);
-                } else if (source[i] == a && source[i + 1] == 0X01) {
-                    byte b = 0X7D;
-                    list.add(b);
-                } else if (source[i] == a && source[i + 1] == 0X02) {
-                    byte b = 0X5B;
-                    list.add(b);
-                } else if (source[i] == a && source[i + 1] == 0X03) {
-                    byte b = 0X5D;
-                    list.add(b);
-                } else if (source[i] == a && source[i + 1] == 0X04) {
-                    byte b = 0X2C;
-                    list.add(b);
-                } else if (source[i] == a && source[i + 1] == 0X05) {
-                    byte b = 0X2A;
-                    list.add(b);
+            //不能相邻两个对比,而是这个和下一个,如果需要转换则从第三个开始
+            // 从0开始
+            int index = 0;
+            int saveIndex = 0;
+            while (index < source.length) {
+                //对数据和下一个数据进行判断处理
+                byte first = source[index];
+                boolean combine = false;
+                if (index != source.length - 1) {
+                    byte second = source[index + 1];
+                    //相邻对比
+                    //如果需要转换,那么则+2
+                    combine = true;
+                    if (first == 0X7D && second == 0X01) {
+                        result[saveIndex] = 0X7D;
+                    } else if (first == 0X7D && second == 0X02) {
+                        result[saveIndex] = 0X5B;
+                    } else if (first == 0X7D && second == 0X03) {
+                        result[saveIndex] = 0X5D;
+                    } else if (first == 0X7D && second == 0X04) {
+                        result[saveIndex] = 0X2C;
+                    } else if (first == 0X7D && second == 0X05) {
+                        result[saveIndex] = 0X2A;
+                    } else {
+                        combine = false;
+                        //如果不合并的话呢?设置一个数据即可
+                        result[saveIndex] = first;
+                    }
                 } else {
-                    list.add(source[i]);
-                    list.add(source[i + 1]);
+                    //最后一位数据了
+                    result[saveIndex] = first;
                 }
+                if (combine) {
+                    //移2位
+                    index += 2;
+                } else {
+                    //没有合并数据,移动到下一位
+                    index += 1;
+                }
+                saveIndex++;
             }
+            ICLogger.d("total size == > " + saveIndex);
+            //这个就是数据下标,创建一个新的数组,保存数据
+            byte[] bestResult = new byte[saveIndex];//下标从0开始,所以+1,但是,这是++后的结果,所以不就加了。
+            if (bestResult.length >= 0) {
+                System.arraycopy(result, 0, bestResult, 0, bestResult.length);
+            }
+            return bestResult;
         }
-        byte[] result = new byte[list.size()];
-        for (int i = 0; i < list.size(); i++) {
-            result[i] = list.get(i);
-        }
+        //把文件写出来,对比一下先吧。
         return result;
     }
 
index 6bc56d2..1e6052f 100644 (file)
@@ -46,10 +46,10 @@ import static java.lang.Math.abs;
 /**
  * 防走丢的协议:
  * 端口号:9000
- * 协议号:
+ * 协议号:3
  * 地址:
- *  - 测试地址:fzd.xcloudtech.com
- *  - 正式地址:api.xcloudtech.com
+ * - 测试地址:fzd.xcloudtech.com
+ * - 正式地址:api.xcloudtech.com
  */
 public class protocol_fzd {
     private static final String TAG = "ICSrv." + protocol_fzd.class.getSimpleName();
@@ -337,35 +337,41 @@ public class protocol_fzd {
     }
 
     private void handlerVoiceContent(byte[] bytes) {
-        ICLogger.d("voice length == > " + bytes.length);
-        String cmdStr = new String(bytes);
-        String[] split = cmdStr.split(",");
-        ICLogger.d("split[0].length() == > " + split[0].length());
-        //处理数据
-        byte[] dataPackage = Util.subArray(bytes, split[0].length() + 1, bytes.length - 1);
-        //String packageStr = new String(dataPackage);
-        //ICLogger.d("packageStr == > " + packageStr);
-        String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/Android/VoiceCache";
-        byte[] result = Util.fzdTransfer(dataPackage, false);
-        ICLogger.d("dataPackage LENGTH == > " + result.length);
-        String amrFilepath = Util.saveFile(result,
-                0, result.length,
-                new File(dir),
-                "receive_" + System.currentTimeMillis() + ".amr");
-        ICLogger.d("amrFilepath == > " + amrFilepath + " dataPackage size == > " + dataPackage.length);
-        //播放一下吧
-        MediaPlayer mediaPlayer = new MediaPlayer();
         try {
-            mediaPlayer.setDataSource(amrFilepath);
-            mediaPlayer.prepare();
-            ICLogger.d("playVoice.. ");
-            mediaPlayer.start();
-        } catch (IOException e) {
-            ICLogger.d("playVoice: ERROR ");
+            ICLogger.d("voice length == > " + bytes.length);
+            String cmdStr = new String(bytes);
+            String[] split = cmdStr.split(",");
+            ICLogger.d("split[0].length() == > " + split[0].length());
+            //处理数据
+            byte[] dataPackage = Util.subArray(bytes, split[0].length() + 1, bytes.length - 1);
+            //String packageStr = new String(dataPackage);
+            //ICLogger.d("packageStr == > " + packageStr);
+            String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/Android/VoiceCache";
+            byte[] result = Util.fzdTransfer(dataPackage, false);
+            ICLogger.d("dataPackage LENGTH == > " + result.length);
+            String amrFilepath = Util.saveFile(result,
+                    0, result.length,
+                    new File(dir),
+                    "receive_" + System.currentTimeMillis() + ".amr");
+            //播放一下吧
+            //后面上了UI可以注释掉
+            MediaPlayer mediaPlayer = new MediaPlayer();
+            try {
+                mediaPlayer.setDataSource(amrFilepath);
+                mediaPlayer.prepare();
+                ICLogger.d("playVoice.. ");
+                mediaPlayer.start();
+            } catch (IOException e) {
+                ICLogger.d("playVoice: ERROR ");
+            }
+            //TODO:
+            //回复服务器状态
+            mConnection.send(TK("1"));
+        } catch (Exception e) {
+            e.printStackTrace();
+            //失败
+            mConnection.send(TK("0"));
         }
-        //TODO:
-        //回复服务器状态
-        mConnection.send(TK("1"));
     }
 
     private static String getHexLen(int len) {