Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...