0

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);
QHu91_IT
  • 189
  • 2
  • 12
  • In onAvailable set the network on wifi. `network.`. Your compiler will let you choose the function which i forgot the name of. – blackapps Aug 18 '20 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: https://issuetracker.google.com/issues/138335744. Still don't know how to workaround it. – QHu91_IT Aug 19 '20 at 09: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? – blackapps Aug 19 '20 at 09:10
  • Here the list function of Network: https://i.imgur.com/yWvQmD5.png can you see it, thanks. – QHu91_IT Aug 19 '20 at 09:19
  • 2
    Its a bit different sorry but its all here: https://stackoverflow.com/questions/63227000/android-10-wifi-connection-cant-reach-gateway?noredirect=1#comment112099948_63227000 `connectivityManager.bindProcessToNetwork(network);` – blackapps Aug 19 '20 at 09:19
  • Now I know that wifi connected and only app can access other will be not, the system still show icon wifi no internet connection but it is ok. For continue connect other wifi need disconnect current wifi first. – QHu91_IT Aug 19 '20 at 13:13

1 Answers1

1

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.

Tuan
  • 64
  • 7