[Solved]Cocos2d-x check internet connection availability

Hello. I did a serious googling over checking internet availability on cocos2d-x (in my case android). Came up with this code in cocos2d-x forum:

#include "curl/include/android/curl/curl.h"

int score;
int interstitialCounter = 0;

bool isInternetConnected(void) {
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        if (res == 0)
            return true;
        else
            return false;
    }
    else
        return false;
}

This code itself does the job, however i gues it is not an asynchronous and causes lag. Is there a prober and an asynchronous way to do it?

You have to make your own platform-dependant code for that.
Your solution is far from OK:

  1. it might take a long time before returning that internet is down
  2. you’ll use data (not good at all for people on cellular) just to check if internet is available

Any solution on how to do that?

I once searched for that to do my own connectivity class.

For android, you have:

For iOS you have Apple’s Reachability class

For windows, it’s quite easy, there is a windows Api for that (InternetGetConnectedState)

Thank you, I’ll give it a try.

How can I integrate it to my project?

On my side, I created a library in c++ which uses a different file depending on the platform.
a .mm file on ios
a .java file on android and another .cpp for the JNI bridge
directly a .cpp file on windows8.1

You can easily to the same I guess.
I cannot share my files, they are part of a big library.

do anyone have plugin for that ?

1 Like

That would be great if anyone shares one.

and i need a sample cpp for the bridge if possible.

Anyone can help?

Are you allowed (ie app store approval) to perform your ‘reachability’ using the method’s above - even though they use some cellular data etc. Is it ok to ping google as long as you handle it elegantly (no crashes or poor ui).

Or, must you confirm reachability before attempting any internet access?

Useful to know for this thread.

Thanks.

I’m not sure it’s disallowed by Apple (or Microsoft/google), but it’s definitely not a good practice to charge the user with unnecessary cellular data (even a dns+ping message). Especially if we can achieve the same goal using system functions.

I’m fairly new at this. Trying to integrate emil’s code:

bool checkConn = false;
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, "com/emil/android/util/Connectivity", "isConnected", "()Z")) {
        jboolean result = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
        if (result == JNI_TRUE) {
            checkConn = true;

        } else {
            checkConn = false;
        }
    }
    if(checkConn){
        log("baglanti: connected");
    }else{
        log("baglanti: not connected");
    }

The Connectivity.java file is located in proj.android/com/emil/android/util. But the checkConn above always returns false. Can you point what I am missing here?

I’m guessing the getStaticMethodInfo method returns false, thus you are not calling the java method.

Yes, indeed it is. But why? Is it the path of the Connectivity.java?

For those who want to use here is how i did it:

Connectivity.java(Located in: proj.android/src/com/emil/android/util/)

package com.emil.android.util;

import org.cocos2dx.lib.Cocos2dxActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * Check device's network connectivity and speed 
 * @author emil http://stackoverflow.com/users/220710/emil
 *
 */
public class Connectivity extends Cocos2dxActivity{
  
    /**
     * Get the network info
     * @param context
     * @return
     */
    public static NetworkInfo getNetworkInfo(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo();
    }
    
    /**
     * Check if there is any connectivity
     * @param context
     * @return
     */
    public static boolean isConnected(){
        NetworkInfo info = Connectivity.getNetworkInfo(Cocos2dxActivity.getContext());
        return (info != null && info.isConnected());
    }
}

In one of your header files:(Mine is Definitions.h)

#include "jni.h"
#include "platform/android/jni/JniHelper.h"

bool isInternetConnected(void) {
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, "com/emil/android/util/Connectivity", "isConnected", "()Z")) {
        jboolean result = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
        if (result == JNI_TRUE) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

Then in one of your cpp file include the Definitions.h
And finally in cpp file check if internet connection is available:

if(isInternetConnected()){
    // your code
}
1 Like

please check the link http://stackoverflow.com/questions/31875785/internet-connection-checking-android-ios-cocos2dx-v3-6 .what’s wrong with this code…? Thank you Friends!!

hello i wrote a wrapper for ios and android internet checking. You can reach it fromthis github link

It has three features (hasInternetConnection, isConnectionFast, isConnectionMobile) for iOS and android but if you want you can add another ones. Hope It helps someone.

1 Like

Hello sir, i am adding this class that time i will occur bellow issue. please help me to solve it.

Undefined symbols for architecture x86_64:
OBJC_CLASS$_Reachability”, referenced from:
objc-class-ref in Connectivity.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Thanks,
DK

1 Like