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[转]的更多相关文章

  1. Android SQLite Database Tutorial

    表名: 列(字段): 联系人实体类:构造方法,setters .getters方法 File:   Contact.java package com.example.sqlitetest; publi ...

  2. Android - JSON Parser Tutorial

    Android provides four different classes to manipulate JSON data. These classes are JSONArray,JSONObj ...

  3. android 的 ExpandableListView Example Tutorial

    https://www.journaldev.com/9942/android-expandablelistview-example-tutorial Welcome to Android Expan ...

  4. 【Android】 Android-wifi 直连 wifi direct wifi p2p

    现在,Android的支持Wi -Fi的直接点对点点对点(P2P)Android系统的供电设备和其他类型的设备,没有一个热点或互联网连接之间的连接.Android框架提供了一套Wi - Fi的P2P的 ...

  5. 【转】获取android设备 id

    关于本文档 Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID.例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID.在本文档结尾处提供了作为参考的示例代码片段 ...

  6. Android设备的ID

    Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID.例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID.在本文档结尾处提供了作为参考的示例代码片段. 范围 本 ...

  7. 如何检索Android设备的唯一ID

    关于本文档 Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID.例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID.在本文档结尾处提供了作为参考的示例代码片段 ...

  8. android intent收集转载汇总

    Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);                 ComponentName comp = ...

  9. Android 外部启动activity,自定义action,action常量大全

    从任意app,启动另外一个app的activity: 1.   Intent i = new Intent();           ComponentName cn = new ComponentN ...

随机推荐

  1. Java异常处理机构(日常笔记)

    try{ 需要保护的代码块 }catch(异常类型 实例){ 捕捉到异常时的代码处理块 }[可有0~多个catch语句] finaly{ 不管异常是否发生都要执行的代码块}

  2. EXCEL 对比数据是否重复

    1.同一列 后一行对比前面所有行 查找是否重复 =IF(COUNTIF(B$2:B2,B2)>1,"重复","") 2.两行两列(多行多列) 两行两列 = ...

  3. Python Virtualenv运行Django环境配置

    系统: RHEL6.5 版本说明: Python-3.5.0 Django-1.10.4 virtualenv:为每个项目建立不同的/独立的Python环境,你将为每个项目安装所有需要的软件包到它们各 ...

  4. 良心版Dolby Home Theater v4.1安装教程

    感(pi)谢(pan)一下两个教程: 文库文章链接:http://wenku.baidu.com/link?url=beBg_apvCuY3xiCXk4zl65Q7AmeCjoDGMol03K0xhk ...

  5. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  6. Javascript中数组的基本操作

    删除数组指定的某个元素 via首先可以给JS的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为: Array.prototype.indexOf = function(val) ...

  7. oracle内存粒度

    一,什么是内存粒度? When a database instance starts up, the amount of memory allocated is determined by the a ...

  8. oracle创建DBLINK

    1.查看Global_name参数 show parameter global_name;  该参数为true时,你在本地建立的DBLINK的名称必须和远程的Global_name一致才行. 2.查看 ...

  9. 在子线程更新主线程的UI组件

    1.实例化一个 private Handler handlerBublishTopic = new Handler(Looper.getMainLooper()); 2. handlerBublish ...

  10. Andriod学习笔记5:通过NDK在C++中实现日志输出

    开发环境 android studio 1.5.1 实现步骤 新建android项目 项目名称为AndroidCLog,选择Empty Activity模板,其他默认即可. 下载配置ndk 在项目上右 ...