Reputation: 402
Working with ranging beacons and its working fine, well theoritically. I have 3 beacons, and a simple algorithm to detect only one beacon at a time at the shortest distance, but due to the continious fluctuation of of RSSI values I am ending up detecting the other beacons VERY frequently. Well that will not be so harmful if I can 'pause' a little more between detection. My betweenscan perdiod is, setForegroundBetweenScanPeriod(0l)
. Now the question is, should I increase the setForegroundBetweenScanPeriod()
to achieve that 'pause' property or handle it manually using some sort of timer? Is the algorithm correct to detect one beacon that is at the shortest distance? Here is my code
protected void onCreate(Bundle savedInstanceState) {
...
beaconManager.setForegroundScanPeriod(1100l);
beaconManager.setForegroundBetweenScanPeriod(0l);
beaconManager.bind(this);
...
}
@Override
public void onBeaconServiceConnect(){
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
//find the beacon with shortest distance
int idx = -1; //when no beacon is there
double min = Double.MAX_VALUE;
for (int i = 0; i < beaconArray.length; i++) {
double d = ((Beacon) beaconArray[i]).getDistance();
if (d < min) {
min = d;
idx = i; //1st beacon in the array
}
}
if (count == 0 && idx != -1) {
//show the data associated with the beacon
}
Upvotes: 3
Views: 1435
Reputation: 64916
The algorithm looks correct. I would not change the scan period to make the closest beacon more stable, as this will cause side effects like delays in detection.
I typically do three things to make the closest beacon determination more stable:
Make the closest beacon a class variable and also add a timestamp as a class variable that indicates when the closest beacon changed. You can then add logic to refuse to change the closest beacon if it has already changed in the last n seconds.
Add a minimum percentage distance change needed to switch to another beacon as the closest beacon. (E.g. refuse to change the closest beacon unless another beacon is at least 10% closer.)
Increase the averaging period for distance estimates. By default it is 20 seconds, but you can increase it to get more data points and less noise on your distance estimates:
RunningAverageRssiFilter.setSampleExpirationMilliseconds(30000l);
The timings on option 1 and 3 depend on your use case. If you need quick changes in estimating the closest beacon, you may not be able to make these times very big.
Finally, you should also make sure your beacons are transmitting as frequently as possible. A 10 Hz transmission rate gives 10x as many RSSI samples for distance estimates than a 1 Hz transmission rate, and therefore much smoother distance estimates.
Upvotes: 3