BluetoothAim.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. public class BluetoothAim : MonoBehaviour
  8. {
  9. BluetoothHelper bluetoothHelper;
  10. BluetoothHelperCharacteristic characteristicWrite;
  11. BluetoothHelperService bluetoothService;
  12. string targetDeviceName = "Bbow_20210501";
  13. string deviceName = "";
  14. bool canConnect = true;
  15. [SerializeField] Text textUI;
  16. public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
  17. public bool hasData = false;
  18. public static bool scanLock = false; //防止同时扫描冲突
  19. public static BluetoothAim ins;
  20. void Start() {
  21. ins = this;
  22. }
  23. void OnDestroy()
  24. {
  25. if (bluetoothHelper != null)
  26. {
  27. bluetoothHelper.Disconnect();
  28. }
  29. }
  30. private bool userDoConnect = false;
  31. private bool doConnect = false;
  32. public void DoConnect() {
  33. if (status == BluetoothStatusEnum.Connect) {
  34. userDoConnect = true;
  35. doConnect = true;
  36. SetStatus(BluetoothStatusEnum.Connecting);
  37. } else if (status == BluetoothStatusEnum.ConnectSuccess) {
  38. userDoConnect = false;
  39. doConnect = false;
  40. OnDisconnect();
  41. bluetoothHelper.Disconnect();
  42. }
  43. }
  44. void OnDisconnect() {
  45. hasData = false;
  46. canConnect = true;
  47. SetStatus(BluetoothStatusEnum.ConnectFail);
  48. }
  49. void Update()
  50. {
  51. if (userDoConnect && status == BluetoothStatusEnum.Connect) {
  52. DoConnect();
  53. }
  54. if (doConnect) Connect();
  55. }
  56. void SetStatus(BluetoothStatusEnum statusValue)
  57. {
  58. status = statusValue;
  59. if (status == BluetoothStatusEnum.ConnectFail) {
  60. Sequence sequence = DOTween.Sequence();
  61. sequence.AppendInterval(2f);
  62. sequence.AppendCallback(delegate() {
  63. if (status == BluetoothStatusEnum.ConnectFail) {
  64. status = BluetoothStatusEnum.Connect;
  65. }
  66. });
  67. sequence.SetUpdate(true);
  68. DeviceReconnectView.Show();
  69. }
  70. }
  71. void Connect()
  72. {
  73. if (BluetoothShoot.scanLock)
  74. {
  75. return;
  76. }
  77. if (!canConnect)
  78. {
  79. return;
  80. }
  81. doConnect = false;
  82. scanLock = true;
  83. canConnect = false;
  84. SetStatus(BluetoothStatusEnum.Connecting);
  85. try
  86. {
  87. BluetoothHelper.BLE = true;
  88. bluetoothHelper = BluetoothHelper.GetNewInstance();
  89. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  90. {
  91. Log("连接成功\n" + helper.getDeviceName());
  92. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  93. foreach (BluetoothHelperService service in helper.getGattServices())
  94. {
  95. if (service.getName().ToLower().StartsWith("0000fff0"))
  96. {
  97. bluetoothService = service;
  98. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  99. {
  100. if (characteristic.getName().ToLower().StartsWith("0000fff2"))
  101. {
  102. characteristicWrite = characteristic;
  103. }
  104. else if (characteristic.getName().ToLower().StartsWith("0000fff1"))
  105. {
  106. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  107. ch.setService(bluetoothService.getName());
  108. bluetoothHelper.Subscribe(ch);
  109. }
  110. }
  111. }
  112. }
  113. if (DebugForDevice.ins) {
  114. CallDelay(1, OpenInfrared);
  115. CallDelay(2, OpenReceiveData);
  116. } else {
  117. CallDelay(1, OpenReceiveData);
  118. }
  119. };
  120. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  121. {
  122. Log("连接失败\n" + helper.getDeviceName());
  123. OnDisconnect();
  124. };
  125. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  126. {
  127. hasData = true;
  128. byte[] bytes = value;
  129. // Log(String.Join(",", bytes));
  130. if (DebugForDevice.ins) DebugForDevice.ins.OnDataReceived(bytes);
  131. BluetoothClient.UploadData(0, bytes);
  132. if (AimHandler.ins)
  133. {
  134. AimHandler.ins.OnDataReceived(bytes);
  135. }
  136. };
  137. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  138. {
  139. scanLock = false;
  140. foreach (BluetoothDevice device in nearbyDevices)
  141. {
  142. if (device.DeviceName == targetDeviceName)
  143. {
  144. deviceName = device.DeviceName;
  145. bluetoothHelper.setDeviceName(deviceName);
  146. bluetoothHelper.Connect();
  147. Log("发现设备\n" + device.DeviceName);
  148. return;
  149. }
  150. }
  151. canConnect = true;
  152. Log("没有发现设备");
  153. SetStatus(BluetoothStatusEnum.ConnectFail);
  154. };
  155. bluetoothHelper.ScanNearbyDevices();
  156. Log("正在扫描设备");
  157. }
  158. catch (Exception e)
  159. {
  160. Debug.Log(e.Message);
  161. canConnect = true;
  162. Log("请打开蓝牙");
  163. }
  164. }
  165. void OpenInfrared() {
  166. WriteData("1");
  167. Log("开启红外数据采集\n" + deviceName);
  168. }
  169. void OpenReceiveData()
  170. {
  171. WriteData("3");
  172. Log("瞄准模块准备完成\n" + deviceName);
  173. }
  174. void CallDelay(float delayTime, TweenCallback callback)
  175. {
  176. Sequence sequence = DOTween.Sequence();
  177. sequence.PrependInterval(delayTime).AppendCallback(callback);
  178. sequence.SetUpdate(true);
  179. }
  180. public void WriteData(string data)
  181. {
  182. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  183. ch.setService(bluetoothService.getName());
  184. bluetoothHelper.WriteCharacteristic(ch, data);
  185. }
  186. void Log(string text)
  187. {
  188. if (textUI)
  189. {
  190. textUI.text = text;
  191. }
  192. }
  193. }