javaandroidunity-game-enginewifiandroid-connectivitymanager

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.


Solution

  • 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.