BluetoothAim.cs 6.3 KB

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