| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ArduinoBluetoothAPI;
- using System;
- #if UNITY_ANDROID
- using UnityEngine.Android;
- #endif
- public class BLEServicesManager : MonoBehaviour
- {
- private BluetoothHelper bluetoothHelper;
- private float timer;
- void Start()
- {
- #if UNITY_ANDROID
- var callbacks = new PermissionCallbacks();
- callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
- callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
- callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
- if (!Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_SCAN") || !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_ADVERTISE") || !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_CONNECT"))
- Permission.RequestUserPermissions(new string[] { "android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_ADVERTISE", "android.permission.BLUETOOTH_CONNECT" }, callbacks);
- #else
- setupBluetooth();
- #endif
- timer = 0;
-
- }
- private void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> devices){
- Debug.Log("FOund " + devices.Count);
- if(devices.Count == 0){
- bluetoothHelper.ScanNearbyDevices();
- return;
- }
- foreach(var d in devices)
- {
- Debug.Log(d.DeviceName);
- }
-
- try
- {
- bluetoothHelper.setDeviceName("HC-08");
- bluetoothHelper.Connect();
- Debug.Log("Connecting");
- }catch(Exception ex)
- {
- bluetoothHelper.ScanNearbyDevices();
- Debug.Log(ex.Message);
- }
- }
- void OnDestroy()
- {
- if (bluetoothHelper != null)
- bluetoothHelper.Disconnect();
- }
- void Update(){
- if(bluetoothHelper == null)
- return;
- if(!bluetoothHelper.isConnected())
- return;
- timer += Time.deltaTime;
- if(timer < 5)
- return;
- timer = 0;
- sendData();
- }
- void sendData(){
- // Debug.Log("Sending");
- // BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("FFE1");
- // ch.setService("FFE0"); //this line is mandatory!!!
- // bluetoothHelper.WriteCharacteristic(ch, new byte[]{0x44, 0x55, 0xff});
- Debug.Log("Sending");
- BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214");
- ch.setService("19B10000-E8F2-537E-4F6C-D104768A1214"); //this line is mandatory!!!
- bluetoothHelper.WriteCharacteristic(ch, "1234");
- }
- void read(){
- BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic("2A24");
- ch.setService("180A");//this line is mandatory!!!
- bluetoothHelper.ReadCharacteristic(ch);
- }
- void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string permissionName)
- {
- Debug.Log($"{permissionName} PermissionDeniedAndDontAskAgain");
- }
- void PermissionCallbacks_PermissionDenied(string permissionName)
- {
- Debug.Log($"{permissionName} PermissionCallbacks_PermissionDenied");
- }
- void PermissionCallbacks_PermissionGranted(string permissionName)
- {
- Debug.Log($"{permissionName} PermissionCallbacks_PermissionGranted");
- setupBluetooth();
- }
- void setupBluetooth()
- {
- try
- {
- if (bluetoothHelper != null)
- return;
- Debug.Log("HI");
- BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology
- bluetoothHelper = BluetoothHelper.GetInstance();
- bluetoothHelper.OnConnected += (helper) =>
- {
- List<BluetoothHelperService> services = helper.getGattServices();
- foreach (BluetoothHelperService s in services)
- {
- Debug.Log("Service : " + s.getName());
- foreach (BluetoothHelperCharacteristic item in s.getCharacteristics())
- {
- Debug.Log(item.getName());
- }
- }
- Debug.Log("Connected");
- BluetoothHelperCharacteristic c = new BluetoothHelperCharacteristic("ffe1");
- c.setService("ffe0");
- bluetoothHelper.Subscribe(c);
- //sendData();
- };
- bluetoothHelper.OnConnectionFailed += (helper) =>
- {
- Debug.Log("Connection failed");
- };
- bluetoothHelper.OnScanEnded += OnScanEnded;
- bluetoothHelper.OnServiceNotFound += (helper, serviceName) =>
- {
- Debug.Log(serviceName);
- };
- bluetoothHelper.OnCharacteristicNotFound += (helper, serviceName, characteristicName) =>
- {
- Debug.Log(characteristicName);
- };
- bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
- {
- Debug.Log(characteristic.getName());
- Debug.Log(value[0]);
- };
- // BluetoothHelperService service = new BluetoothHelperService("FFE0");
- // service.addCharacteristic(new BluetoothHelperCharacteristic("FFE1"));
- // BluetoothHelperService service2 = new BluetoothHelperService("180A");
- // service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
- // bluetoothHelper.Subscribe(service);
- // bluetoothHelper.Subscribe(service2);
- // bluetoothHelper.ScanNearbyDevices();
- // BluetoothHelperService service = new BluetoothHelperService("19B10000-E8F2-537E-4F6C-D104768A1214");
- // service.addCharacteristic(new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214"));
- //BluetoothHelperService service2 = new BluetoothHelperService("180A");
- //service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
- // bluetoothHelper.Subscribe(service);
- //bluetoothHelper.Subscribe(service2);
- bluetoothHelper.ScanNearbyDevices();
- }
- catch (Exception ex)
- {
- Debug.Log(ex.StackTrace);
- }
- }
- }
|