BLEServicesManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. public class BLEServicesManager : MonoBehaviour
  7. {
  8. private BluetoothHelper bluetoothHelper;
  9. private float timer;
  10. void Start()
  11. {
  12. timer = 0;
  13. try{
  14. Debug.Log("HI");
  15. BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology
  16. bluetoothHelper = BluetoothHelper.GetInstance();
  17. bluetoothHelper.OnConnected += (helper) => {
  18. List<BluetoothHelperService> services = helper.getGattServices();
  19. foreach (BluetoothHelperService s in services)
  20. {
  21. Debug.Log("Service : " + s.getName());
  22. foreach (BluetoothHelperCharacteristic item in s.getCharacteristics())
  23. {
  24. Debug.Log(item.getName());
  25. }
  26. }
  27. Debug.Log("Connected");
  28. BluetoothHelperCharacteristic c = new BluetoothHelperCharacteristic("ffe1");
  29. c.setService("ffe0");
  30. bluetoothHelper.Subscribe(c);
  31. //sendData();
  32. };
  33. bluetoothHelper.OnConnectionFailed += (helper)=>{
  34. Debug.Log("Connection failed");
  35. };
  36. bluetoothHelper.OnScanEnded += OnScanEnded;
  37. bluetoothHelper.OnServiceNotFound += (helper, serviceName) =>
  38. {
  39. Debug.Log(serviceName);
  40. };
  41. bluetoothHelper.OnCharacteristicNotFound += (helper, serviceName, characteristicName) =>
  42. {
  43. Debug.Log(characteristicName);
  44. };
  45. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  46. {
  47. Debug.Log(characteristic.getName());
  48. Debug.Log(value[0]);
  49. };
  50. // BluetoothHelperService service = new BluetoothHelperService("FFE0");
  51. // service.addCharacteristic(new BluetoothHelperCharacteristic("FFE1"));
  52. // BluetoothHelperService service2 = new BluetoothHelperService("180A");
  53. // service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
  54. // bluetoothHelper.Subscribe(service);
  55. // bluetoothHelper.Subscribe(service2);
  56. // bluetoothHelper.ScanNearbyDevices();
  57. // BluetoothHelperService service = new BluetoothHelperService("19B10000-E8F2-537E-4F6C-D104768A1214");
  58. // service.addCharacteristic(new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214"));
  59. //BluetoothHelperService service2 = new BluetoothHelperService("180A");
  60. //service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
  61. // bluetoothHelper.Subscribe(service);
  62. //bluetoothHelper.Subscribe(service2);
  63. bluetoothHelper.ScanNearbyDevices();
  64. }catch(Exception ex){
  65. Debug.Log(ex.StackTrace);
  66. }
  67. }
  68. private void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> devices){
  69. Debug.Log("FOund " + devices.Count);
  70. if(devices.Count == 0){
  71. bluetoothHelper.ScanNearbyDevices();
  72. return;
  73. }
  74. foreach(var d in devices)
  75. {
  76. Debug.Log(d.DeviceName);
  77. }
  78. try
  79. {
  80. bluetoothHelper.setDeviceName("HC-08");
  81. bluetoothHelper.Connect();
  82. Debug.Log("Connecting");
  83. }catch(Exception ex)
  84. {
  85. bluetoothHelper.ScanNearbyDevices();
  86. Debug.Log(ex.Message);
  87. }
  88. }
  89. void OnDestroy()
  90. {
  91. if (bluetoothHelper != null)
  92. bluetoothHelper.Disconnect();
  93. }
  94. void Update(){
  95. if(bluetoothHelper == null)
  96. return;
  97. if(!bluetoothHelper.isConnected())
  98. return;
  99. timer += Time.deltaTime;
  100. if(timer < 5)
  101. return;
  102. timer = 0;
  103. sendData();
  104. }
  105. void sendData(){
  106. // Debug.Log("Sending");
  107. // BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("FFE1");
  108. // ch.setService("FFE0"); //this line is mandatory!!!
  109. // bluetoothHelper.WriteCharacteristic(ch, new byte[]{0x44, 0x55, 0xff});
  110. Debug.Log("Sending");
  111. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214");
  112. ch.setService("19B10000-E8F2-537E-4F6C-D104768A1214"); //this line is mandatory!!!
  113. bluetoothHelper.WriteCharacteristic(ch, "10001000"); //string: 10001000 is this binary? no, as string.
  114. }
  115. void read(){
  116. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("2A24");
  117. ch.setService("180A");//this line is mandatory!!!
  118. bluetoothHelper.ReadCharacteristic(ch);
  119. //Debug.Log(System.Text.Encoding.ASCII.GetString(x));
  120. }
  121. }