一個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);