BluetoothAim.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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, delegate() {
  121. WriteData(SceneManager.GetActiveScene().name.StartsWith("Game") ? "b" :"B");
  122. });
  123. };
  124. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  125. {
  126. Log("连接失败\n" + helper.getDeviceName());
  127. OnDisconnect();
  128. };
  129. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  130. {
  131. if (!hasData) hasDataTime = JC.CS.Utility.GetTimestamp();
  132. hasData = true;
  133. byte[] bytes = value;
  134. // Log(String.Join(",", bytes));
  135. BluetoothClient.UploadData(0, bytes);
  136. if (AimHandler.ins)
  137. {
  138. AimHandler.ins.OnDataReceived(bytes);
  139. }
  140. };
  141. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  142. {
  143. scanLock = false;
  144. foreach (BluetoothDevice device in nearbyDevices)
  145. {
  146. if (device.DeviceName == targetDeviceName)
  147. {
  148. deviceName = device.DeviceName;
  149. bluetoothHelper.setDeviceName(deviceName);
  150. bluetoothHelper.Connect();
  151. Log("发现设备\n" + device.DeviceName);
  152. return;
  153. }
  154. }
  155. canConnect = true;
  156. Log("没有发现设备");
  157. SetStatus(BluetoothStatusEnum.ConnectFail);
  158. };
  159. bluetoothHelper.ScanNearbyDevices();
  160. Log("正在扫描设备");
  161. }
  162. catch (Exception e)
  163. {
  164. Debug.Log(e.Message);
  165. canConnect = true;
  166. Log("请打开蓝牙");
  167. }
  168. }
  169. void OpenInfrared()
  170. {
  171. WriteData("1");
  172. Log("红外线准备完成\n" + deviceName);
  173. }
  174. void OpenReceiveData()
  175. {
  176. WriteData("3");
  177. Log("瞄准模块准备完成\n" + deviceName);
  178. }
  179. void CallDelay(float delayTime, TweenCallback callback)
  180. {
  181. Sequence sequence = DOTween.Sequence();
  182. sequence.PrependInterval(delayTime).AppendCallback(callback);
  183. sequence.SetUpdate(true);
  184. }
  185. public void WriteData(string data)
  186. {
  187. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  188. ch.setService(bluetoothService.getName());
  189. bluetoothHelper.WriteCharacteristic(ch, data);
  190. }
  191. void Log(string text)
  192. {
  193. if (textUI)
  194. {
  195. textUI.text = text;
  196. }
  197. }
  198. }