android WIFI的一些屬性

package com.example.wifitest;

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;

public class WifiAdmin {
    // 定義WifiManager對象
    private WifiManager mWifiManager;
    // 定義WifiInfo對象
    private WifiInfo mWifiInfo;
    // 掃描出的網絡連接列表
    private List<ScanResult> mWifiList;
    // 網絡連接列表
    private List<WifiConfiguration> mWifiConfiguration;
    // 定義一個WifiLock
    WifiLock mWifiLock;

    // 構造器
    public WifiAdmin(Context context) {
        // 取得WifiManager對象
        mWifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        // 取得WifiInfo對象
        mWifiInfo = mWifiManager.getConnectionInfo();
    }

    // 打開WIFI
    public void openWifi() {
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
        }
    }

    // 關閉WIFI
    public void closeWifi() {
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
        }
    }

    // 檢查當前WIFI狀態
    public int checkState() {
        return mWifiManager.getWifiState();
    }

    // 鎖定WifiLock
    public void acquireWifiLock() {
        mWifiLock.acquire();
    }

    // 解鎖WifiLock
    public void releaseWifiLock() {
        // 判斷時候鎖定
        if (mWifiLock.isHeld()) {
            mWifiLock.acquire();
        }
    }

    // 創建一個WifiLock
    public 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_" + new Integer(i + 1).toString() + ":");
            // 將ScanResult信息轉換成一個字符串包
            // 其中把包括:BSSID、SSID、capabilities、frequency、level
            stringBuilder.append((mWifiList.get(i)).toString());
            stringBuilder.append("/n");
        }
        return stringBuilder;
    }

    // 得到MAC地址
    public String getMacAddress() {
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
    }

    // 得到接入點的BSSID
    public String getBSSID() {
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
    }

    // 得到IP地址
    public int getIPAddress() {
        return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
    }

    // 得到連接的ID
    public 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.wepKeys[0] = "";
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        }
        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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            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;
    }


}

 

 

 

 

}
//分為三種情況:1沒有密碼2用wep加密3用wpa加密

改動主要集中在CreateWifiInfo這個方法中,並且添加瞭一個私有方法:

(1)將與方法的第三個參數有關的變量都改成int型,或者使用原作者的枚舉型(存在bug需要改正),但枚舉會在後續的開發中遇到些困難;

(2)在if(type == 3)中註釋掉“config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);”,並添加“

config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);“這兩句,否則當wifi熱點需要輸入密碼時,無法加入網絡。

(3)在代碼末尾添加瞭方法IsExsits,原因在於如果按照網上介紹的方法成功加入指定的wifi後,都會在終端的wifi列表中新添加一個以該指定ssid的網絡,所以每運行一次程序,列表中就會多一個相同名字的ssid。而該方法就是檢查wifi列表中是否有以輸入參數為名的wifi熱點,如果存在,則在CreateWifiInfo方法開始配置wifi網絡之前將其移除,以避免ssid的重復:

WifiConfiguration tempConfig = this.IsExsits(SSID);         

          if(tempConfig != null) {

          mWifiManager.removeNetwork(tempConfig.networkId);

          }

以上便是wifi工具類的建立,之後就可以在其他部分實例化這個類,調用其中的方法完成加入指定ssid的wifi熱點,還是先上代碼吧,建立一個名為Test_wifiActivity.java的文件(同上,沒有包含包聲明和import語句):

public class Test_wifiActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        WifiAdmin wifiAdmin = new WifiAdmin(this);
        wifiAdmin.openWifi();
        wifiAdmin.addNetwork(wifiAdmin.CreateWifiInfo("XXX", "XXX", 3));
    }
}

很簡單,如果是接入wifi,大體上隻涉及到openWifi(打開wifi)、CreateWifiInfo(配置wifi網絡信息)和addNetwork(添加配置好的網絡並連接),對CreateWifiInfo進行簡單的說明:第一參數是SSID的名稱;第二個參數是指定SSID網絡的密碼,當不需要密碼是置空(”“);第三個參數是熱點類型:1-無密碼 / 2-WEP密碼驗證(未測試)/ 3-WAP或WAP2 PSK密碼驗證。
最後就是在Manifest中添加相應的權限瞭:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" ></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" ></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *