我的项目是iOS设备中的Xamarin窗体,它使用WIFI与其他设备协同工作。我想知道是否可以先知道当前连接的ssid名称。如果我的应用程序失去了链接,它是否可以悄悄地尝试重新连接到WiFi,而不需要用户做任何事情呢?这是我的逻辑图
发布于 2022-10-26 02:16:43
您可以使用DependencyService获取SSID名称,您可以在服务中引用Xamarin.Forms DependencyService,尝试如下代码:
string ssid = "";
string[] supportedInterfaces;
StatusCode status = CaptiveNetwork.TryGetSupportedInterfaces(out supportedInterfaces);
if ((status = CaptiveNetwork.TryGetSupportedInterfaces(out supportedInterfaces)) == StatusCode.OK)
{
foreach (var item in supportedInterfaces)
{
NSDictionary info;
status = CaptiveNetwork.TryCopyCurrentNetworkInfo(item, out info);
if (status == StatusCode.OK)
{
ssid = info[CaptiveNetwork.NetworkInfoKeySSID].ToString();
}
}
}在您的info.plist中,您应该添加以下键:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your Description</string>获得SSID名称(和密码)后,可以使用NEHostspotConfiguration进行连接。
NEHotspotConfiguration hotspotConfiguration = new NEHotspotConfiguration(ssid,pwd,false);
hotspotConfiguration.JoinOnce = true;
NEHotspotConfigurationManager.SharedManager.ApplyConfiguration(hotspotConfiguration, (NSError error) =>
{
if (error != null)
{
if (error?.LocalizedDescription == "already associated.")
{
IsConnected = true;
}
else
{
IsConnected = false;
}
}
else
{
IsConnected = true;
}
});您还可以在Apple门户上访问WiFi信息、应用程序ID的热点功能,并将它们添加到Entitlements.plist中。不要忘记重新生成您的供应文件。希望我的回答能帮到你~
https://stackoverflow.com/questions/74178879
复制相似问题