Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by gibbie_learnersedge · Sep 21, 2016 at 06:29 PM · connectioninternet

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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;
     }        
 }
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaurabhStudio · Sep 23, 2016 at 06:07 AM 0
Share

You can try this code.

avatar image gibbie_learnersedge · Sep 23, 2016 at 06:14 AM 0
Share

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?

avatar image SaurabhStudio · Sep 23, 2016 at 06:20 AM 0
Share

You can put invoke of every 5sec.

avatar image gibbie_learnersedge SaurabhStudio · Sep 23, 2016 at 06:23 AM 0
Share

How do I that? $$anonymous$$y apologies unity noob.

avatar image SaurabhStudio · Sep 23, 2016 at 06:29 AM 2
Share
      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

avatar image gibbie_learnersedge SaurabhStudio · Sep 23, 2016 at 06:39 AM 0
Share

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?

avatar image SaurabhStudio gibbie_learnersedge · Sep 23, 2016 at 06:45 AM 0
Share

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.

avatar image TheSaviour SaurabhStudio · Sep 23, 2016 at 06:52 AM 0
Share

$$anonymous$$ay I ask, what's wrong with using Network.TestConnection()?

avatar image gibbie_learnersedge TheSaviour · Sep 23, 2016 at 06:54 AM 0
Share

It doesn't work on Android. It works on PC though.

avatar image BHAVYA_PRASHANT SaurabhStudio · Mar 16, 2021 at 03:44 AM 0
Share

Thank you so much! Works perfectly on Unity Editor and Android.

avatar image
2

Answer by Lishin · Sep 27, 2016 at 06:09 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by codemaker2015 · Apr 05, 2020 at 09:59 AM

 if(Application.internetReachability == NetworkReachability.NotReachable)
         {
             Debug.Log("Error. Check internet connection!");
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gibbie_learnersedge · Sep 23, 2016 at 03:07 AM 0
Share

Is it possible to use Java's libraries and import them to Unity C# mono develop?

avatar image elenzil gibbie_learnersedge · Sep 23, 2016 at 04:22 PM 0
Share

saurabhstudio's answer is definitely the way to go, but regarding java from unity, yes: https://docs.unity3d.com/ScriptReference/AndroidJavaObject.Call.html

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android game: allow internet for just specific domain 0 Answers

iOS internet reachability for other platform? 2 Answers

Detect if device is connected to Wifi 3 Answers

Android: disable/enable internet connectivity! 0 Answers

Occasionally issue with Wifi connection 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges