Android的Wifi连接
2019-10-28 本文已影响0人
None_Ling
幕后
最近在做Wifi连接的功能,在网上查找了很多资料,可用的也比较少,最后遇到很多了问题,一路走来也解决了很多问题,特此记录。
8.0Wifi无法扫描
- 6.0版本中如果未开启GPS是无法获取到扫描列表
- 需要动态申请
ACCESS_COARSE_LOCATION
权限
WifiManager的getScanResults()返回列表为0
Wifi的加密方式
Wifi加密方式有很多种方式:
加密方式 | 场景 | 配置 |
---|---|---|
None | 开放网络,不加密 | 无需密码 |
WEP | 旧的加密方式,不推荐使用 | 仅需密码 |
WPA/WPA2 | 最常见的加密方式 | 仅需密码 |
EAP | 企业加密方式 | ID+密码验证 |
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和密码。
<?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
中。
代码
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;
}