1

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?

Ton
  • 9,235
  • 15
  • 59
  • 103

3 Answers3

5

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);
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • I used this and I worked well: String sWifiId = wifi.getConnectionInfo().getSSID(); if(sWifiId == null) { for(int i=1; i<=10; i++) { try {Thread.sleep(1000);} catch (InterruptedException e) {}; sWifiId = wifi.getConnectionInfo().getSSID(); if(sWifiId != null) break; } } – Ton Mar 30 '13 at 11:48
3

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.

Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71
  • Bu tit is connected. In fact, I see the wifi icon in the status bar. – Ton Mar 29 '13 at 18:56
  • 1
    @Halim: I've upvoted your answer for two reasons: a) This is a good idea, perhaps the device is losing connection and then rapidly reconnecting, and b) Congratulations on your Reputation surpassing 1000!! – David Manpearl Mar 29 '13 at 19:01
  • Thanks :) Hope it helps, it's the most important ! – Halim Qarroum Mar 29 '13 at 19:01
  • Do you have a link to that quote? I didn't find it here: http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID() – Johan Walles May 10 '16 at 14:05
0

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.

BitBot
  • 458
  • 4
  • 14