BluetoothAim.cs 6.8 KB

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