Android 10 wifi connection can't reach gateway

前端 未结 1 1763
既然无缘
既然无缘 2021-01-16 08:57

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

1条回答
  •  离开以前
    2021-01-16 09:23

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

    }

    0 讨论(0)
提交回复
热议问题