4

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...

26
  • Try to load that url in a browser on your phone. Commented Aug 3, 2020 at 11:48
  • You cannot use the http:// protocol unless you request clearTextTraffic in manifest file. Commented Aug 3, 2020 at 11:49
  • I try to load the url in chrome and it's the same, I can't reach my webserver... The network doesn't have internet connection does that change something ? Commented Aug 4, 2020 at 7:01
  • "You cannot use the http:// protocol unless you request clearTextTraffic in manifest file" what does that means ? Commented Aug 4, 2020 at 7:02
  • 1
    The problem is for all routers/access points. Yes when I connect via my app it doesn't switch it's default route to wifi, how can I switch to wifi route ? Commented Aug 13, 2020 at 12:39

1 Answer 1

3

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);

}

Sign up to request clarification or add additional context in comments.

3 Comments

Hii emahrv. I am also having the same issue like wifi is connected but no network. i have also added that bindProcessToNetwork. but didn't work. can you please tell any other possibilities for this?
I am having the same issue. And this is not working for me
It seems that it's an issue that many devs are having, here is the Issue Tracker issuetracker.google.com/issues/138335744

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.