ScanScene2.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. using UnityEngine.UI;
  7. public class ScanScene2 : MonoBehaviour
  8. {
  9. // Use this for initialization
  10. BluetoothHelper bluetoothHelper;
  11. BluetoothHelper bluetoothHelper2;
  12. public Text text;
  13. public GameObject sphere;
  14. string received_message;
  15. void Start()
  16. {
  17. try
  18. {
  19. BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology
  20. bluetoothHelper = BluetoothHelper.GetInstance();
  21. bluetoothHelper.OnConnected += OnConnected;
  22. bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
  23. bluetoothHelper.OnDataReceived += OnMessageReceived; //read the data
  24. bluetoothHelper.OnScanEnded += OnScanEnded;
  25. bluetoothHelper.setTerminatorBasedStream("\n");
  26. bluetoothHelper2 = BluetoothHelper.GetNewInstance();
  27. bluetoothHelper2.OnConnected += OnConnected;
  28. bluetoothHelper2.OnConnectionFailed += OnConnectionFailed;
  29. bluetoothHelper2.OnScanEnded += OnScanEnded2;
  30. bluetoothHelper2.OnCharacteristicChanged += (helper, value, characteristic) =>
  31. {
  32. Debug.Log(characteristic.getName());
  33. Debug.Log(System.Text.Encoding.ASCII.GetString(value));
  34. };
  35. BluetoothHelperService service = new BluetoothHelperService("180D");
  36. service.addCharacteristic(new BluetoothHelperCharacteristic("2A37"));
  37. service.addCharacteristic(new BluetoothHelperCharacteristic("2A38"));
  38. service.addCharacteristic(new BluetoothHelperCharacteristic("2A39"));
  39. bluetoothHelper2.Subscribe(service);
  40. bluetoothHelper.ScanNearbyDevices();
  41. text.text = "start scan";
  42. }
  43. catch (BluetoothHelper.BlueToothNotEnabledException ex)
  44. {
  45. sphere.GetComponent<Renderer>().material.color = Color.yellow;
  46. Debug.Log(ex.ToString());
  47. text.text = ex.Message;
  48. }
  49. }
  50. IEnumerator blinkSphere()
  51. {
  52. sphere.GetComponent<Renderer>().material.color = Color.cyan;
  53. yield return new WaitForSeconds(0.5f);
  54. sphere.GetComponent<Renderer>().material.color = Color.green;
  55. }
  56. //Asynchronous method to receive messages
  57. void OnMessageReceived(BluetoothHelper helper)
  58. {
  59. //StartCoroutine(blinkSphere());
  60. received_message = helper.Read();
  61. text.text = received_message;
  62. Debug.Log(System.DateTime.Now.Second);
  63. //Debug.Log(received_message);
  64. }
  65. void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices)
  66. {
  67. Debug.Log("1 ended");
  68. if (nearbyDevices.Count == 0)
  69. {
  70. helper.ScanNearbyDevices();
  71. return;
  72. }
  73. foreach (BluetoothDevice device in nearbyDevices)
  74. {
  75. if (device.DeviceName == "HC-08")
  76. Debug.Log("FOUND!!");
  77. }
  78. text.text = "HC-08";
  79. bluetoothHelper2.setDeviceName("HC-08");
  80. // bluetoothHelper.setDeviceAddress("00:21:13:02:16:B1");
  81. bluetoothHelper2.Connect();
  82. bluetoothHelper2.isDevicePaired();
  83. }
  84. void OnScanEnded2(BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices)
  85. {
  86. Debug.Log("2 ended " + nearbyDevices.Count);
  87. if (nearbyDevices.Count == 0)
  88. {
  89. helper.ScanNearbyDevices();
  90. return;
  91. }
  92. foreach (BluetoothDevice device in nearbyDevices)
  93. {
  94. Debug.Log(device.DeviceName);
  95. if (device.DeviceName == "HUAWEI Y7 Prime 2018")
  96. Debug.Log("FOUND!!");
  97. }
  98. helper.setDeviceName("HUAWEI Y7 Prime 2018");
  99. helper.Connect();
  100. }
  101. void Update()
  102. {
  103. //Debug.Log(bluetoothHelper.IsBluetoothEnabled());
  104. if (!bluetoothHelper.IsBluetoothEnabled())
  105. {
  106. bluetoothHelper.EnableBluetooth(true);
  107. }
  108. }
  109. void OnConnected(BluetoothHelper helper)
  110. {
  111. sphere.GetComponent<Renderer>().material.color = Color.green;
  112. try
  113. {
  114. helper.StartListening();
  115. if(helper.getId() == bluetoothHelper.getId()) //1st instance connected, connect the second
  116. bluetoothHelper2.ScanNearbyDevices();
  117. }
  118. catch (Exception ex)
  119. {
  120. Debug.Log(ex.Message);
  121. }
  122. }
  123. void OnConnectionFailed(BluetoothHelper helper)
  124. {
  125. sphere.GetComponent<Renderer>().material.color = Color.red;
  126. Debug.Log("Connection Failed");
  127. }
  128. //Call this function to emulate message receiving from bluetooth while debugging on your PC.
  129. void OnGUI()
  130. {
  131. if (bluetoothHelper != null)
  132. bluetoothHelper.DrawGUI();
  133. else
  134. return;
  135. if (bluetoothHelper.isConnected())
  136. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
  137. {
  138. bluetoothHelper.Disconnect();
  139. sphere.GetComponent<Renderer>().material.color = Color.blue;
  140. }
  141. if (bluetoothHelper.isConnected())
  142. if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send text"))
  143. {
  144. //bluetoothHelper.SendData(new byte[] { 0, 1, 2, 3, 4 });
  145. bluetoothHelper.SendData("Hi From unity");
  146. }
  147. }
  148. void OnDestroy()
  149. {
  150. if (bluetoothHelper != null)
  151. bluetoothHelper.Disconnect();
  152. if (bluetoothHelper2 != null)
  153. bluetoothHelper2.Disconnect();
  154. }
  155. }