问题
@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:
- onAvailable
- onCapabilitiesChanged
- onBlockedStatusChanged (blocked: false)
- onCapabilitiesChanged
- onLost
all methods do nothing (just call super.method())
Hardware: OnePLus 6 with Android Q
来源:https://stackoverflow.com/questions/59984518/android-q-wifinetworkspecifier-loosing-wifi-immediately-after-connection-is-est