0

I'm trying to know what floor I'm at by getting the distance of 2 beacons with the same id1,1d2 and the id3 is 1 for beacon 1 and 2 for beacon 2...but my code does not work :( ... Im not that good in programming and I dont know how to use most of the functions or methods of altbeacon .... can someone help me how to get distance of beacons and make a condtion base on that distances of 2 beacons .... Is there a sample codes in doing this?

    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            if((beacon.getId3().equals(Identifier.parse("1")) && beacon.getDistance() < 0.5) && (beacon.getDistance() > 5 && beacon.getId3().equals(Identifier.parse("2"))
            logToDisplay("1st floor");
            }
    }
KB24
  • 1
  • what "doesn't work" about your code? you need to be more specific, is it crashing, is it not being called, is it something else? – panini Oct 05 '14 at 20:15
  • the logtoDisplay is not being called .... I test it using 2 phones at my hand with 1 has locate app for altbeacon to know the distance of the 2 beacons and make sure that im in < 5 distance in beacon 1 and >5 meters in beacon 2 at the same time in my other hand I have a phone with my app that wont display anyting in ranging activity – KB24 Oct 05 '14 at 20:23

1 Answers1

0

this is the math for distance

var R = 6371; // km
  var φ1 = toRad(lat1);
  var φ2 = toRad(lat2);
  var Δφ = toRad((lat2-lat1));
  var Δλ = toRad((lon2-lon1));

  var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
        Math.cos(φ1) * Math.cos(φ2) *
        Math.sin(Δλ/2) * Math.sin(Δλ/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

  var d = R * c;

  // printing distance to 2 decimal points in KM
  $("#distance").append(d.toFixed(2) + " Km")

you will need to get the distance of the two beacons, save them to a variable and then write something like this

var beacon1 = foo
var beacon2 = baa   
if ( beacon1 - beacon2 <= 0.5 ) {
            do something 
       } else {
      do something else 
      }
a-windett
  • 151
  • 1
  • 7
  • is this for altbeacon? ....cause there is a beacon.distance() in altbeacon library and its working fine but my problem is when I try to make I condtion with both beacons involved its not working – KB24 Oct 05 '14 at 21:02