使用Xamarin.iOS和Xamarin.Android,可以检查是否启用了Wi-Fi? 并且,如果禁用,则可以启用它吗? 一旦启用,如何搜索并连接到网络?
答案 0 :(得分:9)
WifiManager wifiManager = (WifiManager)GetSystemService(WifiService);
if (!wifiManager.IsWifiEnabled)
wifiManager.SetWifiEnabled(true);
else
wifiManager.SetWifiEnabled(false);
android.permission.CHANGE_WIFI_STATE
var networkSSID = "Stack";
var networkPass = "Overflow";
var config = new WifiConfiguration();
config.Ssid = '"' + networkSSID + '"';
// For WPA/WPA2, WEP is different (still using WEP? shame on you ;-)
config.PreSharedKey = '"' + networkPass + '"';
wifiManager.AddNetwork(config);
IList<WifiConfiguration> myWifi = wifiManager.ConfiguredNetworks;
wifiManager.Disconnect();
wifiManager.EnableNetwork(myWifi.FindFirst(x => x.Ssid.Contains(networkSSID)), true);
wifiManager.Reconnect();