建立连接后,Android Q,WifiNetworkSpecifier会立即失去Wifi [英] Android Q, WifiNetworkSpecifier loosing Wifi immediately after connection is established

查看:140
本文介绍了建立连接后,Android Q,WifiNetworkSpecifier会立即失去Wifi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @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);
}

这是我用来从我的应用程序连接到新Wifi的代码.我得到一个IP地址,状态栏中显示的wifi符号非常简短.在下一瞬间,Wifi符号消失,系统对话框再次可见,以连接到wifi.

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.

当我调试Callback时,它将按以下顺序进行操作:

When I debug the Callback it is going through the methods in this Order:

  1. onAvailable
  2. onCapabilitiesChanged
  3. onBlockedStatusChanged(已阻止:否)
  4. onCapabilitiesChanged
  5. onLost

所有方法什么都不做(只需调用super.method())

all methods do nothing (just call super.method())

硬件:具有Android Q的OnePLus 6

Hardware: OnePLus 6 with Android Q

推荐答案

我一直在跟踪&研究了很多.在此处

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

如Google在此处所述所述,某些OEM Roms并未坚持"请求",因此连接会立即断开.一加(OnePlus)在其某些较新的模型中解决了此问题,但并非全部.对于某些Android机型上的某些手机型号,此错误将继续存在,因此需要成功进行后备(即不中断网络连接的手动连接).没有已知的解决方法

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)不能帮助保持OnePlus上的连接,如按照这里

    google无法帮助,他们说这是无法控制的这里

    google cannot help, they have stated it is out of their hands here

    OnePlus论坛帖子确认更新后可在某些型号(但不是全部)上使用,请参阅此处&此处

  • OnePlus forum posts confirming it working for some models (but not all) after an update, see here, here & here

    这篇关于建立连接后,Android Q,WifiNetworkSpecifier会立即失去Wifi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆