Mobile device detect internet connection. How?

0 favourites
  • 12 posts
From the Asset Store
A futuristic device, can be used as radarscreen, optionscreen, infodisplay, research device, ...
  • It seems like an on completed Ajax event gets run even when there is no internet connection. Is that right?

    I'm trying to make a reliable way to detect internet connection on device and I would have thought the Ajax On Completed request wouldn't trigger without a connection but apparently not.

    I am currently at a loss of how to detect internet connection on device. I've spent 2 days on this so far.!?!?

    Anyone know the answer? Thanks

  • Still no luck. I even tried doing this script which works perfectly fine on PC but on device it always returns as connected..

    function NetworkTest(runtime)
    {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "GET", "https://www.mysite.com", true );
    	xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
    	xmlHttp.send();
    	
    
    	xmlHttp.onerror =function ()
    	{
    		runtime.callFunction("Change_Connection_Status",0);//NOT CONNECTED
    	}
    	
    	xmlHttp.onload =function ()
    	{
    		
    		runtime.callFunction("Change_Connection_Status",1);//CONNECTED
    	}
    	
    }
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Is there a reason you don't use Browser.isOnline?

  • Yes, that only works on PC. It doesn't do anything on mobile. God knows why not. :(

  • In browser or as App? It works for me in Chrome on Android.

  • As an exported debug apk on device it doesn't seem to work. Does it work for you like that?

  • As an exported debug apk on device it doesn't seem to work. Does it work for you like that?

    I cannot export to apk at the moment. So i tested only in browser.

    If Browser.isOnLine doesn't work in apks. I would consider that as a bug. It's not mentioned that it doesn't work. I would make a bug-report.

    Have you tried to use PlatformInfo.ConnectionTyp? it should return 'none' if it's not conneted, I think.

  • I tried that too but it didn't work either. From what I remember it always returned the same on device.

    I think it was already reported as a bug but I don't think it was addressed There is a report here: github.com/Scirra/Construct-3-bugs/issues/3087

  • Have you tried in construct2 runtime?

    However this issue is closed. I would make a new bug-report. Even it doesn't work it should mentioned in the manual.

  • I wouldn't bother trying to work out if you are online or not. It's surprisingly difficult to tell if you are online, e.g. suppose you can reach a few websites but not most - does that count as online? The best approach is: just go ahead and do whatever networking stuff you need to, and if it fails, you're probably not online.

  • I understand but there are a some points to consider:

    -Even when I do my Network stuff on Android the connection always reports success regardless of whether it fails or not.

    -There is a disparity between the PC version and the Android version.

    For example: the following works fine on PC but reports always connected on Android with all wifi/mobile data disconeccted..why?

    var xmlHttp = new XMLHttpRequest();
    	
    	//alert("Csxsxs");
    	xmlHttp.open( "GET", "https://www.pigobo.com", true );
    	xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
    	xmlHttp.send();
    	
    	
    	xmlHttp.onreadystatechange = function(e) {
    	
    		if (xmlHttp.readyState != 4) {
    			runtime.callFunction("Change_Connection_Status",0);
    		 return;
    		}
    
    		if (xmlHttp.status == 200) {
    			runtime.callFunction("Change_Connection_Status",1);
    		}
    		else
    		{
    			runtime.callFunction("Change_Connection_Status",0);
    		}
    	}
    

    TBH what consists of online or not is really a design choice I should be making but the issue is that when this kind of thing is happening I am unable to carry it out consistently on different devices (PC or mobile); which is probably the real issue.

    That said, if this question can be answered then I'm a happy..

    Is there a consistent way I can check if my app can connect to a specific website (on a mobile device) and return success/fail?

    Cheers

  • -Even when I do my Network stuff on Android the connection always reports success regardless of whether it fails or not.

    It shouldn't do. My best guess is it really is working, or you're getting a cached result, which you don't need to be online for.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)