BluetoothNew.cs 4.2 KB

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