How to check if Wi-Fi is connected on Android Programmatically ?

To check if your Android device is already connected to a Wi-Fi network, you can use the Android Connectivity Manager. Here’s how you can do it in Java:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Check if the device is connected to any network (Wi-Fi, mobile data, etc.)
        boolean isConnected = isNetworkConnected(this);

        if (isConnected) {
            // Check if the device is connected to a Wi-Fi network
            boolean isWifiConnected = isWifiConnected(this);

            if (isWifiConnected) {
                // Get information about the connected Wi-Fi network
                WifiInfo wifiInfo = getWifiInfo(this);
                String ssid = wifiInfo.getSSID(); // Get the SSID (name) of the network

                // You can now use 'ssid' to get the name of the connected Wi-Fi network.
            }
        }
    }

    // Check if the device is connected to any network
    private boolean isNetworkConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnected();
    }

    // Check if the device is connected to a Wi-Fi network
    private boolean isWifiConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return wifiNetwork != null && wifiNetwork.isConnected();
    }

    // Get information about the connected Wi-Fi network
    private WifiInfo getWifiInfo(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        return wifiManager.getConnectionInfo();
    }
}

In this example:

  1. We use the ConnectivityManager to check if the device is connected to any network using the isNetworkConnected method.
  2. If the device is connected to a network, we then use the ConnectivityManager and WifiManager to check if it’s connected to a Wi-Fi network using the isWifiConnected method.
  3. If the device is connected to Wi-Fi, we use the getWifiInfo method to get information about the connected Wi-Fi network, including the SSID (name) of the network.

Make sure to add the necessary permissions in your AndroidManifest.xml file to access network-related information:

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

With this code, you can determine if your Android device is connected to Wi-Fi and obtain information about the connected Wi-Fi network.

Leave a Comment