HRB_Bluetooth.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. /* 蓝牙瞄准模块 */
  8. public class HRB_Bluetooth : MonoBehaviour
  9. {
  10. readonly string targetDeviceName = "Bbow_20210501";
  11. string targetDeviceService {
  12. get {
  13. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3) {
  14. #if UNITY_ANDROID
  15. return "0000fff0";
  16. #else
  17. return "fff0";
  18. #endif
  19. }
  20. return "6e400001";
  21. }
  22. }
  23. string targetDeviceCharacteristicWrite {
  24. get {
  25. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3) {
  26. #if UNITY_ANDROID
  27. return "0000fff2";
  28. #else
  29. return "fff2";
  30. #endif
  31. }
  32. return "6e400002";
  33. }
  34. }
  35. string targetDeviceCharacteristicNotify {
  36. get {
  37. if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3) {
  38. #if UNITY_ANDROID
  39. return "0000fff1";
  40. #else
  41. return "fff1";
  42. #endif
  43. }
  44. return "6e400003";
  45. }
  46. }
  47. BluetoothHelper bluetoothHelper;
  48. BluetoothHelperCharacteristic characteristicWrite;
  49. BluetoothHelperService bluetoothService;
  50. string deviceName = "";
  51. bool canConnect = true;
  52. [SerializeField] Text textUI;
  53. public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
  54. public Action onFindTargetDevice;
  55. void OnDestroy()
  56. {
  57. DisconnectBleHelper();
  58. }
  59. private bool userDoConnect = false;
  60. private bool doConnect = false;
  61. public Func<bool> action_DoConnectInterceptor;
  62. public void DoConnect()
  63. {
  64. if (action_DoConnectInterceptor != null) {
  65. if (action_DoConnectInterceptor.Invoke()) return;
  66. }
  67. if (status == BluetoothStatusEnum.Connect)
  68. {
  69. userDoConnect = true;
  70. doConnect = true;
  71. SetStatus(BluetoothStatusEnum.Connecting);
  72. }
  73. else if (status == BluetoothStatusEnum.ConnectSuccess)
  74. {
  75. userDoConnect = false;
  76. doConnect = false;
  77. OnDisconnect();
  78. DisconnectBleHelper();
  79. }
  80. }
  81. void OnDisconnect()
  82. {
  83. canConnect = true;
  84. SetStatus(BluetoothStatusEnum.ConnectFail);
  85. BowCamera.isTouchMode = true;
  86. if (AimHandler.ins) AimHandler.ins.SetMsOldDefault();
  87. }
  88. void Update()
  89. {
  90. if (userDoConnect && status == BluetoothStatusEnum.Connect)
  91. {
  92. DoConnect();
  93. }
  94. if (doConnect) Connect();
  95. }
  96. void SetStatus(BluetoothStatusEnum statusValue)
  97. {
  98. status = statusValue;
  99. if (status == BluetoothStatusEnum.ConnectFail)
  100. {
  101. Sequence sequence = DOTween.Sequence();
  102. sequence.AppendInterval(2f);
  103. sequence.AppendCallback(delegate ()
  104. {
  105. if (status == BluetoothStatusEnum.ConnectFail)
  106. {
  107. status = BluetoothStatusEnum.Connect;
  108. }
  109. });
  110. sequence.SetUpdate(true);
  111. SimulateMouseController.ins?.SetBleConnected(false);
  112. } else if (status == BluetoothStatusEnum.ConnectSuccess) {
  113. SimulateMouseController.ins?.SetBleConnected(true);
  114. }
  115. }
  116. void DisconnectBleHelper()
  117. {
  118. if (bluetoothHelper != null) bluetoothHelper.Disconnect();
  119. }
  120. void Connect()
  121. {
  122. if (BluetoothShoot.scanLock)
  123. {
  124. return;
  125. }
  126. if (!canConnect)
  127. {
  128. return;
  129. }
  130. doConnect = false;
  131. canConnect = false;
  132. SetStatus(BluetoothStatusEnum.Connecting);
  133. ConnectBleHelper();
  134. }
  135. void ConnectBleHelper()
  136. {
  137. #if UNITY_ANDROID
  138. PopupMgr.ins.ClearAllTip();
  139. if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
  140. {
  141. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
  142. return;
  143. }
  144. if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectBleHelper, (permission) => {
  145. if (permission.Contains("LOCATION"))
  146. {
  147. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
  148. }
  149. else if (permission.Contains("BLUETOOTH"))
  150. {
  151. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
  152. }
  153. })) return;
  154. #endif
  155. try
  156. {
  157. BluetoothHelper.BLE = true;
  158. bluetoothHelper = BluetoothHelper.GetNewInstance();
  159. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  160. {
  161. Log("连接成功\n" + helper.getDeviceName());
  162. SetStatus(BluetoothStatusEnum.ConnectSuccess);
  163. BowCamera.isTouchMode = false;
  164. foreach (BluetoothHelperService service in helper.getGattServices())
  165. {
  166. if (service.getName().ToLower().StartsWith(targetDeviceService))
  167. {
  168. bluetoothService = service;
  169. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  170. {
  171. if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicWrite))
  172. {
  173. characteristicWrite = characteristic;
  174. }
  175. else if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicNotify))
  176. {
  177. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  178. ch.setService(bluetoothService.getName());
  179. bluetoothHelper.Subscribe(ch);
  180. }
  181. }
  182. }
  183. }
  184. };
  185. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  186. {
  187. Log("连接失败\n" + helper.getDeviceName());
  188. OnDisconnect();
  189. };
  190. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  191. {
  192. if (status != BluetoothStatusEnum.ConnectSuccess) return;
  193. };
  194. int scanCount = 0;
  195. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  196. {
  197. foreach (BluetoothDevice device in nearbyDevices)
  198. {
  199. Log("发现设备 " + device.DeviceName);
  200. if (device.DeviceName == targetDeviceName)
  201. {
  202. deviceName = device.DeviceName;
  203. // bluetoothHelper.setDeviceName(deviceName);
  204. // bluetoothHelper.Connect();
  205. Log("匹配设备 " + device.DeviceName);
  206. onFindTargetDevice?.Invoke();
  207. return;
  208. }
  209. }
  210. if (scanCount < 3)
  211. { //如果没扫描到,则重新扫描,达到延迟提示失败的效果
  212. scanCount++;
  213. bluetoothHelper.ScanNearbyDevices();
  214. }
  215. else
  216. {
  217. canConnect = true;
  218. Log("没有发现设备");
  219. SetStatus(BluetoothStatusEnum.ConnectFail);
  220. }
  221. };
  222. bluetoothHelper.ScanNearbyDevices();
  223. Log("正在扫描设备");
  224. }
  225. catch (Exception e)
  226. {
  227. Debug.LogError(e);
  228. HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
  229. }
  230. }
  231. void HandleConnectException(string errorText)
  232. {
  233. canConnect = true;
  234. status = BluetoothStatusEnum.Connect;
  235. userDoConnect = false;
  236. PopupMgr.ins.ShowTip(errorText);
  237. }
  238. void Log(string text)
  239. {
  240. if (textUI) textUI.text = text;
  241. Debug.Log(string.Format("[{0}]{1}", typeof(HRB_Bluetooth).Name, text));
  242. }
  243. }