BluetoothShoot.cs 6.3 KB

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