BluetoothAim.cs 5.6 KB

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