BluetoothBC_Old.cs 4.2 KB

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