No internet access Android devices

Hi,

I’m developing a game that requires Internet access to show ads and interacting with Google Play services, of course the build.settins file sets “android.permission.INTERNET” in the usesPermissions table for Android.

However, if the device has no internet access and ugly warning is displayed as soon as the game tries to init Google Play services, indicating there’s no Internet access.

I’m wondering if it’s possible, somehow, to detect no internet connection is available to skip executing methods requiring internet access.

Regards,

Olman Q,

Something like this:

local socket = require("socket") local http = require("socket.http") local function testNetworkConnection()     print("testing connection")     if http.request( "http://google.com/" ) == nil then         print("cant connect to google")         return false     end     print("got a connection")     return true end

Thanks Rob

If you want to specify a timeout you can use the following snippet I come up with based on several posts on this forum.

local function is\_internet\_connection\_available() local socket = require("socket") local test = socket.tcp() test:settimeout(1000) -- Set timeout to 1 second local netConn = test:connect("www.google.com", 80) if netConn == nil then return false end test:close() return true end

@coronasdk771 –  I think settimeout function takes the parameter in “seconds” ( not nanoseconds) – so, the timeout line in your code should be :    test:settimeout(1)  – for 1 second

What’s an appropriate amount of time to set as the timeout value? I’m assuming the overhead here is similar to a ping, and on 3G/4G/WiFi connection this will return in under a second. But what about slower connections, like in-flight wifi, airport wifi, and developing countries? 

I guess one way to answer this is “as long as you can reasonably ask your users to wait” but I’m curious if anyone has any stats on how long this can take. Thanks!

Doesn’t your function always return true?

Should it not be…

if netConn == nil then return false else return true end test:close() 

@aaronkarp

I always test the internet connection just before I need it giving me more time in the settimeout parameter.

No, it will return false if the condition netConn is nil. The return statement aborts the function when it happens.

Your code:

if netConn == nil then return false else return true end test:close()

will never get to test:close() because it will return in either the if or the else.

Rob

You live and learn, I didn’t know that, obviously.

I’ve only ever returned true so far in my code, didn’t know return was literally return, that’s why I asked.

I desperately need an answer to this Rob: https://forums.coronalabs.com/topic/58143-need-help-with-the-social-plugin-for-facebook/

Something like this:

local socket = require("socket") local http = require("socket.http") local function testNetworkConnection()     print("testing connection")     if http.request( "http://google.com/" ) == nil then         print("cant connect to google")         return false     end     print("got a connection")     return true end

Thanks Rob

If you want to specify a timeout you can use the following snippet I come up with based on several posts on this forum.

local function is\_internet\_connection\_available() local socket = require("socket") local test = socket.tcp() test:settimeout(1000) -- Set timeout to 1 second local netConn = test:connect("www.google.com", 80) if netConn == nil then return false end test:close() return true end

@coronasdk771 –  I think settimeout function takes the parameter in “seconds” ( not nanoseconds) – so, the timeout line in your code should be :    test:settimeout(1)  – for 1 second

What’s an appropriate amount of time to set as the timeout value? I’m assuming the overhead here is similar to a ping, and on 3G/4G/WiFi connection this will return in under a second. But what about slower connections, like in-flight wifi, airport wifi, and developing countries? 

I guess one way to answer this is “as long as you can reasonably ask your users to wait” but I’m curious if anyone has any stats on how long this can take. Thanks!

Doesn’t your function always return true?

Should it not be…

if netConn == nil then return false else return true end test:close() 

@aaronkarp

I always test the internet connection just before I need it giving me more time in the settimeout parameter.

No, it will return false if the condition netConn is nil. The return statement aborts the function when it happens.

Your code:

if netConn == nil then return false else return true end test:close()

will never get to test:close() because it will return in either the if or the else.

Rob

You live and learn, I didn’t know that, obviously.

I’ve only ever returned true so far in my code, didn’t know return was literally return, that’s why I asked.

I desperately need an answer to this Rob: https://forums.coronalabs.com/topic/58143-need-help-with-the-social-plugin-for-facebook/