lvjincheng 4 ani în urmă
părinte
comite
36bf56bc5b

+ 71 - 0
Assets/BowArrow/Scenes/Game.unity

@@ -4180,6 +4180,64 @@ Transform:
   m_Father: {fileID: 1320522156}
   m_RootOrder: 1
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!1 &992649507
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 992649509}
+  - component: {fileID: 992649508}
+  - component: {fileID: 992649510}
+  m_Layer: 0
+  m_Name: BluetoothClient
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!114 &992649508
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 992649507}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: e5464d6e6018e9446afd6b54a9ef1431, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  serverIP: 110.43.54.43
+  text: {fileID: 0}
+--- !u!4 &992649509
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 992649507}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0.0541991, y: -0.07259831, z: 0.14766908}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_RootOrder: 8
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!114 &992649510
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 992649507}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 0640518c4d8627a41a16e391f6b84960, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
 --- !u!1 &1009582747
 GameObject:
   m_ObjectHideFlags: 0
@@ -7059,6 +7117,7 @@ GameObject:
   m_Component:
   - component: {fileID: 1680053129}
   - component: {fileID: 1680053130}
+  - component: {fileID: 1680053131}
   m_Layer: 0
   m_Name: CameraToLook
   m_TagString: Untagged
@@ -7099,6 +7158,18 @@ MonoBehaviour:
   SetIdentity: {fileID: 1478558957}
   CalibrationButton: {fileID: 1990278318}
   MagScaleText: {fileID: 1105832657}
+--- !u!114 &1680053131
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1680053128}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 0640518c4d8627a41a16e391f6b84960, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
 --- !u!1 &1687911469
 GameObject:
   m_ObjectHideFlags: 0

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

@@ -27,6 +27,7 @@ public class BluetoothAim : MonoBehaviour
     void Start()
     {
         aimHandler = new AimHandler(controlObj, SetIdentity, CalibrationButton, MagScaleText);
+        BluetoothDispatcher.aim = aimHandler.OnDataReceived;
     }
 
     void OnDestroy()
@@ -100,6 +101,7 @@ public class BluetoothAim : MonoBehaviour
             bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
             {
                 byte[] bytes = value;
+                BluetoothClient.UploadData(0, bytes);
                 aimHandler.OnDataReceived(bytes);
             };
             bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>

+ 1 - 0
Assets/BowArrow/Scripts/Bluetooth/BluetoothShoot.cs

@@ -83,6 +83,7 @@ public class BluetoothShoot : MonoBehaviour
             bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
             {
                 byte[] bytes = value;
+                BluetoothClient.UploadData(1, bytes);
                 if (ShootCheck.ins != null)
                 {
                     ShootCheck.ins.OnDataReceived(bytes);

+ 1 - 0
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -18,6 +18,7 @@ public class ShootCheck : MonoBehaviour
     void Start()
     {
         ins = this;
+        BluetoothDispatcher.shoot = OnDataReceived;
     } 
 
     void OnDestroy()

+ 6 - 6
Assets/DebugShoot/BluetoothClient.cs

@@ -17,7 +17,7 @@ public class BluetoothClient : MonoBehaviour
     bool loaded = false;
     [SerializeField] string serverIP = "110.43.54.43";
     [SerializeField] Text text;
-    public static System.Action<byte[]> onDataReceived;
+    public static System.Action<byte, byte[]> onDataReceived;
     static BluetoothClient ins;
 
     void Start()
@@ -40,7 +40,7 @@ public class BluetoothClient : MonoBehaviour
         JCData data = JsonUtility.FromJson<JCData>(text);
         if (data.func == "receiveData") 
         {
-            string byteStr = data.args[0];
+            string byteStr = data.args[1];
             string[] byteStrs = byteStr.Split(',');
             byte[] bytes = new byte[byteStrs.Length];
             for (int i = 0; i < bytes.Length; i++)
@@ -50,7 +50,7 @@ public class BluetoothClient : MonoBehaviour
             Log("接收数据\n" + byteStr);
             if (onDataReceived != null)
             {
-                onDataReceived(bytes);
+                onDataReceived(byte.Parse(data.args[0]), bytes);
             }
         }
         else if (data.func == "loadTempEntity") 
@@ -69,13 +69,13 @@ public class BluetoothClient : MonoBehaviour
         return JsonUtility.ToJson(data);
     }
 
-    public static void UploadData(byte[] bytes) 
+    public static void UploadData(byte sign, byte[] bytes) 
     {
         if (ins && ins.loaded)
         {
             string data = String.Join(",", bytes);
-            ins.ws.Send(ins.PackData("uploadData", new string[]{data}, 1));
-            ins.Log("正在上传数据");
+            ins.ws.Send(ins.PackData("uploadData", new string[]{sign.ToString(), data}, 1));
+            ins.Log("正在上传数据\n" + bytes);
         }
     }
 

+ 26 - 0
Assets/DebugShoot/BluetoothDispatcher.cs

@@ -0,0 +1,26 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class BluetoothDispatcher : MonoBehaviour
+{
+    public static System.Action<byte[]> aim;
+    public static System.Action<byte[]> shoot;
+
+    void Start()
+    {
+        BluetoothClient.onDataReceived = Dispatch;
+    }
+
+    void Dispatch(byte sign, byte[] data)
+    {
+        Debug.Log(sign + "---" + data);
+        if (sign == 0 && aim != null)
+        {
+            aim(data);
+        } else if (sign == 1 && shoot != null)
+        {
+            shoot(data);
+        }
+    }
+}

+ 1 - 1
Assets/DebugShoot/BluetoothNew.cs.meta → Assets/DebugShoot/BluetoothDispatcher.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 09564a4e51862624cbffae6e76118f7e
+guid: 0640518c4d8627a41a16e391f6b84960
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 127
Assets/DebugShoot/BluetoothNew.cs

@@ -1,127 +0,0 @@
-using System;
-using System.Collections.Generic;
-using UnityEngine;
-using UnityEngine.UI;
-using ArduinoBluetoothAPI;
-
-public class BluetoothNew : MonoBehaviour
-{
-    BluetoothHelper bluetoothHelper;
-    BluetoothHelperCharacteristic characteristicWrite;
-    BluetoothHelperService bluetoothService;
-    string deviceName = "";
-    bool canConnect = true;
-
-    // string targetDeviceName = "BGBox_202012";
-    string targetDeviceName = "Bbow_20210501";
-
-    [SerializeField] Text textUI;
-
-    void OnDestroy()
-    {
-        if (bluetoothHelper != null)
-        {
-            bluetoothHelper.Disconnect();
-        }
-    }
-
-    void FixedUpdate()
-    {
-        Connect();
-    }
-
-    void Connect()
-    {
-        if (!canConnect)
-        {
-            return;
-        }
-        canConnect = false;
-        try
-        {
-            BluetoothHelper.BLE = true;
-            bluetoothHelper = BluetoothHelper.GetNewInstance();
-
-            bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
-            {
-                Log("连接成功\n" + helper.getDeviceName());
-                foreach (BluetoothHelperService service in helper.getGattServices())
-                {
-                  
-                    if (service.getName().ToLower().StartsWith("0000fff0"))
-                    {
-                        bluetoothService = service;
-
-                        foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
-                        {
-                            if (characteristic.getName().ToLower().StartsWith("0000fff2"))
-                            {
-                                characteristicWrite = characteristic;
-                            }
-                            else if (characteristic.getName().ToLower().StartsWith("0000fff1"))
-                            {
-
-                                BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
-                                ch.setService(bluetoothService.getName());
-                                bluetoothHelper.Subscribe(ch);
-                            }
-                        }
-                    }
-                }
-                Invoke("OpenReceiveData", 1);
-            };
-            bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
-            {
-                canConnect = true;
-                Log("连接失败\n" + helper.getDeviceName());
-            };
-            bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
-            {
-                byte[] bytes = value;
-                BluetoothClient.UploadData(bytes);
-            };
-            bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
-            {
-                foreach (BluetoothDevice device in nearbyDevices)
-                {
-                    if (device.DeviceName == targetDeviceName)
-                    {
-                        deviceName = device.DeviceName;
-                        bluetoothHelper.setDeviceName(deviceName);
-                        bluetoothHelper.Connect();
-                        Log("发现设备\n" + device.DeviceName);
-                        return;
-                    }
-                }
-                canConnect = true;
-                Log("没有发现设备");
-            };
-
-            bluetoothHelper.ScanNearbyDevices();
-            Log("正在扫描设备");
-        }
-        catch (Exception e)
-        {
-            Debug.Log(e.Message);
-            canConnect = true;
-            Log("请打开蓝牙");
-        }
-    }
-
-    void OpenReceiveData()
-    {
-        BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
-        ch.setService(bluetoothService.getName());
-        bluetoothHelper.WriteCharacteristic(ch, "3");
-        // bluetoothHelper.WriteCharacteristic(ch, "5");
-        Log("开始接收信息\n" + deviceName);
-    }
-
-    void Log(string text)
-    {
-        if (textUI != null) 
-        {
-            textUI.text = text;
-        }
-    }
-}

+ 3 - 4
Assets/DebugShoot/DebugBluetooth.unity

@@ -134,7 +134,7 @@ GameObject:
   - component: {fileID: 407940832}
   - component: {fileID: 407940833}
   m_Layer: 5
-  m_Name: Line (1)
+  m_Name: YAxis
   m_TagString: Untagged
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
@@ -319,10 +319,9 @@ MonoBehaviour:
   m_GameObject: {fileID: 417067449}
   m_Enabled: 1
   m_EditorHideFlags: 0
-  m_Script: {fileID: 11500000, guid: 09564a4e51862624cbffae6e76118f7e, type: 3}
+  m_Script: {fileID: 11500000, guid: 0640518c4d8627a41a16e391f6b84960, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  textUI: {fileID: 2081173761}
 --- !u!114 &417067452
 MonoBehaviour:
   m_ObjectHideFlags: 0
@@ -744,7 +743,7 @@ GameObject:
   - component: {fileID: 909293987}
   - component: {fileID: 909293988}
   m_Layer: 5
-  m_Name: Line
+  m_Name: XAxis
   m_TagString: Untagged
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0