瀏覽代碼

刚连上设备时,电量及时显示

lvjincheng 4 年之前
父節點
當前提交
7cba5bf676

+ 0 - 6
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -206,12 +206,6 @@ 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);

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

@@ -4,6 +4,7 @@ using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using DG.Tweening;
+using UnityEngine.SceneManagement;
 
 public class BluetoothAim : MonoBehaviour
 {
@@ -16,6 +17,7 @@ public class BluetoothAim : MonoBehaviour
     [SerializeField] Text textUI;
     public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
     public bool hasData = false;
+    public long hasDataTime;
     public static bool scanLock = false; //防止同时扫描冲突
     public static BluetoothAim ins;
 
@@ -122,8 +124,11 @@ public class BluetoothAim : MonoBehaviour
                         }
                     }
                 }
-                CallDelay(1, OpenInfrared);
-                CallDelay(2, OpenReceiveData);
+                CallDelay(1, delegate() {
+                    WriteData(SceneManager.GetActiveScene().name == "Game" ? "b" :"B");
+                });
+                CallDelay(2, OpenInfrared);
+                CallDelay(3, OpenReceiveData);
             };
             bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
             {
@@ -132,6 +137,7 @@ public class BluetoothAim : MonoBehaviour
             };
             bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
             {
+                if (!hasData) hasDataTime = JCUtil.GetTimestamp();
                 hasData = true;
                 byte[] bytes = value;
                 // Log(String.Join(",", bytes));

+ 1 - 1
Assets/BowArrow/Scripts/View/DeviceBatteryView.cs

@@ -50,7 +50,7 @@ public class DeviceBatteryView : MonoBehaviour
     void RequestBatteryForBow()
     {
         try {
-            if (BluetoothAim.ins.hasData) {
+            if (BluetoothAim.ins.hasData && JCUtil.GetTimestamp() - BluetoothAim.ins.hasDataTime > 5000) {
                 BluetoothAim.ins.WriteData(SceneManager.GetActiveScene().name == "Game" ? "b" :"B");
             }
         } catch (Exception) {}

+ 14 - 0
Assets/JC/JCUtil.cs

@@ -0,0 +1,14 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using System;
+
+public class JCUtil : MonoBehaviour
+{
+    // Start is called before the first frame update
+    public static long GetTimestamp()
+    {
+        TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
+        return (long)ts.TotalMilliseconds;
+    }
+}

+ 11 - 0
Assets/JC/JCUtil.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 4ab93499c9f5f2c409f6272fe65faceb
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: