Geoab
Geoab

Reputation: 75

Android getActiveNetwork always returns null

I am developing a Tango Application with Unity and the Tango SDK, however, I need to be able to check the device's Wifi connectivity and connect to a Wifi Network accordingly.

With that in mind I started working on an Android Unity Network Plugin, but I am having troubles checking the device's connectivity; even though Wifi is on and the Device is indeed connected to a Wifi network, getActiveNetwork keeps returning null.

I spent a couple of days searching for a workaround, or an alternative implementation, but I couldn't find anything that works, below is the code I ended up using to perform the check after looking through the many Android Connectivity related questions I looked at, as well as all the permissions I am using in the Manifest.

(I would like to point out that I am currently returning an integer as a means to quickly debug the function when called via Unity C# Scripts, and right now the function always returns 0.)

 public int IsConnectedToWifi(){

        //SCCActivity activity = new SCCActivity();
        ConnectivityManager cm = (ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm == null) return  -2;
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();


        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                DebugToast(activeNetwork.getTypeName());
                return 1;
                //return activeNetwork.isConnected();
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                DebugToast(activeNetwork.getTypeName());
                return 2;
                //return false;
            }
        }
        else {
           DebugToast("There is no active Network");
            return 0;
            //return false;
        }

        DebugToast("Failed to get a Connectivity Manager");
        return -1;
        //return false;
    }

In AndroidManifest file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

I would really appreciate any advice or guidance, thank you.

Upvotes: 3

Views: 2650

Answers (4)

jadro
jadro

Reputation: 21

I is a bit old but maybe it helps someone. In my case when I changed the order of permissions in manifest in a way that ACCESS_NETWORK_STATE is before INTERNET then it worked

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

strange but works

Upvotes: 2

Geoab
Geoab

Reputation: 75

Upon further debugging and looking through similar cases, I found and solved the issue.

I will leave my solution here in case someone else comes across a similar problem.

Basically, getActiveNetwork wasn't really returning null at all, it turns out that even though the permission was listed on the xml, "android.permission.ACCESS_NETWORK_STATE" was never granted and the function was throwing up an exception. The cause of the problem was the fact that, as I mentioned before, I am working on a Tango Application using the Tango SDK for Unity, and the xml provided by the Tango SDK was overwriting the xml from the Java plugin.

The solution was simple; I just added the permissions to the Tango SDK's xml instead.

Upvotes: 0

Fabio
Fabio

Reputation: 2824

I have been using this successfully in devices with kitkat and later:

public static boolean isConnectedWifi(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}

I have also compared it to ConnectivityManager.TYPE_MOBILE in the past but can't remember how good or bad it worked.

Upvotes: 0

AJay
AJay

Reputation: 1193

Hey I don't know if you really want to check that is phone is connected to WIFI or MOBILE_INTERNET.

But if you just want to check that if mobile have connectivity than below code works fine for me

public static final boolean isNetworkAvailable(Context context) {

    boolean connected = false;
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = connectivityManager.getActiveNetworkInfo();
    if (nf != null && (nf.isConnected() || nf.isConnectedOrConnecting())) {
        // we are connected to a network
        connected = true;
    } else
        connected = false;

    return connected;
}

Upvotes: 0

Related Questions