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