BluetoothAim.cs 5.8 KB

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