Android的Wifi連接
- 2019 年 10 月 29 日
- 筆記
幕後
最近在做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+密碼驗證 |
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; }