10
  @RequiresApi(api = Build.VERSION_CODES.Q)
public void openSystemDialogToConnectToWifi(String ssid, ConnectivityManager.NetworkCallback callback) {
    WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
    builder.setSsid(ssid);
    builder.setWpa2Passphrase("secret");


    WifiNetworkSpecifier 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) getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.requestNetwork(networkRequest, callback);
}

This is the code I use, to connect to a new Wifi from my App. I get an Ip-Adress, the wifi Symbol is visible very briefly in the status bar. In the next Moment, Wifi-Symbol is gone and the system Dialog is visible again, to connect to the wifi.

When I debug the Callback it is going through the methods in this Order:

  1. onAvailable
  2. onCapabilitiesChanged
  3. onBlockedStatusChanged (blocked: false)
  4. onCapabilitiesChanged
  5. onLost

all methods do nothing (just call super.method())

Hardware: OnePLus 6 with Android Q

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Johannes
  • 101
  • 1
  • 4

3 Answers3

1

I have been tracking & researching this a lot. All my findings along with my current most optimal solution can be found here

But more specific to your question is the following info taken from the link

As stated here by Google, some OEM Roms are not 'holding on to the request' and therefore the connection is dropping instantly. OnePlus have fixed this problem in some of their later models but not all. This bug will continuously exist for certain phone models on certain Android builds, therefore a successful fallback (i.e. a manual connection with no network disruption) is required. No known workaround is available

  • removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) wont help keep the connection on a OnePlus as stated here

  • setting the Bssid wont help keep the connection on a OnePlus as stated here

  • google cannot help, they have stated it is out of their hands here

  • OnePlus forum posts confirming it working for some models (but not all) after an update, see here, here & here

LethalMaus
  • 798
  • 1
  • 8
  • 20
  • 1
    You could improve your answer following this tutorial: https://stackoverflow.com/help/how-to-answer For instance, you could give more information instead of the link. – Pankwood Jan 05 '21 at 13:39
0

I was facing the same issue before, later modified the "NetworkRequest" as below

WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
    .setSsid(ssid)
    .setWpa2Passphrase(password)
    .build();
NetworkRequest networkRequest = new NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
        .addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
        .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING)
        .setNetworkSpecifier(specifier)
        .build();

I just removed few of the unwanted/default Network capabilities given in the "onCapabilitiesChanged()" callback. Now the wifi is not fluctuating and no fail over is happening.

hacker
  • 8,919
  • 12
  • 62
  • 108
  • I modified networkRequest as suggested above answer still its keeps disconnecting. any idea about this – Jayanth Mar 12 '20 at 10:23
  • Please check below link may help, - https://forums.oneplus.com/threads/can-not-connect-to-a-hotspot-with-wifinetworkspecifier.1180889/ - https://issuetracker.google.com/issues/143549613 – Krunal Indrodiya Apr 23 '20 at 11:07
  • The link and the answer doesn't help. That issue still persists. Is there any work around? – Rethinavel Oct 22 '20 at 16:04
0

Created new issue, due to fact that older was closed https://issuetracker.google.com/u/1/issues/170406306

user1221256
  • 329
  • 1
  • 2
  • 7