BluetoothAim.cs 6.4 KB

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