Héctor
Héctor

Reputation: 26034

Android WiFi Connection Info

I'm getting wifi connection info this way:

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();

But, could getConnectionInfo method return null in any case? Android documentation reference say this:

Return dynamic information about the current Wi-Fi connection, if any is active.

I guess if the device is not connected to any network it will return null, won't it?

Thanks

Upvotes: 1

Views: 793

Answers (1)

Nakul
Nakul

Reputation: 1329

Yes it will return null, if it is not connected to the wi-fi.

But if you are also checking whether internet is available by using above code, then it is not 100% correct way to do so. Cause it may happen that your device is connected to wifi but no internet is available. so for that you have to ping any live site (such as google,facebook, or any other trusted website) and check if the returned status code is 200.

I hope this helps.

Upvotes: 1

Related Questions