Search code examples
androidibeaconaltbeacon

Altbeacon - Detecting beacon only within the IMMEDIATE range and discarding any outside this range


I want the app to see the beacons within the "Immediate" range only. In one of the article (I don't have the link) I read that the strings such as Immediate/Near/Far is obsolete with altbeacon or something rather it is suggested to use beacon.getDistance() < 0.5 for Immediate ranged beacons. But unfortunately I don't have any clue how to implement that.

I tried the following code proposed by one article to find the beacon at the shortest distance but seems to not working properly (most probably because of fluctuating rssi and testing by keeping the beacons in short distances to each other... dont know why they want min = Integer.MAX_VALUE.... but i was atleast expecting some result )

public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                Object[] beaconArray = beacons.toArray();

                //find the beacon with shortest distance
                int count=-1; //when no beacon is there
                int min = Integer.MAX_VALUE;

                for (int i=0; i < beaconArray.length; i++){
                    int d=((Beacon)beaconArray[i]).getRssi();
                        if(d < min){
                            min=d;
                            count=i; //1st beacon in the array
                        }
                }

              //play with the beacon at the shortest distance
              uuid = ((Beacon)beaconArray[count]).getId1().toString();

Some tips will be a blessing for me.


Solution

  • The terms immediate, near and far are arbitrary distance range buckets implemented in iOS. We dropped them in the 2.0 Android Beacon Library because the terms are vague and implementing specific distance buckets in terms of meters is very easy. Here is how you can do equivalent functionality:

    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        for (Beacon beacon: beacons) {
             if (beacon.getDistance() < 0.5) {
                 // This beacon is immediate (< 0.5 meters)
             }
             else if(beacon.getDistance() < 3.0) { 
                 // This beacon is near (0.5 to 3 meters)
             }
             else {
               // This beacon is far (> 3 meters)
             }      
         }
    }