0

i am developing an android application (single-mode) in which app is connected to wifi programmatcally. Everything works and i get a message "Connected" But when i use internet in the application, it is not working. I have attached code and sample image. please help.

  WifiNetworkSpecifier.Builder builder = null;
    WifiNetworkSpecifier wifiNetworkSpecifier = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        builder = new WifiNetworkSpecifier.Builder();
        builder.setSsid(networkSSID);
        builder.setWpa2Passphrase(networkPass);
        wifiNetworkSpecifier = builder.build();
        NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
        networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
        networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED);
        networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
        NetworkRequest networkRequest = networkRequestBuilder.build();
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(@NonNull Network network) {
                    super.onAvailable(network);
                    connectionStatus[0] = "true";


                }

                @Override
                public void onLost(Network network) {
                    connectionStatus[0] = "false";
                }
            });
        

enter image description here

1 Answer 1

0

i was able to figure out the problem by adding the following line of code in onAvailble function

cm.bindProcessToNetwork(network);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.