Reputation: 447
My app connects to a hotspot and then remove it. My issue is that the phone does not reconnect after to the previous WiFi network with the API 29. Any idea how to use new API?
This code used to work but is now obsolete:
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();
new code that fails to reconnect to available 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);
Upvotes: 0
Views: 859
Reputation: 74184
The Reconnect
and RemoveNetwork
methods was deprecated in API level 29/Q and always return false on that API level.
You should now use WifiNetworkSpecifier.Builder()
on 29/Q to have an application-level scope wifi connection and since it is app scoped, you do not have to remove it and reconnect to the user's previous wifi network as the OS will now do it for you.
Upvotes: 1