Android Q programmatically connect to different WiFi AP for internet, internet and autoconnect not working

·

1 min read

https://issuetracker.google.com/issues/138335744


 public static CompletableFuture<String> connectWifi(MainActivity context, String ssid, String password) {
        CompletableFuture<String> future = new CompletableFuture<>();
        try {
               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

                System.out.println("Device is above Android 10....");
                WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
                        .setSsid(ssid)
                        .setWpa2Passphrase(password)
                        .build();

                NetworkRequest networkRequest = new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                        .setNetworkSpecifier(wifiNetworkSpecifier)
                        .build();

                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivityManager != null) {
                    connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
                        @Override
                        public void onAvailable(Network network) {
                            Log.d(TAG, "Network is available: " + network);
                            connectivityManager.bindProcessToNetwork(network);
                            future.complete("connected");
                        }

                        @Override
                        public void onUnavailable() {
                            Log.d(TAG, "Network is unavailable");
                            future.complete("notConnected");
                        }

                        @Override
                        public void onLosing(Network network, int maxMsToLive) {
                            Log.d(TAG, "Network is losing with maxMsToLive: " + maxMsToLive);
                            future.complete("networkLoosing");
                        }

                        @Override
                        public void onLost(Network network) {
                            Log.d(TAG, "Network is lost: " + network);
                            System.out.println("fix working....");
                            connectivityManager.bindProcessToNetwork(null);
                            future.complete("networkLost");
                        }
                    });
                } else {
                    future.complete("notConnected");
                }
            } else {
                Log.d(TAG, "Android level is low so Executing legacy method");
                future.complete(connectToWifiLegacy(context, ssid, password));
            }
        } catch (Exception e) {
            e.printStackTrace();
            future.completeExceptionally(e);
        }
        return future;
    }