@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:
all methods do nothing (just call super.method())
Hardware: OnePLus 6 with Android Q
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.
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.
Open the Android settings and go to Network & Internet. Select Wi-Fi > Wi-Fi preferences. Turn off the Connect to public networks toggle switch.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With