Reputation: 111
@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
Upvotes: 10
Views: 3008
Reputation: 956
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
Upvotes: 1
Reputation: 329
Created new issue, due to fact that older was closed https://issuetracker.google.com/u/1/issues/170406306
Upvotes: 0
Reputation: 8957
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.
Upvotes: 0