- Home /
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?
Looks like it's fixed in 4.0 and won't be fixed in 3.5.6 as far as I'm concerned.
Answer by oliverdb · Oct 03, 2011 at 12:12 PM
I have the same error. Its a bug in Unity
I haven't tested it on the new Unity, so maybe it's fixed. If you verified it still doesn't work as expected, file a bug report.
I meant Unity 3.5.6, Unity 4 is still beta AFAI$$anonymous$$.
I'm under Unity 3.5.6 and still the bug. $$anonymous$$aybe unity 4 is still beta but free beta so maybe I can test it tonight. Hope it's fixed -_-
Answer by hotshot03 · Apr 03, 2013 at 08:06 AM
IEnumerator CheckInternet(){ WWW internet = new WWW("www.google.com"); yield return internet; if(internet.error!=null) isInternet=false; else{ isInternet=true; }
}
Answer by eriQue · Sep 15, 2011 at 08:15 AM
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.
Says: This application can access the following on your phone: Network communication, full internet access.
Did you manage to get Application.internetReachability and/or Ping to work on Android?
Some find a solution to this cause I did this on my Android device :
And it never finds Internet connection é_è
Answer by naglers · Jan 13, 2014 at 03:46 PM
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
Answer by tonic · Jul 04, 2014 at 02:02 PM
(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: http://j.mp/IRVUN
Edit: That asset also jumps through the hoops needed to support Android 9.0+ Network Security Config.