Android WIFI-系统连接WIFI显示网络连接受限

问题描述

使用Android设备打开设置,选择WIFI输入正确密码连接,会显示已连接,无网络,然后变成网络连接受限,实际可以使用此WIFI进行上网。

问题分析

异常Log

D NetworkMonitor/100: PROBE_DNS www.google.com 107ms OK 104.244.46.85
D NetworkMonitor/100: PROBE_DNS connectivitycheck.gstatic.com 118ms OK 203.208.50.66
D NetworkMonitor/100: PROBE_HTTP http://connectivitycheck.gstatic.com/generate_204 time=35ms ret=302 request={Connection=[close], User-Agent=[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.32 Safari/537.36]} headers={null=[HTTP/1.0 302 Moved Temporarily], Allow=[GET,POST,HEAD], Location=[http://10.10.1.2:8080/cn/index.html], MIME-Version=[1.0], Pragma=[No-Cache], Server=[AP Server 1.0], X-Android-Received-Millis=[1615220820212], X-Android-Response-Source=[NETWORK 302], X-Android-Selected-Protocol=[http/1.0], X-Android-Sent-Millis=[1615220820196]}
D NetworkMonitor/100: PROBE_HTTPS https://www.google.com/generate_204 Probe failed with exception javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
D NetworkMonitor/100: isCaptivePortal: isSuccessful()=false isPortal()=true RedirectUrl=http://10.10.1.2:8080/cn/index.html isPartialConnectivity()=false Time=172ms

从Log中可以看出来,连接WIFI会进行网络的校验,从而判断是否可用,HTTP校验成功,但是HTTPS校验失败,因此会显示网络连接受限,其连接过程中会出现短暂的已连接,无网络是正常现象。HTTPS校验的网址https://www.google.com/generate_204,由于在国内无法访问,所以会校验失败,可采取的做法是替换此网址。

packages/modules/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java

#校验源码部分
private CaptivePortalProbeResult sendDnsAndHttpProbes(ProxyInfo proxy, URL url, int probeType) {
        // Pre-resolve the captive portal server host so we can log it.
        // Only do this if HttpURLConnection is about to, to avoid any potentially
        // unnecessary resolution.
        final String host = (proxy != null) ? proxy.getHost() : url.getHost();
        // This method cannot safely report probe results because it might not be running on the
        // state machine thread. Reporting results here would cause races and potentially send
        // information to callers that does not make sense because the state machine has already
        // changed state.
        sendDnsProbe(host);
        return sendHttpProbe(url, probeType, null);
}

# HTTPS网址相关源码
private final URL mCaptivePortalHttpsUrl;
mCaptivePortalHttpsUrl = makeURL(getCaptivePortalServerHttpsUrl());
public String getCaptivePortalServerHttpUrl() {
    return getSettingFromResource(mContext, R.string.config_captive_portal_http_url,
            R.string.default_captive_portal_http_url, CAPTIVE_PORTAL_HTTP_URL);
}

解决方案

packages/modules/NetworkStack/res/values/config.xml

<!-- HTTP URL for network validation, to use for detecting captive portals. -->
<string name="default_captive_portal_http_url" translatable="false">http://connectivitycheck.gstatic.com/generate_204</string>
<!-- HTTPS URL for network validation, to use for confirming internet connectivity. -->
- <string name="default_captive_portal_https_url" translatable="false">https://www.google.com/generate_204</string>
+ <string name="default_captive_portal_https_url" translatable="false">https://developers.google.cn/generate_204</string>

网上还有另一种解法如下,就是关闭校验,此解法可以实现不显示网络连接受限的问题,但是在连接需要登陆的WIFI时,无法连接

frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

loadSetting(stmt, Settings.Global.LID_BEHAVIOR, defaultLidBehavior);
loadStringSetting(stmt, Settings.Global.NTP_SERVER_2,
            R.string.def_ntp_server_2);
+ loadSetting(stmt, Global.CAPTIVE_PORTAL_MODE, Global.CAPTIVE_PORTAL_MODE_IGNORE);

关于captive_portal_mode的值

0:彻底禁用检测

1:检测到需要登录则弹窗提醒(默认值)

2:检测到需要登录则自动断开此热点并不再自动连接

相关推荐
H1001 小时前
重构(二)
android·重构
拓端研究室1 小时前
R基于贝叶斯加法回归树BART、MCMC的DLNM分布滞后非线性模型分析母婴PM2.5暴露与出生体重数据及GAM模型对比、关键窗口识别
android·开发语言·kotlin
zhangphil2 小时前
Android简洁缩放Matrix实现图像马赛克,Kotlin
android·kotlin
m0_512744642 小时前
极客大挑战2024-web-wp(详细)
android·前端
lw向北.2 小时前
Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
android·开发语言·qt
不爱学习的啊Biao2 小时前
【13】MySQL如何选择合适的索引?
android·数据库·mysql
Clockwiseee3 小时前
PHP伪协议总结
android·开发语言·php
mmsx9 小时前
android sqlite 数据库简单封装示例(java)
android·java·数据库
众拾达人12 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
吃着火锅x唱着歌13 小时前
PHP7内核剖析 学习笔记 第四章 内存管理(1)
android·笔记·学习