BluetoothShoot.cs 4.9 KB

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