BluetoothAim.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using ArduinoBluetoothAPI;
  2. using System;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. public class BluetoothAim : MonoBehaviour
  7. {
  8. BluetoothHelper bluetoothHelper;
  9. BluetoothHelperCharacteristic characteristicWrite;
  10. BluetoothHelperService bluetoothService;
  11. string targetDeviceName = "Bbow_20210501";
  12. string deviceName = "";
  13. bool canConnect = true;
  14. [SerializeField] Text textUI;
  15. public static bool scanLock = false; //防止同时扫描冲突
  16. void OnDestroy()
  17. {
  18. if (bluetoothHelper != null)
  19. {
  20. bluetoothHelper.Disconnect();
  21. }
  22. }
  23. void FixedUpdate()
  24. {
  25. Connect();
  26. }
  27. void Connect()
  28. {
  29. if (BluetoothShoot.scanLock)
  30. {
  31. return;
  32. }
  33. if (!canConnect)
  34. {
  35. return;
  36. }
  37. scanLock = true;
  38. canConnect = false;
  39. try
  40. {
  41. BluetoothHelper.BLE = true;
  42. bluetoothHelper = BluetoothHelper.GetNewInstance();
  43. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  44. {
  45. Log("连接成功\n" + helper.getDeviceName());
  46. foreach (BluetoothHelperService service in helper.getGattServices())
  47. {
  48. if (service.getName().ToLower().StartsWith("0000fff0"))
  49. {
  50. bluetoothService = service;
  51. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  52. {
  53. if (characteristic.getName().ToLower().StartsWith("0000fff2"))
  54. {
  55. characteristicWrite = characteristic;
  56. }
  57. else if (characteristic.getName().ToLower().StartsWith("0000fff1"))
  58. {
  59. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  60. ch.setService(bluetoothService.getName());
  61. bluetoothHelper.Subscribe(ch);
  62. }
  63. }
  64. }
  65. }
  66. Invoke("OpenReceiveData", 1);
  67. };
  68. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  69. {
  70. canConnect = true;
  71. Log("连接失败\n" + helper.getDeviceName());
  72. };
  73. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  74. {
  75. byte[] bytes = value;
  76. // Log(String.Join(",", bytes));
  77. BluetoothClient.UploadData(0, bytes);
  78. if (AimHandler.ins)
  79. {
  80. AimHandler.ins.OnDataReceived(bytes);
  81. }
  82. };
  83. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  84. {
  85. scanLock = false;
  86. foreach (BluetoothDevice device in nearbyDevices)
  87. {
  88. if (device.DeviceName == targetDeviceName)
  89. {
  90. deviceName = device.DeviceName;
  91. bluetoothHelper.setDeviceName(deviceName);
  92. bluetoothHelper.Connect();
  93. Log("发现设备\n" + device.DeviceName);
  94. return;
  95. }
  96. }
  97. canConnect = true;
  98. Log("没有发现设备");
  99. };
  100. bluetoothHelper.ScanNearbyDevices();
  101. Log("正在扫描设备");
  102. }
  103. catch (Exception e)
  104. {
  105. Debug.Log(e.Message);
  106. canConnect = true;
  107. Log("请打开蓝牙");
  108. }
  109. }
  110. void OpenReceiveData()
  111. {
  112. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  113. ch.setService(bluetoothService.getName());
  114. bluetoothHelper.WriteCharacteristic(ch, "3");
  115. Log("瞄准模块准备完成\n" + deviceName);
  116. }
  117. void Log(string text)
  118. {
  119. if (textUI)
  120. {
  121. textUI.text = text;
  122. }
  123. }
  124. }