Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Q, WifiNetworkSpecifier loosing Wifi immediately after connection is established

  @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

like image 620
Johannes Avatar asked Jan 30 '20 11:01

Johannes


People also ask

Why does Android Wi-Fi keep turning off?

This is because many apps use Wi-Fi, and they can run in the background, thus consuming your Android phone's battery. So, chances are your device will turn off the Wi-Fi to preserve the battery. If you have a battery-saving mode on, your smartphone will shut off the Wi-Fi whenever required to save on battery.

Why is my Wi-Fi connected but not working Android?

Restart your device. If restarting doesn't work, switch between Wi-Fi and mobile data: Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different. Turn Wi-Fi off and mobile data on, and check if there's a difference.

How do I stop my Wi-Fi from automatically changing?

Open the Android settings and go to Network & Internet. Select Wi-Fi > Wi-Fi preferences. Turn off the Connect to public networks toggle switch.

How do I fix my Wi-Fi icon on my Android?

Go to settings, then on Wireless and Network check to ensure that the WiFi icon is turned on. Alternatively, draw down the notification bar menu, then enable WiFi icon if it's off. Many users have reported having fixed android wifi problem by simply disabling airplane mode.


1 Answers

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

like image 80
LethalMaus Avatar answered Oct 16 '22 23:10

LethalMaus