BluetoothShoot.cs 6.6 KB

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