0

I am trying to enable or disable Wifi from Unity on my Android device. I tried to do the different things I found on the forum without success.

If I do:

using(var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
     string wifiServiceName = unityPlayer.Get<string>("WIFI_SERVICE");
     using(var wifiManager = unityPlayer.Call<AndroidJavaObject>("getSystemService", wifiServiceName))
     {
         wifiManager.Call("setWifiEnabled", false);
     }
}

I have an error saying that WIFI_SERVICE doesn't exist.

If I do:

using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
    using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService","wifi"))
    {
        wifiManager.Call<AndroidJavaObject>("setWifiEnabled", false);
    }   
}

I have an error saying that setWifiEnabled is not a function, (nor a static function if I do CallStatic).

I have my manifest.xml correctly merged, I can check that I have all the permissions on the application manager.

I spent few hours trying to figure out how to do that and I am stuck!

Does anyone know a simple way to do so?

Thanks a lot for your help,

Benjamin

1 Answer 1

3

According to Android Doc, setWifiEnabled takes bool as parameter and returns bool too.

Your second code is almost close. You got the parameter right but failed to provide the return type. You put AndroidJavaObject as the return type instead of bool.

In your second code, simply replace wifiManager.Call<AndroidJavaObject>("setWifiEnabled", false); with wifiManager.Call<bool>("setWifiEnabled", false);.

This should work, assuming that you have your permission in place. One advice to you is to put your code in a try catch clause. This will prevent some weird behavior if something is null or failed in your Android function calls.

public bool setWifiEnabled(bool enabled)
{
    using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
    {
        try
        {
            using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
            {
                return wifiManager.Call<bool>("setWifiEnabled", enabled);
            }
        }
        catch (Exception e)
        {
        }
    }
    return false;
}

public bool isWifiEnabled()
{
    using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
    {
        try
        {
            using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
            {
                return wifiManager.Call<bool>("isWifiEnabled");
            }
        }
        catch (Exception e)
        {

        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Permissions <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> and <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
com.unity3d.player.UnityPlayer, should we change to be the package name in Unity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.