BluetoothBC.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using ArduinoBluetoothAPI;
  6. public class BluetoothBC : MonoBehaviour
  7. {
  8. BluetoothHelper bluetoothHelper;
  9. BluetoothHelperCharacteristic characteristicWrite;
  10. BluetoothHelperService bluetoothService;
  11. string deviceName = "";
  12. bool canConnect = true;
  13. void OnDestroy()
  14. {
  15. if (bluetoothHelper != null)
  16. {
  17. bluetoothHelper.Disconnect();
  18. }
  19. }
  20. void FixedUpdate()
  21. {
  22. Connect();
  23. }
  24. void Connect()
  25. {
  26. if (!canConnect)
  27. {
  28. return;
  29. }
  30. canConnect = false;
  31. try
  32. {
  33. BluetoothHelper.BLE = true;
  34. bluetoothHelper = BluetoothHelper.GetNewInstance();
  35. bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
  36. {
  37. Log("连接成功\n" + helper.getDeviceName());
  38. foreach (BluetoothHelperService service in helper.getGattServices())
  39. {
  40. if (service.getName().ToLower().StartsWith("0000fff0"))
  41. {
  42. bluetoothService = service;
  43. foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
  44. {
  45. if (characteristic.getName().ToLower().StartsWith("0000fff2"))
  46. {
  47. characteristicWrite = characteristic;
  48. }
  49. else if (characteristic.getName().ToLower().StartsWith("0000fff1"))
  50. {
  51. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
  52. ch.setService(bluetoothService.getName());
  53. bluetoothHelper.Subscribe(ch);
  54. }
  55. }
  56. }
  57. }
  58. Invoke("OpenReceiveData", 1);
  59. Invoke("SetReceiveDataInterval20", 2);
  60. };
  61. bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
  62. {
  63. canConnect = true;
  64. Log("连接失败\n" + helper.getDeviceName());
  65. };
  66. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  67. {
  68. byte[] bytes = value;
  69. float ay = ToAcceleratedSpeed(bytes[6], bytes[7]);
  70. if (ShootCheck.ins.check(ay)) {
  71. if (ArmBow.ins != null) {
  72. ArmBow.ins.ADS_fire();
  73. }
  74. }
  75. };
  76. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  77. {
  78. foreach (BluetoothDevice device in nearbyDevices)
  79. {
  80. if (device.DeviceName == "BGBox_202012")
  81. {
  82. deviceName = device.DeviceName;
  83. bluetoothHelper.setDeviceName(deviceName);
  84. bluetoothHelper.Connect();
  85. Log("发现设备\n" + device.DeviceName);
  86. return;
  87. }
  88. }
  89. canConnect = true;
  90. Log("没有发现设备");
  91. };
  92. bluetoothHelper.ScanNearbyDevices();
  93. Log("正在扫描设备");
  94. }
  95. catch (Exception e)
  96. {
  97. Debug.Log(e.Message);
  98. canConnect = true;
  99. Log("请打开蓝牙");
  100. }
  101. }
  102. void OpenReceiveData()
  103. {
  104. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  105. ch.setService(bluetoothService.getName());
  106. bluetoothHelper.WriteCharacteristic(ch, "3");
  107. Log("开始接收信息\n" + deviceName);
  108. }
  109. void SetReceiveDataInterval20()
  110. {
  111. bluetoothHelper.WriteCharacteristic(characteristicWrite, "b");
  112. Log("修改接收频率\n" + deviceName);
  113. }
  114. float ToAcceleratedSpeed(byte b1, byte b2)
  115. {
  116. int value = TwoByteToInt(b1, b2);
  117. return (float)value / 32768 * 16;
  118. }
  119. int TwoByteToInt(byte b1, byte b2)
  120. {
  121. ushort twoByte = (ushort)(b1 * 256 + b2);
  122. short shortNum = (short)twoByte;
  123. return (int)shortNum;
  124. }
  125. void Log(string text)
  126. {
  127. if (DebugText.ins != null) {
  128. DebugText.ins.show(text);
  129. }
  130. }
  131. }