dimanche 4 décembre 2016

Java (android studio) : if statement not running even though it is evaluated to true

I am working on an android program that needs to identify what Wifi network a user is connected to before it implements a certain action. This is the code that I run to accomplish this:

     public String isConectedToPrivateNet(StringBuilder ssid) {


    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    ssid.replace(0, ssid.length(), wifiInfo.getSSID());
        Log.e("Data(b4 iF): ", ssid.toString());
    if(ssid.toString().equals("ACHILLES 101010")) {
        Log.e("Data (iN iF): ", ssid.toString());

    }else {
        ssid.replace(0, ssid.length(), "DIDN'T START");

    }

    return ssid.toString();

}

Basically this is supposed to just return the the SSID name if the if statement is true right? But it doesn't, instead it returns the "DIDN'T START". When I read my logs though, the "Data(b4 if): " shows that SSID being "ACHILLES 101010". I therefore assume that the if statement should evaluate to true, why doesn't it do so?

Here is something interesting though: If I replace the .equals(), with a .contains(), the code in the if statement block runs perfectly, and the SSID is returned. Why is this so?

*A few notes: the StringBuilder is passed as being empty here is it's declaration and initialisation

public StringBuilder ssid = new StringBuilder("");

* I also tried to use StringBuffer, and the same occurred.

Aucun commentaire:

Enregistrer un commentaire