2

I'd like my android device to connect to a wifi hotspot. I created a new wificonfiguration and add it into the wifimanager, this wificonfiguration has NetworkId.Then I invoke the function wifi.enableNetwork(NetworkId, true).

After that, I think the supplicant will go through obtaining ip address, authentication, and at last physically connect to the hotspot. So is there a way to identify if the wifi is physically connected or not?

I would prefer a handler-like method.

henryyao
  • 1,618
  • 5
  • 22
  • 34

4 Answers4

10

You can try this:

ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
    // Your code here
}

Edit: More details:

Register a BroadcastReceiver in your manifest like so:

<receiver android:name="WifiReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        <action android:name="android.net.wifi.STATE_CHANGE"/>
    </intent-filter>
</receiver>

Then put the code above on the onReceive() method of your receiver like so:

@Override
public void onReceive(Context context, final Intent intent) {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifi.isConnected()) {
        // Your code here
    }
}
Krauxe
  • 6,038
  • 2
  • 21
  • 22
6

You can check all the network. If you only want WIFI you can remove checking other 2 network.

public static boolean hasInternetConnection()
{
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected())
    {
        return true;
    }
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected())
    {
        return true;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected())
    {
        return true;
    }
    return false;
}

Don't forget to add following in manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Sandeep
  • 2,708
  • 20
  • 27
2

This may help you .

public static boolean isInternetAvailable(Context context) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    boolean connectionavailable = false;
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        try {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected()) haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected()) haveConnectedMobile = true;
            if (informationabtnet.isAvailable()
                && informationabtnet.isConnected())
                connectionavailable = true;
            Log.i("ConnectionAvailable", "" + connectionavailable);
        } catch (Exception e) {
            System.out.println("Inside utils catch clause , exception is"
                + e.toString());
            e.printStackTrace();
        }
    }
    return haveConnectedWifi || haveConnectedMobile;
}
Mr_and_Mrs_D
  • 27,070
  • 30
  • 156
  • 325
Aman
  • 21
  • 2
0

getNetworkInfo(int) method is deprecated. You can apply something like this

 ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) { 
        // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi

        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile network
        }
    } else {
        // not connected to the internet
    }

Also, please add this permission under AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
RayChongJH
  • 361
  • 3
  • 5