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 −

  1. WifiManager mainWifiObj;
  2. 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 −

  1. lass WifiScanReceiver extends BroadcastReceiver {
  2. public void onReceive(Context c, Intent intent) {
  3. }
  4. }
  5. WifiScanReceiver wifiReciever = new WifiScanReceiver();
  6. 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 −

  1. List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
  2. String data = wifiScanList.get(0).toString();

一般情况下网路环境不怎么变,尽量减少wifi扫描的次数,我自己写程序的时候发现每次扫描的时候要10秒左右才能完成。。。

另外,有的手机 WIFI断开的时候会触发WIFI scan。

下面是一个修改过的WIFI riceiver,利用handler传播消息

  1. package com.example.longer3d.wifi;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.net.NetworkInfo;
  7. import android.net.wifi.SupplicantState;
  8. import android.net.wifi.WifiInfo;
  9. import android.net.wifi.WifiManager;
  10. import android.os.Handler;
  11. import android.util.Log;
  12.  
  13. public class WifiStatusReceiver extends BroadcastReceiver {
  14.  
  15. /**
  16. * wifi连接成功
  17. */
  18. public static final int WIFI_HANDLER_CONNECT_SUCCESS = 0x101;
  19. /**
  20. * wifi连接失败
  21. */
  22. public static final int WIFI_HANDLER_CONNECT_FAILD = 0x102;
  23. /**
  24. * wifi连接失败,密码错误,但是只是偶尔会捕抓到=_=..
  25. */
  26. public static final int WIFI_HANDLER_CONNECT_FAILD_AUTH = 0x103;
  27.  
  28. private Handler handler;
  29.  
  30. public WifiStatusReceiver(Handler handler) {
  31. this.handler = handler;
  32. }
  33.  
  34. @Override
  35. public void onReceive(Context context, Intent intent) {
  36. String action = intent.getAction();
  37. switch (action) {
  38. case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
  39. //
  40. // 查看源码SupplicantStateTracker.java
  41. //
  42. SupplicantState supl_state = ((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
  43. if (supl_state == SupplicantState.COMPLETED) {
  44. // 添加回调
  45. handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_SUCCESS);
  46. Log.e("SupplicantState", "wifi COMPLETED");
  47. } else if (supl_state == SupplicantState.DISCONNECTED) {
  48. handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_FAILD);
  49. Log.e("SupplicantState", "wifi DISCONNECTED");
  50. } else {
  51. Log.e("SupplicantState", String.valueOf(supl_state));
  52. Log.e("SupplicantState", "wifi Unknown");
  53. }
  54.  
  55. if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR)) {
  56. handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_FAILD_AUTH);
  57. Log.e("SupplicantState", "WIFI验证失败!");
  58. }
  59. break;
  60.  
  61. // wifi连接上与否
  62. // 会调用的比较快,和多次,不知道为什么,先不使用
  63. case WifiManager.NETWORK_STATE_CHANGED_ACTION:
  64. // Log.e("receiver", "网络状态改变");
  65. NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  66. if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
  67. // handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_FAILD);
  68. Log.e("receiver", "wifi网络连接断开");
  69. } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
  70. // 获取当前wifi名称
  71. WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  72. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  73. Log.e("receiver", "连接到网络 " + wifiInfo.getSSID());
  74. // 添加回调
  75. // handler.sendEmptyMessage(WIFI_HANDLER_CONNECT_SUCCESS);
  76. }
  77. break;
  78. // wifi打开与否
  79. case WifiManager.WIFI_STATE_CHANGED_ACTION:
  80. int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
  81. if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
  82. Log.e("receiver", "系统关闭wifi");
  83. } else if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
  84. Log.e("receiver", "系统开启wifi");
  85. }
  86. break;
  87. }
  88. }
  89. }

搬-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. 漫谈iOS程序的证书和签名机制

    接触iOS开发半年,曾经也被这个主题坑的摸不着头脑,也在淘宝上买过企业证书签名这些服务,有大神都做了一个全自动的发布打包(不过此大神现在不卖企业证书了),甚是羡慕和崇拜.于是,花了一点时间去研究了一下 ...

  2. HTTP访问错误大全

    400 - 错误的请求. ·401 - 访问被拒绝.IIS 定义了许多不同的 401 错误,它们指明更为具体的错误原因.这些具体的错误代码在浏览器中显示,但不在 IIS 日志中显示: ·401.1 - ...

  3. Centos7学习之静态IP设置方法介绍

      1.编辑 ifcfg-eth0 文件,vim 最小化安装时没有被安装,需要自行安装不描述. # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改 ...

  4. IIS与Apache共用80端口

    Windows server 2003服务器上安装有默认 IIS 6和Apache两个服务器,IIS运行的一个.net程序,apache运行php程序,现在想让它们同时都能通过80端口访问,设置起来还 ...

  5. 用opencsv文件读写CSV文件

    首先明白csv文件长啥样儿: 用excel打开就变成表格了,看不到细节 推荐用其它简单粗暴一点儿的编辑器,比如Notepad++, csv文件内容如下: csv文件默认用逗号分隔各列. 有了基础的了解 ...

  6. Stack Overflow: The Architecture - 2016 Edition(Translation)

    原文: https://nickcraver.com/blog/2016/02/17/stack-overflow-the-architecture-2016-edition/ 作者:Nick Cra ...

  7. 【APICloud】APICloud基础学习与快速入门

    前言:回顾这几天学习情况,总的来说APICloud官网它的学习资料和社区还是足够了,但是我必须吐槽一句,实在是过于混乱了,视频资料文档资料它一股脑地都堆在了那里,这几天基本处于在各个地方跳转,然后现在 ...

  8. Git 简介

    版本控制 什么是版本控制? 我需要版本控制吗? - 如果你还没使用过版本控制系统,或许你会有以上疑问,甚至更多疑问.希望后面的回答能让你喜欢上版本控制系统,喜欢上Git. 什么是版本控制:顾名思义,版 ...

  9. 条码固定资产管理PDA应用

    条码固定资产管理解决方案 一.客户挑战与需求 随着企业经营管理的不断升级,固定资产管理的高效化.智能化管理越来越受到企业管理人员的重视.然而,固定资产具有数量大.种类多.价值高.使用周期长.使用地点分 ...

  10. oracle(sql)基础篇系列(四)——数字字典、索引、序列、三范式

      数字字典表 --查看当前用户下面有哪些张表 select * from user_tables; select table_name from user_tables;   --查看当前用户下面有 ...