Check if internet is available

652 views
Skip to first unread message

Vadim Ivanov

unread,
Oct 29, 2013, 7:00:09 PM10/29/13
to rob...@googlegroups.com
Hello first of all - RoboVM is really awesome! Our ported games works without any issues(excluding external sdks), I am really greatful for all the work you guys put in it and it will not be forgotten ;) We are currently trying to make everything work on iOS as it works on android and we need a way to detect if internet is available on iOS backend(libgdx). I have seen an "issue"(as enchantment) on github(https://github.com/robovm/robovm/issues/165) about the bindings for Reachability and NetworkStatus. I have found out on google that those are used to check if internet is available and since these seems to be missing, I still can't get a proper view on how the bindings are done so I cannot make them myself for the time being. Is there any other way to detect internet availability using robovm? 

If not I am thinking of just making a http request with timeout and checking if a response was received(I know that there are alot of ways this can go wrong, but this would be an easy workaround). Though it would be alot better to use the reliable way :)

Thanks :)

Niklas Therning

unread,
Nov 1, 2013, 5:53:40 AM11/1/13
to Vadim Ivanov, rob...@googlegroups.com
I think the only required functions you need are SCNetworkReachabilityCreateWithName() and SCNetworkReachabilityGetFlags(), right? Here's a hacky workaround which will let you do this (not tested, should work in theory):

@Library("SystemConfiguration")
public class Reachability {

  static {
    Bro.bind();
  }

  public static class Flags {
    public static final int TransientConnection = 1<<0;
    public static final int Reachable = 1<<1;
    public static final int ConnectionRequired = 1<<2;
    public static final int ConnectionOnTraffic = 1<<3;
    public static final int InterventionRequired = 1<<4;
    public static final int ConnectionOnDemand = 1<<5;
    public static final int IsLocalAddress = 1<<16;
    public static final int IsDirect = 1<<17;
    public static final int IsWWAN = 1<<18;
    public static final int ConnectionAutomatic = ConnectionOnTraffic;
  }

  @Bridge public static native @Pointer long SCNetworkReachabilityCreateWithName(@Pointer long allocator, BytePtr nodename);
  @Bridge public static native boolean SCNetworkReachabilityGetFlags(@Pointer long target, IntPtr flags);

}

Use it like this:

long targetRef = Reachability.SCNetworkReachabilityCreateWithName(0, BytePtr.toBytePtrAsciiZ("www.google.com"));
IntPtr flagsPtr = new IntPtr();
if (Reachability.SCNetworkReachabilityGetFlags(targetRef, flagsPtr)) {
  int flags = flagsPtr.get();
  if ((flags & Reachability.Flags.Reachable) != 0) {
    // Reachable
  }
}

Again, it should work in theory. Not tested. Let me know how it goes. Please note that targetRef should be released using CFRelease() in order to not leak memory. I've left that out here but you can bind it similarly but use @Library("CoreFoundation") instead.


--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

João Neto

unread,
Mar 9, 2014, 10:32:04 PM3/9/14
to rob...@googlegroups.com
Man, works perfectly!
Thanks!

Matthew Robinson

unread,
May 4, 2014, 3:11:32 AM5/4/14
to rob...@googlegroups.com
I implemented this and it does indeed work, thanks! However, the "SCNetworkReachabilityGetFlags" call has a very long timeout (like 1 minute) if there is no internet connection (which I simulated on my Mac using Apple's "Network Link Conditioner" tool). Any thoughts on another way to do this that makes the do-we-have-an-internet-connection is less time please?

Niklas Therning

unread,
May 7, 2014, 3:48:17 AM5/7/14
to Matthew Robinson, rob...@googlegroups.com
I think your best bet is to find some C code e.g. on stackoverflow that does what you want with a shorter timeout. Then come back here and we can see if we can help you do the same from within Java.


On Sun, May 4, 2014 at 9:11 AM, Matthew Robinson <mpr9...@gmail.com> wrote:
I implemented this and it does indeed work, thanks! However, the "SCNetworkReachabilityGetFlags" call has a very long timeout (like 1 minute) if there is no internet connection (which I simulated on my Mac using Apple's "Network Link Conditioner" tool). Any thoughts on another way to do this that makes the do-we-have-an-internet-connection is less time please?

--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages