lvjincheng hace 4 años
padre
commit
67b98f8bd1

+ 11 - 5
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -206,9 +206,20 @@ public class AimHandler : MonoBehaviour
         return (float) shortNum; 
     }
 
+    public static long GetTimestamp()
+    {
+        TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
+        return (long)ts.TotalMilliseconds;
+    }
+
     public void OnDataReceived(byte[] bytes)
     {
         // Debug.Log("瞄准模块数据长度" + bytes.Length);
+        if (bytes[0] == 0x5b) {
+            string info = ShootCheck.ins.ShootByInfrared(bytes);
+            if (DebugForDevice.ins) DebugForDevice.ins.LogInfrared(info);
+            return;
+        }
         if (bytes.Length != 27)
         {
             if (bytes.Length == 2) 
@@ -216,11 +227,6 @@ public class AimHandler : MonoBehaviour
                 DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
                 return;
             }
-            if (bytes.Length >= 3 && bytes[2] == 5)
-            {
-                string info = ShootCheck.ins.ShootByInfrared(bytes);
-                if (DebugForDevice.ins) DebugForDevice.ins.LogInfrared(bytes, info);
-            }
             return;
         }
         if (bytes[7] == 0 && bytes[8] == 0 && bytes[9] == 0 && bytes[10] == 0 && bytes[11] == 0 && bytes[12] == 0)

+ 8 - 1
Assets/BowArrow/Scripts/Bluetooth/BluetoothAim.cs

@@ -122,7 +122,8 @@ public class BluetoothAim : MonoBehaviour
                         }
                     }
                 }
-                CallDelay(1, OpenReceiveData);
+                CallDelay(1, OpenInfrared);
+                CallDelay(2, OpenReceiveData);
             };
             bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
             {
@@ -170,6 +171,12 @@ public class BluetoothAim : MonoBehaviour
         }
     }
 
+    void OpenInfrared()
+    {
+        WriteData("1");
+        Log("红外线准备完成\n" + deviceName);
+    }
+
     void OpenReceiveData()
     {
         WriteData("3");

+ 12 - 5
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -225,7 +225,7 @@ public class ShootCheck : MonoBehaviour
         return (int)shortNum;
     }
 
-    public float arrowWeight = 60; //实体模具箭重,单位克
+    public float arrowWeight = 75; //实体模具箭重,单位克
 
     bool check(float ax, float ay, float az, string t)
     {
@@ -325,12 +325,19 @@ public class ShootCheck : MonoBehaviour
     }
 
     public string ShootByInfrared(byte[] bytes) {
-        float time1 = ((bytes[5] << 8) + bytes[6]) * 0.1f;
-        float time2 = ((bytes[7] << 8) + bytes[8]) * 0.1f; 
+        int id = bytes[1];
+        float time1 = bytes[2] * 0.1f;
+        float time2 = bytes[3] * 0.1f;
+        int sumCheck = bytes[0] + bytes[1] + bytes[2] + bytes[3];
+        sumCheck &= 0xff;
+        bool sumCheckRes = sumCheck == bytes[4];
         float speed = 0.05f / (time1 / 1000 + time2 / 1000);
         shootSpeed = Mathf.Sqrt(speed * speed * arrowWeight / LoginMgr.myUserInfo.actualArrowWeight);  
-        string logTxt = $"时长1:{time1}毫秒,时长2:{time2}毫秒,弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s";
-        Debug.Log(logTxt);
+        string logTxt = $"序号{id},时长1:{time1}毫秒,时长2:{time2}毫秒,和校验结果:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s";
+        Debug.LogWarning(logTxt);
+        try {
+            if (sumCheckRes) BluetoothAim.ins.WriteData("I"); //如果数据正确,则回复硬件
+        } catch (Exception) {}
         try {
             ShootCheck.ins.webSocket.Send(logTxt);
         } catch (Exception) {}