2

I am ranging beacons and my target is to process the beacons collection in didRangeBeaconsInregion so that I get the closest one in the collection and show on screen a string related to the beacon itselt (beacon1=red, beacon2=blue...). My current ibeacons advertising rate is 1Hz (and I can't configure them to increase it yet).

I have tried the following:

public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            Beacon closest = null;
            double distance = 100.0;
            logToDisplay(beacons.size() + " beacons in collection");

            for (Beacon beacon : beacons) {

                if (distance > beacon.getDistance()) {
                    closest = beacon;
                    distance = beacon.getDistance();
                }

            }

            if (closest == null) {
                logToDisplay(getCurrentTimeStamp() + " | Closest = null");
            }
            else if (closest.getId3().toInt() == 1) {
                logToDisplay(getCurrentTimeStamp() + " | BLUE");
            }
            else if (closest.getId3().toInt() == 3) {
                logToDisplay(getCurrentTimeStamp() + " | RED");
            }
            else if (closest.getId3().toInt() == 4) {
                logToDisplay(getCurrentTimeStamp() + " | GREEN");
            } // ... and so on with some more colours

        }

    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {   }
}

As far as I know, the didRangeBeaconsinRegion has a beacons collection which is a buffer of the beacons seen in the last second. As a consequence of my beacons having only a 1Hz ad rate, the collection size is very small and I can't get consistent results.

I think that there are two solutions:

  1. Increasing ad rate (not possible for me yet).
  2. Increasing the 1 second period from the didRangeBeaconsinRegion method so that the beacons collection buffers the beacons seen in the last 5 seconds (for example).

Is it possible to do this? How can I do it? Is this set with something like beaconManager.setForegroundScanPeriod(5000l);?

Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64

1 Answers1

2

Yes, you can do what you say, and it should help make results more consistent.

Add the line you mention right after you first get a beaconManager instance: beaconManager.setForegroundScanPeriod(5000l);

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • It works. Thank you! One question regarding this: I have noticed the beacons collection size is equal to the number of different detected beacons. So if I detect 3 times the same beacon within the ForegroundScanPeriod (5seconds), the collection size is 1. Is it possible to have all detections (even if they are from the same beacon) in the collection, so that the collection size from the example would be 3? –  Mar 19 '15 at 10:35
  • Sorry, that's not possible with the current library design. – davidgyoung Mar 19 '15 at 13:17