Search code examples
javaibeacon

iBeacon.getProximityUuid() can't equal to string?


I try to compare iBeacon.getProximityUuid() with the custom string, but it can't work.
I'm very sure the first char is "a", but the result always return false!
I use the RadiusNetwork's iBeacon library.

String tempstr = iBeacon.getProximityUuid().substring(0, 1);
if (tempstr == "a") {
    return true;
}
else {
    return false;
}

Solution

  • You must change Change if (tempstr == "a") to if (tempstr.equals("a")). In Java, the == operator is used for object equality. Because the two objects aren't the same instance, tempstr == "a" always returns false. Using the .equals method actually compares the strings.