您好,登錄后才能下訂單哦!
如何在Android中連接匿名WiFi?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
1.權(quán)限
Android中要使用系統(tǒng)功能一般都要申請權(quán)限,這里wifi需要的權(quán)限有
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> // 需要系統(tǒng)權(quán)限 [定位權(quán)限]
因?yàn)榭梢岳脀ifi進(jìn)行定位,所以這里需要申請定位權(quán)限,在6.0以上設(shè)備,定位權(quán)限需要主動(dòng)申請。
2.API
類名 | 功能 |
---|---|
WifiManager | wifi統(tǒng)一管理類,進(jìn)行各種wifi操作 |
WifiInfo | 描述當(dāng)前連接的wifi熱點(diǎn)信息 |
WifiConfiguration | wifi網(wǎng)絡(luò)配置信息 |
ScanResult | 描述掃描出的wifi熱點(diǎn)的信息 |
WifiManager類是framework層暴露的api,用來管理wifi。
val mWifiManager = mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
通過他可以得到:1.已經(jīng)配置的網(wǎng)絡(luò)列表。2.當(dāng)前連接的wifi。3.掃描到的wifi。4.以及一些常量表示廣播的意圖等
ScanResult類用于存放wifi掃描結(jié)果信息,主要有以下內(nèi)容:
屬性 | 描述 |
---|---|
SSID | 描述wifi熱點(diǎn)的名稱,就是大家搜索到的直接名稱,如ChinaNet |
BSSID | 姑且理解成熱點(diǎn)的mac地址,但實(shí)際有所不同 |
networkID | 數(shù)字型的id |
level | 描述wifi信號強(qiáng)弱的值,值是負(fù)數(shù),絕對值越小,信號越強(qiáng) |
capabilities | 如加密方式,如WEP |
3.監(jiān)聽設(shè)備wifi狀態(tài)的改變
wifi狀態(tài)的改變是會(huì)導(dǎo)致廣播事件的發(fā)生。
val filter = IntentFilter() filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) //監(jiān)聽wifi狀態(tài)改變 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) //監(jiān)聽掃描到wifi列表改變
private val mReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (TextUtils.isEmpty(action)) return when (action) { WifiManager.WIFI_STATE_CHANGED_ACTION -> { } WifiManager.SCAN_RESULTS_AVAILABLE_ACTION -> { } } } }
WifiManager之中有當(dāng)前狀態(tài)的enum類型,可以看下表:
名稱 | 描述 |
---|---|
WIFI_STATE_DISABLING | wifi正在關(guān)閉 |
WIFI_STATE_DISABLED | wifi關(guān)閉 |
WIFI_STATE_ENABLING | wifi正在開啟 |
WIFI_STATE_ENABLED | wifi開啟 |
WIFI_STATE_UNKNOWN | wifi未知 |
連接普通wifi
連接wifi我大致分為以下幾步:
獲取想要連接WiFi熱點(diǎn)的SSID、加密方式信息,和用戶輸入的密碼
根據(jù)上述信息來創(chuàng)建wifiConfigruation對象
調(diào)用WifiManager的方法,傳入wifiConfigruation,完成wifi連接
public static void connectNewWifi(Context mContext, WifiConfiguration wifiConfiguration) { WifiManager wifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); int networkId = wifiManager.addNetwork(wifiConfiguration); wifiManager.enableNetwork(networkId, true); }
所以重點(diǎn)是怎樣創(chuàng)建wifiConfigruation,不同的加密方式的wifi,創(chuàng)建過程也不太一樣:
public WifiConfiguration createWifiConfig(String SSID, @WifiCipherType int wifiCipherType, String password) { WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = convertToQuotedString(SSID); switch (wifiCipherType) { case WifiCipherType.SECURITY_NONE: wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); break; case WifiCipherType.SECURITY_WEP: wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE); wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); if (!TextUtils.isEmpty(password)) { int length = password.length(); // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) if ((length == 10 || length == 26 || length == 58) && password.matches("[0-9A-Fa-f]*")) { wifiConfiguration.wepKeys[0] = password; } else { wifiConfiguration.wepKeys[0] = '"' + password + '"'; } } break; case WifiCipherType.SECURITY_PSK: wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK); if (!TextUtils.isEmpty(password)) { if (password.matches("[0-9A-Fa-f]{64}")) { wifiConfiguration.preSharedKey = password; } else { wifiConfiguration.preSharedKey = '"' + password + '"'; } } break; case WifiCipherType.SECURITY_EAP: wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP); wifiConfiguration.allowedKeyManagement.set(KeyMgmt.IEEE8021X); wifiConfiguration.enterpriseConfig = new WifiEnterpriseConfig(); int eapMethod = 0; int phase2Method = 0; wifiConfiguration.enterpriseConfig.setEapMethod(eapMethod); wifiConfiguration.enterpriseConfig.setPhase2Method(phase2Method); if (!TextUtils.isEmpty(password)) { wifiConfiguration.enterpriseConfig.setPassword(password); } break; default: break; } return wifiConfiguration; }
至此wifi就連接完成了,然后可以在廣播中獲取連接結(jié)果。相應(yīng)的wifi配置信息會(huì)被保存在/data/misc/wifi/wpa_supplicant.conf中:
network={ ssid="test" psk="88888888" key_mgmt=WPA-PSK disabled=1 id_str="%7B%22creatorUid%22%3A%221000%22%2C%22configKey%22%3A%22%5C%22test%5C%22WPA_PSK%22%7D" }
連接匿名wifi
匿名wifi相較于普通wifi,不同之處在于不會(huì)廣播其SSID,所以就不能被直接掃描到,需要我們輸入wifi的SSID來主動(dòng)進(jìn)行掃描,先來看下匿名wifi的配置信息:
network={ ssid="test2" scan_ssid=1 bssid=56:28:f8:fa:f8:a0 psk="44444444" key_mgmt=WPA-PSK disabled=1 id_str="%7B%22creatorUid%22%3A%221000%22%2C%22configKey%22%3A%22%5C%22test2%5C%22WPA_PSK%22%7D" }
可以看到,多了一個(gè)scan_ssid屬性,查看WifiConfiguration,確實(shí)有一個(gè)屬性可以設(shè)置
/** * This is a network that does not broadcast its SSID, so an * SSID-specific probe request must be used for scans. */ public boolean hiddenSSID;
所以在創(chuàng)建WifiConfiguration的時(shí)候多設(shè)置下這個(gè)屬性就行了:
configuration.hiddenSSID = true
關(guān)于如何在Android中連接匿名WiFi問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。