BTManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 += () => {
  20. // //this is called when you receive data FROM your arduino
  21. // string receivedData;
  22. // receivedData = BTHelper.Read(); // returns a string
  23. // //since you are sending an array, convert the string to array :
  24. // char[] data = receivedData.ToCharArray();
  25. // //do Whatever you want
  26. // };
  27. BTHelper.OnDataReceived += (helper) => {
  28. try{
  29. string xx = helper.Read();
  30. char[] data = xx.ToCharArray();
  31. if(data.Length != 3)
  32. {
  33. return;
  34. }
  35. int i = 0;
  36. if(data[0] != 'S')
  37. return;
  38. if(data[1] == 'E')
  39. i=1;
  40. if(data[2] > 7)
  41. return;
  42. spheres[data[2]-2].GetComponent<Renderer>().material = materials[i];
  43. }catch(Exception ex)
  44. {
  45. x += ex.Message;
  46. }
  47. };
  48. }catch(Exception ex){
  49. Debug.Log(ex);
  50. x = ex.ToString();
  51. }
  52. }
  53. void OnBluetoothConnected(BluetoothHelper helper)
  54. {
  55. try{
  56. helper.StartListening();
  57. StartCoroutine(blinkLED());
  58. }catch (Exception ex){
  59. x += ex.ToString();
  60. Debug.Log(ex.Message);
  61. }
  62. }
  63. IEnumerator blinkLED()
  64. {
  65. byte[] turn_on = new byte[]{(byte)'E' /*E stands for enable */, 2};
  66. byte[] turn_off = new byte[]{(byte)'D' /*D stands for disable */, 2};
  67. x += BTHelper.isConnected().ToString();
  68. while(BTHelper.isConnected())
  69. {
  70. Debug.Log("ON");
  71. for(byte i=2; i<8; i++)
  72. {
  73. turn_on[1] = i;
  74. try{
  75. BTHelper.SendData(turn_on);
  76. }catch(Exception){}
  77. yield return new WaitForSeconds(0.3f);
  78. }
  79. Debug.Log("OFF");
  80. for(byte i=2; i<8; i++)
  81. {
  82. turn_off[1] = i;
  83. try{
  84. BTHelper.SendData(turn_off);
  85. }catch(Exception){}
  86. yield return new WaitForSeconds(0.3f);
  87. }
  88. }
  89. }
  90. void OnGUI()
  91. {
  92. if(BTHelper == null)
  93. return;
  94. BTHelper.DrawGUI();
  95. if(!BTHelper.isConnected())
  96. if(GUI.Button(new Rect(Screen.width/2-Screen.width/10, Screen.height/10, Screen.width/5, Screen.height/10), "Connect"))
  97. {
  98. if(BTHelper.isDevicePaired())
  99. BTHelper.Connect (); // tries to connect
  100. }
  101. if(BTHelper.isConnected())
  102. if(GUI.Button(new Rect(Screen.width/2-Screen.width/10, Screen.height - 2*Screen.height/10, Screen.width/5, Screen.height/10), "Disconnect"))
  103. {
  104. BTHelper.Disconnect ();
  105. }
  106. }
  107. void OnDestroy()
  108. {
  109. if(BTHelper!=null)
  110. BTHelper.Disconnect();
  111. }
  112. }