BluetoothShoot.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Invoke("SetReceiveDataInterval20", 2);
  68. Invoke("ShowFinish", 3);
  69. };
  70. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  71. {
  72. canConnect = true;
  73. Log("连接失败\n" + helper.getDeviceName());
  74. };
  75. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  76. {
  77. byte[] bytes = value;
  78. if (ShootCheck.ins != null)
  79. {
  80. ShootCheck.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, "5");
  115. Log("开始接收信息\n" + deviceName);
  116. }
  117. void SetReceiveDataInterval20()
  118. {
  119. bluetoothHelper.WriteCharacteristic(characteristicWrite, "b");
  120. Log("修改接收频率\n" + deviceName);
  121. }
  122. void ShowFinish()
  123. {
  124. Log("射击模块准备完成\n" + deviceName);
  125. }
  126. void Log(string text)
  127. {
  128. if (textUI != null)
  129. {
  130. textUI.text = text;
  131. }
  132. }
  133. }