With Android 10, I'm using the following method to connect to my Wifi Access Point :
@RequiresApi(api = Build.VERSION_CODES.Q)
public static void connectToWifiAccessPoint(String AP_SSID, String AP_PASSWORD, ConnectivityManager connectivityManager) {
WifiNetworkSpecifier.Builder builder = null;
builder = new WifiNetworkSpecifier.Builder();
builder.setSsid(AP_SSID);
builder.setWpa2Passphrase(AP_PASSWORD);
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
connectivityManager.bindProcessToNetwork(network);
}
});
}
A few seconds after calling this method, the OS displays this pop-up:
After hitting the connect button, the same pop-up appears a few seconds later, and the smartphone is still not connected to the access point.
Any idea how to fix this please ?
I struggle couple days ago with wifi connection on Andrdoid Q, the link of my question in stackOverflow.
So, the solution is compile your app with targetSdkVersion 28. and for connection to wifi use this function :
public void connectToWifi(String ssid, String key) {
Log.e(TAG, "connection wifi pre Q");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + key + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
if (netId == -1) netId = getExistingNetworkId(wifiConfig.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}