BluetoothShoot.cs 5.0 KB

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