Search code examples
c#androidunity-game-enginewifi

Enable/Disable Wifi on Android in Unity


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


Solution

  • 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;
    }