Android Wifi控制方法總結,androidwifi

來源:互聯網
上載者:User

Android Wifi控制方法總結,androidwifi

寫了一個工具類來輔助處理wifi相關狀態控制和串連等功能,參考了網上的相關檔案,並修改了裡面的一些bug和問題,本文給出的方法均經過驗證可用~

package com.xys.jrdtraining.network;import java.util.List;import android.content.Context;import android.net.wifi.ScanResult;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.net.wifi.WifiManager.WifiLock;/** * wifi ctrl utils class *  * @author yisheng.xu *  */public class WifiOpenHelper {// 定義WifiManager對象private WifiManager mWifiManager;// 定義WifiInfo對象private WifiInfo mWifiInfo;// 掃描出的網路連接列表private List<ScanResult> mWifiList;// 網路連接列表private List<WifiConfiguration> mWifiConfiguration;// 定義一個WifiLockWifiLock mWifiLock;// 構造器public WifiOpenHelper(Context context) {// 取得WifiManager對象mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);// 取得WifiInfo對象mWifiInfo = mWifiManager.getConnectionInfo();}// 開啟WIFIpublic void openWifi() {if (!mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(true);}}// 斷開當前網路public void disconnectWifi() {if (!mWifiManager.isWifiEnabled()) {mWifiManager.disconnect();}}// 關閉WIFIpublic void closeWifi() {if (mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(false);}}// 檢查當前WIFI狀態public int checkState() {return mWifiManager.getWifiState();}// 鎖定WifiLockpublic void acquireWifiLock() {mWifiLock.acquire();}// 解鎖WifiLockpublic void releaseWifiLock() {// 判斷時候鎖定if (mWifiLock.isHeld()) {mWifiLock.acquire();}}// 建立一個WifiLockpublic void creatWifiLock() {mWifiLock = mWifiManager.createWifiLock("Test");}// 得到配置好的網路public List<WifiConfiguration> getConfiguration() {return mWifiConfiguration;}// 指定配置好的網路進行串連public void connectConfiguration(int index) {// 索引大於配置好的網路索引返回if (index > mWifiConfiguration.size()) {return;}// 串連配置好的指定ID的網路mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,true);}public void startScan() {mWifiManager.startScan();// 得到掃描結果mWifiList = mWifiManager.getScanResults();// 得到配置好的網路連接mWifiConfiguration = mWifiManager.getConfiguredNetworks();}// 得到網路列表public List<ScanResult> getWifiList() {return mWifiList;}// 查看掃描結果public StringBuilder lookUpScan() {StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < mWifiList.size(); i++) {stringBuilder.append("Index_" + Integer.valueOf(i + 1).toString()+ ":");// 將ScanResult資訊轉換成一個字串包// 其中把包括:BSSID、SSID、capabilities、frequency、levelstringBuilder.append((mWifiList.get(i)).toString());stringBuilder.append("/n");}return stringBuilder;}// 得到MAC地址public String getMacAddress() {return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();}// 得到存取點的BSSIDpublic String getBSSID() {return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();}// 得到IP地址public int getIPAddress() {return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();}// 得到串連的IDpublic int getNetworkId() {return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();}// 得到WifiInfo的所有資訊包public String getWifiInfo() {return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();}// 添加一個網路並串連public void addNetwork(WifiConfiguration wcg) {int wcgID = mWifiManager.addNetwork(wcg);boolean b = mWifiManager.enableNetwork(wcgID, true);System.out.println("a--" + wcgID);System.out.println("b--" + b);}// 斷開指定ID的網路public void disconnectWifi(int netId) {mWifiManager.disableNetwork(netId);mWifiManager.disconnect();}public WifiConfiguration CreateWifiInfo(String SSID, String Password,int Type) {WifiConfiguration config = new WifiConfiguration();config.allowedAuthAlgorithms.clear();config.allowedGroupCiphers.clear();config.allowedKeyManagement.clear();config.allowedPairwiseCiphers.clear();config.allowedProtocols.clear();config.SSID = "\"" + SSID + "\"";WifiConfiguration tempConfig = this.IsExsits(SSID);if (tempConfig != null) {mWifiManager.removeNetwork(tempConfig.networkId);}if (Type == 1) // WIFICIPHER_NOPASS{config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);}if (Type == 2) // WIFICIPHER_WEP{config.hiddenSSID = true;config.wepKeys[0] = "\"" + Password + "\"";config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);config.wepTxKeyIndex = 0;}if (Type == 3) // WIFICIPHER_WPA{config.preSharedKey = "\"" + Password + "\"";config.hiddenSSID = true;config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);config.status = WifiConfiguration.Status.ENABLED;}return config;}private WifiConfiguration IsExsits(String SSID) {List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();for (WifiConfiguration existingConfig : existingConfigs) {if (existingConfig.SSID.equals("\"" + SSID + "\"")) {return existingConfig;}}return null;}}

以上~


Android開發的WIFI控制

wifiConfigurations為空白了吧。getConfiguredNetworks()的說明

Upon failure to fetch or when when Wi-Fi is turned off, it can be null.
擷取失敗或wifi關閉時候,返回null
 
android怎控制只可以串連固定的wifi

把自動搜尋關了,所以已經記錄的WIFI熱點都刪掉,機器裡應該叫遺忘吧
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.