ScanSceneManager.cs 6.3 KB

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