0

I'm a newbie in android development and I am using altbeacon library and the reference app for altbeacons we also have a 2 Pibeacons....... the reference works fine and able to detect both beacons ...I want to do something if the app detected a specific beacon ...for example i want to display a string if I detected a beacon in near proximity....the first 2 if statement works fine but if I added a condition to determine which beacon is in near proximity the 3rd if statement does not work....the first beacon minor is 1 and the 2nd is 2

    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    if (beacons.size() > 0) {
        for (Beacon beacon: beacons) {
            if(BeaconProximity.getProximityString(beacon.getDistance()) == "Near"){
                logToDisplay("Hello");              
            }

            if(BeaconProximity.getProximityString(beacon.getDistance()) == "Immediate"){
                logToDisplay("Hi");             
            }

            if(BeaconProximity.getProximityString(beacon.getDistance()) == "Near" && beacon.getId3 == Identifier.parse("1"){

            logToDisplay("World");              
            }
       }
    }      

if I only display beacon.getId3 and Identifier.parse("1") like this code logToDisplay(beacon.getId3()+"="+Identifier.parse("1") it will output 1=1 they are equal but if I make that a condition ... It does not work ...so I dont know whats wrong or am I missing something or Is there another way to insert a command or codes if I detected a specific beacon in near,far,immediate proximity?

KB24
  • 1

2 Answers2

0

There is at least one issue. In Java, you can't compare strings with == because that checks to see if they are the exact same object. Instead of:

BeaconProximity.getProximityString(beacon.getDistance()) == "Near"

Use:

BeaconProximity.getProximityString(beacon.getDistance()).equals("Near")

Secondly, I am not sure what the BeaconProximity class does. That is not part of the standard Android Beacon Library. If your code does not work with the fix above, you should post the definition of the BeaconProximity class.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I didn't see BeaconProximity in AltBeacon now. Is it obsolete? How can we get an equivalent iOS proximity string now? – tedyyu Dec 26 '14 at 08:51
  • 2
    The BeaconProximity class is not part of the library. If you want to get the equivalent of immediate/near/far on iOS, simply use `beacon.geDistance()`. A value < 0.5 indicates immediate, from 0.5-3.0 indicates near, and > 3.0 indicates far. – davidgyoung Dec 26 '14 at 12:33
0

Thanks david.....KB24 is my friend account and I'm the one who ask the question ......I use the beaconhelper class from Beacon Scanner & Logger App by Justin O'Dwyer which also uses altbeacon library and just rename it to BeaconProximity to determine if the beacon is near,far,immediate or unknown.....actually the 1st code below works fine with just comparing ==

    BeaconProximity.getProximityString(beacon.getDistance()) == "Near" 

but if I add another condition with a code below... it will not work

    beacon.getId3() == Identifier.parse("1")

and your suggestion solve my problem =) ..... with the code below

    beacon.getId3().equals(Identifier.parse("1"))
strygwyr
  • 3
  • 2
  • Yes, it is the same general issue with any object in Java. You cannot compare different objects for equality with ==, you must use the .equals method. – davidgyoung Oct 01 '14 at 21:15