搬-Android - Wi-Fi Tutorial[轉]

來源:互聯網
上載者:User

標籤:

http://www.tutorialspoint.com/android/android_wi_fi.htm

Android allows applications to access to view the access the state of the wireless connections at very low level. Application can access almost all the information of a wifi connection.

The information that an application can access includes connected network‘s link speed,IP address, negotiation state(協商狀態), other networks information. Applications can also scan, add, save, terminate and initiate Wi-Fi connections.

Android provides WifiManager API to manage all aspects(方面) of WIFI connectivity. We can instantiate this class by calling getSystemService method. Its syntax is given below −

WifiManager mainWifiObj;mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 

In order to scan a list of wireless networks, you also need to register your BroadcastReceiver. It can be registered using registerReceiver method with argument of your receiver class object. Its syntax is given below −

lass WifiScanReceiver extends BroadcastReceiver {   public void onReceive(Context c, Intent intent) {   }}WifiScanReceiver wifiReciever = new WifiScanReceiver();registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));  

The wifi scan can be start by calling the startScan method of the WifiManager class. This method returns a list of ScanResult objects. You can access any object by calling the get method of list. Its syntax is given below −

List<ScanResult> wifiScanList = mainWifiObj.getScanResults();String data = wifiScanList.get(0).toString();
一般情況下網路環境不怎麼變,盡量減少wifi掃描的次數,我自己寫程式的時候發現每次掃描的時候要10秒左右才能完成。。。

另外,有的手機 WIFI斷開的時候會觸發WIFI scan。

下面是一個修改過的WIFI riceiver,利用handler傳播訊息

package com.example.longer3d.wifi;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.NetworkInfo;import android.net.wifi.SupplicantState;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.os.Handler;import android.util.Log;public class WifiStatusReceiver extends BroadcastReceiver {    /**     * wifi串連成功     */    public static final int WIFI_HANDLER_CONNECT_SUCCESS = 0x101;    /**     * wifi串連失敗     */    public static final int WIFI_HANDLER_CONNECT_FAILD = 0x102;    /**     * wifi串連失敗,密碼錯誤,但是只是偶爾會捕抓到=_=..     */    public static final int WIFI_HANDLER_CONNECT_FAILD_AUTH = 0x103;    private Handler handler;    public WifiStatusReceiver(Handler handler) {        this.handler = handler;    }    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        switch (action) {        case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:            //            // 查看源碼SupplicantStateTracker.java            //            SupplicantState supl_state = ((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));            if (supl_state == SupplicantState.COMPLETED) {                // 添加回調                handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_SUCCESS);                Log.e("SupplicantState", "wifi COMPLETED");            } else if (supl_state == SupplicantState.DISCONNECTED) {                handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_FAILD);                Log.e("SupplicantState", "wifi DISCONNECTED");            } else {                Log.e("SupplicantState", String.valueOf(supl_state));                Log.e("SupplicantState", "wifi Unknown");            }            if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR)) {                handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_FAILD_AUTH);                Log.e("SupplicantState", "WIFI驗證失敗!");            }            break;        // wifi串連上與否        // 會調用的比較快,和多次,不知道為什麼,先不使用        case WifiManager.NETWORK_STATE_CHANGED_ACTION:            // Log.e("receiver", "網路狀態改變");            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);            if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {                // handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_FAILD);                Log.e("receiver", "wifi網路連接斷開");            } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {                // 擷取當前wifi名稱                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);                WifiInfo wifiInfo = wifiManager.getConnectionInfo();                Log.e("receiver", "串連到網路 " + wifiInfo.getSSID());                // 添加回調                // handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_SUCCESS);            }            break;        // wifi開啟與否        case WifiManager.WIFI_STATE_CHANGED_ACTION:            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);            if (wifiState == WifiManager.WIFI_STATE_DISABLED) {                Log.e("receiver", "系統關閉wifi");            } else if (wifiState == WifiManager.WIFI_STATE_ENABLED) {                Log.e("receiver", "系統開啟wifi");            }            break;        }    }}

 

搬-Android - Wi-Fi Tutorial[轉]

聯繫我們

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