Search code examples
androidstringequalsandroid-wifiwifimanager

Why does two same strings doesnt equals?


I have a following code:

int nb=0;
wifiInfo = wifi.getConnectionInfo();
ssid = wifiInfo.getSSID();
wifi.startScan();
result=wifi.getScanResults();
int sizeList=vysledek.size();

if(result==null) {

    Log.d( LOG,"ERROR");

} else {

    for(int i=0; i<sizeList; i++) {

        Log.d("Check",result.get(i).SSID);
        if (result.get(i).SSID.equalsIgnoreCase(ssid)) {
            Log.d("SSID",ssid);
             nb++;
        }
        //Log.d("nb:",Integer.toString(nb)+"Size:"+Integer.toString(sizeList));

    }
}

I´m connected to the wifi network called ABC and result.get(0).SSID, result.get(1).SSID and result.get(2).SSID are also ABC. Why does that if statement doesn't increase nb? nb is still 0 in all cases. Thanks all


Solution

  • getScanResults() will give SSID with double quote e.g. your ABC will be "ABC" when accessed through result.get(i).SSID.

    So remove those quotes before you compare.

    /**
     * remove double quote from start and end of the string
     */
    private String unornamatedSsid(String ssid) {
        ssid = ssid.replaceFirst("^\"", "");
        return ssid.replaceFirst("\"$", "");
    }
    

    Use above function:

    String scannedSSid = unornamatedSsid(result.get(i).SSID);
    if (scannedSSid.equalsIgnoreCase(ssid)) {
    }