LedOnOff.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. using System.Text;
  7. public class LedOnOff : MonoBehaviour
  8. {
  9. // Use this for initialization
  10. BluetoothHelper bluetoothHelper;
  11. string deviceName;
  12. string received_message;
  13. private string tmp;
  14. void Start()
  15. {
  16. deviceName = "HC-05"; //bluetooth should be turned ON;
  17. try
  18. {
  19. BluetoothHelper.BLE = false;
  20. bluetoothHelper = BluetoothHelper.GetInstance(deviceName);
  21. bluetoothHelper.OnConnected += OnConnected;
  22. bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
  23. bluetoothHelper.OnDataReceived += OnMessageReceived; //read the data
  24. bluetoothHelper.OnScanEnded += OnScanEnded;
  25. bluetoothHelper.setTerminatorBasedStream("\n");
  26. if(!bluetoothHelper.ScanNearbyDevices())
  27. {
  28. //scan didnt start (on windows desktop (not UWP))
  29. //try to connect
  30. bluetoothHelper.Connect();//this will work only for bluetooth classic.
  31. //scanning is mandatory before connecting for BLE.
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. Debug.Log(ex.Message);
  37. write(ex.Message);
  38. }
  39. }
  40. private void write(string msg)
  41. {
  42. tmp += ">"+ msg + "\n";
  43. }
  44. void OnMessageReceived(BluetoothHelper helper)
  45. {
  46. received_message = helper.Read();
  47. Debug.Log(received_message);
  48. write("Received : " +received_message);
  49. }
  50. void OnConnected(BluetoothHelper helper)
  51. {
  52. try
  53. {
  54. helper.StartListening();
  55. }
  56. catch (Exception ex)
  57. {
  58. Debug.Log(ex.Message);
  59. write(ex.Message);
  60. }
  61. }
  62. void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> devices){
  63. if(helper.isDevicePaired()) //we did found our device (with BLE) or we already paired the device (for Bluetooth Classic)
  64. helper.Connect();
  65. else
  66. helper.ScanNearbyDevices(); //we didn't
  67. }
  68. void OnConnectionFailed(BluetoothHelper helper)
  69. {
  70. write("Connection Failed");
  71. Debug.Log("Connection Failed");
  72. }
  73. //Call this function to emulate message receiving from bluetooth while debugging on your PC.
  74. void OnGUI()
  75. {
  76. tmp = GUI.TextField(new Rect(Screen.width / 4, Screen.height / 10, Screen.width / 2, Screen.height / 10 - 10), tmp);
  77. if (bluetoothHelper != null)
  78. bluetoothHelper.DrawGUI();
  79. else
  80. return;
  81. if (!bluetoothHelper.isConnected())
  82. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Connect"))
  83. {
  84. if (bluetoothHelper.isDevicePaired())
  85. bluetoothHelper.Connect(); // tries to connect
  86. else
  87. write("Cannot connect, device is not found, for bluetooth classic, pair the device\n\tFor BLE scan for nearby devices");
  88. }
  89. if (bluetoothHelper.isConnected())
  90. {
  91. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
  92. {
  93. bluetoothHelper.Disconnect();
  94. write("Disconnected");
  95. }
  96. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Turn On"))
  97. {
  98. bluetoothHelper.SendData("O");
  99. write("Sending O");
  100. }
  101. if (GUI.Button(new Rect(Screen.width / 2 + Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Turn Off"))
  102. {
  103. bluetoothHelper.SendData("F");
  104. write("Sending F");
  105. }
  106. }
  107. }
  108. void OnDestroy()
  109. {
  110. if (bluetoothHelper != null)
  111. bluetoothHelper.Disconnect();
  112. }
  113. }