xamarin maui 获取iOS Wi-Fi ssid 和切换热点hotspot
在xamarin或Maui中,如果你想获取手机当前WiFi的SSID,或者切换指定的WiFi,请随我来操作。
一、在你的iOS开发者账号的Identity中编辑权限,选择Access WiFi information 和 hotsport两个权限,必选。
二、在VS中打开项目,选择 工具->选项->xamarin->App 账户,选择账号并更新预配置文件。
三、选择iOS项目,iOS捆绑包签名->自定义权利选中Entitlements.plist文件
四、在Entitlements.plist文件右击选择 打开方式->iOS权利编辑器->勾选访问WiFi信息和热点配置。
五、定义Dependency接口,在iOS平台实现如下方法:
1 public string GetWiFiSSID() 2 { 3 try 4 { 5 6 7 8 NSDictionary dict; 9 var status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict); 10 11 if (status == StatusCode.NoKey) 12 return ""; 13 if (dict != null) 14 { 15 var bssid = dict[CaptiveNetwork.NetworkInfoKeyBSSID]; 16 var ssid = dict[CaptiveNetwork.NetworkInfoKeySSID]; 17 18 return ssid.ToString(); 19 } 20 //var ssiddat = dict [CaptiveNetwork.NetworkInfoKeySSIDData]; 21 22 //if (withMacAddress) 23 // return string.Format("{0} [{1}]", ssid, bssid); 24 //else 25 // return ssid.ToString(); 26 27 28 string[] wifilist; 29 30 CaptiveNetwork.TryGetSupportedInterfaces(out wifilist); 31 32 foreach (string intf in wifilist) 33 { 34 NSDictionary dict2; 35 CaptiveNetwork.TryCopyCurrentNetworkInfo(intf, out dict2); 36 37 //if (status == StatusCode.NoKey) 38 // return ""; 39 40 var bssid2 = dict2[CaptiveNetwork.NetworkInfoKeyBSSID]; 41 var ssid2 = dict2[CaptiveNetwork.NetworkInfoKeySSID]; 42 var ssiddat2 = dict2[CaptiveNetwork.NetworkInfoKeySSIDData]; 43 } 44 return ""; 45 46 } 47 catch(Exception ex) 48 { 49 return ""; 50 } 51 }
1 public async void SwitchWifi(string sSSID, string password) 2 { 3 try 4 { 5 //NEHotspotConfigurationManager.SharedManager.GetConfiguredSsids((listWifi) => 6 //{ 7 // if (listWifi != null) 8 // { 9 // foreach (string ssid_selected in listWifi) 10 // { 11 // if (ssid_selected == sSSID) 12 // { 13 // NEHotspotConfigurationManager.SharedManager.RemoveConfiguration(sSSID); 14 // } 15 // } 16 // } 17 //}); 18 var wiFiConfig = new NEHotspotConfiguration(sSSID, password, false); 19 wiFiConfig.JoinOnce = false; 20 //await NEHotspotConfigurationManager.SharedManager.ApplyConfigurationAsync(wiFiConfig); 21 //wifiManager = new NEHotspotConfigurationManager(); 22 //wifiManager.ApplyConfiguration(wiFiConfig, err => 23 //{ 24 // Console.WriteLine(""); 25 //}); 26 NEHotspotConfigurationManager.SharedManager.ApplyConfiguration(wiFiConfig, a => 27 { 28 Console.WriteLine(""); 29 }); 30 } 31 catch (Exception ex) 32 { 33 34 } 35 }