How to make sure you have Wi-Fi on your Android

Asked 2 years ago, Updated 2 years ago, 134 views

I don't want to download anything when the Wi-Fi is not connected, but all I can do is say that it's available when the Wi-Fi is available. What I'm saying is that Wi-Fi is possible and you might be using 3G.

How do I check if the Wi-Fi is connected?

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;
        }

This sauce doesn't work out as I expected. Even if the Wi-Fi is already connected, it only says that I am getting an IP address. What should I do?

android android-wifi wifimanager

2022-09-21 15:52

1 Answers

You can get the status from the wifi adapter by using Connectivity Manager. Then, you can receive whether the Wi-Fi is connected and whether the Wi-Fi is available now.

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

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

Note: You need to add permission to the Android manifest file as shown below.

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

Note 2: PublicNetworkInfogetNetworkInfo (intnetworkType) is currently 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(android.net.Network) instead.

Please use getAllNetworks() and getNetworkInfo(android.net.Network) because it was deprecated from API23.


2022-09-21 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.