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 correct, because, as I said, most of the times it works.
I use this filter for the broadcast receiver:
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
In the broadcast I do this:
if("android.net.wifi.supplicant.CONNECTION_CHANGE".equals(intent.getAction()))
{ boolean bConected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
if(bConnected == true)
{ WifiManager wifi = (WifiManager) Contexto.getSystemService(Context.WIFI_SERVICE);
String MyName = wifi.getConnectionInfo().getSSID();
Sometimes MyName is null here even if Wifi is connected correctly
}
}
Any ideas?
I use similar code regularly and I have never received null
when connected.
Here is my code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String myName = 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:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String myName = info.getSSID();
}
}, 1000);