I try to make a simple app that connect the smartphone to a specify wifi network and redirect to a webview that show a webserver connected on the wifi network by flashing a
I finally found the solution, just need to add a line that tell the app to bind the process to the wifi network you just connect.
connectivityManager.bindProcessToNetwork(network);
The final code :
public void successFlash(String ssid, String password) {
WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
.setSsid(ssid)
.setWpa2Passphrase(password)
.build();
NetworkRequest request = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.setNetworkSpecifier(specifier)
.build();
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
public void onAvailable(Network network) {
connectivityManager.bindProcessToNetwork(network);
openActivity_redirect_webview();
}
@Override
public void onUnavailable() {
// do failure processing here..
openQrcodeActivity();
}
};
connectivityManager.requestNetwork(request, networkCallback);
}