Velorexe / Unity-Android-Bluetooth-Low-Energy

A Unity Android plugin to support basic Bluetooth Low Energy interactions.
The Unlicense
101 stars 21 forks source link

Subscribing to multiple characteristics #30

Closed lucapauri closed 7 months ago

lucapauri commented 1 year ago

Hi, I'm trying to use your asset and it works perfectly. The only issue is that I want to subscribe to multiple characteristics but I only get the notifications for the first one. Is there any way I can solve this? Thanks in advance!

This is my code:

using UnityEngine;
using Android.BLE;
using Android.BLE.Commands;
using UnityEngine.Android;
using System.Text;

public class BleCommunication : MonoBehaviour
{
    private SubscribeToCharacteristic subscribePitch;
    private SubscribeToCharacteristic subscribeYaw;
    private SubscribeToCharacteristic subscribeRoll;
    private SubscribeToCharacteristic subscribePosX;
    private SubscribeToCharacteristic subscribePosY;
    private SubscribeToCharacteristic subscribePosZ;
    private float pitch;
    private float yaw;
    private float roll;
    private float posX;
    private float posY;
    private float posZ;

    private bool connected = false;
    private bool subscribed = false;

    [SerializeField]
    private int _scanTime = 1000000;

    private float _scanTimer = 0f;

    private bool _isScanning = false;

    public void Start()
    {
        if (!_isScanning)
        {
            _isScanning = true;
            BleManager.Instance.QueueCommand(new DiscoverDevices(OnDeviceFound, _scanTime * 1000));
        }
    }

    private void Update()
    {
        if (_isScanning)
        {
            Debug.Log("Scanning");
            _scanTimer += Time.deltaTime;
            if (_scanTimer > _scanTime)
            {
                _scanTimer = 0f;
                _isScanning = false;
            }
        }
    }

    private void OnDeviceFound(string name, string device)
    {
        if(name == "05:C0:CB:A6:2F:77")
        {
            _isScanning = false;
            BleManager.Instance.QueueCommand(new ConnectToDevice("05:C0:CB:A6:2F:77", OnConnected));
        }
    }

    private void OnConnected(string uid)
    {
        connected = true;
        Debug.Log("Connected");
        Subscribe();
    }

    private void Subscribe()
    {
        subscribePitch = new SubscribeToCharacteristic("05:C0:CB:A6:2F:77", "181a", "2a5d", (byte[] value) =>
        {
            pitch = System.BitConverter.ToSingle(value, 0);
            Debug.Log("Pitch: " + pitch);
        });
        subscribeYaw = new SubscribeToCharacteristic("05:C0:CB:A6:2F:77", "181a", "2a5c", (byte[] value) =>
        {
            yaw = System.BitConverter.ToSingle(value, 0);
            Debug.Log("Yaw: " + yaw);
        });
        subscribeRoll = new SubscribeToCharacteristic("05:C0:CB:A6:2F:77", "181a", "2a5b", (byte[] value) =>
        {
            roll = System.BitConverter.ToSingle(value, 0);
            Debug.Log("Roll: " + roll);
        });
        subscribePosX = new SubscribeToCharacteristic("05:C0:CB:A6:2F:77", "181a", "2a60", (byte[] value) =>
        {
            posX = System.BitConverter.ToSingle(value, 0);
            Debug.Log("PosX: " + posX);
        });
        subscribePosY = new SubscribeToCharacteristic("05:C0:CB:A6:2F:77", "181a", "2a61", (byte[] value) =>
        {
            posY = System.BitConverter.ToSingle(value, 0);
            Debug.Log("PosY: " + posY);
        });
        subscribePosZ = new SubscribeToCharacteristic("05:C0:CB:A6:2F:77", "181a", "2a62", (byte[] value) =>
        {
            posZ = System.BitConverter.ToSingle(value, 0);
            Debug.Log("PosZ: " + posZ);
        });
        BleManager.Instance.QueueCommand(subscribeYaw);
        BleManager.Instance.QueueCommand(subscribePitch);
        BleManager.Instance.QueueCommand(subscribeRoll);
        BleManager.Instance.QueueCommand(subscribePosX);
        BleManager.Instance.QueueCommand(subscribePosY);
        BleManager.Instance.QueueCommand(subscribePosZ);
        subscribed = true;
    }
Velorexe commented 1 year ago

Hi @lucapauri ,

That's an interesting one, I'm not completely sure. I myself haven't tested connecting to multiple Characteristics before, so it might be an oversight that I've missed. If you go into LogCat, does it show that it receives values from the other Characteristics? Or is it limited to just the first one?

From there I can determine if it's a problem with the Java library or if Unity is picking the values up in the wrong way (or just not passing them to the other commands).