ScanSceneManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. using UnityEngine.UI;
  7. using System.Runtime.InteropServices;
  8. public class ScanSceneManager : MonoBehaviour
  9. {
  10. // Use this for initialization
  11. BluetoothHelper bluetoothHelper;
  12. string deviceName = "HC-06";
  13. public Text text;
  14. public GameObject sphere;
  15. string received_message;
  16. void Start()
  17. {
  18. try
  19. {
  20. BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology
  21. bluetoothHelper = BluetoothHelper.GetInstance();
  22. bluetoothHelper.OnConnected += OnConnected;
  23. bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
  24. bluetoothHelper.OnDataReceived += OnMessageReceived; //read the data
  25. bluetoothHelper.OnScanEnded += OnScanEnded;
  26. //FOR CUSTOM UUID with BLE
  27. //BluetoothHelperCharacteristic characteristic = new BluetoothHelperCharacteristic("beb5483e-36e1-4688-b7f5-ea07361b26a8");
  28. //characteristic.setService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
  29. //bluetoothHelper.setTxCharacteristic(characteristic);
  30. //bluetoothHelper.setRxCharacteristic(characteristic);
  31. bluetoothHelper.setTerminatorBasedStream("\n");
  32. //bluetoothHelper.setLengthBasedStream();
  33. //bluetoothHelper.setFixedLengthBasedStream(10);
  34. // if(bluetoothHelper.isDevicePaired())
  35. // sphere.GetComponent<Renderer>().material.color = Color.blue;
  36. // else
  37. // sphere.GetComponent<Renderer>().material.color = Color.grey;
  38. // bluetoothHelper.ScanNearbyDevices();
  39. if (!bluetoothHelper.ScanNearbyDevices())
  40. {
  41. //text.text = "cannot start scan";
  42. sphere.GetComponent<Renderer>().material.color = Color.black;
  43. // bluetoothHelper.setDeviceAddress("00:21:13:02:16:B1");
  44. bluetoothHelper.setDeviceName(deviceName);
  45. bluetoothHelper.Connect();
  46. }
  47. else
  48. {
  49. text.text = "start scan";
  50. // sphere.GetComponent<Renderer>().material.color = Color.green;
  51. }
  52. }
  53. catch (BluetoothHelper.BlueToothNotEnabledException ex)
  54. {
  55. sphere.GetComponent<Renderer>().material.color = Color.yellow;
  56. Debug.Log(ex.ToString());
  57. text.text = ex.Message;
  58. }
  59. }
  60. IEnumerator blinkSphere()
  61. {
  62. sphere.GetComponent<Renderer>().material.color = Color.cyan;
  63. yield return new WaitForSeconds(0.5f);
  64. sphere.GetComponent<Renderer>().material.color = Color.green;
  65. }
  66. //Asynchronous method to receive messages
  67. void OnMessageReceived(BluetoothHelper helper)
  68. {
  69. //StartCoroutine(blinkSphere());
  70. received_message = helper.Read();
  71. text.text = received_message;
  72. Debug.Log(System.DateTime.Now.Second);
  73. //Debug.Log(received_message);
  74. }
  75. void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices)
  76. {
  77. text.text = "Found " + nearbyDevices.Count + " devices";
  78. if (nearbyDevices.Count == 0){
  79. helper.ScanNearbyDevices();
  80. return;
  81. }
  82. foreach(BluetoothDevice device in nearbyDevices)
  83. {
  84. if(device.DeviceName == deviceName)
  85. Debug.Log("FOUND!!");
  86. }
  87. text.text = deviceName;
  88. bluetoothHelper.setDeviceName(deviceName);
  89. // bluetoothHelper.setDeviceAddress("00:21:13:02:16:B1");
  90. bluetoothHelper.Connect();
  91. bluetoothHelper.isDevicePaired();
  92. }
  93. void Update()
  94. {
  95. //Debug.Log(bluetoothHelper.IsBluetoothEnabled());
  96. if (!bluetoothHelper.IsBluetoothEnabled())
  97. {
  98. bluetoothHelper.EnableBluetooth(true);
  99. }
  100. }
  101. void OnConnected(BluetoothHelper helper)
  102. {
  103. sphere.GetComponent<Renderer>().material.color = Color.green;
  104. try
  105. {
  106. List<BluetoothHelperService> services = helper.getGattServices();
  107. foreach(BluetoothHelperService s in services)
  108. {
  109. Debug.Log("Service : " + s.getName());
  110. foreach (BluetoothHelperCharacteristic item in s.getCharacteristics())
  111. {
  112. Debug.Log(item.getName());
  113. }
  114. }
  115. helper.StartListening();
  116. }
  117. catch (Exception ex)
  118. {
  119. Debug.Log(ex.Message);
  120. }
  121. }
  122. void OnConnectionFailed(BluetoothHelper helper)
  123. {
  124. sphere.GetComponent<Renderer>().material.color = Color.red;
  125. Debug.Log("Connection Failed");
  126. }
  127. //Call this function to emulate message receiving from bluetooth while debugging on your PC.
  128. void OnGUI()
  129. {
  130. if (bluetoothHelper != null)
  131. bluetoothHelper.DrawGUI();
  132. else
  133. return;
  134. // if(!bluetoothHelper.isConnected())
  135. // if(GUI.Button(new Rect(Screen.width/2-Screen.width/10, Screen.height/10, Screen.width/5, Screen.height/10), "Connect"))
  136. // {
  137. // if(bluetoothHelper.isDeviceFound())
  138. // bluetoothHelper.Connect (); // tries to connect
  139. // else
  140. // sphere.GetComponent<Renderer>().material.color = Color.magenta;
  141. // }
  142. if (bluetoothHelper.isConnected())
  143. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
  144. {
  145. bluetoothHelper.Disconnect();
  146. sphere.GetComponent<Renderer>().material.color = Color.blue;
  147. }
  148. if (bluetoothHelper.isConnected())
  149. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send text"))
  150. {
  151. bluetoothHelper.SendData(new byte[] { 0, 1, 2, 3, 4 });
  152. //bluetoothHelper.SendData("This is a very long long long long text");
  153. }
  154. }
  155. void OnDestroy()
  156. {
  157. if (bluetoothHelper != null)
  158. bluetoothHelper.Disconnect();
  159. }
  160. }