BluetoothAim.cs 5.9 KB

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