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 QR Code.
When I flash my QR Code (Simple Wifi QR Code) the app ask if I want to connect to the wifi network, I confirm and then it redirect to a webview but the webview is blank... It looks like I'm connected to the wifi but I can't reach the network...
Part of my QR Code Activity :
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) {
openActivity_redirect_webview();
}
@Override
public void onUnavailable() {
// do failure processing here..
openQrcodeActivity();
}
};
connectivityManager.requestNetwork(request, networkCallback);
}
My webview Activity :
public class activity_redirect_webview extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redirect_webview);
webView = (WebView) findViewById(R.id.redirect_webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("http://192.168.4.1/index.html");
}
}
Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-feature android:name="android.permission.WRITE_SETTINGS"
android:required="false"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.wifi" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
EDIT : My phone is a Samsung Galaxy S20 Ultra with Android 10
I install a Network Analyzer app and it confirm that I'm not really connected to the wifi network.
Manualy connection to the wifi network :
Screenshot Network Analyzer Manualy connected to wifi
Connection by scanning QR Code from My App :
Screenshot Succes QR Code scan, confirm connection
Screenshot wifi settings actual network Connected via My App
Screenshot Network Analyzer connected to wifi via My App
As we can see it's look like i'm connected to the wifi network but it doesn't gives me ip address and I can't ping my web server...
Does I need more permissions in AndrooidManifest ??
I don't understand why when I use my app to connect to a wifi network i got this problem... I try with others wifi network and it's the same. I'm totally stuck and I found no solution on forums...
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);
}