0

After Android Q, I connected to WiFi through the following code:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q)
        {
            NetworkSpecifier specifier =
                    new WifiNetworkSpecifier.Builder()
                            .setSsidPattern(new PatternMatcher(ssid, PatternMatcher.PATTERN_PREFIX))
                            .setWpa2Passphrase(password)
                            .build();

        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                        .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                        .setNetworkSpecifier(specifier)
                        .build();

        final ConnectivityManager connectivityManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
  ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                // do success processing here..
                Log.d(TAG, "onAvailable:" + network);
                super.onAvailable(network);
                LogUtil.e("network.getSocketFactory()",network.getSocketFactory()+"------->");
                boolean b = connectivityManager.bindProcessToNetwork(network);         
            }

            /**
             * 
             */
            @Override
            public void onLost(Network network) {
                super.onLost(network);
                Log.e(TAG, "onLost ==>" + network.toString());

            }

            @Override
            public void onUnavailable() {
                // do failure processing here..
                LogUtil.e("qwe","failed");
            }
        };
        connectivityManager.requestNetwork(request, networkCallback);
    }

However, there is a problem. The connected WiFi can only be used within the range of my APP. The browser of the mobile phone or other APPs cannot use the network.

Does anyone know what the problem is, I hope everyone tells me how to deal with it so that other apps can use the network.

thank you very much.

Ankit Mahadik
  • 2,275
  • 22
  • 35
caoxu
  • 1
  • Does this answer your question? [Connect to Wifi in Android Q programmatically](https://stackoverflow.com/questions/63124728/connect-to-wifi-in-android-q-programmatically) – NizarETH Nov 11 '20 at 12:13

1 Answers1

1

The WifiNetworkSpecifier is intended for local peer-to-peer connection. It provides no internet and is available only inside your app. This is how it's designed (see the note on the blue background here).

To get the internet connection that will work in the entire device use Suggestion API (also available only from Android 10).

Mohru
  • 725
  • 1
  • 7
  • 17