本文介绍了在Unity中的Android上启用/禁用Wifi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android设备上从Unity启用或禁用Wifi.我尝试做在论坛上发现的不同事情,但没有成功.

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.

如果我这样做:

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

我有一个错误,说WIFI_SERVICE不存在.

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

如果我这样做:

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

我有一个错误,说setWifiEnabled不是函数,(如果我执行CallStatic也不是静态函数).

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

我已正确合并我的manifest.xml,我可以检查我是否拥有对应用程序管理器的所有权限.

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?

非常感谢您的帮助,

本杰明

推荐答案

根据 Android文档setWifiEnabledbool作为参数,并且也返回bool .

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

您的第二个代码几乎快要结束了.参数正确,但是无法提供返回类型.您将AndroidJavaObject而不是bool用作返回类型.

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.

在第二个代码中,只需将wifiManager.Call<AndroidJavaObject>("setWifiEnabled", false);替换为wifiManager.Call<bool>("setWifiEnabled", false);.

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

这应该有效,前提是您已获得许可.给您的一种建议是将您的代码放在try catch子句中.如果您的Android函数调用中的某些内容为null或失败,这将防止某些奇怪的行为.

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

这篇关于在Unity中的Android上启用/禁用Wifi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:47