BluetoothAim.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. /* 蓝牙瞄准模块 */
  9. /* 管理1p和2p蓝牙连接,脚本外调用逻辑如果同时连接1p和2p设备,以前的逻辑还是以1p为主,如果只连2p,则使用2p数据*/
  10. public class BluetoothAim : MonoBehaviour
  11. {
  12. #region 弓箭蓝牙模块基本信息
  13. readonly string targetDeviceName = "Bbow_20210501";
  14. string targetDeviceService
  15. {
  16. get
  17. {
  18. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
  19. {
  20. #if UNITY_ANDROID
  21. return "0000fff0";
  22. #else
  23. return "fff0";
  24. #endif
  25. }
  26. return "6e400001";
  27. }
  28. }
  29. string targetDeviceCharacteristicWrite
  30. {
  31. get
  32. {
  33. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
  34. {
  35. #if UNITY_ANDROID
  36. return "0000fff2";
  37. #else
  38. return "fff2";
  39. #endif
  40. }
  41. return "6e400002";
  42. }
  43. }
  44. string targetDeviceCharacteristicNotify
  45. {
  46. get
  47. {
  48. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3)
  49. {
  50. #if UNITY_ANDROID
  51. return "0000fff1";
  52. #else
  53. return "fff1";
  54. #endif
  55. }
  56. return "6e400003";
  57. }
  58. }
  59. #endregion
  60. //当前选择的设备
  61. BluetoothPlayer currentBLEPlayer = BluetoothPlayer.FIRST_PLAYER;
  62. //1p蓝牙连接
  63. BluetoothHelper bluetoothHelper;
  64. BluetoothHelperCharacteristic characteristicWrite;
  65. BluetoothHelperService bluetoothService;
  66. string deviceName = "";
  67. bool canConnect = true;
  68. [SerializeField] Text textUI;
  69. public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
  70. int dataCount = 0;
  71. public bool hasData = false;
  72. public long hasDataTime;
  73. public static bool scanLock = false; //防止同时扫描冲突
  74. public static BluetoothAim ins;
  75. void Start()
  76. {
  77. ins = this;
  78. InitAutoDormancy();
  79. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  80. new GameObject("BleUDP").AddComponent<BleUDP>();
  81. #endif
  82. }
  83. void OnDestroy()
  84. {
  85. //删除ble。1p,2p 都删除
  86. DisconnectBleHelper();
  87. DisconnectSmartBowHelper2P();
  88. }
  89. private bool userDoConnect = false;
  90. private bool doConnect = false;
  91. public Func<bool> action_DoConnectInterceptor;
  92. public void DoConnect()
  93. {
  94. if (action_DoConnectInterceptor != null) {
  95. if (action_DoConnectInterceptor.Invoke()) return;
  96. }
  97. if (status == BluetoothStatusEnum.Connect)
  98. {
  99. connectCanceled = false;
  100. userDoConnect = true;
  101. doConnect = true;
  102. SetStatus(BluetoothStatusEnum.Connecting);
  103. }
  104. else if (status == BluetoothStatusEnum.ConnectSuccess)
  105. {
  106. userDoConnect = false;
  107. doConnect = false;
  108. OnDisconnect();
  109. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  110. BleUDP.ins.Disconnect();
  111. #else
  112. DisconnectBleHelper();
  113. #endif
  114. }
  115. }
  116. //连接取消,退回登录时需要取消连接(扫描)
  117. [NonSerialized] public bool connectCanceled = false;
  118. void OnDisconnect()
  119. {
  120. curMac = null;
  121. dataCount = 0;
  122. hasData = false;
  123. canConnect = true;
  124. SetStatus(BluetoothStatusEnum.ConnectFail);
  125. BowCamera.isTouchMode = true;
  126. DestroyWhenDisconenct();
  127. if (AimHandler.ins) AimHandler.ins.SetMsOldDefault();
  128. }
  129. float notUserDoConnectTime = 0;
  130. void Update()
  131. {
  132. updateSmartBowHelper2P();
  133. if (userDoConnect && status == BluetoothStatusEnum.Connect)
  134. {
  135. DoConnect();
  136. }
  137. if (doConnect) Connect();
  138. if (CommonConfig.StandaloneMode)
  139. {
  140. if (!userDoConnect && status == BluetoothStatusEnum.Connect)
  141. {
  142. if (notUserDoConnectTime == 0)
  143. {
  144. DoConnect();
  145. notUserDoConnectTime = 0.1f;
  146. }
  147. else
  148. {
  149. notUserDoConnectTime += Time.deltaTime;
  150. if (notUserDoConnectTime > 2.5f)
  151. {
  152. DoConnect();
  153. notUserDoConnectTime = 0.1f;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. void SetStatus(BluetoothStatusEnum statusValue)
  160. {
  161. status = statusValue;
  162. if (status == BluetoothStatusEnum.ConnectFail)
  163. {
  164. Sequence sequence = DOTween.Sequence();
  165. sequence.AppendInterval(2f);
  166. sequence.AppendCallback(delegate ()
  167. {
  168. if (status == BluetoothStatusEnum.ConnectFail)
  169. {
  170. status = BluetoothStatusEnum.Connect;
  171. }
  172. });
  173. sequence.SetUpdate(true);
  174. SimulateMouseController.ins?.SetBleConnected(false);
  175. } else if (status == BluetoothStatusEnum.ConnectSuccess) {
  176. SimulateMouseController.ins?.SetBleConnected(true);
  177. }
  178. }
  179. void DisconnectBleHelper()
  180. {
  181. if (bluetoothHelper != null) bluetoothHelper.Disconnect();
  182. }
  183. void DisconnectSmartBowHelper2P() {
  184. //2p
  185. if (smartBowHelper2P != null) smartBowHelper2P.Disconnect();
  186. }
  187. void Connect()
  188. {
  189. //if (BluetoothShoot.scanLock)
  190. //{
  191. // return;
  192. //}
  193. if (!canConnect)
  194. {
  195. return;
  196. }
  197. doConnect = false;
  198. doConnect2P = false;
  199. scanLock = true;
  200. canConnect = false;
  201. _scanCanRetryCount = 4;
  202. SetStatus(BluetoothStatusEnum.Connecting);
  203. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  204. ConnectBleByUDP();
  205. #else
  206. ConnectBleHelper();
  207. #endif
  208. }
  209. int _scanCanRetryCount = 4;
  210. void ConnectBleHelper()
  211. {
  212. #if UNITY_ANDROID
  213. PopupMgr.ins.ClearAllTip();
  214. if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
  215. {
  216. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
  217. return;
  218. }
  219. if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectBleHelper, (permission) => {
  220. if (permission.Contains("LOCATION"))
  221. {
  222. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
  223. }
  224. else if (permission.Contains("BLUETOOTH"))
  225. {
  226. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
  227. }
  228. })) return;
  229. #endif
  230. try
  231. {
  232. BluetoothHelper.BLE = true;
  233. bluetoothHelper = BluetoothHelper.GetNewInstance();
  234. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  235. {
  236. Log("连接成功\n" + helper.getDeviceName());
  237. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  238. if (connectCanceled) {
  239. Debug.Log("ble connectCanceled");
  240. DoConnect();
  241. return;
  242. }
  243. BowCamera.isTouchMode = false;
  244. foreach (BluetoothHelperService service in helper.getGattServices())
  245. {
  246. if (service.getName().ToLower().StartsWith(targetDeviceService))
  247. {
  248. bluetoothService = service;
  249. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  250. {
  251. if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicWrite))
  252. {
  253. characteristicWrite = characteristic;
  254. }
  255. else if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicNotify))
  256. {
  257. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  258. ch.setService(bluetoothService.getName());
  259. bluetoothHelper.Subscribe(ch);
  260. }
  261. }
  262. }
  263. }
  264. // CallDelay(1, OpenInfrared);
  265. // CallDelay(2, OpenReceiveData);
  266. // CallDelay(3, RequestBattery);
  267. CallDelay(2, () =>
  268. {
  269. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  270. InitWhenConenct();
  271. if (CommonConfig.StandaloneMode)
  272. {
  273. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home"
  274. && PersistenHandler.ins.menuBackCtr.views.Count == 0
  275. && !FindObjectOfType<NewUserGuider>())
  276. {
  277. ViewMgr.Instance.ShowView<GameStartView>();
  278. }
  279. }
  280. });
  281. };
  282. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  283. {
  284. Log("连接失败\n" + helper.getDeviceName());
  285. OnDisconnect();
  286. };
  287. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  288. {
  289. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  290. // logger.Log(String.Join(",", value));
  291. if (!hasData) {
  292. hasData = true;
  293. hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
  294. }
  295. dataCount++;
  296. if (curMac == null && dataCount < 500) {
  297. UploadMacAddress(value);
  298. }
  299. byte[] bytes = value;
  300. // Log(String.Join(",", bytes));
  301. BluetoothClient.UploadData(0, bytes);
  302. if (AimHandler.ins)
  303. {
  304. AimHandler.ins.OnDataReceived(bytes);
  305. }
  306. };
  307. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  308. {
  309. scanLock = false;
  310. if (connectCanceled) {
  311. userDoConnect = false;
  312. canConnect = true;
  313. status = BluetoothStatusEnum.Connect;
  314. Debug.Log("ble connectCanceled");
  315. return;
  316. }
  317. foreach (BluetoothDevice device in nearbyDevices)
  318. {
  319. Log("发现设备 " + device.DeviceName);
  320. if (device.DeviceName == targetDeviceName)
  321. {
  322. deviceName = device.DeviceName;
  323. bluetoothHelper.setDeviceName(deviceName);
  324. bluetoothHelper.Connect();
  325. Log("匹配设备 " + device.DeviceName);
  326. return;
  327. }
  328. }
  329. if (_scanCanRetryCount > 0) {
  330. _scanCanRetryCount--;
  331. scanLock = true;
  332. ConnectBleHelper();
  333. } else {
  334. userDoConnect = false;
  335. canConnect = true;
  336. Log("没有发现设备");
  337. TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
  338. SetStatus(BluetoothStatusEnum.ConnectFail);
  339. }
  340. };
  341. bluetoothHelper.ScanNearbyDevices();
  342. Log("正在扫描设备");
  343. }
  344. catch (Exception e)
  345. {
  346. Debug.LogError(e);
  347. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
  348. }
  349. }
  350. void HandleConnectException(string errorText)
  351. {
  352. scanLock = false;
  353. canConnect = true;
  354. // SetStatus(BluetoothStatusEnum.ConnectFail);
  355. status = BluetoothStatusEnum.Connect;
  356. userDoConnect = false;
  357. PopupMgr.ins.ShowTip(errorText);
  358. }
  359. void ConnectBleByUDP()
  360. {
  361. try
  362. {
  363. BleUDP.ins.OnConnected = () =>
  364. {
  365. Log("连接成功\n" + deviceName);
  366. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  367. BowCamera.isTouchMode = false;
  368. InitWhenConenct();
  369. };
  370. BleUDP.ins.OnConnectionFailed = () =>
  371. {
  372. Log("连接失败\n" + deviceName);
  373. OnDisconnect();
  374. };
  375. BleUDP.ins.OnCharacteristicChanged = (byte[] value) =>
  376. {
  377. //if (!hasData) {
  378. // hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
  379. // UploadMacAddress(value);
  380. //}
  381. //hasData = true;
  382. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  383. if (!hasData)
  384. {
  385. hasData = true;
  386. hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
  387. }
  388. dataCount++;
  389. if (curMac == null && dataCount < 500)
  390. {
  391. UploadMacAddress(value);
  392. }
  393. byte[] bytes = value;
  394. // Log(String.Join(",", bytes));
  395. BluetoothClient.UploadData(0, bytes);
  396. if (AimHandler.ins)
  397. {
  398. AimHandler.ins.OnDataReceived(bytes);
  399. }
  400. };
  401. BleUDP.ins.OnScanEnded = () =>
  402. {
  403. scanLock = false;
  404. deviceName = targetDeviceName;
  405. BleUDP.ins.Connect();
  406. Log("发现设备\n" + deviceName);
  407. };
  408. BleUDP.ins.ScanNearbyDevices();
  409. }
  410. catch (Exception e)
  411. {
  412. Debug.LogError(e.Message);
  413. Debug.LogError(e.StackTrace);
  414. scanLock = false;
  415. canConnect = true;
  416. SetStatus(BluetoothStatusEnum.ConnectFail);
  417. }
  418. }
  419. #region 自动进入/退出休眠状态, 这里做程指令发送队列,为了控制连续发送指令的间隔,避免硬件收不到或处理不过来
  420. class CmdToSend
  421. {
  422. public string[] cmds;
  423. public Action onComplete;
  424. public Func<bool> canDo;
  425. public CmdToSend(string[] cmds, Action onComplete, Func<bool> canDo)
  426. {
  427. this.cmds = cmds;
  428. this.onComplete = onComplete;
  429. this.canDo = canDo;
  430. }
  431. }
  432. Queue<CmdToSend> cmdWaitingList = new Queue<CmdToSend>();
  433. bool isSendCmdLocked = false;
  434. bool canAutoDormancy = false;
  435. bool isStartUp = false;
  436. JCUnityLib.CountLock needModularAwake = new JCUnityLib.CountLock();
  437. void CheckAndStartUp()
  438. {
  439. if (needModularAwake.IsLocked())
  440. {
  441. StartUp();
  442. }
  443. else
  444. {
  445. Dormancy();
  446. }
  447. }
  448. void InitAutoDormancy()
  449. {
  450. // GlobalEventCenter.ins.onGameSceneLoad += () => {
  451. // needModularAwake.Lock();
  452. // CheckAndStartUp();
  453. // };
  454. // GlobalEventCenter.ins.onGameSceneDestroy += () => {
  455. // needModularAwake.Unlock();
  456. // CheckAndStartUp();
  457. // };
  458. // GlobalEventCenter.ins.onSimulateMouseAwakeChanged += (waked) => {
  459. // if (waked) needModularAwake.Lock();
  460. // else needModularAwake.Unlock();;
  461. // CheckAndStartUp();
  462. // };
  463. // GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged += (waked) => {
  464. // if (waked) needModularAwake.Lock();
  465. // else needModularAwake.Unlock();;
  466. // CheckAndStartUp();
  467. // };
  468. //暂时关闭自动休眠,默认是需要模块保持激活
  469. needModularAwake.Lock();
  470. }
  471. void InitWhenConenct()
  472. {
  473. canAutoDormancy = true;
  474. List<string> cmds = new List<string>();
  475. cmds.Add("M"); //获取Mac地址
  476. cmds.Add("b"); //确保开启stm32
  477. cmds.Add("b"); //获取初始电量
  478. cmds.Add("1"); //开启发送逻辑
  479. Action onComplete = null;
  480. if (needModularAwake.IsLocked())
  481. {
  482. cmds.Add("w"); //红外灯开启
  483. cmds.Add("3"); //九轴开启
  484. onComplete = () =>
  485. {
  486. isStartUp = true;
  487. };
  488. }
  489. else
  490. {
  491. cmds.Add("s"); //红外灯关闭
  492. cmds.Add("S"); //Stm32关闭
  493. cmds.Add("4"); //九轴关闭
  494. onComplete = () =>
  495. {
  496. isStartUp = false;
  497. };
  498. }
  499. SendCDM(null, onComplete, cmds.ToArray());
  500. }
  501. void DestroyWhenDisconenct()
  502. {
  503. canAutoDormancy = false;
  504. sendCMD_CheckAndDoStop(null);
  505. }
  506. //启动
  507. void StartUp()
  508. {
  509. SendCDM(() =>
  510. {
  511. return !isStartUp;
  512. }, () =>
  513. {
  514. isStartUp = true;
  515. }, "b", "w", "3");
  516. }
  517. //休眠
  518. void Dormancy()
  519. {
  520. SendCDM(() =>
  521. {
  522. return isStartUp;
  523. }, () =>
  524. {
  525. isStartUp = false;
  526. }, "4", "s", "S");
  527. }
  528. void SendCDM(Func<bool> canDo, Action onComplete, params string[] cmds)
  529. {
  530. CmdToSend cmdToSend = new CmdToSend(cmds, onComplete, canDo);
  531. if (isSendCmdLocked)
  532. {
  533. cmdWaitingList.Enqueue(cmdToSend);
  534. return;
  535. }
  536. sendCMD_NotCheck(cmdToSend);
  537. }
  538. void sendCMD_NotCheck(CmdToSend cmdToSend)
  539. {
  540. if (cmdToSend.canDo != null && !cmdToSend.canDo.Invoke())
  541. {
  542. sendCMD_CheckNext();
  543. return;
  544. }
  545. isSendCmdLocked = true;
  546. Sequence sequence = DOTween.Sequence();
  547. sequence.PrependInterval(0.3f);
  548. foreach (var cmd in cmdToSend.cmds)
  549. {
  550. sequence.AppendCallback(() =>
  551. {
  552. bool stopped = sendCMD_CheckAndDoStop(sequence);
  553. if (!stopped) WriteData(cmd);
  554. });
  555. sequence.AppendInterval(0.5f);
  556. }
  557. sequence.AppendCallback(() =>
  558. {
  559. bool stopped = sendCMD_CheckAndDoStop(sequence);
  560. if (!stopped)
  561. {
  562. isSendCmdLocked = false;
  563. cmdToSend.onComplete?.Invoke();
  564. sendCMD_CheckNext();
  565. }
  566. });
  567. sequence.SetUpdate(true);
  568. }
  569. void sendCMD_CheckNext()
  570. {
  571. if (cmdWaitingList.Count <= 0) return;
  572. CmdToSend cmdToSend = cmdWaitingList.Dequeue();
  573. sendCMD_NotCheck(cmdToSend);
  574. }
  575. bool sendCMD_CheckAndDoStop(Sequence sequence)
  576. {
  577. if (canAutoDormancy) return false;
  578. isStartUp = false;
  579. isSendCmdLocked = false;
  580. cmdWaitingList.Clear();
  581. if (sequence != null) sequence.Kill();
  582. return true;
  583. }
  584. #endregion
  585. public void RequestBattery()
  586. {
  587. if (!isStartUp) return;
  588. if (isSendCmdLocked) return;
  589. WriteData("b");
  590. }
  591. public void ReplyInfraredShoot()
  592. {
  593. if (isSendCmdLocked) return;
  594. WriteData("I");
  595. }
  596. void CallDelay(float delayTime, TweenCallback callback)
  597. {
  598. Sequence sequence = DOTween.Sequence();
  599. sequence.PrependInterval(delayTime).AppendCallback(callback);
  600. sequence.SetUpdate(true);
  601. }
  602. public void WriteData(string data)
  603. {
  604. //Debug.Log("WriteData:" + data);
  605. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  606. BleUDP.ins.SendMsg(data);
  607. #else
  608. if (DebugDeviceCMD.ins) DebugDeviceCMD.ins.ShowCMD(data);
  609. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  610. ch.setService(bluetoothService.getName());
  611. bluetoothHelper.WriteCharacteristic(ch, data);
  612. #endif
  613. }
  614. void Log(string text)
  615. {
  616. if (textUI)
  617. {
  618. textUI.text = text;
  619. }
  620. Debug.Log(string.Format("[{0}]{1}", typeof(BluetoothAim).Name, text));
  621. }
  622. [NonSerialized] public string curMac;
  623. void UploadMacAddress(byte[] bytes) {
  624. string mac = System.Text.Encoding.ASCII.GetString(bytes);
  625. if (mac != null) mac = mac.Trim();
  626. if (CheckIsMacValid(mac)) {
  627. SideTipView.ShowTip("Mac获取成功:" + mac, Color.white);
  628. //需要增加一个判断,判断是否对应的mac设备。不是需要进行重新连接
  629. if (!AimHandler.ins.aimDeviceInfo.bInitMac)
  630. {
  631. Debug.Log("设置设备mac:"+ mac);
  632. AimHandler.ins.SetAimDeviceMac(mac);
  633. }
  634. else if (AimHandler.ins.aimDeviceInfo.mac != mac) {
  635. Debug.Log("设备不一样,断开连接");
  636. DoConnect();
  637. //延迟一点时间后重新触发扫描
  638. CallDelay(4, () =>
  639. {
  640. Debug.Log("CallDelay 重新连接");
  641. DoConnect();
  642. });
  643. return;
  644. }
  645. curMac = mac;
  646. LoginMgr.myUserInfo.mac = mac;
  647. UserComp.Instance.saveMac();
  648. } else {
  649. SideTipView.ShowTip("Mac获取失败", Color.yellow);
  650. }
  651. }
  652. bool CheckIsMacValid(string mac) {
  653. if (mac == null) return false;
  654. if (!mac.StartsWith("{")) return false;
  655. if (!mac.EndsWith("}")) return false;
  656. if (!mac.Contains(":")) return false;
  657. char[] validChars = {'{','}',':','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  658. foreach (var c in mac.ToCharArray()) {
  659. if (Array.IndexOf(validChars, c) == -1) return false;
  660. }
  661. if (mac.Length != 19) return false;
  662. string macNoneFrame = mac.Substring(1, mac.Length - 2);
  663. string[] macNoneFrameSplits = macNoneFrame.Split(':');
  664. if (macNoneFrameSplits.Length != 6) return false;
  665. foreach (var item in macNoneFrameSplits) {
  666. if (item.Length != 2) return false;
  667. foreach (var c in item.ToCharArray())
  668. if (Array.IndexOf(validChars, c) < 3) return false;
  669. }
  670. return true;
  671. }
  672. #region 2p蓝牙部分,使用了SmartBowSDK
  673. //2p蓝牙连接 使用 SmartBowSDK
  674. SmartBowHelper smartBowHelper2P;
  675. //陀螺仪校准进度
  676. public int smartBowHelper2_Progress { get => (int)(smartBowHelper2P.GetGyrProgress() * 100); }
  677. bool doConnect2P = false;
  678. bool userDoConnect2P = false;
  679. int _scanCanRetryCount2P = 4;
  680. bool canConnect2P = true;
  681. bool lerpForRotation2P = true;
  682. float lerpTimeRate2P = 7;
  683. private Quaternion newRotation2P = Quaternion.identity;
  684. public void onCreateSmartBowHelper2P() {
  685. smartBowHelper2P = SmartBowHelper.NewInstance();
  686. smartBowHelper2P.OnBluetoothModuleInited += () =>
  687. {
  688. Debug.Log("OnBluetoothModuleInited");
  689. //判断是否是同一个mac
  690. //需要增加一个判断,判断是否对应的mac设备。不是需要进行重新连接
  691. string mac = smartBowHelper2P.GetMacAddress();
  692. if (!AimHandler.ins.aimDeviceInfo.bInitMac)
  693. {
  694. Debug.Log("smartBowHelper2P 设置设备mac:" + mac);
  695. AimHandler.ins.SetAimDeviceMac(mac);
  696. }
  697. else if (AimHandler.ins.aimDeviceInfo.mac != mac)
  698. {
  699. Debug.Log("设备不一样,断开连接");
  700. DoConnect2P();
  701. //延迟一点时间后重新触发扫描
  702. CallDelay(4, () =>
  703. {
  704. Debug.Log("CallDelay 重新连接");
  705. DoConnect2P();
  706. });
  707. return;
  708. }
  709. UpdateConnectText();
  710. smartBowHelper2P.StartRotationSensor();
  711. smartBowHelper2P.StartShootingSensor();
  712. //if (SB_EventSystem.ins)
  713. //{
  714. // //唤起/隐藏虚拟鼠标
  715. // SB_EventSystem.ins.AwakenSimulateMouse();
  716. //}
  717. };
  718. smartBowHelper2P.OnBluetoothError += (error, message) =>
  719. {
  720. Debug.Log("smartBowHelper2P error:" + error);
  721. if (error == BluetoothError.ScanNotFoundTargetDevice)
  722. {
  723. scanLock = false;
  724. PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
  725. if (_scanCanRetryCount2P > 0)
  726. {
  727. _scanCanRetryCount2P--;
  728. scanLock = true;
  729. ConnectSmartBowHelper2P();
  730. }
  731. else
  732. {
  733. userDoConnect2P = false;
  734. canConnect2P = true;
  735. Log("没有发现设备");
  736. TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
  737. }
  738. return;
  739. }
  740. PopupMgr.ins.ShowTip(message);
  741. };
  742. smartBowHelper2P.OnBluetoothStatusChanged += (oldStatus, newStatus) =>
  743. {
  744. UpdateConnectText();
  745. };
  746. smartBowHelper2P.OnRotationUpdate += (r) =>
  747. {
  748. newRotation2P = r;
  749. //GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r);
  750. //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r);
  751. };
  752. smartBowHelper2P.OnShooting += OnShot2P;
  753. smartBowHelper2P.OnFunctionKeyPress += () =>
  754. {
  755. //如果是双人场景,触发提示
  756. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "GameDouble")
  757. {
  758. if (GameObject.Find("AutoResetViewNewRight")) return;
  759. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  760. resetView.name = "AutoResetViewNewRight";
  761. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  762. autoResetViewNewScript.setPosRight();
  763. autoResetViewNewScript.action_OnDestroy += () =>
  764. {
  765. smartBowHelper2P.ResetAim();
  766. };
  767. }
  768. else {
  769. smartBowHelper2P.ResetAim();
  770. }
  771. };
  772. //window蓝牙连接
  773. if (currentBLEPlayer == BluetoothPlayer.SECONDE_PLAYER && BluetoothWindows.IsWindows()) {
  774. Debug.Log(" BleWinHelper.RegisterTo");
  775. BleWinHelper.RegisterTo(smartBowHelper2P.gameObject);
  776. }
  777. }
  778. void OnShot2P(float speed)
  779. {
  780. if (GameController.ins.GetArmBowDoublePlayer(PlayerType.SecondPlayer) != null)
  781. {
  782. //本地双人模式下处理2P ,1P在 ShootCheck 类处理
  783. GameController.ins.GetArmBowDoublePlayer(PlayerType.SecondPlayer).ADS_fire(true, speed);
  784. }
  785. }
  786. void UpdateConnectText()
  787. {
  788. var newStatus = smartBowHelper2P.GetBluetoothStatus();
  789. if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) {
  790. if (GameController.ins) {
  791. GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = true;
  792. }
  793. Debug.Log("<color=blue>未连接</color>(点击连接)");
  794. scanLock = false;
  795. if (_scanCanRetryCount2P > 0)
  796. {
  797. _scanCanRetryCount2P--;
  798. scanLock = true;
  799. ConnectSmartBowHelper2P();
  800. }
  801. else
  802. {
  803. userDoConnect2P = false;
  804. canConnect2P = true;
  805. Log("没有发现设备");
  806. PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
  807. TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
  808. }
  809. }else if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) {
  810. Debug.Log("<color=#FF670D>连接中</color>");
  811. if (GameController.ins)
  812. {
  813. GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
  814. }
  815. }else if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
  816. {
  817. if (GameController.ins) {
  818. GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
  819. }
  820. if (smartBowHelper2P.IsBluetoothModuleInited()) Debug.Log("<color=green>已连接</color>(点击断开)");
  821. else Debug.Log("<color=green>已连接</color><color=blue>(正在初始化)</color>");
  822. }
  823. }
  824. public void ConnectSmartBowHelper2P() {
  825. //#if UNITY_ANDROID
  826. // PopupMgr.ins.ClearAllTip();
  827. // if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
  828. // {
  829. // HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
  830. // return;
  831. // }
  832. // if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectSmartBowHelper2P, (permission) => {
  833. // if (permission.Contains("LOCATION"))
  834. // {
  835. // HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
  836. // }
  837. // else if (permission.Contains("BLUETOOTH"))
  838. // {
  839. // HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
  840. // }
  841. // })) return;
  842. //#endif
  843. if (smartBowHelper2P == null)
  844. {
  845. onCreateSmartBowHelper2P();
  846. CallDelay(1, () =>
  847. {
  848. Debug.Log("Connect*********");
  849. smartBowHelper2P.Connect();
  850. });
  851. }
  852. else {
  853. smartBowHelper2P.Connect();
  854. }
  855. }
  856. public void DoConnect2P()
  857. {
  858. Debug.Log("DoConnect2P");
  859. if (smartBowHelper2P == null || smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.None)
  860. {
  861. connectCanceled = false;
  862. userDoConnect2P = true;
  863. doConnect2P = true;;
  864. Connect2P();
  865. }
  866. else if (smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
  867. {
  868. userDoConnect2P = false;
  869. doConnect2P = false;
  870. canConnect2P = true;
  871. DisconnectSmartBowHelper2P();
  872. }
  873. }
  874. void Connect2P()
  875. {
  876. //if (BluetoothShoot.scanLock)
  877. //{
  878. // return;
  879. //}
  880. if (!canConnect2P)
  881. {
  882. return;
  883. }
  884. doConnect = false;
  885. doConnect2P = false;
  886. scanLock = true;
  887. canConnect2P = false;
  888. _scanCanRetryCount2P = 4;
  889. //SetStatus2P(BluetoothStatusEnum.Connecting);
  890. ConnectSmartBowHelper2P();
  891. }
  892. //初始化陀螺仪
  893. public void OnCalibrateGyr2P()
  894. {
  895. if (smartBowHelper2P.IsGyrCalibrating()) smartBowHelper2P.StopGyrCalibration();
  896. else smartBowHelper2P.StartGyrCalibration();
  897. }
  898. //初始化地磁计
  899. public void OnCalibrateMag2P()
  900. {
  901. smartBowHelper2P.StartMagCalibration();
  902. }
  903. #endregion
  904. #region 当前选择操作的设备 如1p或者2p
  905. public SmartBowHelper getSmartBowHelper2P() {
  906. return smartBowHelper2P;
  907. }
  908. public void setBLEPlayer(BluetoothPlayer blePlayer)
  909. {
  910. //切换蓝牙用户时候,判断是否正在连接,如果正在匹配连接的话,取消连接状态
  911. if (blePlayer != currentBLEPlayer) {
  912. Debug.Log("Set BluetoothPlayer status:" + status);
  913. onCancelAllConnecting();
  914. }
  915. currentBLEPlayer = blePlayer;
  916. Debug.Log("Set BluetoothPlayer:" + currentBLEPlayer);
  917. }
  918. public BluetoothPlayer getBLEPlayer()
  919. {
  920. //Debug.Log("Get BluetoothPlayer:" + currentBLEPlayer);
  921. return currentBLEPlayer;
  922. }
  923. //更新跟随点位置
  924. public void UpdateCameraToLookNewPoint2P() {
  925. if (CameraToLookNew.ins != null)
  926. {
  927. Transform m_controlObj = CameraToLookNew.ins.transform;
  928. if (lerpForRotation2P)
  929. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation2P, Time.deltaTime * lerpTimeRate2P);
  930. else
  931. m_controlObj.localRotation = newRotation2P;
  932. }
  933. }
  934. public int get2PBattery() {
  935. return smartBowHelper2P == null? 0 : smartBowHelper2P.GetBattery();
  936. }
  937. //主更新调用
  938. void updateSmartBowHelper2P() {
  939. UpdateCameraToLookNewPoint2P();
  940. }
  941. #endregion
  942. //取消正在连接的连接
  943. public void onCancelAllConnecting(BluetoothStatusEnum _bluetoothStatus = BluetoothStatusEnum.ConnectFail) {
  944. // status != BluetoothStatusEnum.ConnectSuccess
  945. Debug.LogWarning("onCancelAllConnecting 1:" + status);
  946. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  947. if (BleUDP.ins != null && status == BluetoothStatusEnum.Connecting)
  948. {
  949. //Debug.LogWarning("onCancelAllConnecting 2:" + status);
  950. userDoConnect = false;
  951. doConnect = false;
  952. OnDisconnect();
  953. BleUDP.ins.Disconnect();
  954. SetStatus(_bluetoothStatus);
  955. Debug.LogWarning("onCancelAllConnecting 3:" + status);
  956. }
  957. #else
  958. if (bluetoothHelper != null && status == BluetoothStatusEnum.Connecting)
  959. {
  960. Debug.LogWarning("onCancelAllConnecting 2:" + status);
  961. userDoConnect = false;
  962. doConnect = false;
  963. OnDisconnect();
  964. DisconnectBleHelper();
  965. SetStatus(_bluetoothStatus);
  966. }
  967. #endif
  968. //smartBowHelper2P.GetBluetoothStatus() != SmartBowSDK.BluetoothStatusEnum.Connected
  969. if (smartBowHelper2P != null && smartBowHelper2P.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
  970. {
  971. userDoConnect2P = false;
  972. doConnect2P = false;
  973. canConnect2P = true;
  974. DisconnectSmartBowHelper2P();
  975. //smartBowHelper2P.Disconnect();
  976. Debug.Log("onCancelAllConnecting smartBowHelper2P 2:" + smartBowHelper2P.GetBluetoothStatus());
  977. }
  978. }
  979. }