Subscribing to multiple characteristics
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;
}
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).
I have a similar use case and am curious if this was solved? Would also be curious for the script to subscribe to those multiple characteristics that was used.
Hello @ebubar
I believe the cause is rooted from the fact that BLE is a pretty fragile piece of work, where doing too many actions at the same time will cause it to ignore/reject any new actions before the old one is finished. At least, that's what I encountered when reworking the package.
Since in the old package, no solution or callback is given to see if the subscribe went through properly, I'd replace the sequence of subscribe commands with a Unity Coroutine and space them out with 0.25 seconds inbetween. I believe that'll be enough time for BLE to persist the notification indicator to the descriptors of the characteristics.
In the mean time I'll try to look for a better fitted solution.