Demo.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. public class Demo : MonoBehaviour
  7. {
  8. private BluetoothHelper helper;
  9. private bool isScanning;
  10. private bool isConnecting;
  11. private string data;
  12. private string tmp;
  13. private LinkedList<BluetoothDevice> devices;
  14. // Start is called before the first frame update
  15. void Start()
  16. {
  17. data = "";
  18. tmp = "";
  19. try{
  20. BluetoothHelper.BLE = false;
  21. helper = BluetoothHelper.GetInstance();
  22. helper.OnConnected += OnConnected;
  23. helper.OnConnectionFailed += OnConnectionFailed;
  24. helper.OnScanEnded += OnScanEnded;
  25. helper.OnDataReceived += OnDataReceived;
  26. helper.setCustomStreamManager(new MyStreamManager()); //implement your own way of delimiting the messages
  27. //helper.setTerminatorBasedStream("\n"); //every messages ends with new line character
  28. }catch(Exception e){
  29. Debug.LogError(e);
  30. }
  31. }
  32. void OnDataReceived(BluetoothHelper helper){
  33. data += "\n<" + helper.Read();
  34. }
  35. void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> devices){
  36. this.isScanning = false;
  37. this.devices = devices;
  38. }
  39. void OnConnected(BluetoothHelper helper){
  40. isConnecting=false;
  41. helper.StartListening();
  42. }
  43. void OnConnectionFailed(BluetoothHelper helper){
  44. isConnecting = false;
  45. Debug.Log("Connection lost");
  46. }
  47. void OnGUI(){
  48. if(helper == null)
  49. return;
  50. if(!helper.isConnected() && !isScanning && !isConnecting){
  51. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Start Scanning")){
  52. isScanning = helper.ScanNearbyDevices();
  53. }
  54. if(devices != null && devices.First != null) {
  55. draw();
  56. }
  57. }else if(!helper.isConnected() && isScanning){
  58. GUI.TextArea(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Scanning...");
  59. }else if(helper.isConnected()){
  60. GUI.TextArea(new Rect(Screen.width / 4, 2 * Screen.height / 10, Screen.width / 2, 7*Screen.height / 10), data);
  61. tmp = GUI.TextField(new Rect(Screen.width / 4, Screen.height / 10, Screen.width / 2, Screen.height / 10 - 10), tmp);
  62. if (GUI.Button(new Rect( 3 * Screen.width / 4 + 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send"))
  63. {
  64. helper.SendData(tmp);
  65. data += "\n>"+tmp;
  66. tmp = "";
  67. }
  68. if (GUI.Button(new Rect(3 * Screen.width / 4 + 10, 8 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
  69. {
  70. helper.Disconnect();
  71. }
  72. }
  73. }
  74. private void draw(){
  75. LinkedListNode<BluetoothDevice> node = devices.First;
  76. for (int i = 0; i < 9; i++)
  77. {
  78. for (int j = 0; j < 3; j++)
  79. {
  80. string bluetoothName = node.Value.DeviceName;
  81. if (GUI.Button(new Rect((j + 1) * Screen.width / 5 + 5, (i + 2) * Screen.height / 10 + 5, Screen.width / 5 - 10, Screen.height / 10 - 10), bluetoothName + " - " + node.Value.Rssi))
  82. {
  83. helper.setDeviceName(bluetoothName);
  84. try{
  85. helper.Connect();
  86. isConnecting = true;
  87. }catch(Exception){
  88. isConnecting = false;
  89. }
  90. }
  91. node = node.Next;
  92. if (node == null)
  93. return;
  94. }
  95. }
  96. }
  97. void OnDestroy(){
  98. if(helper != null)
  99. helper.Disconnect();
  100. }
  101. }