Question

Several people have asked questions regarding getting the SSID, all of them only partionly work. According to the Android API wifiInfo.getSSID() should return a string, but no matter what I do the if statement returns false. I want to check if my phone is connected to "DieKantankys"

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    //Log.d("wifiInfo", wifiInfo.toString());
    //Log.d("NetworkId",wifiInfo.getNetworkId());

    if(wifiInfo.getSSID()=="DieKantankys"){
        setContentView(R.layout.group_choose_activity);
    }else{
        setContentView(R.layout.not_connected_to_scouting_wifi_error);
    }

}

What am I doing wrong?

Was it helpful?

Solution

but no matter what I do the if statement returns false.

At first, when you are comparing Strings, you have to use equals() because you want to compare values and not references:

if (wifiInfo.getSSID().equals("DieKantankys")) {
   // do your stuff
}

For that reason it did't work for you. Your current scenario will always return false because you're comparing String references with ==

Note: Sometimes is very "handy" to use equalsIgnoreCase() - with an usage of this method, comparison is case-insensitive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top