android下擷取無線wif訊號、ssid、MAC等操作類

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.