前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android的Wifi连接

Android的Wifi连接

作者头像
None_Ling
发布2019-10-29 17:31:04
2.5K0
发布2019-10-29 17:31:04
举报
文章被收录于专栏:Android相关Android相关

幕后

最近在做Wifi连接的功能,在网上查找了很多资料,可用的也比较少,最后遇到很多了问题,一路走来也解决了很多问题,特此记录。

8.0Wifi无法扫描

  • 6.0版本中如果未开启GPS是无法获取到扫描列表
  • 需要动态申请ACCESS_COARSE_LOCATION权限

解决Android6.0以上扫描WIFI获得列表为空

WifiManager的getScanResults()返回列表为0

Android6.0 扫描WiFi列表的问题

Wifi的加密方式

Wifi加密方式有很多种方式:

加密方式

场景

配置

None

开放网络,不加密

无需密码

WEP

旧的加密方式,不推荐使用

仅需密码

WPA/WPA2

最常见的加密方式

仅需密码

EAP

企业加密方式

ID+密码验证

代码语言:javascript
复制
    static final int SECURITY_NONE = 0;
    static final int SECURITY_WEP = 1;
    static final int SECURITY_PSK = 2;
    static final int SECURITY_EAP = 3;

    private int getType(ScanResult result) {
        if (result == null) {
            return SECURITY_NONE;
        }
        String capbility = result.capabilities;
        if (capbility == null || capbility.isEmpty()) {
            return SECURITY_NONE;
        }
        // 如果包含WAP-PSK的话,则为WAP加密方式
        if (capbility.contains("WPA-PSK") || capbility.contains("WPA2-PSK")) {
            return SECURITY_WPA;
        } else if (capbility.contains("WPA2-EAP")) {
            return SECURYTI_EAP;
        } else if (capbility.contains("WEP")) {
            return SECURITY_WEP;
        } else if (capbility.contains("ESS")) {
            // 如果是ESS则没有密码
            return SECURITY_NONE;
        }
        return SECURITY_NONE;
    }

Root下的Wifi存储位置

在有了Root权限后,可以在/data/misc/wifi/WifiConfigStore.xml中看到已经连接/保存配置的Wifi信息,包括Id和密码。

代码语言:javascript
复制
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<WifiConfigStoreData>
<int name="Version" value="1" />
<NetworkList>
<Network>
<WifiConfiguration>
<string name="ConfigKey">SSID+WPA_EAP</string>
<string name="SSID">SSID</string>
<null name="BSSID" />
<null name="PreSharedKey" />
<null name="WEPKeys" />
<int name="WEPTxKeyIndex" value="0" />
<boolean name="HiddenSSID" value="false" />
<boolean name="RequirePMF" value="false" />
<byte-array name="AllowedKeyMgmt" num="1">0c</byte-array>
<byte-array name="AllowedProtocols" num="1">03</byte-array>
<byte-array name="AllowedAuthAlgos" num="1">01</byte-array>
<byte-array name="AllowedGroupCiphers" num="1">0f</byte-array>
<byte-array name="AllowedPairwiseCiphers" num="1">06</byte-array>
<boolean name="Shared" value="true" />
<int name="WapiPskKeyType" value="-1" />
<null name="WapiAsCert" />
<null name="WapiUserCert" />
<int name="EapSimSlot" value="-1" />
<int name="AutoJoinNetwork" value="1" />
<int name="Status" value="0" />
<null name="FQDN" />
<null name="ProviderFriendlyName" />
<null name="LinkedNetworksList" />
<null name="DefaultGwMacAddress" />
<boolean name="ValidatedInternetAccess" value="true" />
<boolean name="NoInternetAccessExpected" value="false" />
<int name="UserApproved" value="0" />
<boolean name="MeteredHint" value="false" />
<int name="MeteredOverride" value="0" />
<boolean name="UseExternalScores" value="false" />
<int name="NumAssociation" value="2" />
<int name="CreatorUid" value="1000" />
<string name="CreatorName">android.uid.system:1000</string>
<string name="CreationTime">time=10-18 20:06:33.868</string>
<int name="LastUpdateUid" value="1000" />
<string name="LastUpdateName">android.uid.system:1000</string>
<int name="LastConnectUid" value="1000" />
<boolean name="IsLegacyPasspointConfig" value="false" />
<long-array name="RoamingConsortiumOIs" num="0" />
</WifiConfiguration>
<NetworkStatus>
<string name="SelectionStatus">NETWORK_SELECTION_ENABLED</string>
<string name="DisableReason">NETWORK_SELECTION_ENABLE</string>
<null name="ConnectChoice" />
<long name="ConnectChoiceTimeStamp" value="-1" />
<boolean name="HasEverConnected" value="true" />
</NetworkStatus>
<IpConfiguration>
<string name="IpAssignment">DHCP</string>
<string name="ProxySettings">NONE</string>
</IpConfiguration>
<WifiEnterpriseConfiguration>
<string name="Identity">身份</string>
<string name="AnonIdentity"></string>
<string name="Password">密码</string>
<string name="ClientCert"></string>
<string name="CaCert"></string>
<string name="SubjectMatch"></string>
<string name="Engine">0</string>
<string name="EngineId"></string>
<string name="PrivateKeyId"></string>
<string name="AltSubjectMatch"></string>
<string name="DomSuffixMatch"></string>
<string name="CaPath"></string>
<int name="EapMethod" value="0" />
<int name="Phase2Method" value="0" />
<string name="PLMN"></string>
<string name="Realm"></string>
</WifiEnterpriseConfiguration>
</Network>
</NetworkList>
<PasspointConfigData>
<long name="ProviderIndex" value="0" />
</PasspointConfigData>
</WifiConfigStoreData>

如果需要进行Wifi连接的开发的话,则在系统的Wifi连接后,对比缺少哪些字段,在代码中进行设置即可。

在配置Wifi时,也必须要Root/System权限才能够连接

常见问题

1. 无法保存WifiEnterpriseConfiguration

原因

在EAP的连接方式中,必须在enterpriseConfig中设置EapMethod以及Phase2Method,否则系统不会将该配置保存到WifiConfigStore.xml中。

代码
代码语言:javascript
复制
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
// 对输入的配置设置EAP加密方式
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    // 设置EAP方式,如果不设置,则无法连接上该Wifi
    config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
    config.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
     // 设置身份
     config.enterpriseConfig.setIdentity(account);
     // 设置密码
      config.enterpriseConfig.setPassword(pass);
     }
    config.status = WifiConfiguration.Status.ENABLED;
  }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 幕后
  • 8.0Wifi无法扫描
  • Wifi的加密方式
  • Root下的Wifi存储位置
  • 常见问题
    • 1. 无法保存WifiEnterpriseConfiguration
      • 原因
      • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档