question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StartScanningForDevicesAsync always times out

See original GitHub issue

Looking at the code in AdapterBase.cs, on both the master branch and the 1.3.0 branch, the StartScanningForDevicesAsync function looks like this:

   public async Task StartScanningForDevicesAsync(Guid[] serviceUuids = null, Func<IDevice, bool> deviceFilter = null, bool allowDuplicatesKey = false, CancellationToken cancellationToken = default(CancellationToken))
    {
      if (IsScanning) {
        Trace.Message("Adapter: Already scanning!");
        return;
      }

      IsScanning = true;
      serviceUuids = serviceUuids ?? new Guid[0];
      _currentScanDeviceFilter = deviceFilter ?? (d => true);
      _scanCancellationTokenSource = new CancellationTokenSource();

      try {
        using (cancellationToken.Register(() => _scanCancellationTokenSource?.Cancel())) {
          await StartScanningForDevicesNativeAsync(serviceUuids, allowDuplicatesKey, _scanCancellationTokenSource.Token);
          await Task.Delay(ScanTimeout, _scanCancellationTokenSource.Token);
          Trace.Message("Adapter: Scan timeout has elapsed.");
          CleanupScan();
          ScanTimeoutElapsed(this, new System.EventArgs());
        }
      } catch (TaskCanceledException) {
        CleanupScan();
        Trace.Message("Adapter: Scan was cancelled.");
      }
    }

I don’t understand the intended usage of this method. The name of the method is StartScanningForDevicesAsync which makes it sound like it should return (complete) once scanning has been started. However, from what I can tell it just starts scanning, then delays for a configurable amount of time, and then stops scanning and calls the timeout elapsed callback.

In my application, I need to start scanning and then scan indefinitely until a certain device is found, at which point I would call StopScanningForDevicesAsync in order to connect. I don’t really want a scan timeout.

Is the intended usage of this method that it not return until the cancellation token is cancelled? That seems a bit unusual to me but maybe I’m missing something.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JSchaenzlecommented, Jul 19, 2017

@bed007 I definitely agree with you in most cases. I am a fan of saving user’s battery life. However, there are going to be use cases where that’s not feasible. For example, if you’re making an app that searches for a device to come into range, you probably want to keep scanning as long as the user still has the app in the foreground until the device is found.

It just seems to me like this method is assuming too much. I think that incorporating a ScanTimeout really should be the responsibility of the application that uses this library, not the library itself. Having a method that only returns by way of being cancelled doesn’t seem right either. If it’s going to stay that way I would propose changing the name to something like ScanForDevicesUntilCancelledAsync.

If I’m the only one that feels this way I’ll shut up and you can close the issue. 😃 But if others agree then I don’t mind spiking out the change and submitting a PR.

0reactions
xabrecommented, Aug 16, 2017

We could add the timeout as a parameter with a default value. This way the user is aware of this functionality 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

StartScanningForDevicesAsync always times out #233
The name of the method is StartScanningForDevicesAsync which makes it sound like it should return (complete) once scanning has been started.
Read more >
Xamarin Plugin.BLE why data read doesn't change?
After various try I got the solution: To read effectively the data from the BLE module, is necessary to use the ValueUpdateHandler.
Read more >
Plugin.BLE 3.0.0-beta.2
It must be set before calling StartScanningForDevicesAsync() . Changing it while scanning, will not affect the current scan. Connect to device.
Read more >
xamarin-bluetooth-le
In iOS, everything works fine. In Android, the system starts to choke and many of the notifications fail to arrive in time, causing...
Read more >
Xamarin.Forms — Xamarin Community Forums
I am using Visual Studio For Mac. My Xamarin Forms project is quite large and contains many content pages. Some of them will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found