1

I connect to wifi by code below, It can connect successful but no internet connection (android show icon network with x), don't know why.

WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
    .setSsid(ssid)
    .setWpa2Passphrase(password)
    .build();

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

connectivityManager = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);

networkCallback = new ConnectivityManager.NetworkCallback() {
    @Override
    public void onAvailable(@NonNull Network network) {
        super.onAvailable(network);
        connectivityManager.bindProcessToNetwork(network);
    }
};
connectivityManager.requestNetwork(networkRequest, networkCallback);
6
  • In onAvailable set the network on wifi. network.. Your compiler will let you choose the function which i forgot the name of. Commented Aug 18, 2020 at 15:57
  • Thanks but can you explain more, I still don't know what function need to use. I searching a lot and found that link may problem come from android 10: issuetracker.google.com/issues/138335744. Still don't know how to workaround it. Commented Aug 19, 2020 at 9:08
  • I forgot the name. But if you just type network. in Android Studio you are presented with a lot of functions to choose from. Which one to choose should be clear then. Now does AS act as i said? Commented Aug 19, 2020 at 9:10
  • Here the list function of Network: i.imgur.com/yWvQmD5.png can you see it, thanks. Commented Aug 19, 2020 at 9:19
  • 2
    Its a bit different sorry but its all here: stackoverflow.com/questions/63227000/… connectivityManager.bindProcessToNetwork(network); Commented Aug 19, 2020 at 9:19

1 Answer 1

2

Change your network request

val networkRequest: NetworkRequest = NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .setNetworkSpecifier(wifiNetworkSpecifier)
                    .build()

But I got an issue when I called to request to add a network again, networkCallback called in onUnavailable, and nothing to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.