jacksinrow
jacksinrow

Reputation: 49

How to compatible with Android10.0 Wi Fi connection "addnetwork" method Return -1?

"Addnetwork" is used below Android 9.0. This method returns correctly, but it fails when Android 10.0 is used. It always returns - 1. What is the reason? This is my core code:

    public WifiConfiguration createWifiInfo(String SSID, String Password,
                                        int Type) {
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\"";
    WifiConfiguration tempConfig = this.isExsits(SSID);
    if (tempConfig != null) {
        mWifiManager.removeNetwork(tempConfig.networkId);
    }

    if (Type == 1) // WIFICIPHER_NOPASS
    {
        config.wepKeys[0] = "";
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;
    }
  
    return config;
}

Partial code:

 WifiConfiguration wifiInfo = createWifiInfo("", "", 1);
 networkId = mWifiManager.addNetwork(wifiInfo);//result: networkId:-1

Upvotes: 0

Views: 465

Answers (1)

Alexander Hoffmann
Alexander Hoffmann

Reputation: 6014

WifiManager.addNetwork(WifiConfiguration config) has been deprecated in API level 29. Instead, developers are expected to use the WifiNetworkSpecifier or "Wi-Fi suggestion API" as stated in the link above. Both APIs show a different system dialog which asks the user to connect to a specified network.

You can find an example for the WifiSuggestions API in this StackOverflow thread: Creating a custom wifi setup

Upvotes: 2

Related Questions