BluetoothShoot.cs 4.7 KB

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