Android Q, WifiNetworkSpecifier loosing Wifi immediately after connection is established

房东的猫 提交于 2020-02-02 13:07:28

问题


  @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

来源:https://stackoverflow.com/questions/59984518/android-q-wifinetworkspecifier-loosing-wifi-immediately-after-connection-is-est

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!