搬-Android - Wi-Fi Tutorial[转]

Posted EPHUIZI

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搬-Android - Wi-Fi Tutorial[转]相关的知识,希望对你有一定的参考价值。

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[转]的主要内容,如果未能解决你的问题,请参考以下文章

Android Wi-Fi Peer-to-Peer(Android的Wi-Fi P2P对等网络)

同时使用 Android 以太网和 Wi-Fi

Wi-fi无法连接到Android Studio模拟器

通过 Wi-Fi 使用 TCP 将 Android 应用程序连接到 IoT 设备

如何通过 Wi-Fi 调试 Android 6.0? (netcfg 问题)

使用 Android 智能手机的 Wi-Fi 热点进行 Chrome 投射[关闭]