我正在尝试在Android(Jelly Bean 4.2.2)上启用Wifi区域(Wifi AP),但我找不到解决方法。

我实际上可以启用/禁用wifi连接,但不能启用AP(共享Wifi)。

我正在使用Xamarin(C#),但它在SDK上使用相同的API,因此Java中的任何示例也都可以使用(只需进行少量编辑)。

谢谢大家,祝你有美好的一天。

最佳答案

我一段时间前找到了一种方法,这是:

//Enable / Disable Wifi AP
public void SetHotSpot(Boolean On)  {

    WifiManager wifiManager = (WifiManager) context.GetSystemService(Context.WifiService);
    Method[] wmMethods = wifiManager.Class.GetDeclaredMethods();
    WifiConfiguration wifiConfig = new WifiConfiguration();
    Boolean enabled = false;

    //Verify The Wifi AP State And Get The Actual Config
    foreach (Method m in wmMethods) {

        //Get Wifi AP State
        if (m.Name.Equals ("isWifiApEnabled")) {

            enabled = (Boolean) m.Invoke (wifiManager);
        }

        //Get Actual Config And Change Password
        if (m.Name.Equals ("getWifiApConfiguration")) {

            try {

                wifiConfig = (WifiConfiguration)m.Invoke (wifiManager, null);
                Random rand = new Random ();

                wifiConfig.Ssid = "Wifi Name " + rand.Next(0, 10) + rand.Next(0, 10) + rand.Next(0, 10) + rand.Next(0, 10);
                wifiConfig.PreSharedKey = "Password" + rand.Next(0, 10) + rand.Next(0, 10);
            } catch (Exception ex) {

                Log.Info (Activity, "Fail Setting New Wifi Name And Password\n\n" + ex);
            }
        }
    }

    //If Received Flag Is TRUE And Wifi AP Is Disabled, Enable It
    if (On && !enabled) {

        //Start Wifi AP With New Configuration
        foreach (Method m in wmMethods) {

            if (m.Name.Equals ("setWifiApEnabled")) {

                try {

                    m.Invoke (wifiManager, wifiConfig, true);
                } catch (Exception ex) {

                    Log.Info (Activity, "Fail Enabling Wifi AP\n\n" + ex);
                }
                break;
            }
        }
    }

    //If Received Flag Is FALSE And Wifi AP Is Enabled, Disable It
    else if (!On && enabled) {

        foreach(Method method in wmMethods) {

            if(method.Name.Equals("setWifiApEnabled"))  {

                try {

                    method.Invoke(wifiManager, wifiConfig, false);
                } catch (Exception ex) {

                    Log.Info (Activity, "Fail Disabling Wifi AP\n\n" + ex);
                }
                break;
            }
        }
    }
}

关于c# - 如何使用Xamarin(C#)在Android上启用Wifi区域(Wifi AP)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21862998/

10-14 17:26