BluetoothShoot.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using ArduinoBluetoothAPI;
  6. public class BluetoothShoot : MonoBehaviour
  7. {
  8. BluetoothHelper bluetoothHelper;
  9. BluetoothHelperCharacteristic characteristicWrite;
  10. BluetoothHelperService bluetoothService;
  11. string deviceName = "";
  12. bool canConnect = true;
  13. [SerializeField] string targetDeviceName = "BGBox_202012";
  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 (BluetoothAim.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. BluetoothClient.UploadData(1, bytes);
  77. if (ShootCheck.ins != null)
  78. {
  79. ShootCheck.ins.OnDataReceived(bytes);
  80. }
  81. };
  82. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  83. {
  84. scanLock = false;
  85. foreach (BluetoothDevice device in nearbyDevices)
  86. {
  87. if (device.DeviceName == targetDeviceName)
  88. {
  89. deviceName = device.DeviceName;
  90. bluetoothHelper.setDeviceName(deviceName);
  91. bluetoothHelper.Connect();
  92. Log("发现设备\n" + device.DeviceName);
  93. return;
  94. }
  95. }
  96. canConnect = true;
  97. Log("没有发现设备");
  98. };
  99. bluetoothHelper.ScanNearbyDevices();
  100. Log("正在扫描设备");
  101. }
  102. catch (Exception e)
  103. {
  104. Debug.Log(e.Message);
  105. canConnect = true;
  106. Log("请打开蓝牙");
  107. }
  108. }
  109. void OpenReceiveData()
  110. {
  111. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  112. ch.setService(bluetoothService.getName());
  113. bluetoothHelper.WriteCharacteristic(ch, "5");
  114. Log("射击模块准备完成\n" + deviceName);
  115. }
  116. void Log(string text)
  117. {
  118. if (textUI != null)
  119. {
  120. textUI.text = text;
  121. }
  122. }
  123. }