强制Android在没有互联网的情况下使用Wifi网络 [英] Force Android to Use Wifi network with no internet

查看:633
本文介绍了强制Android在没有互联网的情况下使用Wifi网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要通过WiFi网络进行通信的android应用,该网络将无法访问互联网.问题在于,即使连接了WiFi,当wifi网络上没有连接互联网时,android仍会选择使用蜂窝/移动数据.

I am building an android app that needs to communicate over a WiFi network that will not have any internet access. The problem is that even when the WiFi is connected android chooses to use cellular/mobile data when no connection internet is present on the wifi network.

我已经阅读了许多有关该问题的文章,其中许多涉及到对设备进行植根,但对于生产应用程序则不可能(对设备进行植根(不是是一种选择)).其他解决方案(例如下面的代码)建议使用bindProcessToNetwork(),它可以在我的Sony Z2上完美运行,但不能在我测试过的其他设备(都运行6.0.1)上正常运行

I have read many posts on the issue many of which involve rooting the device but that is not possible with a production app (rooting devices is not an option). other solution (like my code bellow) suggest using bindProcessToNetwork() which works perfectly on my Sony Z2 but not on other devices I have tested on (all running 6.0.1)

private void bindToNetwork() {
    final ConnectivityManager connectivityManager = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new NetworkRequest.Builder();
        //set the transport type do WIFI
        builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


                    connectivityManager.bindProcessToNetwork(null);
                    if (barCodeData.getSsid().contains("ap-name")) {
                        connectivityManager.bindProcessToNetwork(network);
                    }

                } else {
                    //This method was deprecated in API level 23
                    ConnectivityManager.setProcessDefaultNetwork(null);
                    if (barCodeData.getSsid().contains("ap-name")) {

                        ConnectivityManager.setProcessDefaultNetwork(network);
                    }
                }

                connectivityManager.unregisterNetworkCallback(this);
            }
        });
    }
}

推荐答案

能否尝试将全局设置captive_portal_detection_enabled设置为0(假).

Could you try and set the global setting captive_portal_detection_enabled to 0 (false).

实际上发生的是,默认情况下,每次您连接到wifi时,FW都会针对服务器(通常是google)进行测试,以查看它是否是强制性的wifi(需要登录).因此,如果您的wifi未连接到Google,则此检查将失败.此后,设备将知道wifi没有互联网连接,并且不会自动连接到它.

What's actually happening is that by default, everytime you connect to a wifi, the FW will test against a server (typically google) to see if it's a captive wifi (needs login). So if your wifi is not connected to google, this check will fail. After that, the device knows that wifi has no internet connection and simply will not autoconnect to it.

将此设置设置为0,将避免进行此检查.

Setting this setting to 0, will avoid this check.

以编程方式: Settings.Global.putInt(getContentResolver(), Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 0);

您可能需要直接使用字符串"captive_portal_detection_enabled",而不是根据Android版本不可见的常量.

You may need to use the string "captive_portal_detection_enabled" directly, instead of the constant that's not visible depending on Android version.

这篇关于强制Android在没有互联网的情况下使用Wifi网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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