ExpertGenie
ExpertGenie

Reputation: 195

Scan period of beacons in android not working as expected

I am working with beacons in xamarin android. I want the scan to be done over 60 seconds and a period of 30 seconds to wait between the next cycle of scan.

public async void DetectAvailableBeacons()
{ 
        _monitorNotifier = new MonitorNotifier();

        _rangeNotifier = new RangeNotifier();

        _tagRegion = new Region("Region",null, null, null);

        _beaconManager.Bind(this);

        _beaconManager.SetBackgroundScanPeriod(60000);
        _beaconManager.SetForegroundScanPeriod(60000);
        _beaconManager.SetBackgroundBetweenScanPeriod(30000);
        _beaconManager.SetForegroundBetweenScanPeriod(30000); 

        _rangeNotifier.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegionComplete;

    }

The method RangingBeaconsInRegionComplete fills my list foundBeacons with the beacons detected.

Also, I have a method that gets all the available detected beacons as follows:

ObservableCollection<DetectedBeacon> BeaconLocator.GetAvailableBeacons()
{
    return !_paused ? foundBeacons : null;
}

Now, I call the method GetAvailableBeacons() as follows:

public void PopulateBeacons() {
   beaconsOnList = be.GetAvailableBeacons(); 
   PopulateBeacons();
}

My issue is that sometimes it is missing some beacons, that is some beacons are not being detected. Also, the interval of scan does not seem to be working properly. Can someone advise what is wrong ?

Upvotes: 0

Views: 290

Answers (1)

davidgyoung
davidgyoung

Reputation: 64995

In order for your scan periods to take effect right away, the easiest solution is to move the calls to set them before the call to bind so it looks like this:

    _beaconManager.SetBackgroundScanPeriod(60000);
    _beaconManager.SetForegroundScanPeriod(60000);
    _beaconManager.SetBackgroundBetweenScanPeriod(30000);
    _beaconManager.SetForegroundBetweenScanPeriod(30000); 

    _beaconManager.Bind(this);

By default, the scan will happen every 1.1 seconds and give you a result of all beacons seen in that period. This is probably what you are seeing happening without the code change above.

With shorter scan intervals, it is common for beacons with low advertising rates (e.g. ones that advertise only once every second) to not appear in the detection list for a single scan cycle. You can solve this by increasing the length of the scan cycle as you say you want to do, by increasing the advertising rate of the beacons, or building code logic that maintains a full list of beacons detected recently even if they weren't seen in the most recent scan cycle.

Short answer: If you increase your scan period successfully, I suspect this problem will go away.

Upvotes: 1

Related Questions