- Home /
How do I check connection to Internet in Android?
Hi There,
Is there a way to check whether if the app is connected to the internet or not? I've look around and most I found are pinging server and WWW request. I am not sure if there are a much ideal way method for me to check if it is connect to the internet or not.
Open to any suggestion please.
Regards, Gibbie
Answer by SaurabhStudio · Sep 23, 2016 at 06:06 AM
using UnityEngine;
public class InternetChecker : MonoBehaviour
{
private const bool allowCarrierDataNetwork = false;
private const string pingAddress = "8.8.8.8"; // Google Public DNS server
private const float waitingTime = 5.0f;
public bool internetConnectBool;
private Ping ping;
private float pingStartTime;
public void Start()
{
bool internetPossiblyAvailable;
switch (Application.internetReachability)
{
case NetworkReachability.ReachableViaLocalAreaNetwork:
internetPossiblyAvailable = true;
break;
case NetworkReachability.ReachableViaCarrierDataNetwork:
//internetPossiblyAvailable = allowCarrierDataNetwork;
internetPossiblyAvailable = true;
break;
default:
internetPossiblyAvailable = false;
break;
}
if (!internetPossiblyAvailable)
{
InternetIsNotAvailable();
return;
}
ping = new Ping(pingAddress);
pingStartTime = Time.time;
}
public void Update()
{
if (ping != null)
{
bool stopCheck = true;
if (ping.isDone)
InternetAvailable();
else if (Time.time - pingStartTime < waitingTime)
stopCheck = false;
else
InternetIsNotAvailable();
if (stopCheck)
ping = null;
}
}
public void InternetIsNotAvailable()
{
//Debug.Log("No Internet");
internetConnectBool = false;
}
public void InternetAvailable()
{
//Debug.Log("Internet is available;)");
internetConnectBool = true;
}
}
Thanks for sharing, it doesn't detect if internet connection changes on runtime. How can I make it so that it will detect as well while it's at run time?
How do I that? $$anonymous$$y apologies unity noob.
using UnityEngine;
public class InternetChecker : $$anonymous$$onoBehaviour
{
private const bool allowCarrierDataNetwork = false;
private const string pingAddress = "8.8.8.8"; // Google Public DNS server
private const float waitingTime = 5.0f;
public bool internetConnectBool;
private Ping ping;
private float pingStartTime;
public void Start()
{
InternetCheck();
}
public void InternetCheck()
{
Invoke("InternetCheck",5f);
bool internetPossiblyAvailable;
switch (Application.internetReachability)
{
case NetworkReachability.ReachableViaLocalAreaNetwork:
internetPossiblyAvailable = true;
break;
case NetworkReachability.ReachableViaCarrierDataNetwork:
//internetPossiblyAvailable = allowCarrierDataNetwork;
internetPossiblyAvailable = true;
break;
default:
internetPossiblyAvailable = false;
break;
}
if (!internetPossiblyAvailable)
{
InternetIsNotAvailable();
return;
}
ping = new Ping(pingAddress);
pingStartTime = Time.time;
}
public void Update()
{
if (ping != null)
{
bool stopCheck = true;
if (ping.isDone)
InternetAvailable();
else if (Time.time - pingStartTime < waitingTime)
stopCheck = false;
else
InternetIsNotAvailable();
if (stopCheck)
ping = null;
}
}
public void InternetIsNotAvailable()
{
//Debug.Log("No Internet");
internetConnectBool = false;
}
public void InternetAvailable()
{
//Debug.Log("Internet is available;)");
internetConnectBool = true;
}
}
opps , Just try this
When I turn off the internet and then turn back on. It doesn't check any more. As long as there is no connection it doesn't check. Why is that?
Oops my bad, Try It again. I have changed. If this not work I have to try it.. I will reply as soon as possible.
$$anonymous$$ay I ask, what's wrong with using Network.TestConnection()?
It doesn't work on Android. It works on PC though.
Thank you so much! Works perfectly on Unity Editor and Android.
Answer by Lishin · Sep 27, 2016 at 06:09 PM
if(Application.internetReachability == NetworkReachability.NotReachable)
{
Debug.Log("Error. Check internet connection!");
}
Answer by codemaker2015 · Apr 05, 2020 at 09:59 AM
if(Application.internetReachability == NetworkReachability.NotReachable)
{
Debug.Log("Error. Check internet connection!");
}
Answer by elenzil · Sep 22, 2016 at 11:56 PM
hm. you could write some java code, perhaps.
hitting a known server might be the best way.
Is it possible to use Java's libraries and import them to Unity C# mono develop?
saurabhstudio's answer is definitely the way to go, but regarding java from unity, yes: https://docs.unity3d.com/ScriptReference/AndroidJavaObject.Call.html
Answer by KEric · Mar 17, 2020 at 09:04 AM
I've created an Android's library that can be used as Unity's plugin for this purpose. If anyone's interested it's available under https://github.com/rixment/awu-plugin Hope it helps, cheers!