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