BluetoothAim.cs 35 KB

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