user6318834
user6318834

Reputation:

How to check working wifi is there or not?

public boolean isConnected(Context context) {
    connected=false;
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.getState() == NetworkInfo.State.CONNECTED && info.isConnected()) {
           Log.e("link",new Gson().toJson(info.getDetailedState()));
            connected=true;
            Needle.onBackgroundThread().execute(new Runnable() {
                @Override
                public void run() {

                    try {
                        HttpURLConnection urlc = (HttpURLConnection) (new URL(mainDomain).openConnection());
                        urlc.setRequestProperty("User-Agent", "Test");
                        urlc.setRequestProperty("Connection", "close");
                        urlc.setConnectTimeout(1500);
                        urlc.connect();
                        connected=true;
                        Log.e("connection",new Gson().toJson(urlc.getResponseCode()));

                    } catch (IOException e) {
                        e.printStackTrace();
                        connected=false;
                    }
                }
            });
            return connected;
        }
    }

    return connected;
}

I have used this code. its working fine, but my issue is that there i need to call server. Is there any other way to check speed or working of WiFi ?

WifiManager wifiManager=(WifiManager)this.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int speedMbps = wifiInfo.getLinkSpeed();

I used above code to check speed but it also not working fine.

Please help me out. Thanks in advance

Upvotes: 0

Views: 87

Answers (2)

Haroon
Haroon

Reputation: 505

You Can try This

public static boolean checkInternetConnection(Context context)
{
    try
    {
        ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if ((wifi != null && wifi.isConnected())
                || (mobile != null && mobile.isConnected()))
        {
            return true;
        }
        log("Connection", "Connection failed");
        return false;
    }
    catch (Exception e)
    {
        // TODO: handle exception
        e.printStackTrace();
        return false;
    }
}

Upvotes: 1

Bam
Bam

Reputation: 530

You can use the following code:

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();

This will let you know if device is connected or not.

Upvotes: 0

Related Questions