views:

93

answers:

2

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.

android.net.wifi.WifiManager m = (WifiManager) getSystemService(WIFI_SERVICE);
android.net.wifi.SupplicantState 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 OBTAINING_IPADDR as the state.

+4  A: 

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(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}
Jason Knight
worked perfectly thanks!
schwiz
I deleted my answer. I like this solution better. Winner, winner, chicken dinner.
Snekse
A: 

I simply use the following:

SupplicantState supState; 
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();

Which will return one of these states at the time you call getSupplicantState();

ASSOCIATED - Association completed.

ASSOCIATING - Trying to associate with an access point.

COMPLETED - All authentication completed.

DISCONNECTED - This state indicates that client is not associated, but is likely to start looking for an access point.

DORMANT - An Android-added state that is reported when a client issues an explicit DISCONNECT command.

FOUR_WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress.

GROUP_HANDSHAKE - WPA Group Key Handshake in progress.

INACTIVE - Inactive state.

INVALID - A pseudo-state that should normally never be seen. SCANNING - Scanning for a network.

UNINITIALIZED - No connection.

Donal Rafferty