schwiz schwiz - 1 year ago 125
Android Question

how to see if wifi is connected in android

I don't want my user to even try downloading something unless they have wifi connected. However I can only seem to be able to tell if wifi is enabled, but they could still have a 3g connection. m = (WifiManager) getSystemService(WIFI_SERVICE); s = m.getConnectionInfo().getSupplicantState();
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(s);
if( state != NetworkInfo.DetailedState.CONNECTED ){
return false;

However the state is not what I would expect, even though wifi is connected I am getting
as the state.

Answer Source

You should be able to use the ConnectivityManager to get the state of the Wifi adapter. From there you can check if it is connected or even available.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever

NOTE: It should be noted (for us n00bies here) that you need to add

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

to your

AndroidManifest.xml for this to work.

NOTE2: public NetworkInfo getNetworkInfo (int networkType) is now deprecated:

This method was deprecated in API level 23. This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo( instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download