3

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
emahrv
  • 91
  • 7
  • Try to load that url in a browser on your phone. – blackapps Aug 03 '20 at 11:48
  • You cannot use the http:// protocol unless you request clearTextTraffic in manifest file. – blackapps Aug 03 '20 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 ? – emahrv Aug 04 '20 at 07:01
  • "You cannot use the http:// protocol unless you request clearTextTraffic in manifest file" what does that means ? – emahrv Aug 04 '20 at 07:02
  • Then your problem has nothing to do with Android. – blackapps Aug 04 '20 at 07:03
  • You do not need an internet connection for such a local ip like 192.168.4.1. What kind of server is running with that address on that device? What kind of device? The last .1 would indicate that it is a router. – blackapps Aug 04 '20 at 07:05
  • Google for usesClearTextTraffic. – blackapps Aug 04 '20 at 07:09
  • If I manually connect my phone on to the wifi network I reach the webserver in chrome, but if I fogot the wifi network and I connect the wifi network through my app I can't reach the webserver so the probleme is in the app. It's an ESP8266 with an embedded webserver – emahrv Aug 04 '20 at 07:20
  • usesClearTextTraffic is already set on "True" in my manifest – emahrv Aug 04 '20 at 07:27
  • Can your app reach the webserver if you manually connect your phone? You did not report this scenario. – blackapps Aug 04 '20 at 07:36
  • Can Chrome reach the webserver if your app connects? You also did not report this scenario. – blackapps Aug 04 '20 at 07:37
  • Manually connect : Chrome reach the webserver well / My app reach webserver well – emahrv Aug 04 '20 at 08:05
  • Connect through my app : Chrome can't reach webserver / my app can't reach webserver – emahrv Aug 04 '20 at 08:05
  • Ok. Now finally all is clear. I did not understand your post at all. – blackapps Aug 04 '20 at 08:17
  • With android 10 the wifi connection as changed, so when I scan my Wifi QR Code a small window open and ask "Device to us with MyApp" and show the wifi network I wan't to connect, I have to confirm, then the wifi is connected but through the app. If I go in wifisettings I see i'm connected to the wifi network but it's write "Connected via MyApp" but when I'm connected via the app I can't reach my webserver. If I mannually connect to the wifi network it's write "Connected" and I can reach my webserver. What I don't understand is why when I connect wifi network via my app i can't reach webserver – emahrv Aug 04 '20 at 08:28
  • Sorry, at the moment i cannot try your code. – blackapps Aug 04 '20 at 09:38
  • Have you time to try my code please ? i'm stuck and I really need help. – emahrv Aug 13 '20 at 12:20
  • I have no ESP8266 running. Is your problem for an esp only or for all routers/access points? – blackapps Aug 13 '20 at 12:27
  • Your last picture has Connectiontype: cell instead of the first one who has Wi-Fi. Try in your code to set a connection type if possible. – blackapps Aug 13 '20 at 12:33
  • 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 ? – emahrv Aug 13 '20 at 12:39
  • I dont know. What is this: setNetworkSpecifier(specifier) doing? – blackapps Aug 13 '20 at 12:42
  • I try on another phone without sim card, same problem – emahrv Aug 13 '20 at 12:53
  • Also answer my question please. – blackapps Aug 13 '20 at 12:55
  • I found the solution ! Found it on a russian forum. Add this connectivityManager.bindProcessToNetwork(network); to use wifi connection and don't switch to cells connection even if there is no internet connection. – emahrv Aug 13 '20 at 13:02
  • Thanks for help @blackapps – emahrv Aug 13 '20 at 13:09
  • On a Russian forum! Great. I knew you were French ;-). – blackapps Aug 13 '20 at 13:20

1 Answers1

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

}

emahrv
  • 91
  • 7
  • 1
    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? – vijju Jun 05 '21 at 12:28
  • 1
    I am having the same issue. And this is not working for me – vanlooverenkoen Dec 02 '21 at 13:22
  • It seems that it's an issue that many devs are having, here is the Issue Tracker https://issuetracker.google.com/issues/138335744 – Gonzalo Ledezma Torres Feb 17 '22 at 15:42