BluetoothAim.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. using UnityEngine.SceneManagement;
  8. /* 蓝牙瞄准模块 */
  9. public class BluetoothAim : MonoBehaviour
  10. {
  11. BluetoothHelper bluetoothHelper;
  12. BluetoothHelperCharacteristic characteristicWrite;
  13. BluetoothHelperService bluetoothService;
  14. string targetDeviceName = "Bbow_20210501";
  15. string deviceName = "";
  16. bool canConnect = true;
  17. [SerializeField] Text textUI;
  18. public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
  19. public bool hasData = false;
  20. public long hasDataTime;
  21. public static bool scanLock = false; //防止同时扫描冲突
  22. public static BluetoothAim ins;
  23. void Start() {
  24. ins = this;
  25. InitAutoDormancy();
  26. }
  27. void OnDestroy()
  28. {
  29. if (bluetoothHelper != null)
  30. {
  31. bluetoothHelper.Disconnect();
  32. }
  33. }
  34. private bool userDoConnect = false;
  35. private bool doConnect = false;
  36. public void DoConnect() {
  37. if (status == BluetoothStatusEnum.Connect) {
  38. userDoConnect = true;
  39. doConnect = true;
  40. SetStatus(BluetoothStatusEnum.Connecting);
  41. } else if (status == BluetoothStatusEnum.ConnectSuccess) {
  42. userDoConnect = false;
  43. doConnect = false;
  44. OnDisconnect();
  45. bluetoothHelper.Disconnect();
  46. }
  47. }
  48. void OnDisconnect() {
  49. hasData = false;
  50. canConnect = true;
  51. SetStatus(BluetoothStatusEnum.ConnectFail);
  52. BowCamera.isTouchMode = true;
  53. DestroyWhenDisconenct();
  54. }
  55. void Update()
  56. {
  57. if (userDoConnect && status == BluetoothStatusEnum.Connect) {
  58. DoConnect();
  59. }
  60. if (doConnect) Connect();
  61. }
  62. void SetStatus(BluetoothStatusEnum statusValue)
  63. {
  64. status = statusValue;
  65. if (status == BluetoothStatusEnum.ConnectFail) {
  66. Sequence sequence = DOTween.Sequence();
  67. sequence.AppendInterval(2f);
  68. sequence.AppendCallback(delegate() {
  69. if (status == BluetoothStatusEnum.ConnectFail) {
  70. status = BluetoothStatusEnum.Connect;
  71. }
  72. });
  73. sequence.SetUpdate(true);
  74. DeviceReconnectView.Show();
  75. }
  76. }
  77. void Connect()
  78. {
  79. if (BluetoothShoot.scanLock)
  80. {
  81. return;
  82. }
  83. if (!canConnect)
  84. {
  85. return;
  86. }
  87. doConnect = false;
  88. scanLock = true;
  89. canConnect = false;
  90. SetStatus(BluetoothStatusEnum.Connecting);
  91. try
  92. {
  93. BluetoothHelper.BLE = true;
  94. bluetoothHelper = BluetoothHelper.GetNewInstance();
  95. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  96. {
  97. Log("连接成功\n" + helper.getDeviceName());
  98. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  99. BowCamera.isTouchMode = false;
  100. foreach (BluetoothHelperService service in helper.getGattServices())
  101. {
  102. if (service.getName().ToLower().StartsWith("0000fff0"))
  103. {
  104. bluetoothService = service;
  105. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  106. {
  107. if (characteristic.getName().ToLower().StartsWith("0000fff2"))
  108. {
  109. characteristicWrite = characteristic;
  110. }
  111. else if (characteristic.getName().ToLower().StartsWith("0000fff1"))
  112. {
  113. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  114. ch.setService(bluetoothService.getName());
  115. bluetoothHelper.Subscribe(ch);
  116. }
  117. }
  118. }
  119. }
  120. // CallDelay(1, OpenInfrared);
  121. // CallDelay(2, OpenReceiveData);
  122. // CallDelay(3, RequestBattery);
  123. CallDelay(2, () => {
  124. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  125. InitWhenConenct();
  126. });
  127. };
  128. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  129. {
  130. Log("连接失败\n" + helper.getDeviceName());
  131. OnDisconnect();
  132. };
  133. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  134. {
  135. if (!hasData) hasDataTime = JC.CS.Utility.GetTimestamp();
  136. hasData = true;
  137. byte[] bytes = value;
  138. // Log(String.Join(",", bytes));
  139. BluetoothClient.UploadData(0, bytes);
  140. if (AimHandler.ins)
  141. {
  142. AimHandler.ins.OnDataReceived(bytes);
  143. }
  144. };
  145. int scanCount = 0;
  146. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  147. {
  148. scanLock = false;
  149. foreach (BluetoothDevice device in nearbyDevices)
  150. {
  151. if (device.DeviceName == targetDeviceName)
  152. {
  153. deviceName = device.DeviceName;
  154. bluetoothHelper.setDeviceName(deviceName);
  155. bluetoothHelper.Connect();
  156. Log("发现设备\n" + device.DeviceName);
  157. return;
  158. }
  159. }
  160. if (scanCount < 3) { //如果没扫描到,则重新扫描,达到延迟提示失败的效果
  161. scanCount++;
  162. scanLock = true;
  163. bluetoothHelper.ScanNearbyDevices();
  164. } else {
  165. canConnect = true;
  166. Log("没有发现设备");
  167. SetStatus(BluetoothStatusEnum.ConnectFail);
  168. }
  169. };
  170. bluetoothHelper.ScanNearbyDevices();
  171. Log("正在扫描设备");
  172. }
  173. catch (Exception e)
  174. {
  175. Debug.Log(e.Message);
  176. canConnect = true;
  177. Log("请打开蓝牙");
  178. }
  179. }
  180. #region 自动进入/退出休眠状态, 这里做程指令发送队列,为了控制连续发送指令的间隔,避免硬件收不到或处理不过来
  181. class CmdToSend {
  182. public string[] cmds;
  183. public Action onComplete;
  184. public Func<bool> canDo;
  185. public CmdToSend(string[] cmds, Action onComplete, Func<bool> canDo) {
  186. this.cmds = cmds;
  187. this.onComplete = onComplete;
  188. this.canDo = canDo;
  189. }
  190. }
  191. Queue<CmdToSend> cmdWaitingList = new Queue<CmdToSend>();
  192. bool isSendCmdLocked = false;
  193. bool canAutoDormancy = false;
  194. bool isStartUp = false;
  195. JC.CS.CountLocker needModularAwake = new JC.CS.CountLocker();
  196. void CheckAndStartUp() {
  197. if (needModularAwake.IsLocked()) {
  198. StartUp();
  199. } else {
  200. Dormancy();
  201. }
  202. }
  203. void InitAutoDormancy() {
  204. // GlobalEventCenter.ins.onGameSceneLoad += () => {
  205. // needModularAwake.Lock();
  206. // CheckAndStartUp();
  207. // };
  208. // GlobalEventCenter.ins.onGameSceneDestroy += () => {
  209. // needModularAwake.Unlock();
  210. // CheckAndStartUp();
  211. // };
  212. // GlobalEventCenter.ins.onSimulateMouseAwakeChanged += (waked) => {
  213. // if (waked) needModularAwake.Lock();
  214. // else needModularAwake.Unlock();;
  215. // CheckAndStartUp();
  216. // };
  217. // GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged += (waked) => {
  218. // if (waked) needModularAwake.Lock();
  219. // else needModularAwake.Unlock();;
  220. // CheckAndStartUp();
  221. // };
  222. //暂时关闭自动休眠,默认是需要模块保持激活
  223. needModularAwake.Lock();
  224. }
  225. void InitWhenConenct() {
  226. canAutoDormancy = true;
  227. List<string> cmds = new List<string>();
  228. cmds.Add("b"); //确保开启stm32
  229. cmds.Add("b"); //获取初始电量
  230. cmds.Add("1"); //开启发送逻辑
  231. Action onComplete = null;
  232. if (needModularAwake.IsLocked()) {
  233. cmds.Add("w"); //红外灯开启
  234. cmds.Add("3"); //九轴开启
  235. onComplete = () => {
  236. isStartUp = true;
  237. };
  238. } else {
  239. cmds.Add("s"); //红外灯关闭
  240. cmds.Add("S"); //Stm32关闭
  241. cmds.Add("4"); //九轴关闭
  242. onComplete = () => {
  243. isStartUp = false;
  244. };
  245. }
  246. SendCDM(null, onComplete, cmds.ToArray());
  247. }
  248. void DestroyWhenDisconenct() {
  249. canAutoDormancy = false;
  250. sendCMD_CheckAndDoStop(null);
  251. }
  252. //启动
  253. void StartUp() {
  254. SendCDM(() => {
  255. return !isStartUp;
  256. }, () => {
  257. isStartUp = true;
  258. }, "b", "w", "3");
  259. }
  260. //休眠
  261. void Dormancy() {
  262. SendCDM(() => {
  263. return isStartUp;
  264. }, () => {
  265. isStartUp = false;
  266. }, "4", "s", "S");
  267. }
  268. void SendCDM(Func<bool> canDo, Action onComplete, params string[] cmds) {
  269. CmdToSend cmdToSend = new CmdToSend(cmds, onComplete, canDo);
  270. if (isSendCmdLocked) {
  271. cmdWaitingList.Enqueue(cmdToSend);
  272. return;
  273. }
  274. sendCMD_NotCheck(cmdToSend);
  275. }
  276. void sendCMD_NotCheck(CmdToSend cmdToSend) {
  277. if (cmdToSend.canDo != null && !cmdToSend.canDo.Invoke()) {
  278. sendCMD_CheckNext();
  279. return;
  280. }
  281. isSendCmdLocked = true;
  282. Sequence sequence = DOTween.Sequence();
  283. sequence.PrependInterval(0.3f);
  284. foreach (var cmd in cmdToSend.cmds) {
  285. sequence.AppendCallback(() => {
  286. bool stopped = sendCMD_CheckAndDoStop(sequence);
  287. if (!stopped) WriteData(cmd);
  288. });
  289. sequence.AppendInterval(0.5f);
  290. }
  291. sequence.AppendCallback(() => {
  292. bool stopped = sendCMD_CheckAndDoStop(sequence);
  293. if (!stopped) {
  294. isSendCmdLocked = false;
  295. cmdToSend.onComplete?.Invoke();
  296. sendCMD_CheckNext();
  297. }
  298. });
  299. sequence.SetUpdate(true);
  300. }
  301. void sendCMD_CheckNext() {
  302. if (cmdWaitingList.Count <= 0) return;
  303. CmdToSend cmdToSend = cmdWaitingList.Dequeue();
  304. sendCMD_NotCheck(cmdToSend);
  305. }
  306. bool sendCMD_CheckAndDoStop(Sequence sequence) {
  307. if (canAutoDormancy) return false;
  308. isStartUp = false;
  309. isSendCmdLocked = false;
  310. cmdWaitingList.Clear();
  311. if (sequence != null) sequence.Kill();
  312. return true;
  313. }
  314. #endregion
  315. void OpenInfrared()
  316. {
  317. WriteData("1");
  318. Log("红外线准备完成\n" + deviceName);
  319. }
  320. void OpenReceiveData()
  321. {
  322. WriteData("3");
  323. Log("瞄准模块准备完成\n" + deviceName);
  324. }
  325. public void RequestBattery() {
  326. if (!isStartUp) return;
  327. if (isSendCmdLocked) return;
  328. WriteData("b");
  329. }
  330. public void ReplyInfraredShoot() {
  331. if (isSendCmdLocked) return;
  332. WriteData("I");
  333. }
  334. void CallDelay(float delayTime, TweenCallback callback)
  335. {
  336. Sequence sequence = DOTween.Sequence();
  337. sequence.PrependInterval(delayTime).AppendCallback(callback);
  338. sequence.SetUpdate(true);
  339. }
  340. public void WriteData(string data)
  341. {
  342. if (DebugDeviceCMD.ins) DebugDeviceCMD.ins.ShowCMD(data);
  343. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  344. ch.setService(bluetoothService.getName());
  345. bluetoothHelper.WriteCharacteristic(ch, data);
  346. }
  347. void Log(string text)
  348. {
  349. if (textUI)
  350. {
  351. textUI.text = text;
  352. }
  353. }
  354. }
  355. // public class BluetoothAim : MonoBehaviour
  356. // {
  357. // BluetoothHelper bluetoothHelper;
  358. // BluetoothHelperCharacteristic characteristicWrite;
  359. // BluetoothHelperService bluetoothService;
  360. // string targetDeviceName = "Bbow_20210501";
  361. // string deviceName = "";
  362. // bool canConnect = true;
  363. // [SerializeField] Text textUI;
  364. // public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
  365. // public bool hasData = false;
  366. // public long hasDataTime;
  367. // public static bool scanLock = false; //防止同时扫描冲突
  368. // public static BluetoothAim ins;
  369. // void Start() {
  370. // ins = this;
  371. // }
  372. // void OnDestroy()
  373. // {
  374. // if (bluetoothHelper != null)
  375. // {
  376. // bluetoothHelper.Disconnect();
  377. // }
  378. // }
  379. // private bool userDoConnect = false;
  380. // private bool doConnect = false;
  381. // public void DoConnect() {
  382. // if (status == BluetoothStatusEnum.Connect) {
  383. // userDoConnect = true;
  384. // doConnect = true;
  385. // SetStatus(BluetoothStatusEnum.Connecting);
  386. // } else if (status == BluetoothStatusEnum.ConnectSuccess) {
  387. // userDoConnect = false;
  388. // doConnect = false;
  389. // OnDisconnect();
  390. // bluetoothHelper.Disconnect();
  391. // }
  392. // }
  393. // void OnDisconnect() {
  394. // hasData = false;
  395. // canConnect = true;
  396. // SetStatus(BluetoothStatusEnum.ConnectFail);
  397. // BowCamera.isTouchMode = true;
  398. // }
  399. // void Update()
  400. // {
  401. // if (userDoConnect && status == BluetoothStatusEnum.Connect) {
  402. // DoConnect();
  403. // }
  404. // if (doConnect) Connect();
  405. // }
  406. // void SetStatus(BluetoothStatusEnum statusValue)
  407. // {
  408. // status = statusValue;
  409. // if (status == BluetoothStatusEnum.ConnectFail) {
  410. // Sequence sequence = DOTween.Sequence();
  411. // sequence.AppendInterval(2f);
  412. // sequence.AppendCallback(delegate() {
  413. // if (status == BluetoothStatusEnum.ConnectFail) {
  414. // status = BluetoothStatusEnum.Connect;
  415. // }
  416. // });
  417. // sequence.SetUpdate(true);
  418. // DeviceReconnectView.Show();
  419. // }
  420. // }
  421. // void Connect()
  422. // {
  423. // if (BluetoothShoot.scanLock)
  424. // {
  425. // return;
  426. // }
  427. // if (!canConnect)
  428. // {
  429. // return;
  430. // }
  431. // doConnect = false;
  432. // scanLock = true;
  433. // canConnect = false;
  434. // SetStatus(BluetoothStatusEnum.Connecting);
  435. // try
  436. // {
  437. // BluetoothHelper.BLE = true;
  438. // bluetoothHelper = BluetoothHelper.GetNewInstance();
  439. // bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  440. // {
  441. // Log("连接成功\n" + helper.getDeviceName());
  442. // SetStatus(BluetoothStatusEnum.ConnectSuccess);
  443. // BowCamera.isTouchMode = false;
  444. // foreach (BluetoothHelperService service in helper.getGattServices())
  445. // {
  446. // if (service.getName().ToLower().StartsWith("0000fff0"))
  447. // {
  448. // bluetoothService = service;
  449. // foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  450. // {
  451. // if (characteristic.getName().ToLower().StartsWith("0000fff2"))
  452. // {
  453. // characteristicWrite = characteristic;
  454. // }
  455. // else if (characteristic.getName().ToLower().StartsWith("0000fff1"))
  456. // {
  457. // BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  458. // ch.setService(bluetoothService.getName());
  459. // bluetoothHelper.Subscribe(ch);
  460. // }
  461. // }
  462. // }
  463. // }
  464. // CallDelay(1, OpenInfrared);
  465. // CallDelay(2, OpenReceiveData);
  466. // CallDelay(3, RequestBattery);
  467. // };
  468. // bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  469. // {
  470. // Log("连接失败\n" + helper.getDeviceName());
  471. // OnDisconnect();
  472. // };
  473. // bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  474. // {
  475. // if (!hasData) hasDataTime = JC.CS.Utility.GetTimestamp();
  476. // hasData = true;
  477. // byte[] bytes = value;
  478. // // Log(String.Join(",", bytes));
  479. // BluetoothClient.UploadData(0, bytes);
  480. // if (AimHandler.ins)
  481. // {
  482. // AimHandler.ins.OnDataReceived(bytes);
  483. // }
  484. // };
  485. // bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  486. // {
  487. // scanLock = false;
  488. // foreach (BluetoothDevice device in nearbyDevices)
  489. // {
  490. // if (device.DeviceName == targetDeviceName)
  491. // {
  492. // deviceName = device.DeviceName;
  493. // bluetoothHelper.setDeviceName(deviceName);
  494. // bluetoothHelper.Connect();
  495. // Log("发现设备\n" + device.DeviceName);
  496. // return;
  497. // }
  498. // }
  499. // canConnect = true;
  500. // Log("没有发现设备");
  501. // SetStatus(BluetoothStatusEnum.ConnectFail);
  502. // };
  503. // bluetoothHelper.ScanNearbyDevices();
  504. // Log("正在扫描设备");
  505. // }
  506. // catch (Exception e)
  507. // {
  508. // Debug.Log(e.Message);
  509. // canConnect = true;
  510. // Log("请打开蓝牙");
  511. // }
  512. // }
  513. // void OpenInfrared()
  514. // {
  515. // WriteData("1");
  516. // Log("红外线准备完成\n" + deviceName);
  517. // }
  518. // void OpenReceiveData()
  519. // {
  520. // WriteData("3");
  521. // Log("瞄准模块准备完成\n" + deviceName);
  522. // }
  523. // public void RequestBattery() {
  524. // WriteData("b");
  525. // }
  526. // public void ReplyInfraredShoot() {
  527. // WriteData("I");
  528. // }
  529. // void CallDelay(float delayTime, TweenCallback callback)
  530. // {
  531. // Sequence sequence = DOTween.Sequence();
  532. // sequence.PrependInterval(delayTime).AppendCallback(callback);
  533. // sequence.SetUpdate(true);
  534. // }
  535. // public void WriteData(string data)
  536. // {
  537. // BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  538. // ch.setService(bluetoothService.getName());
  539. // bluetoothHelper.WriteCharacteristic(ch, data);
  540. // }
  541. // void Log(string text)
  542. // {
  543. // if (textUI)
  544. // {
  545. // textUI.text = text;
  546. // }
  547. // }
  548. // }