通过WifiNetworkSpecifier的Android

通过WifiNetworkSpecifier的Android

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

问题描述

我当前正在尝试连接到wifi网络.我使用了以下代码.

I'm currently trying to connect to a wifi network. I used the below code.

        WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
                .setSsid(ssid)
                .setBssid(MacAddress.fromString(bssid))
                .setWpa2Passphrase(password)
                .build();

        NetworkRequest request = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .setNetworkSpecifier(specifier)
                .build();

        ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        manager.requestNetwork(request, new ConnectivityManager.NetworkCallback() {

            @Override
            public void onAvailable(@NonNull Network network) {
                ConnectivityManager.setProcessDefaultNetwork(network);
                super.onAvailable(network);
                NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if (info != null && info.isConnectedOrConnecting()) {
                    if (!emitter.isDisposed()) {
                        emitter.onSuccess(true);
                    }
                } else {
                    if (!emitter.isDisposed()) {
                        emitter.onError(new RuntimeException("OS Disconnected"));
                    }
                }

            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
                if (!emitter.isDisposed()) {
                    emitter.onError(new RuntimeException("Could not connect Wifi"));
                }
            }
        });
    });

问题:上面的代码在Pixel手机和诺基亚手机上运行良好,但是在Oneplus设备上,我得到了祝酒词连接成功祝酒词,并且在该wifi断开连接后立即获得了祝酒词.wifi符号在状态栏中非常短暂地可见.在下一瞬间,Wifi符号消失,系统对话框再次可见,以连接到wifi.

issue: the above code works well with Pixel phone and Nokia but on Oneplus devices, I get toast connection success toast and immediately after this wifi gets disconnected. 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.

在下面提供了权限,并且授予了位置权限,并且该权限也已在设备中启用.

Have given below the permissions, and location permission is granted and it is enabled in the device also.

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

当我调试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()

我已经提到了这些 Android Q,以编程方式连接到其他WiFi互联网接入点
WiFi网络连接在Android Q上不断断开连接 Android 10(Q)中的Wifi网络请求Api连接问题 Android Q,WifiNetworkSpecifier在建立连接后立即失去Wifi

,我找不到解决方案.

网络请求中是否缺少导致断开连接的任何内容.或任何其他解决方案表示赞赏.

Is there anything I'm missing in network request that causes disconnection. or any other solution appreciated.

谢谢.

推荐答案

到2020年9月,该问题似乎仍未在某些设备上得到解决,Oneplus 6,诺基亚手机等似乎都表现出这种成功的连接循环.同时,如果您有iOT设备,则您的应用程序正在尝试自动连接,但失败了,您可以在onFail()中尝试此操作,以关闭连接对话框,并让用户优雅地进入wifi页面以手动连接.

This issue still doesn't seem to be fixed as of September 2020 on certain devices, Oneplus 6, Nokia phones etc seem to be exhibiting this success connection loop. In the meantime if you have a iOT device your app is trying to auto connect and it fails you can try this in the onFail(), to dismiss the connection dialog and take your users gracefully to the wifi page to manually connect.

        connectivityManager.bindProcessToNetwork(null);
        connectivityManager.unregisterNetworkCallback(networkCallback);
        AlertDialog.Builder alert = new AlertDialog.Builder(...);
        alert.setTitle(...);
        alert.setMessage(...);
        alert.setPositiveButton("OK",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(intent);
            }
          });
        alert.show();
      }

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

08-23 05:48