1

I spent an hour googling and everything I see boils down to a simple code:

var wifiManager = ApplicationContext.GetSystemService(Context.WifiService).JavaCast<WifiManager>();

WifiConfiguration conf = new WifiConfiguration() { Ssid = @"""ssid""", PreSharedKey = @"""passw""" };

var id = wifiManager.AddNetwork(conf);
var enabled = wifiManager.EnableNetwork(id, true);

if i set ssid/password to a WiFi which is in the list on my Android phone the id is -1 and nothing happens.

if i set those to a fictitious values like abc/abc I get a realistic id.

so I am not sure - what is the purpose of this code in real-life if you cannot connect to an existing Wi-Fi ?

how do i connect to an 'existing' Wi-Fi in API level 29+? (by existing - I mean the phone has it stored in the list and it is currently visible in Wi-Fi scan)

What I really want is to switch to another Wi-Fi in my home when I move from one room to another

Xamarin.Forms 4.5 | Android 10.0

Boppity Bop
  • 9,613
  • 13
  • 72
  • 151

1 Answers1

0

Please note that WifiManager API is deprecated and will cease to work when targeting Android Q SDK

The documentation of addNetwork says

Add a new network description to the set of configured networks. [emphasis mine]

You - however - wrote

if i set ssid/password to a WiFi which is in the list on my Android phone [...]

which is per definition not a new network, but an existing one, hence addNetwork fails and returns -1.

Try to use GetConfiguredNetworks and filter the List<WifiConfiguration> by the field WifiConfiguration.SSID. You can then use WifiConfiguration.NetworkId for EnableNetwork.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57