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.
Required permissions in the manifest file. Programmatically add a new wifi network to the WifiManager. With this new wifi network added to the WifiManager, when the device is located within this wifi network, the WifiManager will automatically connect to this new wifi network.
No, This is not possible to enable or disable Wi-Fi programmatically from Android-10 API level 29 [Until google provides an alternative solution].
You can only be connected to one network at the time. However, there is a standard called WiFi direct that will allow you to do what you are asking for, but it isn't yet implemented in Android.
Try calling bindProcessToNetwork() in onAvailable() callback to regain network connectivity, it works fine for me.
Connect to network:
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);
ConnectivityManager.NetworkCallback networkCallback = new
ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
Log.d(TAG, "onAvailable:" + network);
cm.bindProcessToNetwork(network);
}
});
cm.requestNetwork(nr, networkCallback);
Disconnect from the bound network:
cm.unregisterNetworkCallback(networkCallback);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With