3

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.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Geoab
  • 75
  • 6

4 Answers4

2

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

Aalexander
  • 4,987
  • 3
  • 11
  • 34
jadro
  • 21
  • 2
0

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;
}
AJay
  • 1,233
  • 10
  • 19
  • Hey, thank you for your reply! I just tested the code, but the result is the same, getActiveNetworkInfo keeps returning null even though the device is connected to a network, and since nf is always null, the function returns false as well. – Geoab Aug 09 '17 at 04:33
  • @Geoab you checking in emulator or real device ! And you connected to WIFI ? – AJay Aug 09 '17 at 04:36
  • I am checking on a real Device, and the device is connected to WIFI – Geoab Aug 09 '17 at 04:39
0

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.

Fabio
  • 2,654
  • 16
  • 31
  • Thank you for replying. I am trying to test your code, but I get a "cannot resolve Connectivity symbol" error, is Connectivity not a part of the android.net.ConnectivityManager package? do I need to import a different package? – Geoab Aug 09 '17 at 04:51
  • My bad, I was sure this was vanilla android code but actually Connectivity is created in my project. What it does in the end is to call getActiveNetworkInfo like the things you've been trying. I'll delete this answer soon since it's not adding anything. – Fabio Aug 09 '17 at 12:13
0

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.

Geoab
  • 75
  • 6