Detecting internet availability on Android

Application.internetReachability always returns NetworkReachability.NotReachable on my Android 2.1. Works as expected in Editor. I also tried using the Ping class to detect internet availability using the following C# code.


IEnumerator CheckConnection()
{
    const float timeout = 10f;
    float startTime = Time.timeSinceLevelLoad;
    Ping ping = new Ping("199.59.148.82");
        
    while (true)
    {
        internetAvailableText = "Checking network...";
        if (ping.isDone)
        {
            internetAvailableText = "Network available.";
            yield break;
        }
        if (Time.timeSinceLevelLoad - startTime > timeout)
        {
            internetAvailableText = "No network.";
            yield break;
        }

        yield return new WaitForEndOfFrame();
    }
}

It always timed out. WWW class works as expected. Anyone had luck detecting internet access on Android?

Is your application perhaps missing the “android.permission.INTERNET” permission in the manifest? Unity should autodetect any usage of networking component but perhaps it fails for some reason…

You can inspect the permissions assigned to the application by looking under Settings > Application > Manage applications, on your device.

I have the same error. Its a bug in Unity

IEnumerator CheckInternet(){
WWW internet = new WWW(“www.google.com”);
yield return internet;
if(internet.error!=null)
isInternet=false;
else{
isInternet=true;
}
}

I found a solution for my android app. Check out the Unity Answer here:
http://answers.unity3d.com/questions/567497/how-to-100-check-internet-availability.html

instead of using the WWW class I use System.Net and HttpWebRequest
this way you can check for more than just web availability. You get 3 levels of connection checking:

  1. No connection

  2. Redirected (this was important for me because if you are connecting to the internet at a college or hotel, they need you to sign in before using the internet which means you’ll get a positive ping / check but you really don’t have internet yet because you can’t send anything to the site you actually want)

  3. Reached the correct page

(Adding a comment to an old posting in case somebody finds this using search…)

I have made an easy asset called Internet Reachability Verifier for Unity, which implements a technique called “captive portal detection”, but still does it only using the built-in WWW class.

Using it you will know if internet is truly reachable or if you’re just hitting e.g. a login page, like mentioned at the point (2) above.

More info here: Internet Reachability Verifier by Strobotnik (for Unity®)

Edit: That asset also jumps through the hoops needed to support Android 9.0+ Network Security Config.

This my “dirty” solution:

first thing is that Android device still have 127.0.0.0 when there isn’t connection, so if you try to ping.isDone it return a true value.
but what about ping.time ? if you are not connected it return a fixed value of 1

That is my method and it works for what I need :

IEnumerator CheckConnectionToMasterServer() {
	Ping pingMasterServer = new Ping("xx.xxx.xxx.xx");
	float startTime = Time.time;
	while (!pingMasterServer.isDone && Time.time < startTime + 5.0f) {

		yield return new WaitForSeconds(5.0f);
	}
	if(pingMasterServer.isDone && pingMasterServer.time > 2) {
		
		Debug.Log ("IntenetON" );

	} if(pingMasterServer.isDone && pingMasterServer.time<=1) {
		
		Debug.Log ("IntenetOFF" );
	}
}

Hope that can help!
Cheers

Use this Code It will work perfectly(unity editor, android, ios, etc)…

if(Application.internetReachability == NetworkReachability.NotReachable)
{
           Debug.Log("Error. Check internet connection!");
}
 else
{
           Debug.Log("Internet Connected");
}