BluetoothNew.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. };
  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. };
  73. bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
  74. {
  75. foreach (BluetoothDevice device in nearbyDevices)
  76. {
  77. if (device.DeviceName == targetDeviceName)
  78. {
  79. deviceName = device.DeviceName;
  80. bluetoothHelper.setDeviceName(deviceName);
  81. bluetoothHelper.Connect();
  82. Log("发现设备\n" + device.DeviceName);
  83. return;
  84. }
  85. }
  86. canConnect = true;
  87. Log("没有发现设备");
  88. };
  89. bluetoothHelper.ScanNearbyDevices();
  90. Log("正在扫描设备");
  91. }
  92. catch (Exception e)
  93. {
  94. Debug.Log(e.Message);
  95. canConnect = true;
  96. Log("请打开蓝牙");
  97. }
  98. }
  99. void OpenReceiveData()
  100. {
  101. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
  102. ch.setService(bluetoothService.getName());
  103. bluetoothHelper.WriteCharacteristic(ch, "3");
  104. // bluetoothHelper.WriteCharacteristic(ch, "5");
  105. Log("开始接收信息\n" + deviceName);
  106. }
  107. void Log(string text)
  108. {
  109. if (textUI != null)
  110. {
  111. textUI.text = text;
  112. }
  113. }
  114. }