BLEServicesManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ArduinoBluetoothAPI;
  5. using System;
  6. #if UNITY_ANDROID
  7. using UnityEngine.Android;
  8. #endif
  9. public class BLEServicesManager : MonoBehaviour
  10. {
  11. private BluetoothHelper bluetoothHelper;
  12. private float timer;
  13. void Start()
  14. {
  15. #if UNITY_ANDROID
  16. var callbacks = new PermissionCallbacks();
  17. callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
  18. callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
  19. callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
  20. if (!Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_SCAN") || !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_ADVERTISE") || !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_CONNECT"))
  21. Permission.RequestUserPermissions(new string[] { "android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_ADVERTISE", "android.permission.BLUETOOTH_CONNECT" }, callbacks);
  22. #else
  23. setupBluetooth();
  24. #endif
  25. timer = 0;
  26. }
  27. private void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> devices){
  28. Debug.Log("FOund " + devices.Count);
  29. if(devices.Count == 0){
  30. bluetoothHelper.ScanNearbyDevices();
  31. return;
  32. }
  33. foreach(var d in devices)
  34. {
  35. Debug.Log(d.DeviceName);
  36. }
  37. try
  38. {
  39. bluetoothHelper.setDeviceName("HC-08");
  40. bluetoothHelper.Connect();
  41. Debug.Log("Connecting");
  42. }catch(Exception ex)
  43. {
  44. bluetoothHelper.ScanNearbyDevices();
  45. Debug.Log(ex.Message);
  46. }
  47. }
  48. void OnDestroy()
  49. {
  50. if (bluetoothHelper != null)
  51. bluetoothHelper.Disconnect();
  52. }
  53. void Update(){
  54. if(bluetoothHelper == null)
  55. return;
  56. if(!bluetoothHelper.isConnected())
  57. return;
  58. timer += Time.deltaTime;
  59. if(timer < 5)
  60. return;
  61. timer = 0;
  62. sendData();
  63. }
  64. void sendData(){
  65. // Debug.Log("Sending");
  66. // BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("FFE1");
  67. // ch.setService("FFE0"); //this line is mandatory!!!
  68. // bluetoothHelper.WriteCharacteristic(ch, new byte[]{0x44, 0x55, 0xff});
  69. Debug.Log("Sending");
  70. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214");
  71. ch.setService("19B10000-E8F2-537E-4F6C-D104768A1214"); //this line is mandatory!!!
  72. bluetoothHelper.WriteCharacteristic(ch, "1234");
  73. }
  74. void read(){
  75. BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("2A24");
  76. ch.setService("180A");//this line is mandatory!!!
  77. bluetoothHelper.ReadCharacteristic(ch);
  78. }
  79. void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string permissionName)
  80. {
  81. Debug.Log($"{permissionName} PermissionDeniedAndDontAskAgain");
  82. }
  83. void PermissionCallbacks_PermissionDenied(string permissionName)
  84. {
  85. Debug.Log($"{permissionName} PermissionCallbacks_PermissionDenied");
  86. }
  87. void PermissionCallbacks_PermissionGranted(string permissionName)
  88. {
  89. Debug.Log($"{permissionName} PermissionCallbacks_PermissionGranted");
  90. setupBluetooth();
  91. }
  92. void setupBluetooth()
  93. {
  94. try
  95. {
  96. if (bluetoothHelper != null)
  97. return;
  98. Debug.Log("HI");
  99. BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology
  100. bluetoothHelper = BluetoothHelper.GetInstance();
  101. bluetoothHelper.OnConnected += (helper) =>
  102. {
  103. List<BluetoothHelperService> services = helper.getGattServices();
  104. foreach (BluetoothHelperService s in services)
  105. {
  106. Debug.Log("Service : " + s.getName());
  107. foreach (BluetoothHelperCharacteristic item in s.getCharacteristics())
  108. {
  109. Debug.Log(item.getName());
  110. }
  111. }
  112. Debug.Log("Connected");
  113. BluetoothHelperCharacteristic c = new BluetoothHelperCharacteristic("ffe1");
  114. c.setService("ffe0");
  115. bluetoothHelper.Subscribe(c);
  116. //sendData();
  117. };
  118. bluetoothHelper.OnConnectionFailed += (helper) =>
  119. {
  120. Debug.Log("Connection failed");
  121. };
  122. bluetoothHelper.OnScanEnded += OnScanEnded;
  123. bluetoothHelper.OnServiceNotFound += (helper, serviceName) =>
  124. {
  125. Debug.Log(serviceName);
  126. };
  127. bluetoothHelper.OnCharacteristicNotFound += (helper, serviceName, characteristicName) =>
  128. {
  129. Debug.Log(characteristicName);
  130. };
  131. bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
  132. {
  133. Debug.Log(characteristic.getName());
  134. Debug.Log(value[0]);
  135. };
  136. // BluetoothHelperService service = new BluetoothHelperService("FFE0");
  137. // service.addCharacteristic(new BluetoothHelperCharacteristic("FFE1"));
  138. // BluetoothHelperService service2 = new BluetoothHelperService("180A");
  139. // service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
  140. // bluetoothHelper.Subscribe(service);
  141. // bluetoothHelper.Subscribe(service2);
  142. // bluetoothHelper.ScanNearbyDevices();
  143. // BluetoothHelperService service = new BluetoothHelperService("19B10000-E8F2-537E-4F6C-D104768A1214");
  144. // service.addCharacteristic(new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214"));
  145. //BluetoothHelperService service2 = new BluetoothHelperService("180A");
  146. //service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
  147. // bluetoothHelper.Subscribe(service);
  148. //bluetoothHelper.Subscribe(service2);
  149. bluetoothHelper.ScanNearbyDevices();
  150. }
  151. catch (Exception ex)
  152. {
  153. Debug.Log(ex.StackTrace);
  154. }
  155. }
  156. }