BTManager.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. public class BTManager : MonoBehaviour {
  7. // Use this for initialization
  8. string message;
  9. private BluetoothHelper BTHelper;
  10. public GameObject[] spheres;
  11. public Material[] materials;
  12. private string x;
  13. void Start () {
  14. try{
  15. x="";
  16. BTHelper = BluetoothHelper.GetInstance("HC-05");
  17. BTHelper.setLengthBasedStream();
  18. BTHelper.OnConnected += OnBluetoothConnected; //OnBluetoothConnected is a function defined later on
  19. BTHelper.OnDataReceived += (helper) => {
  20. try{
  21. string xx = helper.Read();
  22. char[] data = xx.ToCharArray();
  23. if(data.Length != 3)
  24. {
  25. return;
  26. }
  27. int i = 0;
  28. if(data[0] != 'S')
  29. return;
  30. if(data[1] == 'E')
  31. i=1;
  32. if(data[2] > 7)
  33. return;
  34. spheres[data[2]-2].GetComponent<Renderer>().material = materials[i];
  35. }catch(Exception ex)
  36. {
  37. x += ex.Message;
  38. }
  39. };
  40. }catch(Exception ex){
  41. Debug.Log(ex);
  42. x = ex.ToString();
  43. }
  44. }
  45. void OnBluetoothConnected(BluetoothHelper helper)
  46. {
  47. try{
  48. helper.StartListening();
  49. StartCoroutine(blinkLED());
  50. }catch (Exception ex){
  51. x += ex.ToString();
  52. Debug.Log(ex.Message);
  53. }
  54. }
  55. IEnumerator blinkLED()
  56. {
  57. byte[] turn_on = new byte[]{(byte)'E' /*E stands for enable */, 2};
  58. byte[] turn_off = new byte[]{(byte)'D' /*D stands for disable */, 2};
  59. x += BTHelper.isConnected().ToString();
  60. while(BTHelper.isConnected())
  61. {
  62. Debug.Log("ON");
  63. for(byte i=2; i<8; i++)
  64. {
  65. turn_on[1] = i;
  66. try{
  67. BTHelper.SendData(turn_on);
  68. }catch(Exception){}
  69. yield return new WaitForSeconds(0.3f);
  70. }
  71. Debug.Log("OFF");
  72. for(byte i=2; i<8; i++)
  73. {
  74. turn_off[1] = i;
  75. try{
  76. BTHelper.SendData(turn_off);
  77. }catch(Exception){}
  78. yield return new WaitForSeconds(0.3f);
  79. }
  80. }
  81. }
  82. void OnGUI()
  83. {
  84. if(BTHelper == null)
  85. return;
  86. BTHelper.DrawGUI();
  87. if(!BTHelper.isConnected())
  88. if(GUI.Button(new Rect(Screen.width/2-Screen.width/10, Screen.height/10, Screen.width/5, Screen.height/10), "Connect"))
  89. {
  90. if(BTHelper.isDevicePaired())
  91. BTHelper.Connect (); // tries to connect
  92. }
  93. if(BTHelper.isConnected())
  94. if(GUI.Button(new Rect(Screen.width/2-Screen.width/10, Screen.height - 2*Screen.height/10, Screen.width/5, Screen.height/10), "Disconnect"))
  95. {
  96. BTHelper.Disconnect ();
  97. }
  98. }
  99. void OnDestroy()
  100. {
  101. if(BTHelper!=null)
  102. BTHelper.Disconnect();
  103. }
  104. }