Home > Mobile >  Android Wi-fi Connected through App but NOT Working
Android Wi-fi Connected through App but NOT Working

Time:07-20

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

CodePudding user response:

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

cm.bindProcessToNetwork(network);
  • Related