【问题标题】:How Can I Enable The Wifi Zone (Wifi AP) On Android Using Xamarin (C#)?如何使用 Xamarin (C#) 在 Android 上启用 Wifi 区域 (Wifi AP)?
【发布时间】:2014-02-18 19:03:36
【问题描述】:

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

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

我正在开发 Xamarin (C#),但它在 SDK 上使用相同的 API,因此 java 中的任何示例都会有所帮助。

【问题讨论】:

    标签: c# java android xamarin.android android-wifi


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      相关资源
      最近更新 更多