Forum Libraries, Components, and Plugins

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

slow advertisement packets with aritchie/bluetoothle Scan()

I have implemented this in Xamarin for iOS and Android in order to receive advertisement packets

CrossBleAdapter.Current.Scan().Subscribe(scanResult => {});

But scanResults are slow. About 2-20 seconds between.

But when I use another app like "nRF Connect" advertisement packets are received every second.

What Have I forgotten?

Best Answer

  • AllanRitchieAllanRitchie CAInsider, University ✭✭✭
    Accepted Answer

    Scan results will come in faster than your UI can process which makes it seem slow if you are constantly flipping into the UI. If you take a look at the android code, you'll notice I don't do anything to cause impediments to the result, so it is either a weird hardware thing or something happening in external code.

Answers

  • MichelsenMichelsen Member ✭✭

    I tried this, but it only helped slightly:

    ScanConfig sc = new ScanConfig();
    sc.ScanType = BleScanType.LowLatency;

    CrossBleAdapter.Current.Scan(sc).Subscribe(scanResult => {});

  • AllanRitchieAllanRitchie CAInsider, University ✭✭✭
    There isn’t anything I do that would make scan slow. Do you have a small end to end sample? Is the sample app slow (keep in mind I buffered do not to destroy ui thread)?
  • MichelsenMichelsen Member ✭✭

    Thank you for your quick reply.

    Here is a small end to end sample. Regarding the UI thread, I have other things like a second counter and a memory monitor that update the user interface every second without problem. Device Connect and character write also seem to perform ok. It is only the Scan().


    private IDisposable disposable_ScanResult = null;
    ....
    public void BT_StartScanner()
    {
    if (disposable_ScanResult == null)
    {
    ScanConfig sc = new ScanConfig();
    sc.ScanType = BleScanType.LowLatency;

        disposable_ScanResult
            = CrossBleAdapter.Current.Scan(sc).Subscribe(OnScanResult);
    }
    

    }

    void OnScanResult(IScanResult scanResult)
    {
    // This part is called every 2 - 20 seconds. Not every second as expected.
    }

  • MichelsenMichelsen Member ✭✭

    I haven't tried the sample app. What do you mean by "keep in mind I buffered do not to destroy ui thread"?

  • AllanRitchieAllanRitchie CAInsider, University ✭✭✭
    Accepted Answer

    Scan results will come in faster than your UI can process which makes it seem slow if you are constantly flipping into the UI. If you take a look at the android code, you'll notice I don't do anything to cause impediments to the result, so it is either a weird hardware thing or something happening in external code.

  • MichelsenMichelsen Member ✭✭

    I have now been investigating.
    @AllanRitchie , you are probably right about what you say: "...weird hardware thing or something happening in external code"
    I have compared my app with "nRF Connect" app on a series of Android devices and a single iPhone.
    Only a single Android device (Huawei P10) receives the advertisement packets every 1 sec in both my app and "nRF Connect" app.
    On 4 other Android devices (both Huawei and Samsung) and an iPhone SE, advertisement packets are lost in both my app and "nRF Connect" app.

Sign In or Register to comment.