Skip to content Skip to sidebar Skip to footer

Wifi Getssid() Returns Null

I use getSSID() to get the name of the wifi network as soon as a new connection is made. But sometimes I get null for that value. This is my code: Permissions in manifest are corre

Solution 1:

I use similar code regularly and I have never received null when connected.

Here is my code:

WifiManagerwifi= (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfoinfo= wifi.getConnectionInfo();
StringmyName= info.getSSID();

Therefore, I propose that you should wait 400 to 1000ms or so after receipt of the CONNECTION_CHANGE broadcast before requesting the information.


Here is one example that will implement the delay:

finalHandlerhandler=newHandler();
handler.postDelayed(newRunnable() {
    @Overridepublicvoidrun() {
        WifiManagerwifi= (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfoinfo= wifi.getConnectionInfo();
        StringmyName= info.getSSID();
    }
}, 1000);

Solution 2:

The Android Developers website states that :

The SSID may be null if there is no network currently connected.

You're listening to a CONNECTION_CHANGE event, what if the state of the connection changed from connected to disconnected ?

Wifi devices gets sometimes disconnected from an access point and they do reconnect silently without you even noticed it was disconnected.

Solution 3:

I've found out the hard way that the supplicant subsystem is only relevant to the WPA security mechanism, and is really not a good choice to use for monitoring general wifi connection status. The verbiage in the documentation would lead you to believe that it's possible, but I had a lot of trouble when trying to use the supplicant actions, including issues similar to the one you describe.

From the SupplicantState enum documenation:

These enumeration values are used to indicate the current wpa_supplicant state. This is more fine-grained than most users will be interested in. In general, it is better to use NetworkInfo.State.

Using the NETWORK_STATE_CHANGED_ACTION and looking at the NetworkInfo extra I was able to get expected, stable behavior.

Post a Comment for "Wifi Getssid() Returns Null"