BluetoothAim.cs 5.3 KB

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