Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

On android 10 mobile phone. I seldom engaged in an issue.

The Enablenetwork() function doesn't work.

Here is some code

            WifiConfiguration Config = new WifiConfiguration();
            Config.Ssid = "\"" + SSID + "\"";
            temp = WiFi.AddNetwork(Config);
            WiFi.SaveConfiguration();
            bool Enable_Success = WiFi.EnableNetwork(temp, true);

On my device(Samsung s9, android 10)

The function just sometimes works. Sometimes not working.

When not working. I observe the wifi status of my phone.

The wifi just keep connecting to current connected SSID.

Not even tried to connected to target SSID. Just keep it's current status.

What would be the problem?

Thank you!

Maybe you can try disconnect other network first.

        wifiManager.disconnect();// disconnect first
        WifiConfiguration Config = new WifiConfiguration();
        Config.Ssid = "\"" + SSID + "\"";
        temp = WiFi.AddNetwork(Config);
        WiFi.SaveConfiguration();
        bool Enable_Success = WiFi.EnableNetwork(temp, true);

Assuming that the network is open (meaning without any password), then you need to explicitly say it in the configuration object.

Config.AllowedKeyManagement.Set((int)KeyManagementType.None);
                Thank you for your response! But both the current connected SSID, and the target SSID. Are password protected. And the case I engaged is not 100% happen. Sometimes it works. Sometimes not. But both these two case are in same environment.
– CC.Wang
                May 23, 2020 at 2:59

Android 10 uses a different set of functions to connect to wifi than previous versions of Android. You will want to do something like this:

var connectivity_manager = GetSystemService(Context.ConnectivityService) as ConnectivityManager;    
var wifi_network_specifier = (new WifiNetworkSpecifier.Builder()).SetSsid(ssid).Build();
var network_request = (new NetworkRequest.Builder()).AddTransportType(TransportType.Wifi)
    .SetNetworkSpecifier(wifi_network_specifier).Build();
connectivity_manager.RequestNetwork(network_request, network_callback);

Note that this code assumes an open network. If you have a WPA network, you can add the password to the WifiNetworkSpecifier.Builder. Also, as far as I can tell, the new WifiNetworkSpecifier.Builder does not support WEP networks. So, if you are attempting to programmatically connect to a WEP network with Android 10, you may have a difficult time.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.