Unity-Android-Bluetooth-Low-Energy icon indicating copy to clipboard operation
Unity-Android-Bluetooth-Low-Energy copied to clipboard

Unable to re-scan after device is found

Open gepz opened this issue 1 year ago • 4 comments

I'm using the latest alpha version and noticed that if I call SearchForDevicesWithFilter and a device is found, the call to the function no longer works. Below is a simplified version of the code I ran. Could I ask you to do a quick check and note what I did wrong here if you see anything?

public class DeviceA: MonoBehaviour
{
    public string DeviceAddress;
    Coroutine scanningRoutine = null;

    public void StartScanning()
    {
        scanningRoutine = StartCoroutine(ScanForDevices());
    }

    IEnumerator ScanForDevices()
    {
        int scanDuration = 5;
        while (true)
        {
            BleManager.Instance.SearchForDevicesWithFilter(
                scanDuration * 1000,
                OnDeviceFound,
                deviceMac: DeviceAddress
            );
            yield return new WaitForSeconds(scanDuration + 0.1f);
        }
    }

    private void OnDeviceFound(BleDevice device)
    {
        // does nothing with the device and simply restart the scan
        StopCoroutine(scanningRoutine);
        scanningRoutine = null;
        StartScanning();
    }
}

gepz avatar Mar 28 '24 02:03 gepz

Heya,

I'll do a quick check as to why the alpha version is behaving this way, haven't encountered it myself yet, but I'll try finding out what is causing it.

Velorexe avatar Apr 23 '24 08:04 Velorexe

I'm having a similar problem: if the BLE connection is lost, neither scanning nor re-connecting work. Very simple function (debug prints, etc stripped):

        private void OnDisconnected(BleDevice device)
        {
            // unsubscribe if subscribed
            if (_txSubscribed)
            {
                _txCharacteristic.Unsubscribe();
                _txSubscribed = false;
            }
            // there is no callback so add a short delay
            DelaySec(0.1f);

            // restart
//            BleManager.Instance.SearchForDevicesWithFilter(10 * 1000, OnDeviceFound, OnSearchStop, serviceUuid: NusUUID);
            _bleDevice.Connect(OnConnected, OnDisconnected);
        }

Neither of the options above ever results in a callback.

nadiDS2 avatar Jun 28 '24 15:06 nadiDS2

Thanks for the information! Is there anything in Android LogCat that shows that the device is disconnected? Or perhaps any errors? Producing it locally I didn't get the same result.

Velorexe avatar Jun 30 '24 10:06 Velorexe

Please see attached log. I removed the middle with data being sent back-n-forth. sometime before "10:32:13" in the log I reset the device the headset was talking to. I know the device goes back to advertising after 10-15 sec (monitoring it with a sniffer).

Line 574 the disconnect is seen down in the BT stack Line 627 the disconnect seen by my application Line 646 the application issues a "connectToBleDevice" Line 676 the BT stack connects to the device Line 680 is the first error I see. odh_logs_2024-06-28 14.33.25.109.txt

nadiDS2 avatar Jul 01 '24 14:07 nadiDS2