BluetoothNew.cs 4.6 KB

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