| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413 | 
							- using ArduinoBluetoothAPI;
 
- using System;
 
- using UnityEngine;
 
- using System.Collections.Generic;
 
- using UnityEngine.UI;
 
- using DG.Tweening;
 
- using SmartBowSDK;
 
- using System.Collections;
 
- /* 蓝牙瞄准模块 */
 
- /* 管理1p和2p蓝牙连接,脚本外调用逻辑如果同时连接1p和2p设备,以前的逻辑还是以1p为主,如果只连2p,则使用2p数据*/
 
- public class BluetoothAim : MonoBehaviour
 
- {
 
-     #region 弓箭蓝牙模块基本信息
 
-     readonly string targetDeviceName = "Bbow_20210501 | ARTEMIS Pro";//HOUYI Pro
 
-     readonly string targetDeviceNameHOUYIPro = "HOUYI Pro";
 
-     readonly string targetDeviceNameGun = "Pistol | Pistol M9 | Bbow_20210501";
 
-     string targetDeviceService
 
-     {
 
-         get
 
-         {
 
-             if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
 
-             {
 
- #if UNITY_ANDROID
 
-                 return "0000fff0";
 
- #else
 
-                 return "fff0";
 
- #endif
 
-             }
 
-             return "6e400001";
 
-         }
 
-     }
 
-     string targetDeviceCharacteristicWrite
 
-     {
 
-         get
 
-         {
 
-             if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
 
-             {
 
- #if UNITY_ANDROID
 
-                 return "0000fff2";
 
- #else
 
-                 return "fff2";
 
- #endif
 
-             }
 
-             return "6e400002";
 
-         }
 
-     }
 
-     string targetDeviceCharacteristicNotify
 
-     {
 
-         get
 
-         {
 
-             if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
 
-             {
 
- #if UNITY_ANDROID
 
-                 return "0000fff1";
 
- #else
 
-                 return "fff1";
 
- #endif
 
-             }
 
-             return "6e400003";
 
-         }
 
-     }
 
-     #endregion
 
-     //当前选择的设备
 
-     BluetoothPlayer currentBLEPlayer = BluetoothPlayer.FIRST_PLAYER;
 
-     //1p蓝牙连接
 
-     BluetoothHelper bluetoothHelper;
 
-     BluetoothHelperCharacteristic characteristicWrite;
 
-     BluetoothHelperService bluetoothService;
 
-   
 
-     string deviceName = "";
 
-     bool canConnect = true;
 
-     [SerializeField] Text textUI;
 
-     public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
 
-   
 
-     int dataCount = 0;
 
-     public bool hasData = false;
 
-     public long hasDataTime;
 
-     public static bool scanLock = false; //防止同时扫描冲突
 
-     public static BluetoothAim ins;
 
-     //根据设备情况来看是否需要解密,目前就是枪需要进行解密操作
 
-     public bool NeedDecryption = false;
 
-     public BluetoothWindows firstBluetoothWindows { get; set; }
 
-     void Start()
 
-     {
 
-         ins = this;
 
-         InitAutoDormancy();
 
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-         // new GameObject("BleUDP").AddComponent<BleUDP>();
 
-         firstBluetoothWindows = new BluetoothWindows();
 
-         firstBluetoothWindows.OnConnected = OnConnected_windows1;
 
-         firstBluetoothWindows.OnConnectionFailed = OnConnectionFailed_windows1;
 
-         firstBluetoothWindows.OnCharacteristicChanged = OnCharacteristicChanged_windows1;
 
-         //window蓝牙连接
 
-         if (currentBLEPlayer == BluetoothPlayer.FIRST_PLAYER)
 
-         {
 
-             Debug.Log("FIRST_PLAYER BleWinHelper.RegisterTo");
 
-       
 
-             BleWinHelper.RegisterTo(gameObject, firstBluetoothWindows,"1P");
 
-         }
 
- #endif
 
-     }
 
-     void OnDestroy()
 
-     {
 
-         //删除ble。1p,2p 都删除
 
-         DisconnectBleHelper();
 
-         DisconnectSmartBowHelper2P();
 
-     }
 
-     private bool userDoConnect = false;
 
-     private bool doConnect = false;
 
-     public Func<bool> action_DoConnectInterceptor;
 
-     public void DoConnect()
 
-     {
 
-         if (action_DoConnectInterceptor != null) {
 
-             if (action_DoConnectInterceptor.Invoke()) return;
 
-         }
 
-         if (status == BluetoothStatusEnum.Connect)
 
-         {
 
-             connectCanceled = false;
 
-             userDoConnect = true;
 
-             doConnect = true;
 
-             SetStatus(BluetoothStatusEnum.Connecting);
 
-         }
 
-         else if (status == BluetoothStatusEnum.ConnectSuccess)
 
-         {
 
-             userDoConnect = false;
 
-             doConnect = false;
 
-             OnDisconnect();
 
-             DisconnectBleHelper();
 
-             //#if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-             //            //BleUDP.ins.Disconnect();
 
-             //            if (firstBluetoothWindows.Disconnect()) SetStatus(BluetoothStatusEnum.Connect);
 
-             //#else
 
-             //            DisconnectBleHelper();
 
-             //#endif
 
-         }
 
-     }
 
-     //连接取消,退回登录时需要取消连接(扫描)
 
-     [NonSerialized] public bool connectCanceled = false;
 
-     void OnDisconnect()
 
-     {
 
-         curMac = null;
 
-         dataCount = 0;
 
-         hasData = false;
 
-         canConnect = true;
 
-         SetStatus(BluetoothStatusEnum.ConnectFail);
 
-         BowCamera.isTouchMode = true;
 
-         DestroyWhenDisconenct();
 
-         if (AimHandler.ins) AimHandler.ins.SetMsOldDefault();
 
-     }
 
-     float notUserDoConnectTime = 0;
 
-     void Update()
 
-     {
 
-         updateSmartBowHelper2P();
 
-         if (userDoConnect && status == BluetoothStatusEnum.Connect)
 
-         {
 
-             DoConnect();
 
-         }
 
-         if (doConnect) Connect();
 
-         if (CommonConfig.StandaloneMode)
 
-         {
 
-             if (!userDoConnect && status == BluetoothStatusEnum.Connect)
 
-             {
 
-                 if (notUserDoConnectTime == 0)
 
-                 {
 
-                     DoConnect();
 
-                     notUserDoConnectTime = 0.1f;
 
-                 }
 
-                 else
 
-                 {
 
-                     notUserDoConnectTime += Time.deltaTime;
 
-                     if (notUserDoConnectTime > 2.5f)
 
-                     {
 
-                         DoConnect();
 
-                         notUserDoConnectTime = 0.1f;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     void SetStatus(BluetoothStatusEnum statusValue)
 
-     {
 
-         status = statusValue;
 
-         if (status == BluetoothStatusEnum.ConnectFail)
 
-         {
 
-             Sequence sequence = DOTween.Sequence();
 
-             sequence.AppendInterval(2f);
 
-             sequence.AppendCallback(delegate ()
 
-             {
 
-                 if (status == BluetoothStatusEnum.ConnectFail)
 
-                 {
 
-                     status = BluetoothStatusEnum.Connect;
 
-                 }
 
-             });
 
-             sequence.SetUpdate(true);
 
-             SimulateMouseController.ins?.SetBleConnected(false);
 
-         } else if (status == BluetoothStatusEnum.ConnectSuccess) {
 
-             SimulateMouseController.ins?.SetBleConnected(true);
 
-         }
 
-     }
 
-   
 
-     void DisconnectBleHelper()
 
-     {
 
-         if (BluetoothWindows.IsWindows())
 
-         {
 
-             if (firstBluetoothWindows.Disconnect())
 
-                 SetStatus(BluetoothStatusEnum.Connect);
 
-             return;
 
-         }
 
-         if (bluetoothHelper != null) bluetoothHelper.Disconnect();
 
-  
 
-     }
 
-     void DisconnectSmartBowHelper2P() {
 
-         //2p
 
-         if (smartBowHelper2P != null) smartBowHelper2P.Disconnect();
 
-     }
 
-     void Connect()
 
-     {
 
-         //if (BluetoothShoot.scanLock)
 
-         //{
 
-         //    return;
 
-         //}
 
-         if (!canConnect)
 
-         {
 
-             return;
 
-         }
 
-         doConnect = false;
 
-         doConnect2P = false;
 
-         scanLock = true;
 
-         canConnect = false;
 
-         _scanCanRetryCount = 4;
 
-         SetStatus(BluetoothStatusEnum.Connecting);
 
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-         //ConnectBleByUDP();
 
-         firstBluetoothWindows.Connect();
 
- #else
 
-         ConnectBleHelper();
 
- #endif
 
-     }
 
-     int _scanCanRetryCount = 4;
 
-     void ConnectBleHelper()
 
-     {
 
-         #if UNITY_ANDROID
 
-         PopupMgr.ins.ClearAllTip();
 
-         if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
 
-         {
 
-             Debug.Log("BluetoothHelperAndroid.IsBluetoothEnabled():"+ BluetoothHelperAndroid.IsBluetoothEnabled());
 
-             HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
 
-             return;
 
-         }
 
-         if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectBleHelper, (permission) => {
 
-             Debug.Log("permission ==== :" + permission);
 
-             if (permission.Contains("LOCATION"))
 
-             {
 
-                 HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
 
-             }
 
-             else if (permission.Contains("BLUETOOTH"))
 
-             {
 
-                 HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
 
-             }
 
-         })) return;
 
-         #endif
 
-         try
 
-         {
 
-             BluetoothHelper.BLE = true;
 
-             bluetoothHelper = BluetoothHelper.GetNewInstance();
 
-             bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
 
-             {
 
-                 Log("连接成功\n" + helper.getDeviceName());
 
-                 SetStatus(BluetoothStatusEnum.ConnectSuccess);
 
-                 SetMainConnectDeviceType();
 
-                 if (connectCanceled) {
 
-                     Debug.Log("ble connectCanceled");
 
-                     DoConnect();
 
-                     return;
 
-                 }
 
-                 BowCamera.isTouchMode = false;
 
-                 foreach (BluetoothHelperService service in helper.getGattServices())
 
-                 {
 
-                     if (service.getName().ToLower().StartsWith(targetDeviceService))
 
-                     {
 
-                         bluetoothService = service;
 
-                         foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
 
-                         {
 
-                             if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicWrite))
 
-                             {
 
-                                 characteristicWrite = characteristic;
 
-                             }
 
-                             else if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicNotify))
 
-                             {
 
-                                 BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
 
-                                 ch.setService(bluetoothService.getName());
 
-                                 bluetoothHelper.Subscribe(ch);
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 // CallDelay(1, OpenInfrared);
 
-                 // CallDelay(2, OpenReceiveData);
 
-                 // CallDelay(3, RequestBattery);
 
-                 if (CommonConfig.EnableDecryption && NeedDecryption)
 
-                 {
 
-                     // 这里验证指令,开始请求授权
 
-                     // 启动轮询协程
 
-                     StartCoroutine(PollingCoroutine());
 
-                 }
 
-                 else {
 
-                     //除了pc,Android 直接连接
 
-                     CallDelay(2, () =>
 
-                     {
 
-                         if (status != BluetoothStatusEnum.ConnectSuccess) return;
 
-                         InitWhenConenct();
 
-                         if (CommonConfig.StandaloneMode)
 
-                         {
 
-                             if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home"
 
-                                 && PersistenHandler.ins.menuBackCtr.views.Count == 0
 
-                                 && !FindObjectOfType<NewUserGuider>())
 
-                             {
 
-                                 ViewMgr.Instance.ShowView<GameStartView>();
 
-                             }
 
-                         }
 
-                     });
 
-                 }
 
-             };
 
-             bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
 
-             {
 
-                 Log("连接失败\n" + helper.getDeviceName());
 
-                 OnDisconnect();
 
-             };
 
-             bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
 
-             {
 
-                 OnCharacteristicChanged(helper, value, characteristic);
 
-             };
 
-             bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
 
-             {
 
-                 scanLock = false;
 
-                 if (connectCanceled) {
 
-                     userDoConnect = false;
 
-                     canConnect = true;
 
-                     status = BluetoothStatusEnum.Connect;
 
-                     Debug.Log("ble connectCanceled");
 
-                     return;
 
-                 }
 
-                 foreach (BluetoothDevice device in nearbyDevices)
 
-                 {
 
-                     Log("发现设备 " + device.DeviceName);
 
-                     //if (device.DeviceName == targetDeviceName)
 
-                     if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO)
 
-                     {   //需要判断是否是红外弓箭
 
-                         //targetDeviceNameHOUYIPro targetDeviceNameHOUYIPro{ HOUYI Pro }
 
-                         if (targetDeviceNameHOUYIPro.Contains(device.DeviceName))
 
-                         {
 
-                             BLEConnectByName(device.DeviceName);
 
-                             Log("匹配HOUYIPRO设备 " + device.DeviceName);
 
-                             return;
 
-                         }
 
-                     }
 
-                     else if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO)
 
-                     {   //需要判断是否是ARTEMISPro弓箭
 
-                         if (targetDeviceName.Contains(device.DeviceName))
 
-                         {
 
-                             BLEConnectByName(device.DeviceName);
 
-                             Log("匹配ARTEMISPro设备 " + device.DeviceName);
 
-                             return;
 
-                         }
 
-                     }
 
-                     else if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun)
 
-                     {
 
-                         //需要判断是否是枪
 
-                         if (targetDeviceNameGun.Contains(device.DeviceName))
 
-                         {
 
-                             BLEConnectByName(device.DeviceName);
 
-                             Log("匹配枪设备 " + device.DeviceName);
 
-                             return;
 
-                         }
 
-                     }
 
-                     else
 
-                     {   //其余的九轴连接
 
-                         //不允许匹配HOUYIPRO,因为这个没有九轴 targetDeviceName { Bbow_20210501 }
 
-                         if (targetDeviceName.Contains(device.DeviceName))
 
-                         {
 
-                             BLEConnectByName(device.DeviceName);
 
-                             Log("匹配普通设备设备 " + device.DeviceName);
 
-                             return;
 
-                         }
 
-                     }
 
-                 }
 
-                 if (_scanCanRetryCount > 0) {
 
-                     _scanCanRetryCount--;
 
-                     scanLock = true;
 
-                     ConnectBleHelper();
 
-                 } else {
 
-                     userDoConnect = false;
 
-                     canConnect = true;
 
-                     Log("没有发现设备");
 
-                     TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
 
-                     SetStatus(BluetoothStatusEnum.ConnectFail);
 
-                 }
 
-             };
 
-             bluetoothHelper.ScanNearbyDevices();
 
-             Log("正在扫描设备");
 
-         }
 
-         catch (Exception e)
 
-         {
 
-             Debug.LogError(e);
 
-             HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
 
-         }
 
-     }
 
-     void BLEConnectByName(string name) {
 
-         deviceName = name;
 
-         bluetoothHelper.setDeviceName(deviceName);
 
-         bluetoothHelper.Connect();
 
-     }
 
-     void HandleConnectException(string errorText)
 
-     {
 
-         scanLock = false;
 
-         canConnect = true;
 
-         // SetStatus(BluetoothStatusEnum.ConnectFail);
 
-         status = BluetoothStatusEnum.Connect;
 
-         userDoConnect = false;
 
-         PopupMgr.ins.ShowTip(errorText);
 
-     }
 
-     // 协程来处理轮询
 
-     // 目前仅手枪支持该指令。
 
-     // 调用 AUTHOR_SendReq 方法,传递系统Tick值(在这里用一个示例值)
 
-     private bool isPolling = false;
 
-     private int PollingCoroutineCount = 4;
 
-     private IEnumerator PollingCoroutine()
 
-     {
 
-         // 发送请求
 
-         // SendEncrypt();
 
-         // 设置轮询标志
 
-         isPolling = true;
 
-         PollingCoroutineCount = 4;
 
-         uint systemTick = (uint)DateTime.Now.Ticks;
 
-         while (isPolling)
 
-         {
 
-             // 等待一秒
 
-             yield return new WaitForSeconds(1f);
 
-             SendEncrypt(systemTick);
 
-             PollingCoroutineCount--;
 
-         }
 
-     }
 
-     /// <summary>
 
-     /// 1、加密字节由系统生成的随机码加密而成;
 
-     /// 2、蓝牙每次断开并重新连接后,会重新生成随机加密值;
 
-     /// 3、当设备未发送正确的解密信息时,APP会每秒发送1次请求,直到解密成功。
 
-     /// </summary>
 
-     /// <param name="systemTick"></param>
 
-     private void SendEncrypt(uint systemTick) {
 
-         byte[] sendByte = BluetoothDecryptor.AUTHOR_SendReq(systemTick);
 
-         Debug.Log("请求sendByte:" + BitConverter.ToString(sendByte));
 
-         WriteByteData(sendByte);
 
-     }
 
-     // 模拟从硬件读取数据
 
-     public void  StopEncrypt()
 
-     {
 
-         isPolling = false;
 
-     }
 
-     void ConnectBleByUDP()
 
-     {
 
-         try
 
-         {
 
-             BleUDP.ins.OnConnected = () =>
 
-             {
 
-                 Log("连接成功\n" + deviceName);
 
-                 SetStatus(BluetoothStatusEnum.ConnectSuccess);
 
-                 BowCamera.isTouchMode = false;
 
-                 InitWhenConenct();
 
-             };
 
-             BleUDP.ins.OnConnectionFailed = () =>
 
-             {
 
-                 Log("连接失败\n" + deviceName);
 
-                 OnDisconnect();
 
-             };
 
-             BleUDP.ins.OnCharacteristicChanged = (byte[] value) =>
 
-             {
 
-                 //if (!hasData) {
 
-                 //    hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
 
-                 //    UploadMacAddress(value);
 
-                 //}
 
-                 //hasData = true;
 
-                 if (status != BluetoothStatusEnum.ConnectSuccess) return;
 
-                 if (!hasData)
 
-                 {
 
-                     hasData = true;
 
-                     hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
 
-                 }
 
-                 dataCount++;
 
-                 if (curMac == null && dataCount < 500)
 
-                 {
 
-                     UploadMacAddress(value);
 
-                 }
 
-                 byte[] bytes = value;
 
-                 // Log(String.Join(",", bytes));
 
-                 BluetoothClient.UploadData(0, bytes);
 
-                 if (AimHandler.ins)
 
-                 {
 
-                     AimHandler.ins.OnDataReceived(bytes);
 
-                 }
 
-             };
 
-             BleUDP.ins.OnScanEnded = () =>
 
-             {
 
-                 scanLock = false;
 
-                 deviceName = targetDeviceName;
 
-                 BleUDP.ins.Connect();
 
-                 Log("发现设备\n" + deviceName);
 
-             };
 
-             BleUDP.ins.ScanNearbyDevices();
 
-         }
 
-         catch (Exception e)
 
-         {
 
-             Debug.LogError(e.Message);
 
-             Debug.LogError(e.StackTrace);
 
-             scanLock = false;
 
-             canConnect = true;
 
-             SetStatus(BluetoothStatusEnum.ConnectFail);
 
-         }
 
-     }
 
-     #region 自动进入/退出休眠状态, 这里做程指令发送队列,为了控制连续发送指令的间隔,避免硬件收不到或处理不过来
 
-     class CmdToSend
 
-     {
 
-         public string[] cmds;
 
-         public Action onComplete;
 
-         public Func<bool> canDo;
 
-         public CmdToSend(string[] cmds, Action onComplete, Func<bool> canDo)
 
-         {
 
-             this.cmds = cmds;
 
-             this.onComplete = onComplete;
 
-             this.canDo = canDo;
 
-         }
 
-     }
 
-     Queue<CmdToSend> cmdWaitingList = new Queue<CmdToSend>();
 
-     bool isSendCmdLocked = false;
 
-     bool canAutoDormancy = false;
 
-     bool isStartUp = false;
 
-     JCUnityLib.CountLock needModularAwake = new JCUnityLib.CountLock();
 
-     void CheckAndStartUp()
 
-     {
 
-         if (needModularAwake.IsLocked())
 
-         {
 
-             StartUp();
 
-         }
 
-         else
 
-         {
 
-             Dormancy();
 
-         }
 
-     }
 
-     void InitAutoDormancy()
 
-     {
 
-         // GlobalEventCenter.ins.onGameSceneLoad += () => { 
 
-         //     needModularAwake.Lock(); 
 
-         //     CheckAndStartUp();
 
-         // };
 
-         // GlobalEventCenter.ins.onGameSceneDestroy += () => { 
 
-         //     needModularAwake.Unlock(); 
 
-         //     CheckAndStartUp();
 
-         // };
 
-         // GlobalEventCenter.ins.onSimulateMouseAwakeChanged += (waked) => {
 
-         //     if (waked) needModularAwake.Lock();
 
-         //     else needModularAwake.Unlock();;
 
-         //     CheckAndStartUp();
 
-         // };
 
-         // GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged += (waked) => {
 
-         //     if (waked) needModularAwake.Lock();
 
-         //     else needModularAwake.Unlock();;
 
-         //     CheckAndStartUp();
 
-         // };
 
-         //暂时关闭自动休眠,默认是需要模块保持激活
 
-         needModularAwake.Lock();
 
-     }
 
-     void InitWhenConenct()
 
-     {
 
-         canAutoDormancy = true;
 
-         List<string> cmds = new List<string>();
 
-         cmds.Add("M"); //获取Mac地址
 
-         cmds.Add("I"); //获取设备信息
 
-         cmds.Add("b"); //确保开启stm32
 
-         cmds.Add("b"); //获取初始电量
 
-         cmds.Add("1"); //开启发送逻辑
 
-         Action onComplete = null;
 
-         if (needModularAwake.IsLocked())
 
-         {
 
-             cmds.Add("w"); //红外灯开启
 
-             cmds.Add("3"); //九轴开启
 
-             onComplete = () =>
 
-             {
 
-                 isStartUp = true;
 
-             };
 
-         }
 
-         else
 
-         {
 
-             cmds.Add("s"); //红外灯关闭
 
-             cmds.Add("S"); //Stm32关闭
 
-             cmds.Add("4"); //九轴关闭
 
-             onComplete = () =>
 
-             {
 
-                 isStartUp = false;
 
-             };
 
-         }
 
-         SendCDM(null, onComplete, cmds.ToArray());
 
-     }
 
-     void DestroyWhenDisconenct()
 
-     {
 
-         canAutoDormancy = false;
 
-         sendCMD_CheckAndDoStop(null);
 
-     }
 
-     //启动
 
-     void StartUp()
 
-     {
 
-         SendCDM(() =>
 
-         {
 
-             return !isStartUp;
 
-         }, () =>
 
-         {
 
-             isStartUp = true;
 
-         }, "b", "w", "3");
 
-     }
 
-     //休眠
 
-     void Dormancy()
 
-     {
 
-         SendCDM(() =>
 
-         {
 
-             return isStartUp;
 
-         }, () =>
 
-         {
 
-             isStartUp = false;
 
-         }, "4", "s", "S");
 
-     }
 
-     void SendCDM(Func<bool> canDo, Action onComplete, params string[] cmds)
 
-     {
 
-         CmdToSend cmdToSend = new CmdToSend(cmds, onComplete, canDo);
 
-         if (isSendCmdLocked)
 
-         {
 
-             cmdWaitingList.Enqueue(cmdToSend);
 
-             return;
 
-         }
 
-         sendCMD_NotCheck(cmdToSend);
 
-     }
 
-     void sendCMD_NotCheck(CmdToSend cmdToSend)
 
-     {
 
-         if (cmdToSend.canDo != null && !cmdToSend.canDo.Invoke())
 
-         {
 
-             sendCMD_CheckNext();
 
-             return;
 
-         }
 
-         isSendCmdLocked = true;
 
-         Sequence sequence = DOTween.Sequence();
 
-         sequence.PrependInterval(0.3f);
 
-         foreach (var cmd in cmdToSend.cmds)
 
-         {
 
-             sequence.AppendCallback(() =>
 
-             {
 
-                 bool stopped = sendCMD_CheckAndDoStop(sequence);
 
-                 if (!stopped) WriteData(cmd);
 
-             });
 
-             sequence.AppendInterval(0.5f);
 
-         }
 
-         sequence.AppendCallback(() =>
 
-         {
 
-             bool stopped = sendCMD_CheckAndDoStop(sequence);
 
-             if (!stopped)
 
-             {
 
-                 isSendCmdLocked = false;
 
-                 cmdToSend.onComplete?.Invoke();
 
-                 sendCMD_CheckNext();
 
-             }
 
-         });
 
-         sequence.SetUpdate(true);
 
-     }
 
-     void sendCMD_CheckNext()
 
-     {
 
-         if (cmdWaitingList.Count <= 0) return;
 
-         CmdToSend cmdToSend = cmdWaitingList.Dequeue();
 
-         sendCMD_NotCheck(cmdToSend);
 
-     }
 
-     bool sendCMD_CheckAndDoStop(Sequence sequence)
 
-     {
 
-         if (canAutoDormancy) return false;
 
-         isStartUp = false;
 
-         isSendCmdLocked = false;
 
-         cmdWaitingList.Clear();
 
-         if (sequence != null) sequence.Kill();
 
-         return true;
 
-     }
 
-     #endregion
 
-     public void RequestBattery()
 
-     {
 
-         if (!isStartUp) return;
 
-         if (isSendCmdLocked) return;
 
-         WriteData("b");
 
-     }
 
-     /// <summary>
 
-     /// 获取设备信息 手枪、HOUYI Pro 和 ARTEMIS Pro
 
-     /// </summary>
 
-     public void RequestDeviceInfo()
 
-     {
 
-         if (isSendCmdLocked) return;
 
-         WriteData("I");
 
-     }
 
-     /// <summary>
 
-     /// 旧的红外射箭指令
 
-     /// </summary>
 
-     public void ReplyInfraredShoot()
 
-     {
 
-         if (isSendCmdLocked) return;
 
-         WriteData("I");
 
-     }
 
-     void CallDelay(float delayTime, TweenCallback callback)
 
-     {
 
-         Sequence sequence = DOTween.Sequence();
 
-         sequence.PrependInterval(delayTime).AppendCallback(callback);
 
-         sequence.SetUpdate(true);
 
-     }
 
-     public void WriteData(string data)
 
-     {
 
-         //Debug.Log("WriteData:" + data);
 
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-         //BleUDP.ins.SendMsg(data);
 
-         firstBluetoothWindows.Write(data);
 
- #else
 
-         if (DebugDeviceCMD.ins) DebugDeviceCMD.ins.ShowCMD(data);
 
-         BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
 
-         ch.setService(bluetoothService.getName());
 
-         bluetoothHelper.WriteCharacteristic(ch, data);
 
- #endif
 
-     }
 
-     public void WriteByteData(byte[] data)
 
-     {
 
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-         firstBluetoothWindows.WriteByte(data);
 
- #else
 
-         BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
 
-         ch.setService(bluetoothService.getName());
 
-         bluetoothHelper.WriteCharacteristic(ch, data);
 
- #endif
 
-     }
 
-     void Log(string text)
 
-     {
 
-         if (textUI)
 
-         {
 
-             textUI.text = text;
 
-         }
 
-         Debug.Log(string.Format("[{0}]{1}", typeof(BluetoothAim).Name, text));
 
-     }
 
-     [NonSerialized] public string curMac;
 
-     void UploadMacAddress(byte[] bytes) {
 
-         string mac = System.Text.Encoding.ASCII.GetString(bytes);
 
-         if (mac != null) mac = mac.Trim();
 
-         if (CheckIsMacValid(mac)) {
 
-             SideTipView.ShowTip("Mac获取成功:" + mac, Color.white);
 
-             //需要增加一个判断,判断是否对应的mac设备。不是需要进行重新连接
 
-             if (!AimHandler.ins.aimDeviceInfo.bInitMac)
 
-             {
 
-                 Debug.Log("设置设备mac:"+ mac);
 
-                 AimHandler.ins.SetAimDeviceMac(mac);
 
-             }
 
-             else if (AimHandler.ins.aimDeviceInfo.mac != mac) {
 
-                 Debug.Log("设备不一样,断开连接");
 
-                 DoConnect();
 
-                 //延迟一点时间后重新触发扫描
 
-                 CallDelay(4, () =>
 
-                 {
 
-                     Debug.Log("CallDelay 重新连接");
 
-                     DoConnect();
 
-                 });
 
-                 return;
 
-             }
 
-             curMac = mac;
 
-             LoginMgr.myUserInfo.mac = mac;
 
-             UserComp.Instance.saveMac();
 
-         } else {
 
-             SideTipView.ShowTip("Mac获取失败", Color.yellow);
 
-         }
 
-     }
 
-     bool CheckIsMacValid(string mac) {
 
-         if (mac == null) return false;
 
-         if (!mac.StartsWith("{")) return false;
 
-         if (!mac.EndsWith("}")) return false;
 
-         if (!mac.Contains(":")) return false;
 
-         char[] validChars = {'{','}',':','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
 
-         foreach (var c in mac.ToCharArray()) {
 
-             if (Array.IndexOf(validChars, c) == -1) return false;
 
-         }
 
-         if (mac.Length != 19) return false;
 
-         string macNoneFrame = mac.Substring(1, mac.Length - 2);
 
-         string[] macNoneFrameSplits = macNoneFrame.Split(':');
 
-         if (macNoneFrameSplits.Length != 6) return false;
 
-         foreach (var item in macNoneFrameSplits) {
 
-             if (item.Length != 2) return false;
 
-             foreach (var c in item.ToCharArray())
 
-                 if (Array.IndexOf(validChars, c) < 3) return false;
 
-         }
 
-         return true;
 
-     }
 
-     #region 2p蓝牙部分,使用了SmartBowSDK
 
-     //2p蓝牙连接 使用 SmartBowSDK
 
-     SmartBowHelper smartBowHelper2P;
 
-     //陀螺仪校准进度
 
-     public int smartBowHelper2_Progress { get => (int)(smartBowHelper2P.GetGyrProgress() * 100); }
 
-     bool doConnect2P = false;
 
-     bool userDoConnect2P = false;
 
-     int _scanCanRetryCount2P = 4;
 
-     bool canConnect2P = true;
 
-     public bool bNoNeedToReconnect { get; set; } = true;//是否需要重连
 
-     bool lerpForRotation2P = true;
 
-     float lerpTimeRate2P = 7;
 
-     private Quaternion newRotation2P = Quaternion.identity;
 
-     //数据存储标识
 
-     string userTags = "smartbow";
 
-     int deviceId2 = 2;
 
-     public void onCreateSmartBowHelper2P() {
 
-         smartBowHelper2P = SmartBowHelper.NewInstance();
 
-     
 
-         smartBowHelper2P.OnBluetoothModuleInited += () =>
 
-         {
 
-             Debug.Log("OnBluetoothModuleInited");
 
-             //判断是否是同一个mac
 
-             //需要增加一个判断,判断是否对应的mac设备。不是需要进行重新连接
 
-             string mac = smartBowHelper2P.GetMacAddress();
 
-             if (!AimHandler.ins.aimDeviceInfo.bInitMac)
 
-             {
 
-                 Debug.Log("smartBowHelper2P 设置设备mac:" + mac);
 
-                 AimHandler.ins.SetAimDeviceMac(mac);
 
-             }
 
-             else if (AimHandler.ins.aimDeviceInfo.mac != mac)
 
-             {
 
-                 Debug.Log("设备不一样,断开连接");
 
-                 DoConnect2P();
 
-                 //延迟一点时间后重新触发扫描
 
-                 CallDelay(4, () =>
 
-                 {
 
-                     Debug.Log("CallDelay 重新连接");
 
-                     DoConnect2P();
 
-                 });
 
-                 return;
 
-             }
 
-             UpdateConnectText();
 
-             smartBowHelper2P.StartRotationSensor();
 
-             smartBowHelper2P.StartShootingSensor();
 
-             //if (SB_EventSystem.ins)
 
-             //{
 
-             //    //唤起/隐藏虚拟鼠标
 
-             //    SB_EventSystem.ins.AwakenSimulateMouse();
 
-             //}
 
-         };
 
-         smartBowHelper2P.OnBluetoothError += (error, message) =>
 
-         {
 
-             Debug.Log("smartBowHelper2P error:" + error);
 
-             if (error == BluetoothError.ScanNotFoundTargetDevice)
 
-             {
 
-                 scanLock = false;
 
-                 PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
 
-                 if (_scanCanRetryCount2P > 0)
 
-                 {
 
-                     _scanCanRetryCount2P--;
 
-                     scanLock = true;
 
-                     ConnectSmartBowHelper2P();
 
-                 }
 
-                 else
 
-                 {
 
-                     userDoConnect2P = false;
 
-                     canConnect2P = true;
 
-                     Log("没有发现设备");
 
-                     TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
 
-                     
 
-                 }
 
-                 return;
 
-             }
 
-             PopupMgr.ins.ShowTip(message);
 
-         };
 
-         smartBowHelper2P.OnBluetoothStatusChanged += (oldStatus, newStatus) =>
 
-         {
 
-             UpdateConnectText();
 
-         };
 
-         smartBowHelper2P.OnRotationUpdate += (r) =>
 
-         {
 
-             newRotation2P = r;
 
-             //GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r); 
 
-             //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r);
 
-         };
 
-         smartBowHelper2P.OnShooting += OnShot2P;
 
-         smartBowHelper2P.OnFunctionKeyPress += () =>
 
-         {
 
-             //如果是双人场景,触发提示
 
-             if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "GameDouble")
 
-             {
 
-                 if (GameObject.Find("AutoResetViewNewRight")) return;
 
-                 GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
 
-                 resetView.name = "AutoResetViewNewRight";
 
-                 AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
 
-                 autoResetViewNewScript.setPosRight();
 
-                 autoResetViewNewScript.action_OnDestroy += () =>
 
-                 {
 
-                     smartBowHelper2P.ResetAim();
 
-                 };
 
-             }
 
-             else {
 
-                 smartBowHelper2P.ResetAim();
 
-             }
 
-          
 
-         };
 
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-         //用户2window连接 BluetoothWindows.IsWindows() &&
 
-         if ( currentBLEPlayer == BluetoothPlayer.SECONDE_PLAYER)
 
-         {
 
-             Debug.Log("SECONDE_PLAYER BleWinHelper.RegisterTo");
 
-             BleWinHelper com = BleWinHelper.RegisterTo(smartBowHelper2P.gameObject, smartBowHelper2P.CreateBluetoothWindows(),"2P");
 
-         }
 
- #endif
 
-     }
 
-     void OnShot2P(float speed)
 
-     {
 
-         if (GameController.ins.GetArmBowDoublePlayer(PlayerType.SecondPlayer) != null)
 
-         {
 
-             //本地双人模式下处理2P ,1P在 ShootCheck 类处理
 
-             GameController.ins.GetArmBowDoublePlayer(PlayerType.SecondPlayer).ADS_fire(true, speed);
 
-         }
 
-     }
 
-     void UpdateConnectText()
 
-     {
 
-         var newStatus = smartBowHelper2P.GetBluetoothStatus();
 
-         if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) {
 
-             if (GameController.ins) {
 
-                 GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = true;
 
-             }
 
-             Debug.Log("<color=blue>未连接</color>(点击连接)");
 
-             if (!bNoNeedToReconnect) return;
 
-             scanLock = false;
 
-             if (_scanCanRetryCount2P > 0)
 
-             {
 
-                 _scanCanRetryCount2P--;
 
-                 scanLock = true;
 
-                 ConnectSmartBowHelper2P();
 
-             }
 
-             else
 
-             {
 
-                 userDoConnect2P = false;
 
-                 canConnect2P = true;
 
-                 Log("没有发现设备");
 
-                 PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
 
-                 TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
 
-             }
 
-         }else if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) {
 
-             Debug.Log("<color=#FF670D>连接中</color>");
 
-             if (GameController.ins)
 
-             {
 
-                 GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
 
-             }
 
-         }else if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
 
-         {
 
-             if (GameController.ins) {
 
-                 GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
 
-             }
 
-             if (smartBowHelper2P.IsBluetoothModuleInited()) Debug.Log("<color=green>已连接</color>(点击断开)");
 
-             else Debug.Log("<color=green>已连接</color><color=blue>(正在初始化)</color>");
 
-         }
 
-     }
 
-     public void ConnectSmartBowHelper2P() {
 
- //#if UNITY_ANDROID
 
- //        PopupMgr.ins.ClearAllTip();
 
- //        if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
 
- //        {
 
- //            HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
 
- //            return;
 
- //        }
 
- //        if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectSmartBowHelper2P, (permission) => {
 
- //            if (permission.Contains("LOCATION"))
 
- //            {
 
- //                HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
 
- //            }
 
- //            else if (permission.Contains("BLUETOOTH"))
 
- //            {
 
- //                HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
 
- //            }
 
- //        })) return;
 
- //#endif
 
-         if (smartBowHelper2P == null)
 
-         {
 
-             onCreateSmartBowHelper2P();
 
-             CallDelay(1, () =>
 
-             {
 
-                 Debug.Log("Connect*********");
 
-                 smartBowHelper2P.Connect(userTags,deviceId2,true);//不在sdk 判断mac
 
-             });
 
-         }
 
-         else {
 
-             smartBowHelper2P.Connect(userTags, deviceId2,true);
 
-         }
 
-     }
 
-     public void DoConnect2P()
 
-     {
 
-         Debug.Log("DoConnect2P");
 
-         if (smartBowHelper2P == null || smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.None)
 
-         {
 
-             connectCanceled = false;
 
-             userDoConnect2P = true;
 
-             doConnect2P = true;
 
-             bNoNeedToReconnect = true;
 
-             Connect2P();
 
-         }
 
-         else if (smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
 
-         {
 
-             userDoConnect2P = false;
 
-             doConnect2P = false;
 
-             canConnect2P = true;
 
-             DisconnectSmartBowHelper2P();
 
-         }
 
-     }
 
-     void Connect2P()
 
-     {
 
-         //if (BluetoothShoot.scanLock)
 
-         //{
 
-         //    return;
 
-         //}
 
-         if (!canConnect2P)
 
-         {
 
-             return;
 
-         }
 
-         doConnect = false;
 
-         doConnect2P = false;
 
-         scanLock = true;
 
-         canConnect2P = false;
 
-         _scanCanRetryCount2P = 4;
 
-         //SetStatus2P(BluetoothStatusEnum.Connecting);
 
-         ConnectSmartBowHelper2P();
 
-     }
 
-     //初始化陀螺仪
 
-     public  void OnCalibrateGyr2P()
 
-     {
 
-         if (smartBowHelper2P.IsGyrCalibrating()) smartBowHelper2P.StopGyrCalibration();
 
-         else smartBowHelper2P.StartGyrCalibration();
 
-     }
 
-     //初始化地磁计
 
-     public void OnCalibrateMag2P()
 
-     {
 
-         smartBowHelper2P.StartMagCalibration();
 
-     }
 
-     #endregion
 
-     #region 当前选择操作的设备 如1p或者2p
 
-     public SmartBowHelper getSmartBowHelper2P() {
 
-         return smartBowHelper2P;
 
-     }
 
-     public void setBLEPlayer(BluetoothPlayer blePlayer)
 
-     {
 
-         //切换蓝牙用户时候,判断是否正在连接,如果正在匹配连接的话,取消连接状态
 
-         if (blePlayer != currentBLEPlayer) {
 
-             Debug.Log("Set BluetoothPlayer status:" + status);
 
-             onCancelAllConnecting();
 
-         }
 
-         currentBLEPlayer = blePlayer;
 
-         Debug.Log("Set BluetoothPlayer:" + currentBLEPlayer);
 
-     }
 
-     public BluetoothPlayer getBLEPlayer()
 
-     {
 
-         //Debug.Log("Get BluetoothPlayer:" + currentBLEPlayer);
 
-         return currentBLEPlayer;
 
-     }
 
-     //更新跟随点位置
 
-     public void UpdateCameraToLookNewPoint2P() {
 
-         if (CameraToLookNew.ins != null)
 
-         {
 
-             Transform m_controlObj = CameraToLookNew.ins.transform;
 
-             if (lerpForRotation2P)
 
-                 m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation2P, Time.deltaTime * lerpTimeRate2P);
 
-             else
 
-                 m_controlObj.localRotation = newRotation2P;
 
-         }
 
-     }
 
-     public int get2PBattery() {
 
-         return smartBowHelper2P == null? 0 : smartBowHelper2P.GetBattery();
 
-     }
 
-     //主更新调用
 
-     void updateSmartBowHelper2P() {
 
-         UpdateCameraToLookNewPoint2P();
 
-     }
 
-     #endregion
 
-     //取消正在连接的连接
 
-    public  void onCancelAllConnecting(BluetoothStatusEnum _bluetoothStatus = BluetoothStatusEnum.ConnectFail) {
 
-         // status != BluetoothStatusEnum.ConnectSuccess
 
-         Debug.LogWarning("onCancelAllConnecting 1:" + status);
 
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
 
-         //BleUDP.ins != null 
 
-         //BluetoothWindows.IsWindows() &&
 
-         if (status == BluetoothStatusEnum.Connecting)
 
-         {
 
-             //Debug.LogWarning("onCancelAllConnecting 2:" + status);
 
-             userDoConnect = false;
 
-             doConnect = false;
 
-             OnDisconnect();
 
-             // BleUDP.ins.Disconnect();
 
-             firstBluetoothWindows.Disconnect();
 
-             SetStatus(_bluetoothStatus);
 
-             Debug.LogWarning("onCancelAllConnecting 3:" + status);
 
-         }
 
- #else
 
-         if (bluetoothHelper != null && status == BluetoothStatusEnum.Connecting)
 
-         {
 
-             Debug.LogWarning("onCancelAllConnecting 2:" + status);
 
-             userDoConnect = false;
 
-             doConnect = false;
 
-             OnDisconnect(); 
 
-             DisconnectBleHelper();
 
-             SetStatus(_bluetoothStatus);
 
-         }
 
- #endif
 
-         //smartBowHelper2P.GetBluetoothStatus() != SmartBowSDK.BluetoothStatusEnum.Connected
 
-         if (smartBowHelper2P != null && smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
 
-         {
 
-             userDoConnect2P = false;
 
-             doConnect2P = false;
 
-             canConnect2P = true;
 
-             DisconnectSmartBowHelper2P();
 
-             //smartBowHelper2P.Disconnect();
 
-             Debug.Log("onCancelAllConnecting smartBowHelper2P  2:" + smartBowHelper2P.GetBluetoothStatus());
 
-         }
 
-     }
 
-     #region 判断是否是1p设备连接的是HOUYIPRO信息
 
-     public bool isMainConnectToHOUYIPRO() {
 
-         bool _isHOUYIPRO = false;
 
-         foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
 
-         {
 
-             if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && p.type == (int)AimDeviceType.HOUYIPRO)
 
-             {
 
-                 _isHOUYIPRO = true;
 
-             }
 
-         }
 
-         if (_isHOUYIPRO)
 
-         {
 
-             //如果是。判断是否连接
 
-             return status == BluetoothStatusEnum.ConnectSuccess;
 
-         }
 
-         else {
 
-             return false;
 
-         }
 
-       
 
-     
 
-     }
 
-     #endregion
 
-     #region 判断是否是1p设备连接的是ARTEMISPRO信息
 
-     public bool isMainConnectToARTEMISPRO()
 
-     {
 
-         bool _isARTEMISPRO = false;
 
-         foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
 
-         {
 
-             if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && p.type == (int)AimDeviceType.ARTEMISPRO)
 
-             {
 
-                 _isARTEMISPRO = true;
 
-             }
 
-         }
 
-         if (_isARTEMISPRO)
 
-         {
 
-             //如果是。判断是否连接
 
-             return status == BluetoothStatusEnum.ConnectSuccess;
 
-         }
 
-         else
 
-         {
 
-             return false;
 
-         }
 
-     }
 
-     #endregion
 
-     #region 判断是否是1p设备连接的是红外信息,有多种红外设备
 
-     public bool isMainConnectToInfraredDevice()
 
-     {
 
-         bool _isInfraredDevice = false;
 
-         foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
 
-         {
 
-             if ((int)BluetoothPlayer.FIRST_PLAYER == p.id  && (p.type == (int)AimDeviceType.ARTEMISPRO || p.type == (int)AimDeviceType.HOUYIPRO))
 
-             {
 
-                 _isInfraredDevice = true;
 
-             }
 
-         }
 
-         if (_isInfraredDevice)
 
-         {
 
-             //如果是。判断是否连接
 
-             return status == BluetoothStatusEnum.ConnectSuccess;
 
-         }
 
-         else
 
-         {
 
-             return false;
 
-         }
 
-     }
 
-     #endregion
 
-     #region 判断是否是1p设备连接的是Gun信息
 
-     public bool isMainConnectToGun()
 
-     {
 
-         bool _isGun = false;
 
-         foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
 
-         {
 
-             if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && p.type == (int)AimDeviceType.Gun)
 
-             {
 
-                 _isGun = true;
 
-             }
 
-         }
 
-         if (_isGun)
 
-         {
 
-             //如果是。判断是否连接
 
-             return status == BluetoothStatusEnum.ConnectSuccess;
 
-         }
 
-         else
 
-         {
 
-             return false;
 
-         }
 
-     }
 
-     #endregion
 
-     /// <summary>
 
-     /// 设置设备类型
 
-     /// </summary>
 
-     public void SetMainConnectDeviceType() {
 
-         //1号玩家连接蓝牙成功时候,设置一次 GlobalData.MyDeviceMode 
 
-         foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
 
-         {
 
-             if ((int)BluetoothPlayer.FIRST_PLAYER == p.id)
 
-             {
 
-                 if (p.type == (int)AimDeviceType.Gun)
 
-                 {
 
-                     //设置枪
 
-                     HomeView.ins.MyTopBarView.onChangeType(1);
 
-                 }
 
-                 else
 
-                 {
 
-                     //其余默认是弓箭
 
-                     HomeView.ins.MyTopBarView.onChangeType(0);
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     #region 用户1 window ble 连接和部分共用代码
 
-     void OnConnected_windows1()
 
-     {
 
-         Log("[WinBle_1P]连接成功\n" + deviceName);
 
-         SetStatus(BluetoothStatusEnum.ConnectSuccess);
 
-         BowCamera.isTouchMode = false;
 
-         if (CommonConfig.EnableDecryption && NeedDecryption)
 
-         {
 
-             // 这里验证指令,开始请求授权
 
-             // 启动轮询协程
 
-             StartCoroutine(PollingCoroutine());
 
-         }
 
-         else
 
-         {
 
-             //除了pc,Android 直接连接
 
-             InitWhenConenct();
 
-         }
 
-     }
 
-     void OnConnectionFailed_windows1()
 
-     {
 
-         Log("连接失败\n" + deviceName);
 
-         OnDisconnect();
 
-     }
 
-     void OnCharacteristicChanged_windows1(string deviceId,byte[] value)
 
-     {
 
-         OnCharacteristicChanged(null, value, null);
 
-     }
 
-     void OnCharacteristicChanged(BluetoothHelper helper, byte[] value, BluetoothHelperCharacteristic characteristic) {
 
-         if (status != BluetoothStatusEnum.ConnectSuccess) return;
 
-         if (!hasData)
 
-         {
 
-             hasData = true;
 
-             hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
 
-         }
 
-         if (CommonConfig.EnableDecryption && NeedDecryption)
 
-         {
 
-             //Pc 版本先走校验流程
 
-             if (!BluetoothDecryptor.AUTHOR_IsDecrypt())
 
-             {
 
-                 if (value[0] == 0x5a)
 
-                 {
 
-                     // 从硬件读取数据
 
-                     if (value != null && value.Length > 0)
 
-                     {
 
-                         Debug.Log("接收到数据:" + BitConverter.ToString(value));
 
-                         BluetoothDecryptor.AUTHOR_Decrypt(value);
 
-                         if (BluetoothDecryptor.AUTHOR_IsDecrypt())
 
-                         {
 
-                             Debug.Log("解密成功!");
 
-                             //解密成功后
 
-                             StopEncrypt();
 
-                             //开始连接其他信息
 
-                             InitWhenConenct();
 
-                         }
 
-                         else
 
-                         {
 
-                             Debug.Log("解密失败!");
 
-                             //SideTipView.ShowTip("设备通信失败,断开连接", Color.yellow);
 
-                             //断开连接等操作
 
-                             if (PollingCoroutineCount <= 0)
 
-                             {
 
-                                 StopEncrypt();
 
-                                 DoConnect();
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 return;
 
-             }
 
-         }
 
-         dataCount++;
 
-         if (curMac == null && dataCount < 500)
 
-         {
 
-             UploadMacAddress(value);
 
-         }
 
-         byte[] bytes = value;
 
-         // Log(String.Join(",", bytes));
 
-         BluetoothClient.UploadData(0, bytes);
 
-         if (AimHandler.ins)
 
-         {
 
-             AimHandler.ins.OnDataReceived(bytes);
 
-         }
 
-     }
 
-     #endregion
 
- }
 
 
  |