我的应用连接到热点,然后将其删除。我的问题是,使用API 29之后,手机无法重新连接到以前的WiFi网络。您知道如何使用新的API吗?
该代码曾经可以使用,但是现在已经过时了:
WifiManager wifiManager =
(WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
var wifiConfig = new WifiConfiguration{Ssid = $"\"{hotspotName}\""};
var network = wifiManager.ConfiguredNetworks.FirstOrDefault(n => n.Ssid == wifiConfig.Ssid);
wifiManager.Disconnect();
wifiManager.RemoveNetwork(networkId);
wifiManager.Reconnect();
无法重新连接到可用WiFi的新代码:
WifiNetworkSuggestion.Builder wifiNetworkSuggestionBuilder = new
WifiNetworkSuggestion.Builder();
wifiNetworkSuggestionBuilder.SetSsid(ssid);
WifiNetworkSuggestion wifiNetworkSuggestion =
wifiNetworkSuggestionBuilder.Build();
IList<WifiNetworkSuggestion> wifiNetworkSuggestions = new
List<WifiNetworkSuggestion> { wifiNetworkSuggestion };
NetworkStatus networkStatus =
WifiManager.RemoveNetworkSuggestions(wifiNetworkSuggestions);
答案 0 :(得分:1)
在API级别29 / Q中不推荐使用Reconnect
和RemoveNetwork
方法,并且在该API级别始终返回false。
您现在应该在29 / Q上使用WifiNetworkSpecifier.Builder()
来建立应用程序级作用域的wifi连接,并且由于它是应用程序作用域的,因此您不必删除它并重新连接到用户以前作为操作系统的wifi网络现在将为您完成。