android下獲取無線wif信號、ssid、MAC等操作類

一個android下獲取無線wif信號、ssid、MAC等操作的類。

WifiAdmin.java

package com.afu;  
  
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;  
import android.util.Log;  
  
public class WifiAdmin {  
    private final static String TAG = "WifiAdmin";  
    private StringBuffer mStringBuffer = new StringBuffer();  
    private List<ScanResult> listResult;  
    private ScanResult mScanResult;  
    // 定義WifiManager對象   
    private WifiManager mWifiManager;  
    // 定義WifiInfo對象   
    private WifiInfo mWifiInfo;  
    // 網絡連接列表   
    private List<WifiConfiguration> mWifiConfiguration;  
    // 定義一個WifiLock   
    WifiLock mWifiLock;  
  
    /** 
     * 構造方法 
     */  
    public WifiAdmin(Context context) {  
        mWifiManager = (WifiManager) context  
                .getSystemService(Context.WIFI_SERVICE);  
        mWifiInfo = mWifiManager.getConnectionInfo();  
    }  
  
    /** 
     * 打開Wifi網卡 
     */  
    public void openNetCard() {  
        if (!mWifiManager.isWifiEnabled()) {  
            mWifiManager.setWifiEnabled(true);  
        }  
    }  
  
    /** 
     * 關閉Wifi網卡 
     */  
    public void closeNetCard() {  
        if (mWifiManager.isWifiEnabled()) {  
            mWifiManager.setWifiEnabled(false);  
        }  
    }  
  
    /** 
     * 檢查當前Wifi網卡狀態 
     */  
    public void checkNetCardState() {  
        if (mWifiManager.getWifiState() == 0) {  
            Log.i(TAG, "網卡正在關閉");  
        } else if (mWifiManager.getWifiState() == 1) {  
            Log.i(TAG, "網卡已經關閉");  
        } else if (mWifiManager.getWifiState() == 2) {  
            Log.i(TAG, "網卡正在打開");  
        } else if (mWifiManager.getWifiState() == 3) {  
            Log.i(TAG, "網卡已經打開");  
        } else {  
            Log.i(TAG, "沒有獲取到狀態-_-");  
        }  
    }  
  
    /** 
     * 掃描周邊網絡 
     */  
    public void scan() {  
        mWifiManager.startScan();  
        listResult = mWifiManager.getScanResults();  
        if (listResult != null) {  
            Log.i(TAG, "當前區域存在無線網絡,請查看掃描結果");  
        } else {  
            Log.i(TAG, "當前區域沒有無線網絡");  
        }  
    }  
  
    /** 
     * 得到掃描結果 獲取無線信號列表 
     */  
    public String getScanResult() {  
        // 每次點擊掃描之前清空上一次的掃描結果   
        if (mStringBuffer != null) {  
            mStringBuffer = new StringBuffer();  
        }  
        // 開始掃描網絡   
        scan();  
        listResult = mWifiManager.getScanResults();  
        if (listResult != null) {  
            for (int i = 0; i < listResult.size(); i++) {  
                mScanResult = listResult.get(i);  
                mStringBuffer = mStringBuffer.append("NO.").append(i + 1)  
                        .append(" :").append(mScanResult.SSID).append("->")  
                        .append(mScanResult.BSSID).append("->")  
                        .append(mScanResult.capabilities).append("->")  
                        .append(mScanResult.frequency).append("->")  
                        .append(mScanResult.level).append("->")  
                        .append(mScanResult.describeContents()).append("\n\n");  
            }  
        }  
        Log.i(TAG, mStringBuffer.toString());  
        return mStringBuffer.toString();  
    }  
  
    /** 
     * 連接指定網絡 
     */  
    public void connect() {  
          
        mWifiInfo = mWifiManager.getConnectionInfo();  
        if (mWifiInfo != null) {  
            Log.d("wifiInfo", mWifiInfo.toString());    
            Log.d("SSID",mWifiInfo.getSSID());    
        }  
    }  
  
    /** 
     * 斷開當前連接的網絡 
     */  
    public void disconnectWifi() {  
        int netId = getNetworkId();  
        mWifiManager.disableNetwork(netId);  
        mWifiManager.disconnect();  
        mWifiInfo = null;  
    }  
  
    /** 
     * 檢查當前網絡狀態 
     * 
     * @return String 
     */  
    public void checkNetWorkState() {  
        if (mWifiInfo != null) {  
            Log.i(TAG, "網絡正常工作");  
        } else {  
            Log.i(TAG, "網絡已斷開");  
        }  
    }  
  
    /** 
     * 得到連接的ID 
     */  
    public int getNetworkId() {  
        return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();  
    }  
  
    /** 
     * 得到IP地址 
     */  
    public int getIPAddress() {  
        return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();  
    }  
  
    // 鎖定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);  
    }  
  
    // 得到MAC地址   
    public String getMacAddress() {  
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();  
    }  
  
    // 得到接入點的BSSID   
    public String getBSSID() {  
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();  
    }  
  
    // 得到WifiInfo的所有信息包   
    public String getWifiInfo() {  
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();  
    }  
  
    // 添加一個網絡並連接   
    public int addNetwork(WifiConfiguration wcg) {  
        int wcgID = mWifiManager.addNetwork(mWifiConfiguration.get(3));  
        mWifiManager.enableNetwork(wcgID, true);  
        return wcgID;  
    }  
}  

package com.afu;

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;
import android.util.Log;

public class WifiAdmin {
    private final static String TAG = "WifiAdmin";
    private StringBuffer mStringBuffer = new StringBuffer();
    private List<ScanResult> listResult;
    private ScanResult mScanResult;
    // 定義WifiManager對象
    private WifiManager mWifiManager;
    // 定義WifiInfo對象
    private WifiInfo mWifiInfo;
    // 網絡連接列表
    private List<WifiConfiguration> mWifiConfiguration;
    // 定義一個WifiLock
    WifiLock mWifiLock;

    /**
     * 構造方法
     */
    public WifiAdmin(Context context) {
        mWifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        mWifiInfo = mWifiManager.getConnectionInfo();
    }

    /**
     * 打開Wifi網卡
     */
    public void openNetCard() {
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
        }
    }

    /**
     * 關閉Wifi網卡
     */
    public void closeNetCard() {
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
        }
    }

    /**
     * 檢查當前Wifi網卡狀態
     */
    public void checkNetCardState() {
        if (mWifiManager.getWifiState() == 0) {
            Log.i(TAG, "網卡正在關閉");
        } else if (mWifiManager.getWifiState() == 1) {
            Log.i(TAG, "網卡已經關閉");
        } else if (mWifiManager.getWifiState() == 2) {
            Log.i(TAG, "網卡正在打開");
        } else if (mWifiManager.getWifiState() == 3) {
            Log.i(TAG, "網卡已經打開");
        } else {
            Log.i(TAG, "沒有獲取到狀態-_-");
        }
    }

    /**
     * 掃描周邊網絡
     */
    public void scan() {
        mWifiManager.startScan();
        listResult = mWifiManager.getScanResults();
        if (listResult != null) {
            Log.i(TAG, "當前區域存在無線網絡,請查看掃描結果");
        } else {
            Log.i(TAG, "當前區域沒有無線網絡");
        }
    }

    /**
     * 得到掃描結果 獲取無線信號列表
     */
    public String getScanResult() {
        // 每次點擊掃描之前清空上一次的掃描結果
        if (mStringBuffer != null) {
            mStringBuffer = new StringBuffer();
        }
        // 開始掃描網絡
        scan();
        listResult = mWifiManager.getScanResults();
        if (listResult != null) {
            for (int i = 0; i < listResult.size(); i++) {
                mScanResult = listResult.get(i);
                mStringBuffer = mStringBuffer.append("NO.").append(i + 1)
                        .append(" :").append(mScanResult.SSID).append("->")
                        .append(mScanResult.BSSID).append("->")
                        .append(mScanResult.capabilities).append("->")
                        .append(mScanResult.frequency).append("->")
                        .append(mScanResult.level).append("->")
                        .append(mScanResult.describeContents()).append("\n\n");
            }
        }
        Log.i(TAG, mStringBuffer.toString());
        return mStringBuffer.toString();
    }

    /**
     * 連接指定網絡
     */
    public void connect() {
    	
        mWifiInfo = mWifiManager.getConnectionInfo();
        if (mWifiInfo != null) {
            Log.d("wifiInfo", mWifiInfo.toString());  
            Log.d("SSID",mWifiInfo.getSSID());  
        }
    }

    /**
     * 斷開當前連接的網絡
     */
    public void disconnectWifi() {
        int netId = getNetworkId();
        mWifiManager.disableNetwork(netId);
        mWifiManager.disconnect();
        mWifiInfo = null;
    }

    /**
     * 檢查當前網絡狀態
     *
     * @return String
     */
    public void checkNetWorkState() {
        if (mWifiInfo != null) {
            Log.i(TAG, "網絡正常工作");
        } else {
            Log.i(TAG, "網絡已斷開");
        }
    }

    /**
     * 得到連接的ID
     */
    public int getNetworkId() {
        return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
    }

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

    // 鎖定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);
    }

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

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

    // 得到WifiInfo的所有信息包
    public String getWifiInfo() {
        return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
    }

    // 添加一個網絡並連接
    public int addNetwork(WifiConfiguration wcg) {
        int wcgID = mWifiManager.addNetwork(mWifiConfiguration.get(3));
        mWifiManager.enableNetwork(wcgID, true);
        return wcgID;
    }
}
 
調用方法:
 


 
WifiAdmin mWifiAdmin = new WifiAdmin(Main.this);  
String mScanResult = mWifiAdmin.getScanResult();  
scanResult.setText(mScanResult);  

 

發佈留言

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