As in Android Q, several WiFi APIs are restricted. I am trying to use alternate APIs to connect to different Wifi AP for internet.
Below is my code :
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid("wifi-ap-ssid");
builder.setWpa2Passphrase("wifi-ap-password");
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder();
networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest nr = networkRequestBuilder1.build();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.requestNetwork(nr, callback);
This allows me to connect but Internet is disabled. This is working as defined in Android docs.
Alternate way i tried is below :
WifiNetworkSuggestion.Builder wifiNetworkSuggestionBuilder1 = new WifiNetworkSuggestion.Builder();
wifiNetworkSuggestionBuilder1.setSsid("wifi-ap-ssid");
wifiNetworkSuggestionBuilder1.setWpa2Passphrase("wifi-ap-password");
WifiNetworkSuggestion wifiNetworkSuggestion = wifiNetworkSuggestionBuilder1.build();
List<WifiNetworkSuggestion> list = new ArrayList<>();
list.add(wifiNetworkSuggestion);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.removeNetworkSuggestions(new ArrayList<WifiNetworkSuggestion>());
wifiManager.addNetworkSuggestions(list);
declared permission in Manifest :
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Using this didn't change anything in behavior.
Please let know sequence of APIs to connect successfully to different Wifi AP with internet capability.