BluetoothShoot.cs 5.9 KB

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