BluetoothAim.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. using SmartBowSDK;
  8. using System.Collections;
  9. using UnityEngine.SceneManagement;
  10. using BleWinHelper = SmartBowSDK_BleWinHelper.BleWinHelper;
  11. using SmartBowSDK.CMD;
  12. using System.Linq;
  13. /* 蓝牙瞄准模块 */
  14. /* 管理1p和2p蓝牙连接,脚本外调用逻辑如果同时连接1p和2p设备,以前的逻辑还是以1p为主,如果只连2p,则使用2p数据*/
  15. public class BluetoothAim : MonoBehaviour
  16. {
  17. #region 2p蓝牙使用的是sdk,所以需要另外定义
  18. //2p时候过滤的名字
  19. readonly string targetDeviceName2P = "Bbow_20210501 | HOUYI Pro | Pistol | Pistol M9 | ARTEMIS Pro | Pistol M17 | Rifle M416";
  20. #endregion
  21. #region 弓箭蓝牙模块基本信息
  22. readonly string targetDeviceNameAxis = "Bbow_20210501 | ARTEMIS | HOUYI | HOUYI Pro | ARTEMIS Pro";
  23. readonly string targetDeviceName = "Bbow_20210501 | ARTEMIS Pro";//HOUYI Pro
  24. readonly string targetDeviceNameARTEMISPro = "ARTEMIS Pro";
  25. readonly string targetDeviceNameHOUYIPro = "HOUYI Pro";
  26. readonly string targetDeviceNameGun = "Pistol | Pistol M9 | Bbow_20210501";
  27. readonly string targetDeviceNameGun_M17 = "Pistol M17";
  28. readonly string targetDeviceNameGun_M416 = "Rifle M416";
  29. string targetDeviceService
  30. {
  31. get
  32. {
  33. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
  34. {
  35. #if UNITY_ANDROID
  36. return "0000fff0";
  37. #else
  38. return "fff0";
  39. #endif
  40. }
  41. return "6e400001";
  42. }
  43. }
  44. string targetDeviceCharacteristicWrite
  45. {
  46. get
  47. {
  48. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
  49. {
  50. #if UNITY_ANDROID
  51. return "0000fff2";
  52. #else
  53. return "fff2";
  54. #endif
  55. }
  56. return "6e400002";
  57. }
  58. }
  59. string targetDeviceCharacteristicNotify
  60. {
  61. get
  62. {
  63. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
  64. {
  65. #if UNITY_ANDROID
  66. return "0000fff1";
  67. #else
  68. return "fff1";
  69. #endif
  70. }
  71. return "6e400003";
  72. }
  73. }
  74. #endregion
  75. //当前选择的设备
  76. BluetoothPlayer currentBLEPlayer = BluetoothPlayer.FIRST_PLAYER;
  77. //1p蓝牙连接
  78. BluetoothHelper bluetoothHelper;
  79. BluetoothHelperCharacteristic characteristicWrite;
  80. BluetoothHelperService bluetoothService;
  81. string deviceName = "";
  82. bool canConnect = true;
  83. [SerializeField] Text textUI;
  84. public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
  85. int dataCount = 0;
  86. public bool hasData = false;
  87. public long hasDataTime;
  88. public static bool scanLock = false; //防止同时扫描冲突
  89. public static BluetoothAim ins;
  90. //根据设备情况来看是否需要解密,目前就是枪需要进行解密操作
  91. public bool NeedDecryption = false;
  92. public BluetoothWindows firstBluetoothWindows { get; set; }
  93. //首页初始化时候调用一次连接
  94. public bool bStartConnect = false;
  95. private void Awake()
  96. {
  97. #if UNITY_ANDROID
  98. if (CommonConfig.bUseCMD)
  99. {
  100. SmartBowLogger.isDebug = true;
  101. this.isUseCompanionDeviceManager = true;
  102. InitCMDManager();
  103. }
  104. #endif
  105. }
  106. void Start()
  107. {
  108. ins = this;
  109. InitAutoDormancy();
  110. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  111. // new GameObject("BleUDP").AddComponent<BleUDP>();
  112. firstBluetoothWindows = new BluetoothWindows();
  113. firstBluetoothWindows.OnConnected = OnConnected_windows1;
  114. firstBluetoothWindows.OnConnectionFailed = OnConnectionFailed_windows1;
  115. firstBluetoothWindows.OnCharacteristicChanged = OnCharacteristicChanged_windows1;
  116. //window蓝牙连接
  117. if (currentBLEPlayer == BluetoothPlayer.FIRST_PLAYER)
  118. {
  119. Debug.Log("FIRST_PLAYER BleWinHelper.RegisterTo");
  120. BleWinHelper.RegisterTo(gameObject, firstBluetoothWindows, "1P");
  121. }
  122. #endif
  123. }
  124. void OnDestroy()
  125. {
  126. //删除ble。1p,2p 都删除
  127. DisconnectBleHelper();
  128. DisconnectSmartBowHelper2P();
  129. if (CommonConfig.bUseCMD)
  130. ClearCMDManager();
  131. }
  132. /// <summary>
  133. /// 首页初始化的时候,如果是第一次。尝试连接蓝牙。只进行一次
  134. /// </summary>
  135. public void HomeViewConnectBLE()
  136. {
  137. if (bStartConnect) return;
  138. bStartConnect = true;
  139. //当已经连接过一把弓(1P)时,下一次打开 APP,1P 就自动处于连接的状态;用户只要打1.开 APP 和打开模块就可以自动连接,减少连接设备的操作。
  140. Debug.Log("[初始化自动连接蓝牙] 自动开始连接上一次连接的设备 1P?");
  141. AimHandler.ins.OnGetAimDeviceInfos();
  142. if (AimHandler.ins.aimDeviceInfos.arry.Count != 0)
  143. {
  144. //如果需要连接
  145. if (status != BluetoothStatusEnum.ConnectSuccess)
  146. {
  147. //setBLEPlayer(BluetoothPlayer.FIRST_PLAYER);
  148. Debug.Log("[初始化自动连接蓝牙] 当前选择的操作用户:" + getBLEPlayer());
  149. //AimHandler.ins.SetAimDeviceSelectIndex(0);
  150. //第一次进入从存储的资料获取 aimDeviceInfo
  151. AimHandler.ins.onCreateAimDeviceInfoById();
  152. DoConnect();
  153. }
  154. }
  155. }
  156. private bool userDoConnect = false;
  157. private bool doConnect = false;
  158. public Func<bool> action_DoConnectInterceptor;
  159. public void DoConnect()
  160. {
  161. if (action_DoConnectInterceptor != null)
  162. {
  163. if (action_DoConnectInterceptor.Invoke()) return;
  164. }
  165. if (status == BluetoothStatusEnum.Connect)
  166. {
  167. connectCanceled = false;
  168. userDoConnect = true;
  169. doConnect = true;
  170. SetStatus(BluetoothStatusEnum.Connecting);
  171. }
  172. else if (status == BluetoothStatusEnum.ConnectSuccess)
  173. {
  174. userDoConnect = false;
  175. doConnect = false;
  176. OnDisconnect();
  177. DisconnectBleHelper();
  178. //#if UNITY_STANDALONE_WIN || UNITY_EDITOR
  179. // //BleUDP.ins.Disconnect();
  180. // if (firstBluetoothWindows.Disconnect()) SetStatus(BluetoothStatusEnum.Connect);
  181. //#else
  182. // DisconnectBleHelper();
  183. //#endif
  184. }
  185. }
  186. //连接取消,退回登录时需要取消连接(扫描)
  187. [NonSerialized] public bool connectCanceled = false;
  188. void OnDisconnect()
  189. {
  190. curMac = null;
  191. dataCount = 0;
  192. hasData = false;
  193. canConnect = true;
  194. SetStatus(BluetoothStatusEnum.ConnectFail);
  195. BowCamera.isTouchMode = true;
  196. DestroyWhenDisconenct();
  197. if (AimHandler.ins) AimHandler.ins.SetMsOldDefault();
  198. if (isUseCompanionDeviceManager)
  199. {
  200. cmdManager.Disconnect();
  201. }
  202. }
  203. float notUserDoConnectTime = 0;
  204. void Update()
  205. {
  206. updateSmartBowHelper2P();
  207. if (userDoConnect && status == BluetoothStatusEnum.Connect)
  208. {
  209. DoConnect();
  210. }
  211. if (doConnect) Connect();
  212. //if (CommonConfig.StandaloneMode)
  213. //{
  214. // if (!userDoConnect && status == BluetoothStatusEnum.Connect)
  215. // {
  216. // if (notUserDoConnectTime == 0)
  217. // {
  218. // DoConnect();
  219. // notUserDoConnectTime = 0.1f;
  220. // }
  221. // else
  222. // {
  223. // notUserDoConnectTime += Time.deltaTime;
  224. // if (notUserDoConnectTime > 2.5f)
  225. // {
  226. // DoConnect();
  227. // notUserDoConnectTime = 0.1f;
  228. // }
  229. // }
  230. // }
  231. //}
  232. }
  233. void SetStatus(BluetoothStatusEnum statusValue)
  234. {
  235. status = statusValue;
  236. if (status == BluetoothStatusEnum.ConnectFail)
  237. {
  238. Sequence sequence = DOTween.Sequence();
  239. sequence.AppendInterval(2f);
  240. sequence.AppendCallback(delegate ()
  241. {
  242. if (status == BluetoothStatusEnum.ConnectFail)
  243. {
  244. status = BluetoothStatusEnum.Connect;
  245. }
  246. });
  247. sequence.SetUpdate(true);
  248. SimulateMouseController.ins?.SetBleConnected(false);
  249. }
  250. else if (status == BluetoothStatusEnum.ConnectSuccess)
  251. {
  252. SimulateMouseController.ins?.SetBleConnected(true);
  253. }
  254. }
  255. void DisconnectBleHelper()
  256. {
  257. if (BluetoothWindows.IsWindows())
  258. {
  259. if (firstBluetoothWindows.Disconnect())
  260. SetStatus(BluetoothStatusEnum.Connect);
  261. return;
  262. }
  263. if (bluetoothHelper != null) bluetoothHelper.Disconnect();
  264. }
  265. void DisconnectSmartBowHelper2P()
  266. {
  267. //2p
  268. if (smartBowHelper2P != null) smartBowHelper2P.Disconnect();
  269. }
  270. void Connect()
  271. {
  272. //if (BluetoothShoot.scanLock)
  273. //{
  274. // return;
  275. //}
  276. if (!canConnect)
  277. {
  278. return;
  279. }
  280. doConnect = false;
  281. doConnect2P = false;
  282. scanLock = true;
  283. canConnect = false;
  284. _scanCanRetryCount = 4;
  285. SetStatus(BluetoothStatusEnum.Connecting);
  286. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  287. //ConnectBleByUDP();
  288. firstBluetoothWindows.Connect();
  289. #else
  290. if(isUseCompanionDeviceManager)
  291. {
  292. ConnectCMD();
  293. }else
  294. {
  295. ConnectBleHelper();
  296. }
  297. #endif
  298. }
  299. int _scanCanRetryCount = 4;
  300. void ConnectBleHelper()
  301. {
  302. #if UNITY_ANDROID
  303. PopupMgr.ins.ClearAllTip();
  304. if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
  305. {
  306. Debug.Log("BluetoothHelperAndroid.IsBluetoothEnabled():" + BluetoothHelperAndroid.IsBluetoothEnabled());
  307. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
  308. return;
  309. }
  310. if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectBleHelper, (permission) =>
  311. {
  312. Debug.Log("permission ==== :" + permission);
  313. if (permission.Contains("LOCATION"))
  314. {
  315. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
  316. }
  317. else if (permission.Contains("BLUETOOTH"))
  318. {
  319. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
  320. }
  321. })) return;
  322. #endif
  323. try
  324. {
  325. BluetoothHelper.BLE = true;
  326. bluetoothHelper = BluetoothHelper.GetNewInstance();
  327. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  328. {
  329. Log("连接成功\n" + helper.getDeviceName());
  330. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  331. SetMainConnectDeviceType();
  332. if (connectCanceled)
  333. {
  334. Debug.Log("ble connectCanceled");
  335. DoConnect();
  336. return;
  337. }
  338. BowCamera.isTouchMode = false;
  339. foreach (BluetoothHelperService service in helper.getGattServices())
  340. {
  341. if (service.getName().ToLower().StartsWith(targetDeviceService))
  342. {
  343. bluetoothService = service;
  344. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  345. {
  346. if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicWrite))
  347. {
  348. characteristicWrite = characteristic;
  349. }
  350. else if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicNotify))
  351. {
  352. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  353. ch.setService(bluetoothService.getName());
  354. bluetoothHelper.Subscribe(ch);
  355. }
  356. }
  357. }
  358. }
  359. // CallDelay(1, OpenInfrared);
  360. // CallDelay(2, OpenReceiveData);
  361. // CallDelay(3, RequestBattery);
  362. if (CommonConfig.EnableDecryption && NeedDecryption)
  363. {
  364. // 这里验证指令,开始请求授权
  365. // 启动轮询协程
  366. StartCoroutine(PollingCoroutine());
  367. }
  368. else
  369. {
  370. //除了pc,Android 直接连接
  371. CallDelay(2, () =>
  372. {
  373. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  374. InitWhenConenct();
  375. //if (CommonConfig.StandaloneMode)
  376. //{
  377. // if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home"
  378. // && PersistenHandler.ins.menuBackCtr.views.Count == 0
  379. // && !FindObjectOfType<NewUserGuider>())
  380. // {
  381. // ViewMgr.Instance.ShowView<GameStartView>();
  382. // }
  383. //}
  384. });
  385. }
  386. };
  387. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  388. {
  389. Log("连接失败\n" + helper.getDeviceName());
  390. OnDisconnect();
  391. };
  392. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  393. {
  394. OnCharacteristicChanged(helper, value, characteristic);
  395. };
  396. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  397. {
  398. scanLock = false;
  399. if (connectCanceled)
  400. {
  401. userDoConnect = false;
  402. canConnect = true;
  403. status = BluetoothStatusEnum.Connect;
  404. Debug.Log("ble connectCanceled");
  405. return;
  406. }
  407. foreach (BluetoothDevice device in nearbyDevices)
  408. {
  409. int firstPlayerType = AimHandler.ins.GetAimDeviceInfo(BluetoothPlayer.FIRST_PLAYER).type;
  410. Log("发现设备:" + device.DeviceName + ",设备类型:"+ firstPlayerType);
  411. if (firstPlayerType == (int)AimDeviceType.HOUYIPRO)
  412. { //需要判断是否是红外弓箭
  413. if (TryConnectDevice(targetDeviceNameHOUYIPro, device.DeviceName, "HOUYIPRO")) return;
  414. }
  415. else if (firstPlayerType == (int)AimDeviceType.ARTEMISPRO)
  416. { //需要判断是否是ARTEMISPro弓箭
  417. if (TryConnectDevice(targetDeviceNameARTEMISPro, device.DeviceName, "ARTEMISPRO")) return;
  418. }
  419. else if (firstPlayerType == (int)AimDeviceType.Gun)
  420. {
  421. if (TryConnectDevice(targetDeviceNameGun, device.DeviceName, "Pistol")) return;
  422. }
  423. else if (firstPlayerType == (int)AimDeviceType.PistolM17)
  424. {
  425. if (TryConnectDevice(targetDeviceNameGun_M17, device.DeviceName, "PistolM17")) return;
  426. }
  427. else if (firstPlayerType == (int)AimDeviceType.RifleM416)
  428. {
  429. if (TryConnectDevice(targetDeviceNameGun_M416, device.DeviceName, "RifleM416")) return;
  430. }
  431. else
  432. { //其余的九轴连接
  433. //不允许匹配HOUYIPRO,因为这个没有九轴 targetDeviceName { Bbow_20210501 }
  434. //if (targetDeviceName.Contains(device.DeviceName))
  435. //{
  436. // BLEConnectByName(device.DeviceName);
  437. // Log("匹配普通设备设备 " + device.DeviceName);
  438. // return;
  439. //}
  440. if (TryConnectDevice(targetDeviceNameAxis, device.DeviceName, device.DeviceName)) return;
  441. }
  442. }
  443. if (_scanCanRetryCount > 0)
  444. {
  445. _scanCanRetryCount--;
  446. scanLock = true;
  447. ConnectBleHelper();
  448. }
  449. else
  450. {
  451. userDoConnect = false;
  452. canConnect = true;
  453. Log("没有发现设备");
  454. TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
  455. SetStatus(BluetoothStatusEnum.ConnectFail);
  456. }
  457. };
  458. bluetoothHelper.ScanNearbyDevices();
  459. Log("正在扫描设备");
  460. }
  461. catch (Exception e)
  462. {
  463. Debug.LogError(e);
  464. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
  465. }
  466. }
  467. /// <summary>
  468. /// 精确匹配,大小写敏感,多设备名字用 " | " 分隔
  469. /// </summary>
  470. /// <param name="filterNames">过滤字符串</param>
  471. /// <param name="deviceName">真实连接名字</param>
  472. /// <param name="deviceTypeName">日志名字</param>
  473. /// <returns></returns>
  474. private bool TryConnectDevice(string filterNames, string deviceName, string deviceTypeName)
  475. {
  476. if (string.IsNullOrWhiteSpace(filterNames)) return false;
  477. string[] filterArray = filterNames.Split('|'); // 支持多个名字
  478. foreach (var f in filterArray)
  479. {
  480. if (string.Equals(f.Trim(), deviceName.Trim(), StringComparison.OrdinalIgnoreCase))
  481. {
  482. BLEConnectByName(deviceName);
  483. Log($"匹配设备 {deviceTypeName}: {deviceName}");
  484. return true;
  485. }
  486. }
  487. return false;
  488. }
  489. void BLEConnectByName(string name)
  490. {
  491. deviceName = name;
  492. bluetoothHelper.setDeviceName(deviceName);
  493. bluetoothHelper.Connect();
  494. }
  495. void HandleConnectException(string errorText)
  496. {
  497. scanLock = false;
  498. canConnect = true;
  499. // SetStatus(BluetoothStatusEnum.ConnectFail);
  500. status = BluetoothStatusEnum.Connect;
  501. userDoConnect = false;
  502. PopupMgr.ins.ShowTip(errorText);
  503. }
  504. // 协程来处理轮询
  505. // 目前仅手枪支持该指令。
  506. // 调用 AUTHOR_SendReq 方法,传递系统Tick值(在这里用一个示例值)
  507. private bool isPolling = false;
  508. private int PollingCoroutineCount = 4;
  509. private IEnumerator PollingCoroutine()
  510. {
  511. // 发送请求
  512. // SendEncrypt();
  513. // 设置轮询标志
  514. isPolling = true;
  515. PollingCoroutineCount = 4;
  516. uint systemTick = (uint)DateTime.Now.Ticks;
  517. while (isPolling)
  518. {
  519. // 等待一秒
  520. yield return new WaitForSeconds(1f);
  521. SendEncrypt(systemTick);
  522. PollingCoroutineCount--;
  523. }
  524. }
  525. /// <summary>
  526. /// 1、加密字节由系统生成的随机码加密而成;
  527. /// 2、蓝牙每次断开并重新连接后,会重新生成随机加密值;
  528. /// 3、当设备未发送正确的解密信息时,APP会每秒发送1次请求,直到解密成功。
  529. /// </summary>
  530. /// <param name="systemTick"></param>
  531. private void SendEncrypt(uint systemTick)
  532. {
  533. byte[] sendByte = BluetoothDecryptor.AUTHOR_SendReq(systemTick);
  534. Debug.Log("请求sendByte:" + BitConverter.ToString(sendByte));
  535. WriteByteData(sendByte);
  536. }
  537. // 模拟从硬件读取数据
  538. public void StopEncrypt()
  539. {
  540. isPolling = false;
  541. }
  542. void ConnectBleByUDP()
  543. {
  544. try
  545. {
  546. BleUDP.ins.OnConnected = () =>
  547. {
  548. Log("连接成功\n" + deviceName);
  549. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  550. BowCamera.isTouchMode = false;
  551. InitWhenConenct();
  552. };
  553. BleUDP.ins.OnConnectionFailed = () =>
  554. {
  555. Log("连接失败\n" + deviceName);
  556. OnDisconnect();
  557. };
  558. BleUDP.ins.OnCharacteristicChanged = (byte[] value) =>
  559. {
  560. //if (!hasData) {
  561. // hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
  562. // UploadMacAddress(value);
  563. //}
  564. //hasData = true;
  565. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  566. if (!hasData)
  567. {
  568. hasData = true;
  569. hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
  570. }
  571. dataCount++;
  572. if (curMac == null && dataCount < 500)
  573. {
  574. UploadMacAddress(value);
  575. }
  576. byte[] bytes = value;
  577. // Log(String.Join(",", bytes));
  578. BluetoothClient.UploadData(0, bytes);
  579. if (AimHandler.ins)
  580. {
  581. AimHandler.ins.OnDataReceived(bytes);
  582. }
  583. };
  584. BleUDP.ins.OnScanEnded = () =>
  585. {
  586. scanLock = false;
  587. deviceName = targetDeviceName;
  588. BleUDP.ins.Connect();
  589. Log("发现设备\n" + deviceName);
  590. };
  591. BleUDP.ins.ScanNearbyDevices();
  592. }
  593. catch (Exception e)
  594. {
  595. Debug.LogError(e.Message);
  596. Debug.LogError(e.StackTrace);
  597. scanLock = false;
  598. canConnect = true;
  599. SetStatus(BluetoothStatusEnum.ConnectFail);
  600. }
  601. }
  602. #region 自动进入/退出休眠状态, 这里做程指令发送队列,为了控制连续发送指令的间隔,避免硬件收不到或处理不过来
  603. class CmdToSend
  604. {
  605. public string[] cmds;
  606. public Action onComplete;
  607. public Func<bool> canDo;
  608. public CmdToSend(string[] cmds, Action onComplete, Func<bool> canDo)
  609. {
  610. this.cmds = cmds;
  611. this.onComplete = onComplete;
  612. this.canDo = canDo;
  613. }
  614. }
  615. Queue<CmdToSend> cmdWaitingList = new Queue<CmdToSend>();
  616. bool isSendCmdLocked = false;
  617. bool canAutoDormancy = false;
  618. bool isStartUp = false;
  619. JCUnityLib.CountLock needModularAwake = new JCUnityLib.CountLock();
  620. void CheckAndStartUp()
  621. {
  622. if (needModularAwake.IsLocked())
  623. {
  624. StartUp();
  625. }
  626. else
  627. {
  628. Dormancy();
  629. }
  630. }
  631. void InitAutoDormancy()
  632. {
  633. // GlobalEventCenter.ins.onGameSceneLoad += () => {
  634. // needModularAwake.Lock();
  635. // CheckAndStartUp();
  636. // };
  637. // GlobalEventCenter.ins.onGameSceneDestroy += () => {
  638. // needModularAwake.Unlock();
  639. // CheckAndStartUp();
  640. // };
  641. // GlobalEventCenter.ins.onSimulateMouseAwakeChanged += (waked) => {
  642. // if (waked) needModularAwake.Lock();
  643. // else needModularAwake.Unlock();;
  644. // CheckAndStartUp();
  645. // };
  646. // GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged += (waked) => {
  647. // if (waked) needModularAwake.Lock();
  648. // else needModularAwake.Unlock();;
  649. // CheckAndStartUp();
  650. // };
  651. //暂时关闭自动休眠,默认是需要模块保持激活
  652. needModularAwake.Lock();
  653. }
  654. void InitWhenConenct()
  655. {
  656. canAutoDormancy = true;
  657. List<string> cmds = new List<string>();
  658. cmds.Add("M"); //获取Mac地址
  659. cmds.Add("I"); //获取设备信息
  660. cmds.Add("b"); //确保开启stm32
  661. cmds.Add("b"); //获取初始电量
  662. cmds.Add("1"); //开启发送逻辑
  663. Action onComplete = null;
  664. if (needModularAwake.IsLocked())
  665. {
  666. cmds.Add("w"); //红外灯开启
  667. cmds.Add("3"); //九轴开启
  668. onComplete = () =>
  669. {
  670. isStartUp = true;
  671. };
  672. }
  673. else
  674. {
  675. cmds.Add("s"); //红外灯关闭
  676. cmds.Add("S"); //Stm32关闭
  677. cmds.Add("4"); //九轴关闭
  678. onComplete = () =>
  679. {
  680. isStartUp = false;
  681. };
  682. }
  683. SendCDM(null, onComplete, cmds.ToArray());
  684. }
  685. void DestroyWhenDisconenct()
  686. {
  687. canAutoDormancy = false;
  688. sendCMD_CheckAndDoStop(null);
  689. }
  690. //启动
  691. void StartUp()
  692. {
  693. SendCDM(() =>
  694. {
  695. return !isStartUp;
  696. }, () =>
  697. {
  698. isStartUp = true;
  699. }, "b", "w", "3");
  700. }
  701. //休眠
  702. void Dormancy()
  703. {
  704. SendCDM(() =>
  705. {
  706. return isStartUp;
  707. }, () =>
  708. {
  709. isStartUp = false;
  710. }, "4", "s", "S");
  711. }
  712. void SendCDM(Func<bool> canDo, Action onComplete, params string[] cmds)
  713. {
  714. CmdToSend cmdToSend = new CmdToSend(cmds, onComplete, canDo);
  715. if (isSendCmdLocked)
  716. {
  717. cmdWaitingList.Enqueue(cmdToSend);
  718. return;
  719. }
  720. sendCMD_NotCheck(cmdToSend);
  721. }
  722. void sendCMD_NotCheck(CmdToSend cmdToSend)
  723. {
  724. if (cmdToSend.canDo != null && !cmdToSend.canDo.Invoke())
  725. {
  726. sendCMD_CheckNext();
  727. return;
  728. }
  729. isSendCmdLocked = true;
  730. Sequence sequence = DOTween.Sequence();
  731. sequence.PrependInterval(0.3f);
  732. foreach (var cmd in cmdToSend.cmds)
  733. {
  734. sequence.AppendCallback(() =>
  735. {
  736. bool stopped = sendCMD_CheckAndDoStop(sequence);
  737. if (!stopped) WriteData(cmd);
  738. });
  739. sequence.AppendInterval(0.5f);
  740. }
  741. sequence.AppendCallback(() =>
  742. {
  743. bool stopped = sendCMD_CheckAndDoStop(sequence);
  744. if (!stopped)
  745. {
  746. isSendCmdLocked = false;
  747. cmdToSend.onComplete?.Invoke();
  748. sendCMD_CheckNext();
  749. }
  750. });
  751. sequence.SetUpdate(true);
  752. }
  753. void sendCMD_CheckNext()
  754. {
  755. if (cmdWaitingList.Count <= 0) return;
  756. CmdToSend cmdToSend = cmdWaitingList.Dequeue();
  757. sendCMD_NotCheck(cmdToSend);
  758. }
  759. bool sendCMD_CheckAndDoStop(Sequence sequence)
  760. {
  761. if (canAutoDormancy) return false;
  762. isStartUp = false;
  763. isSendCmdLocked = false;
  764. cmdWaitingList.Clear();
  765. if (sequence != null) sequence.Kill();
  766. return true;
  767. }
  768. #endregion
  769. public void RequestBattery()
  770. {
  771. if (!isStartUp) return;
  772. if (isSendCmdLocked) return;
  773. WriteData("b");
  774. }
  775. /// <summary>
  776. /// 获取设备信息 手枪、HOUYI Pro 和 ARTEMIS Pro
  777. /// </summary>
  778. public void RequestDeviceInfo()
  779. {
  780. if (isSendCmdLocked) return;
  781. WriteData("I");
  782. }
  783. /// <summary>
  784. /// 旧的红外射箭指令
  785. /// </summary>
  786. public void ReplyInfraredShoot()
  787. {
  788. if (isSendCmdLocked) return;
  789. WriteData("I");
  790. }
  791. void CallDelay(float delayTime, TweenCallback callback)
  792. {
  793. Sequence sequence = DOTween.Sequence();
  794. sequence.PrependInterval(delayTime).AppendCallback(callback);
  795. sequence.SetUpdate(true);
  796. }
  797. public void WriteData(string data)
  798. {
  799. if (isUseCompanionDeviceManager)
  800. {
  801. cmdManager.WriteString(data);
  802. return;
  803. }
  804. //Debug.Log("WriteData:" + data);
  805. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  806. //BleUDP.ins.SendMsg(data);
  807. firstBluetoothWindows.Write(data);
  808. #else
  809. if (DebugDeviceCMD.ins) DebugDeviceCMD.ins.ShowCMD(data);
  810. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  811. ch.setService(bluetoothService.getName());
  812. bluetoothHelper.WriteCharacteristic(ch, data);
  813. #endif
  814. }
  815. public void WriteByteData(byte[] data)
  816. {
  817. if (isUseCompanionDeviceManager)
  818. {
  819. cmdManager.WriteBytes(data);
  820. return;
  821. }
  822. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  823. firstBluetoothWindows.WriteByte(data);
  824. #else
  825. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  826. ch.setService(bluetoothService.getName());
  827. bluetoothHelper.WriteCharacteristic(ch, data);
  828. #endif
  829. }
  830. void Log(string text)
  831. {
  832. if (textUI)
  833. {
  834. textUI.text = text;
  835. }
  836. Debug.Log(string.Format("[{0}]{1}", typeof(BluetoothAim).Name, text));
  837. }
  838. [NonSerialized] public string curMac;
  839. void UploadMacAddress(byte[] bytes)
  840. {
  841. string mac = System.Text.Encoding.ASCII.GetString(bytes);
  842. if (mac != null) mac = mac.Trim();
  843. if (CheckIsMacValid(mac))
  844. {
  845. SideTipView.ShowTip("Mac获取成功:" + mac, Color.white);
  846. //需要增加一个判断,判断是否对应的mac设备。不是需要进行重新连接
  847. AimDeviceInfo firstPlayer = AimHandler.ins.GetAimDeviceInfo(BluetoothPlayer.FIRST_PLAYER);
  848. if (!firstPlayer.bInitMac)
  849. {
  850. Debug.Log("设置设备mac:" + mac);
  851. AimHandler.ins.SetAimDeviceMac(mac);
  852. }
  853. else if (firstPlayer.mac != mac)
  854. {
  855. Debug.Log("设备不一样,断开连接");
  856. DoConnect();
  857. //延迟一点时间后重新触发扫描
  858. CallDelay(4, () =>
  859. {
  860. Debug.Log("CallDelay 重新连接");
  861. DoConnect();
  862. });
  863. return;
  864. }
  865. curMac = mac;
  866. LoginMgr.myUserInfo.mac = mac;
  867. UserComp.Instance.saveMac();
  868. }
  869. //else {
  870. // SideTipView.ShowTip("Mac获取失败", Color.yellow);
  871. //}
  872. }
  873. bool CheckIsMacValid(string mac)
  874. {
  875. if (mac == null) return false;
  876. if (!mac.StartsWith("{")) return false;
  877. if (!mac.EndsWith("}")) return false;
  878. if (!mac.Contains(":")) return false;
  879. char[] validChars = { '{', '}', ':', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  880. foreach (var c in mac.ToCharArray())
  881. {
  882. if (Array.IndexOf(validChars, c) == -1) return false;
  883. }
  884. if (mac.Length != 19) return false;
  885. string macNoneFrame = mac.Substring(1, mac.Length - 2);
  886. string[] macNoneFrameSplits = macNoneFrame.Split(':');
  887. if (macNoneFrameSplits.Length != 6) return false;
  888. foreach (var item in macNoneFrameSplits)
  889. {
  890. if (item.Length != 2) return false;
  891. foreach (var c in item.ToCharArray())
  892. if (Array.IndexOf(validChars, c) < 3) return false;
  893. }
  894. return true;
  895. }
  896. #region 2p蓝牙部分,使用了SmartBowSDK
  897. //2p蓝牙连接 使用 SmartBowSDK
  898. SmartBowHelper smartBowHelper2P;
  899. //陀螺仪校准进度
  900. public int smartBowHelper2_Progress { get => (int)(smartBowHelper2P.GetGyrProgress() * 100); }
  901. bool doConnect2P = false;
  902. bool userDoConnect2P = false;
  903. int _scanCanRetryCount2P = 4;
  904. bool canConnect2P = true;
  905. public bool bNoNeedToReconnect { get; set; } = true;//是否需要重连
  906. bool lerpForRotation2P = true;
  907. float lerpTimeRate2P = 7;
  908. private Quaternion newRotation2P = Quaternion.identity;
  909. //数据存储标识
  910. string userTags = "smartbow";
  911. int deviceId2 = 2;
  912. public void onCreateSmartBowHelper2P()
  913. {
  914. smartBowHelper2P = SmartBowHelper.NewInstance();
  915. #if UNITY_ANDROID
  916. if (CommonConfig.bUseCMD)
  917. {
  918. //使用CMD API连接,这个只支持Android
  919. smartBowHelper2P.UseCompanionDeviceManager(true);
  920. }
  921. #endif
  922. if (isUseCompanionDeviceManager) {
  923. smartBowHelper2P.SetFilters(ToRegex(targetDeviceName2P));
  924. } else {
  925. smartBowHelper2P.SetFilters(targetDeviceName2P);
  926. }
  927. smartBowHelper2P.OnBluetoothModuleInited += () =>
  928. {
  929. Debug.Log("OnBluetoothModuleInited");
  930. //判断是否是同一个mac
  931. //需要增加一个判断,判断是否对应的mac设备。不是需要进行重新连接
  932. string mac = smartBowHelper2P.GetMacAddress();
  933. AimDeviceInfo secondPlayer = AimHandler.ins.GetAimDeviceInfo(BluetoothPlayer.SECOND_PLAYER);
  934. if (!secondPlayer.bInitMac)
  935. {
  936. Debug.Log("smartBowHelper2P 设置设备mac:" + mac);
  937. AimHandler.ins.SetAimDeviceMac(mac);
  938. }
  939. else if (secondPlayer.mac != mac)
  940. {
  941. Debug.Log("设备不一样,断开连接");
  942. DoConnect2P();
  943. //延迟一点时间后重新触发扫描
  944. CallDelay(4, () =>
  945. {
  946. Debug.Log("CallDelay 重新连接");
  947. DoConnect2P();
  948. });
  949. return;
  950. }
  951. UpdateConnectText();
  952. smartBowHelper2P.StartRotationSensor();
  953. smartBowHelper2P.StartShootingSensor();
  954. //if (SB_EventSystem.ins)
  955. //{
  956. // //唤起/隐藏虚拟鼠标
  957. // SB_EventSystem.ins.AwakenSimulateMouse();
  958. //}
  959. };
  960. smartBowHelper2P.OnCMDState += (state, message) =>
  961. {
  962. userDoConnect2P = false;
  963. doConnect2P = false;
  964. canConnect2P = true;
  965. DisconnectSmartBowHelper2P();
  966. PopupMgr.ins.ShowTip(message);
  967. };
  968. smartBowHelper2P.OnBluetoothError += (error, message) =>
  969. {
  970. Debug.Log("smartBowHelper2P error:" + error);
  971. if (error == BluetoothError.ScanNotFoundTargetDevice)
  972. {
  973. scanLock = false;
  974. PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
  975. if (_scanCanRetryCount2P > 0)
  976. {
  977. _scanCanRetryCount2P--;
  978. scanLock = true;
  979. ConnectSmartBowHelper2P();
  980. }
  981. else
  982. {
  983. userDoConnect2P = false;
  984. canConnect2P = true;
  985. Log("没有发现设备");
  986. TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
  987. }
  988. return;
  989. }
  990. PopupMgr.ins.ShowTip(message);
  991. };
  992. smartBowHelper2P.OnBluetoothStatusChanged += (oldStatus, newStatus) =>
  993. {
  994. UpdateConnectText();
  995. };
  996. smartBowHelper2P.OnRotationUpdate += (r) =>
  997. {
  998. newRotation2P = r;
  999. //GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r);
  1000. //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r);
  1001. };
  1002. smartBowHelper2P.OnShooting += OnShot2P;
  1003. smartBowHelper2P.OnFunctionKeyPress += () =>
  1004. {
  1005. //如果是双人场景,触发提示
  1006. if (SceneManager.GetActiveScene().name == "GameDouble")
  1007. {
  1008. if (GameObject.Find("AutoResetViewNewRight")) return;
  1009. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  1010. resetView.name = "AutoResetViewNewRight";
  1011. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  1012. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  1013. autoResetViewNewScript.setPosRight();
  1014. autoResetViewNewScript.action_OnDestroy += () =>
  1015. {
  1016. smartBowHelper2P.ResetAim();
  1017. };
  1018. }
  1019. else if (SceneManager.GetActiveScene().name == "InfraredGameDouble" || SceneManager.GetActiveScene().name == "WildAttackDouble")
  1020. {
  1021. if (GameObject.Find("AutoResetViewNewRight")) return;
  1022. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  1023. resetView.name = "AutoResetViewNewRight";
  1024. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  1025. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  1026. autoResetViewNewScript.setPosRight();
  1027. autoResetViewNewScript.action_OnDestroy += () =>
  1028. {
  1029. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  1030. {
  1031. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.SecondPlayer);
  1032. }
  1033. else if (SceneManager.GetActiveScene().name == "WildAttackDouble")
  1034. {
  1035. WildAttack.GameMananger.GetInstance().ResetAim(WildAttack.PlayerTypeEnum.Second);
  1036. }
  1037. };
  1038. }
  1039. //else if (SceneManager.GetActiveScene().name.Equals("WildAttackDouble"))
  1040. //{
  1041. // //双人场景直接校准2P
  1042. // WildAttack.GameMananger.GetInstance().ResetAim(WildAttack.PlayerTypeEnum.Second);
  1043. //}
  1044. else
  1045. {
  1046. smartBowHelper2P.ResetAim();
  1047. }
  1048. };
  1049. //#if UNITY_STANDALONE_WIN || UNITY_EDITOR
  1050. // //用户2window连接 BluetoothWindows.IsWindows() &&
  1051. // if (currentBLEPlayer == BluetoothPlayer.SECONDE_PLAYER)
  1052. // {
  1053. // Debug.Log("SECONDE_PLAYER BleWinHelper.RegisterTo");
  1054. // BleWinHelper com = BleWinHelper.RegisterTo(smartBowHelper2P.gameObject, smartBowHelper2P.CreateBluetoothWindows(), "2P");
  1055. // }
  1056. //#endif
  1057. }
  1058. void OnShot2P(float speed)
  1059. {
  1060. if (SceneManager.GetActiveScene().name.Equals("WildAttackDouble"))
  1061. {
  1062. WildAttack.SmartBowController.Instance.OnShooting2P(speed);
  1063. }
  1064. else
  1065. {
  1066. if (GameController.ins.GetArmBowDoublePlayer(PlayerType.SecondPlayer) != null)
  1067. {
  1068. //本地双人模式下处理2P ,1P在 ShootCheck 类处理
  1069. GameController.ins.GetArmBowDoublePlayer(PlayerType.SecondPlayer).ADS_fire(true, speed);
  1070. }
  1071. }
  1072. }
  1073. void UpdateConnectText()
  1074. {
  1075. var newStatus = smartBowHelper2P.GetBluetoothStatus();
  1076. if (newStatus == SmartBowSDK.BluetoothStatusEnum.None)
  1077. {
  1078. if (GameController.ins)
  1079. {
  1080. GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = true;
  1081. }
  1082. Debug.Log("<color=blue>未连接</color>(点击连接)");
  1083. if (!bNoNeedToReconnect) return;
  1084. scanLock = false;
  1085. if (_scanCanRetryCount2P > 0)
  1086. {
  1087. _scanCanRetryCount2P--;
  1088. scanLock = true;
  1089. ConnectSmartBowHelper2P();
  1090. }
  1091. else
  1092. {
  1093. userDoConnect2P = false;
  1094. canConnect2P = true;
  1095. Log("没有发现设备");
  1096. PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
  1097. TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
  1098. }
  1099. }
  1100. else if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting)
  1101. {
  1102. Debug.Log("<color=#FF670D>连接中</color>");
  1103. if (GameController.ins)
  1104. {
  1105. GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
  1106. }
  1107. }
  1108. else if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
  1109. {
  1110. if (GameController.ins)
  1111. {
  1112. GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
  1113. }
  1114. if (smartBowHelper2P.IsBluetoothModuleInited()) Debug.Log("<color=green>已连接</color>(点击断开)");
  1115. else Debug.Log("<color=green>已连接</color><color=blue>(正在初始化)</color>");
  1116. }
  1117. }
  1118. public void ConnectSmartBowHelper2P()
  1119. {
  1120. //#if UNITY_ANDROID
  1121. // PopupMgr.ins.ClearAllTip();
  1122. // if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
  1123. // {
  1124. // HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
  1125. // return;
  1126. // }
  1127. // if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectSmartBowHelper2P, (permission) => {
  1128. // if (permission.Contains("LOCATION"))
  1129. // {
  1130. // HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
  1131. // }
  1132. // else if (permission.Contains("BLUETOOTH"))
  1133. // {
  1134. // HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
  1135. // }
  1136. // })) return;
  1137. //#endif
  1138. if (smartBowHelper2P == null)
  1139. {
  1140. onCreateSmartBowHelper2P();
  1141. CallDelay(1, () =>
  1142. {
  1143. smartBowHelper2P.Connect(userTags, deviceId2, true);//不在sdk 判断mac
  1144. });
  1145. }
  1146. else
  1147. {
  1148. smartBowHelper2P.Connect(userTags, deviceId2, true);
  1149. }
  1150. }
  1151. public void DoConnect2P()
  1152. {
  1153. Debug.Log("DoConnect2P");
  1154. if (smartBowHelper2P == null || smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.None)
  1155. {
  1156. //connectCanceled = false;
  1157. userDoConnect2P = true;
  1158. doConnect2P = true;
  1159. bNoNeedToReconnect = true;
  1160. Connect2P();
  1161. }
  1162. else if (smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
  1163. {
  1164. userDoConnect2P = false;
  1165. doConnect2P = false;
  1166. canConnect2P = true;
  1167. DisconnectSmartBowHelper2P();
  1168. }
  1169. }
  1170. void Connect2P()
  1171. {
  1172. //if (BluetoothShoot.scanLock)
  1173. //{
  1174. // return;
  1175. //}
  1176. if (!canConnect2P)
  1177. {
  1178. return;
  1179. }
  1180. doConnect = false;
  1181. doConnect2P = false;
  1182. scanLock = true;
  1183. canConnect2P = false;
  1184. _scanCanRetryCount2P = 4;
  1185. //SetStatus2P(BluetoothStatusEnum.Connecting);
  1186. ConnectSmartBowHelper2P();
  1187. }
  1188. //初始化陀螺仪
  1189. public void OnCalibrateGyr2P()
  1190. {
  1191. if (smartBowHelper2P.IsGyrCalibrating()) smartBowHelper2P.StopGyrCalibration();
  1192. else smartBowHelper2P.StartGyrCalibration();
  1193. }
  1194. //初始化地磁计
  1195. public void OnCalibrateMag2P()
  1196. {
  1197. smartBowHelper2P.StartMagCalibration();
  1198. }
  1199. #endregion
  1200. #region 当前选择操作的设备 如1p或者2p
  1201. public SmartBowHelper getSmartBowHelper2P()
  1202. {
  1203. return smartBowHelper2P;
  1204. }
  1205. public void setBLEPlayer(BluetoothPlayer blePlayer)
  1206. {
  1207. //切换蓝牙用户时候,判断是否正在连接,如果正在匹配连接的话,取消连接状态
  1208. if (blePlayer != currentBLEPlayer)
  1209. {
  1210. Debug.Log("Set BluetoothPlayer status:" + status);
  1211. onCancelAllConnecting();
  1212. }
  1213. currentBLEPlayer = blePlayer;
  1214. Debug.Log("Set BluetoothPlayer:" + currentBLEPlayer);
  1215. }
  1216. public BluetoothPlayer getBLEPlayer()
  1217. {
  1218. //Debug.Log("Get BluetoothPlayer:" + currentBLEPlayer);
  1219. return currentBLEPlayer;
  1220. }
  1221. //更新跟随点位置
  1222. public void UpdateCameraToLookNewPoint2P()
  1223. {
  1224. if (SceneManager.GetActiveScene().name != "InfraredGameDouble")
  1225. {
  1226. if (CameraToLookNew.ins != null)
  1227. {
  1228. Transform m_controlObj = CameraToLookNew.ins.transform;
  1229. if (lerpForRotation2P)
  1230. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation2P, Time.deltaTime * lerpTimeRate2P);
  1231. else
  1232. m_controlObj.localRotation = newRotation2P;
  1233. }
  1234. }
  1235. }
  1236. public int get2PBattery()
  1237. {
  1238. return smartBowHelper2P == null ? 0 : smartBowHelper2P.GetBattery();
  1239. }
  1240. //主更新调用
  1241. void updateSmartBowHelper2P()
  1242. {
  1243. UpdateCameraToLookNewPoint2P();
  1244. }
  1245. #endregion
  1246. //取消正在连接的连接
  1247. public void onCancelAllConnecting(BluetoothStatusEnum _bluetoothStatus = BluetoothStatusEnum.ConnectFail)
  1248. {
  1249. // status != BluetoothStatusEnum.ConnectSuccess
  1250. //Debug.LogWarning("onCancelAllConnecting 1:" + status);
  1251. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  1252. //BleUDP.ins != null
  1253. //BluetoothWindows.IsWindows() &&
  1254. if (status == BluetoothStatusEnum.Connecting)
  1255. {
  1256. //Debug.LogWarning("onCancelAllConnecting 2:" + status);
  1257. userDoConnect = false;
  1258. doConnect = false;
  1259. isCMDScanning = false;
  1260. OnDisconnect();
  1261. // BleUDP.ins.Disconnect();
  1262. firstBluetoothWindows.Disconnect();
  1263. SetStatus(_bluetoothStatus);
  1264. //Debug.LogWarning("onCancelAllConnecting 3:" + status);
  1265. }
  1266. #else
  1267. //bluetoothHelper != null &&
  1268. if (status == BluetoothStatusEnum.Connecting)
  1269. {
  1270. //Debug.LogWarning("onCancelAllConnecting 2:" + status);
  1271. userDoConnect = false;
  1272. doConnect = false;
  1273. isCMDScanning = false;
  1274. OnDisconnect();
  1275. DisconnectBleHelper();
  1276. SetStatus(_bluetoothStatus);
  1277. }
  1278. #endif
  1279. //smartBowHelper2P.GetBluetoothStatus() != SmartBowSDK.BluetoothStatusEnum.Connected
  1280. if (smartBowHelper2P != null && smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
  1281. {
  1282. userDoConnect2P = false;
  1283. doConnect2P = false;
  1284. canConnect2P = true;
  1285. DisconnectSmartBowHelper2P();
  1286. //smartBowHelper2P.Disconnect();
  1287. Debug.Log("onCancelAllConnecting smartBowHelper2P 2:" + smartBowHelper2P.GetBluetoothStatus());
  1288. }
  1289. }
  1290. #region 判断是否是1p设备连接的是HOUYIPRO信息
  1291. public bool isMainConnectToHOUYIPRO()
  1292. {
  1293. bool _isHOUYIPRO = false;
  1294. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  1295. {
  1296. if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && p.type == (int)AimDeviceType.HOUYIPRO)
  1297. {
  1298. _isHOUYIPRO = true;
  1299. }
  1300. }
  1301. if (_isHOUYIPRO)
  1302. {
  1303. if (CommonConfig.bDisableBluetooth)
  1304. {
  1305. return true;
  1306. }
  1307. else
  1308. {
  1309. //如果是。判断是否连接
  1310. return status == BluetoothStatusEnum.ConnectSuccess;
  1311. }
  1312. }
  1313. else
  1314. {
  1315. return false;
  1316. }
  1317. }
  1318. #endregion
  1319. #region 判断是否是1p设备连接的是ARTEMISPRO信息
  1320. public bool isMainConnectToARTEMISPRO()
  1321. {
  1322. bool _isARTEMISPRO = false;
  1323. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  1324. {
  1325. if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && p.type == (int)AimDeviceType.ARTEMISPRO)
  1326. {
  1327. _isARTEMISPRO = true;
  1328. }
  1329. }
  1330. if (_isARTEMISPRO)
  1331. {
  1332. if (CommonConfig.bDisableBluetooth)
  1333. {
  1334. return true;
  1335. }
  1336. else
  1337. {
  1338. //如果是。判断是否连接
  1339. return status == BluetoothStatusEnum.ConnectSuccess;
  1340. }
  1341. }
  1342. else
  1343. {
  1344. return false;
  1345. }
  1346. }
  1347. #endregion
  1348. #region 判断是否是1p设备连接的是红外信息,有多种红外设备
  1349. public bool isMainConnectToInfraredDevice()
  1350. {
  1351. bool _isInfraredDevice = false;
  1352. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  1353. {
  1354. if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && (p.type == (int)AimDeviceType.ARTEMISPRO || p.type == (int)AimDeviceType.HOUYIPRO))
  1355. {
  1356. _isInfraredDevice = true;
  1357. }
  1358. }
  1359. if (_isInfraredDevice)
  1360. {
  1361. if (CommonConfig.bDisableBluetooth)
  1362. {
  1363. return true;
  1364. }
  1365. else
  1366. {
  1367. //如果是。判断是否连接
  1368. return status == BluetoothStatusEnum.ConnectSuccess;
  1369. }
  1370. }
  1371. else
  1372. {
  1373. return false;
  1374. }
  1375. }
  1376. #endregion
  1377. #region 判断是否是1p设备连接的是Gun信息
  1378. public bool isMainConnectToGun()
  1379. {
  1380. bool _isGun = false;
  1381. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  1382. {
  1383. if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && (p.type == (int)AimDeviceType.Gun || p.type == (int)AimDeviceType.PistolM17 || p.type == (int)AimDeviceType.RifleM416))
  1384. {
  1385. _isGun = true;
  1386. }
  1387. }
  1388. if (_isGun)
  1389. {
  1390. if (CommonConfig.bDisableBluetooth)
  1391. {
  1392. return true;
  1393. }
  1394. else
  1395. {
  1396. //如果是。判断是否连接
  1397. return status == BluetoothStatusEnum.ConnectSuccess;
  1398. }
  1399. }
  1400. else
  1401. {
  1402. return false;
  1403. }
  1404. }
  1405. /// <summary>
  1406. /// 获取连接枪的类型
  1407. /// </summary>
  1408. /// <returns></returns>
  1409. public (bool isGun, AimDeviceType gunType) isMainConnectToGunType()
  1410. {
  1411. bool _isGun = false;
  1412. AimDeviceType aimDeviceType = AimDeviceType.NONE;
  1413. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  1414. {
  1415. if ((int)BluetoothPlayer.FIRST_PLAYER == p.id && (p.type == (int)AimDeviceType.Gun || p.type == (int)AimDeviceType.PistolM17 || p.type == (int)AimDeviceType.RifleM416))
  1416. {
  1417. _isGun = true;
  1418. aimDeviceType = (AimDeviceType)p.type;
  1419. }
  1420. }
  1421. if (_isGun)
  1422. {
  1423. if (CommonConfig.bDisableBluetooth)
  1424. {
  1425. return (true, aimDeviceType);
  1426. }
  1427. else
  1428. {
  1429. //如果是。判断是否连接
  1430. return (status == BluetoothStatusEnum.ConnectSuccess, aimDeviceType);
  1431. }
  1432. }
  1433. else
  1434. {
  1435. return (false, aimDeviceType);
  1436. }
  1437. }
  1438. #endregion
  1439. /// <summary>
  1440. /// 设置设备类型
  1441. /// </summary>
  1442. public void SetMainConnectDeviceType()
  1443. {
  1444. //1号玩家连接蓝牙成功时候,设置一次 GlobalData.MyDeviceMode
  1445. foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
  1446. {
  1447. if ((int)BluetoothPlayer.FIRST_PLAYER == p.id)
  1448. {
  1449. if (p.type == (int)AimDeviceType.Gun || p.type == (int)AimDeviceType.PistolM17 || p.type == (int)AimDeviceType.RifleM416)
  1450. {
  1451. //设置枪
  1452. HomeView.ins.MyTopBarView.onChangeType(1);
  1453. }
  1454. else
  1455. {
  1456. //其余默认是弓箭
  1457. HomeView.ins.MyTopBarView.onChangeType(0);
  1458. }
  1459. }
  1460. }
  1461. }
  1462. #region 用户1 window ble 连接和部分共用代码
  1463. void OnConnected_windows1()
  1464. {
  1465. Log("[WinBle_1P]连接成功\n" + deviceName);
  1466. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  1467. BowCamera.isTouchMode = false;
  1468. if (CommonConfig.EnableDecryption && NeedDecryption)
  1469. {
  1470. // 这里验证指令,开始请求授权
  1471. // 启动轮询协程
  1472. StartCoroutine(PollingCoroutine());
  1473. }
  1474. else
  1475. {
  1476. //除了pc,Android 直接连接
  1477. InitWhenConenct();
  1478. }
  1479. }
  1480. void OnConnectionFailed_windows1()
  1481. {
  1482. Log("连接失败\n" + deviceName);
  1483. OnDisconnect();
  1484. }
  1485. void OnCharacteristicChanged_windows1(string deviceId, byte[] value)
  1486. {
  1487. OnCharacteristicChanged(null, value, null);
  1488. }
  1489. void OnCharacteristicChanged(BluetoothHelper helper, byte[] value, BluetoothHelperCharacteristic characteristic)
  1490. {
  1491. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  1492. if (!hasData)
  1493. {
  1494. hasData = true;
  1495. hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
  1496. }
  1497. if (CommonConfig.EnableDecryption && NeedDecryption)
  1498. {
  1499. //Pc 版本先走校验流程
  1500. if (!BluetoothDecryptor.AUTHOR_IsDecrypt())
  1501. {
  1502. if (value[0] == 0x5a)
  1503. {
  1504. // 从硬件读取数据
  1505. if (value != null && value.Length > 0)
  1506. {
  1507. Debug.Log("接收到数据:" + BitConverter.ToString(value));
  1508. BluetoothDecryptor.AUTHOR_Decrypt(value);
  1509. if (BluetoothDecryptor.AUTHOR_IsDecrypt())
  1510. {
  1511. Debug.Log("解密成功!");
  1512. //解密成功后
  1513. StopEncrypt();
  1514. //开始连接其他信息
  1515. InitWhenConenct();
  1516. }
  1517. else
  1518. {
  1519. Debug.Log("解密失败!");
  1520. //SideTipView.ShowTip("设备通信失败,断开连接", Color.yellow);
  1521. //断开连接等操作
  1522. if (PollingCoroutineCount <= 0)
  1523. {
  1524. StopEncrypt();
  1525. DoConnect();
  1526. }
  1527. }
  1528. }
  1529. }
  1530. return;
  1531. }
  1532. }
  1533. dataCount++;
  1534. if (curMac == null && dataCount < 500)
  1535. {
  1536. UploadMacAddress(value);
  1537. }
  1538. byte[] bytes = value;
  1539. // Log(String.Join(",", bytes));
  1540. BluetoothClient.UploadData(0, bytes);
  1541. if (AimHandler.ins)
  1542. {
  1543. AimHandler.ins.OnDataReceived(bytes);
  1544. }
  1545. }
  1546. #endregion
  1547. #region 管理部分监听通信
  1548. /// <summary>
  1549. /// 监听枪状态
  1550. /// </summary>
  1551. public BleDeviceEvent OnBleDeviceState;
  1552. /// <summary>
  1553. /// 硬件对应的事件
  1554. /// </summary>
  1555. public delegate void BleDeviceEvent(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum);
  1556. internal void InvokeOnBleDevice(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum)
  1557. {
  1558. try
  1559. {
  1560. OnBleDeviceState?.Invoke(bleDeviceType, gunStatusEnum);
  1561. }
  1562. catch (Exception e)
  1563. {
  1564. Debug.LogError(e);
  1565. }
  1566. }
  1567. /// <summary>
  1568. /// 记录硬件反馈的设备状态和连接的设备平台信息
  1569. /// </summary>
  1570. public BluetoothDeviceType myBluetoothDeviceType = BluetoothDeviceType.NONE;
  1571. public ConnectPlatform myConnectPlatform = ConnectPlatform.NONE;
  1572. /// <summary>
  1573. /// 监听设备状态和连接的设备平台信息
  1574. /// </summary>
  1575. public DeviceAndSystemInfoEvent OnDeviceAndSystemInfoEvent;
  1576. /// <summary>
  1577. /// 硬件对应的事件初始化回调
  1578. /// </summary>
  1579. public delegate void DeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bleDeviceType);
  1580. internal void InvokeOnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bleDeviceType)
  1581. {
  1582. try
  1583. {
  1584. myConnectPlatform = connectPlatform;
  1585. myBluetoothDeviceType = bleDeviceType;
  1586. OnDeviceAndSystemInfoEvent?.Invoke(connectPlatform, bleDeviceType);
  1587. }
  1588. catch (Exception e)
  1589. {
  1590. Debug.LogError(e);
  1591. }
  1592. }
  1593. /// <summary>
  1594. /// 获取一次当前记录的信息
  1595. /// </summary>
  1596. public void GetDeviceAndSystemInfoEvent() {
  1597. OnDeviceAndSystemInfoEvent?.Invoke(myConnectPlatform, myBluetoothDeviceType);
  1598. }
  1599. /// <summary>
  1600. /// 返回一个存储用户的新手教程字符串
  1601. /// </summary>
  1602. /// <returns></returns>
  1603. public string GetInfraredGuiderKey() {
  1604. return $"hideInfraredGuider-{LoginMgr.myUserInfo.id}-{myBluetoothDeviceType}";
  1605. }
  1606. /// <summary>
  1607. /// 清除当前红外记录的存储的所有key内容
  1608. /// </summary>
  1609. public void ClearAllInfraredGuider() {
  1610. int userId = LoginMgr.myUserInfo.id;
  1611. foreach (BluetoothDeviceType type in Enum.GetValues(typeof(BluetoothDeviceType)))
  1612. {
  1613. string key = $"hideInfraredGuider-{userId}-{type}";
  1614. PlayerPrefs.DeleteKey(key);
  1615. }
  1616. PlayerPrefs.Save();
  1617. }
  1618. #endregion
  1619. #region 使用CompanionDeviceManager API 管理连接
  1620. public bool isUseCompanionDeviceManager { get; set; } = false;//默认用原本蓝牙扫描连接
  1621. private CMDManager cmdManager;
  1622. private bool isCMDScanning = false;
  1623. private string CmdName = "";
  1624. private string CmdMac = "";
  1625. /// <summary>
  1626. /// 初始化 CompanionDeviceManager 连接
  1627. /// </summary>
  1628. public void InitCMDManager()
  1629. {
  1630. if (cmdManager != null) return;
  1631. cmdManager = new CMDManager();
  1632. cmdManager.SetUUIDs(targetDeviceService, targetDeviceCharacteristicWrite, targetDeviceCharacteristicNotify);
  1633. cmdManager.OnCMDDeviceFound += OnCMDScanFound;
  1634. cmdManager.OnCMDScanFailed += OnCMDScanFailed;
  1635. cmdManager.OnCMDBLEConnected += OnCMDBLEConnected;
  1636. cmdManager.OnCMDBLEDisconnected += OnCMDBLEDisconnected;
  1637. cmdManager.OnCMDBLEReady += OnCMDBLEReady;
  1638. cmdManager.OnCMDBLENotify += OnCMDBLENotify;
  1639. cmdManager.OnPermissionDenied += OnCMDPermissionDenied;
  1640. cmdManager.OnPermissionDontAsk += OnCMDPermissionDontAsk;
  1641. SmartBowLogger.Log(this, "CMDManager 已初始化。");
  1642. }
  1643. /// <summary>
  1644. /// 清空 CompanionDeviceManager 信息
  1645. /// </summary>
  1646. public void ClearCMDManager()
  1647. {
  1648. if (cmdManager != null)
  1649. {
  1650. cmdManager.Cleanup(); // 调用 CMDManager 内部彻底清理方法
  1651. cmdManager.OnCMDDeviceFound -= OnCMDScanFound;
  1652. cmdManager.OnCMDScanFailed -= OnCMDScanFailed;
  1653. cmdManager.OnCMDBLEConnected -= OnCMDBLEConnected;
  1654. cmdManager.OnCMDBLEDisconnected -= OnCMDBLEDisconnected;
  1655. cmdManager.OnCMDBLEReady -= OnCMDBLEReady;
  1656. cmdManager.OnCMDBLENotify -= OnCMDBLENotify;
  1657. cmdManager.OnPermissionDenied -= OnCMDPermissionDenied;
  1658. cmdManager.OnPermissionDontAsk -= OnCMDPermissionDontAsk;
  1659. cmdManager = null;
  1660. SmartBowLogger.Log(this, "CMDManager 已清除。");
  1661. }
  1662. }
  1663. /// <summary>
  1664. /// 使用CompanionDeviceManager连接,仅是 Android 平台
  1665. /// </summary>
  1666. public void ConnectCMD()
  1667. {
  1668. SmartBowLogger.Log(this, $"ConnectCMD isCMDScanning:{isCMDScanning}");
  1669. if (isCMDScanning) return;
  1670. PopupMgr.ins.ClearAllTip();
  1671. if (Application.platform == RuntimePlatform.Android)
  1672. {
  1673. if (!BluetoothHelperAndroid_SDK.IsBluetoothEnabled())
  1674. {
  1675. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
  1676. return;
  1677. }
  1678. }
  1679. try
  1680. {
  1681. //_bluetoothHelper.ScanNearbyDevices();
  1682. //".*"
  1683. AimDeviceInfo firstPlayer = AimHandler.ins.GetAimDeviceInfo(BluetoothPlayer.FIRST_PLAYER);
  1684. if (firstPlayer.bInitMac)
  1685. {
  1686. SmartBowLogger.Log(this, $"CMD ConnectMac:{firstPlayer.mac}");
  1687. cmdManager.ConnectMac(FormatMacWithColon(firstPlayer.mac));
  1688. }
  1689. else
  1690. {
  1691. string regex = ToRegex(targetDeviceNameAxis);
  1692. int firstPlayerType = firstPlayer.type;
  1693. Log("1P设备类型:" + firstPlayerType);
  1694. if (firstPlayerType == (int)AimDeviceType.HOUYIPRO)
  1695. { //需要判断是否是红外弓箭
  1696. regex = ToRegex(targetDeviceNameHOUYIPro);
  1697. }
  1698. else if (firstPlayerType == (int)AimDeviceType.ARTEMISPRO)
  1699. { //需要判断是否是ARTEMISPro弓箭
  1700. regex = ToRegex(targetDeviceNameARTEMISPro);
  1701. }
  1702. else if (firstPlayerType == (int)AimDeviceType.Gun)
  1703. {
  1704. regex = ToRegex(targetDeviceNameGun);
  1705. }
  1706. else if (firstPlayerType == (int)AimDeviceType.PistolM17)
  1707. {
  1708. regex = ToRegex(targetDeviceNameGun_M17);
  1709. }
  1710. else if (firstPlayerType == (int)AimDeviceType.RifleM416)
  1711. {
  1712. regex = ToRegex(targetDeviceNameGun_M416);
  1713. }
  1714. SmartBowLogger.Log(this, $"CMD StartScan:{regex}");
  1715. cmdManager.StartScan(regex);
  1716. }
  1717. isCMDScanning = true;
  1718. SetStatus(BluetoothStatusEnum.Connecting);
  1719. }
  1720. catch (Exception e)
  1721. {
  1722. //HandleConnectError(BluetoothError.Unknown, e.ToString());
  1723. SetStatus(BluetoothStatusEnum.ConnectFail);
  1724. }
  1725. }
  1726. /// <summary>
  1727. /// 把 "A | B | C" 格式的设备名称转成正则 (A|B|C)
  1728. /// 自动去掉空格
  1729. /// </summary>
  1730. string ToRegex(string names)
  1731. {
  1732. if (string.IsNullOrEmpty(names))
  1733. return "";
  1734. var parts = names.Split('|');
  1735. for (int i = 0; i < parts.Length; i++)
  1736. parts[i] = parts[i].Trim(); // 去掉空格
  1737. return "(" + string.Join("|", parts) + ")";
  1738. }
  1739. /**
  1740. * mac.Where(c => Uri.IsHexDigit(c))遍历 mac 字符串中的每一个字符 c。
  1741. * Uri.IsHexDigit(c) 会判断这个字符是否是 16 进制数字(0-9、A-F、a-f)。
  1742. * 所以这一步会 过滤掉所有非 16 进制的字符,比如 -、:、空格等。
  1743. * string.Concat(...)
  1744. * 将过滤后的字符重新拼接成一个新的字符串。
  1745. * .ToUpper()
  1746. * 将整个字符串转成大写。
  1747. */
  1748. string FormatMacWithColon(string mac)
  1749. {
  1750. var hex = string.Concat(mac.Where(c => Uri.IsHexDigit(c))).ToUpper();
  1751. return string.Join(":", Enumerable.Range(0, hex.Length / 2)
  1752. .Select(i => hex.Substring(i * 2, 2)));
  1753. }
  1754. private void OnCMDScanFound(string name, string mac)
  1755. {
  1756. //MAC = D3:5C:89:57:68:DE
  1757. SmartBowLogger.Log(this, $"[SmartBow] CMD 扫描到设备 MAC = " + mac);
  1758. // 监听蓝牙准备就绪
  1759. CmdMac = mac;
  1760. CmdName = name;
  1761. //扫描之后也是这里直接连接
  1762. StartCoroutine(DelayConnect(FormatMacWithColon(CmdMac)));
  1763. }
  1764. private IEnumerator DelayConnect(string mac)
  1765. {
  1766. yield return new WaitForSeconds(0.3f);
  1767. cmdManager.ConnectMac(FormatMacWithColon(mac));
  1768. }
  1769. private void OnCMDScanFailed(CMDScanState state,string reason)
  1770. {
  1771. SmartBowLogger.Log(this, $"[SmartBow] CMD OnCMDScanFailed,"+ reason);
  1772. OnDisconnect();
  1773. isCMDScanning = false;
  1774. userDoConnect = false;
  1775. canConnect = true;
  1776. SetStatus(BluetoothStatusEnum.ConnectFail);
  1777. //if (_scanCanRetryCount > 0)
  1778. //{
  1779. // _scanCanRetryCount--;
  1780. // isCMDScanning = false;
  1781. //}
  1782. //else
  1783. //{
  1784. // userDoConnect = false;
  1785. // canConnect = true;
  1786. // SetStatus(BluetoothStatusEnum.ConnectFail);
  1787. //}
  1788. //smartBowHelper.InvokeOnBluetoothError(BluetoothError.CMDScanError, reason);
  1789. }
  1790. private void OnCMDBLEDisconnected()
  1791. {
  1792. SmartBowLogger.Log(this, $"[SmartBow] CMD OnCMDBLEDisconnected.");
  1793. OnDisconnect();
  1794. isCMDScanning = false;
  1795. }
  1796. /// <summary>
  1797. /// 连接上准备好
  1798. /// </summary>
  1799. private void OnCMDBLEConnected()
  1800. {
  1801. SmartBowLogger.Log(this, $"[SmartBow] CMD OnCMDBLEConnected.");
  1802. }
  1803. /// <summary>
  1804. /// 获取到特征值时候触发
  1805. /// </summary>
  1806. private void OnCMDBLEReady()
  1807. {
  1808. Log("连接成功\n" + CmdName);
  1809. isCMDScanning = false;
  1810. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  1811. SetMainConnectDeviceType();
  1812. BowCamera.isTouchMode = false;
  1813. if (CommonConfig.EnableDecryption && NeedDecryption)
  1814. {
  1815. // 这里验证指令,开始请求授权
  1816. // 启动轮询协程
  1817. StartCoroutine(PollingCoroutine());
  1818. }
  1819. else
  1820. {
  1821. SmartBowLogger.Log(this, $"InitWhenConenct:"+ status);
  1822. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  1823. InitWhenConenct();
  1824. }
  1825. }
  1826. private void OnCMDBLENotify(byte[] value)
  1827. {
  1828. //long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  1829. //SmartBowLogger.Log(this,$"[CMDBleManager→Unity] NotifyArrived: {BitConverter.ToString(value)} | t={now} | size={value.Length}");
  1830. OnCharacteristicChanged(null, value, null);
  1831. }
  1832. public void OnCMDPermissionDenied(string perm)
  1833. {
  1834. SmartBowLogger.LogError(this, $"[App] 用户拒绝权限:{perm}");
  1835. HandleConnectException(TextAutoLanguage2.GetTextByKey("CMDDenied"));
  1836. }
  1837. public void OnCMDPermissionDontAsk(string perm)
  1838. {
  1839. SmartBowLogger.LogError(this, $"[App] 用户永久拒绝权限:{perm}");
  1840. HandleConnectException(TextAutoLanguage2.GetTextByKey("CMDDontAsk"));
  1841. }
  1842. #endregion
  1843. }