BluetoothShoot.cs 6.4 KB

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