Android wifi相关主要广播总结
文章目录
一、前言
wifi应用开发肯定会用到wifi开关和wifi连接状态监听的广播,有些广播比较容易混淆,本文进行详细总结。
比如wifi网络变化广播:CONNECTIVITY_ACTION、WIFI_STATE_CHANGED_ACTION、NETWORK_STATE_CHANGED_ACTION 这三个比较容易搞混。
SUPPLICANT_STATE_CHANGED_ACTION 和 SUPPLICANT_CONNECTION_CHANGE_ACTION 比较容易搞混。
很多wifi相关开发的人,搞了多年还是没有对wifi主要广播进行过梳理总结。
本文对各个广播使用情况进行详细介绍,有需要的可以看看。
本文不仅仅对wifi相关广播做了介绍,还展示了具体情况下的打印日志,
后续还对广播携带数据的查找方式进行了介绍,有一定的研究学习价值,可以收藏学习。
二、wifi主要广播分析介绍
1、简单的广播监听
//注册
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //网络变化广播,一般是ip地址变化
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); //wifi开关变化广播
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); //wifi连接变化广播
mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);//wifi信息验证变化广播
mFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);//wifi开关变化
mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);//扫描到wifi广播
registerReceiver(mReceiver, mFilter, Context.RECEIVER_EXPORTED); //Android14 普通应用要多加一个参数,否则报异常。
//监听
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
LogUtil.debug("action = " + action);
}
};
//反注册
unregisterReceiver(mReceiver1);
上面的代码只是简单监听action日志,未获取action相关的具体信息。
上面是Wifi相关的常用广播广播,还有些不常用的广播。最后介绍一下。
2、wifi 广播对应的 action 字符串:
//网络变化广播
ConnectivityManager.CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
WifiManager.java:
//wifi 开关状态变化广播
String WIFI_STATE_CHANGED_ACTION ="android.net.wifi.WIFI_STATE_CHANGED";
//wifi 连接状态变化广播
String NETWORK_STATE_CHANGED_ACTION = "android.net.wifi.STATE_CHANGE";
//wifi 连接验证状态变化广播 2
String SUPPLICANT_STATE_CHANGED_ACTION ="android.net.wifi.supplicant.STATE_CHANGE";
//wifi 开关状态变化广播2
String SUPPLICANT_CONNECTION_CHANGE_ACTION="android.net.wifi.supplicant.CONNECTION_CHANGE";
//扫描到wifi广播
String SCAN_RESULTS_AVAILABLE_ACTION = "android.net.wifi.SCAN_RESULTS";
可以看到action中 WIFI_STATE_CHANGED 是Wifi开关广播,STATE_CHANGE是Wifi网络变化广播;
可以看到supplicant相关的action中 WIFI_STATE_CHANGE 是Wifi状态改变广播,CONNECTION_CHANGE是Wifi开关状态变化广播;
supplicant 的理解,有些人说是硬件/底层相关的广播,其实不是的。这个只是Wifi不同状态的广播。
supplicant 翻译过来 表示: 请求者。其实就是网络校验相关的广播。开关也有相关校验吧。
所以wifi开关和wifi断开连接都有supplicant相关日志。
但是很多情况其实是不需要 supplicant 广播监听的,但是有些情况可以使用这个广播,
往下看看具体日志,就知道这个 supplicant 的用途了。
3、广播和接收的相关数据
//监听
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
LogUtil.inform("action = " + action);
switch (action) {
case ConnectivityManager.CONNECTIVITY_ACTION: //网络变化广播,一般是ip地址变化
String wifiIp = FunctionUtil.getNetIpAddress("wlan0");
LogUtil.inform("wifiIp = " + wifiIp);
break;
case WifiManager.WIFI_STATE_CHANGED_ACTION: //wifi开关变化广播
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
LogUtil.inform("wifiState = " + wifiState + " (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)");
break;
case WifiManager.NETWORK_STATE_CHANGED_ACTION: //wifi连接变化广播,断开或者断开某个wifi
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
LogUtil.inform("info = " + info);
break;
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION: //wifi信息验证变化广播
NetworkInfo.DetailedState detailedState = WifiInfo.getDetailedStateOf((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
LogUtil.inform("detailedState = " + detailedState);
int error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
LogUtil.inform("error = " + error);
if (error == WifiManager.ERROR_AUTHENTICATING) { // 1 密码错误
}
break;
case WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION: //wifi开关变化,已经废弃
boolean isWifiOn = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
LogUtil.inform("isWifiOn = " + isWifiOn);
break;
case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION: //扫描到wifi广播
//是否扫描成功
boolean lastScanSucceeded = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, true);
LogUtil.inform("lastScanSucceeded = " + lastScanSucceeded); //基本都返回true
List<ScanResult> scanResults = mWifiManager.getScanResults(); //需要动态权限:ACCESS_FINE_LOCATION
LogUtil.inform("scanResults size = " + scanResults.size());
break;
}
}
};
上面是wifi常用广播添加接收 额外数据Extra的代码。
关于这些Action 有什么 Extra数据,后面有介绍。
具体日志打印情况,可以继续往下看。
三、广播相关日志:
下面是上面广播接收后具体的日志打印,有兴趣的可以细细看一下,肯定是会有收获的。
1、wifi扫描广播的日志
只要界面注册监听了这个wifi扫描广播,就会收到这个广播消息。
MainActivity.java(45)::onReceive - action = android.net.wifi.SCAN_RESULTS
MainActivity.java(74)::onReceive - lastScanSucceeded = true
MainActivity.java(76)::onReceive - scanResults size = 52
MainActivity.java(45)::onReceive - action = android.net.wifi.SCAN_RESULTS
2MainActivity.java(74)::onReceive - lastScanSucceeded = true
MainActivity.java(76)::onReceive - scanResults size = 53
MainActivity.java(45)::onReceive - action = android.net.wifi.SCAN_RESULTS
MainActivity.java(74)::onReceive - lastScanSucceeded = true
MainActivity.java(76)::onReceive - scanResults size = 57
wifi扫描的广播是每十秒一次扫描触发。
如果要获取到扫描的wifi列表,需要应用获取到动态权限:ACCESS_FINE_LOCATION
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
可以在代码中动态申请,也可以在原生设置应用中打开这个应用的信息手动允许这个权限。
下面的日志是保留了时间戳的日志。
后面日志比较多,详细看一两个就行,其他的都是类似的。
2、wifi连接、断开的日志
(1)第一次连接的日志
①密码正确情况的日志
1、wifi连接变化广播,CONNECTING状态,连接中
08-28 02:49:25.033 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 02:49:25.035 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,CONNECTING状态,多次
08-28 02:49:25.039 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 02:49:25.040 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 02:49:25.041 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
...
3、wifi连接变化广播,CONNECTING/AUTHENTICATING状态,连接中+验证中
08-28 02:49:25.416 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 02:49:25.417 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
4、wifi连接变化广播 supplicant,AUTHENTICATING 状态,验证中
08-28 02:49:25.419 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 02:49:25.419 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 02:49:25.420 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
5、wifi连接变化广播 supplicant,CONNECTING状态
08-28 02:49:25.445 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 02:49:25.446 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
6、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,请求分配ip
08-28 02:49:25.454 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 02:49:25.455 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
7、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,请求分配ip
08-28 02:49:25.475 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 02:49:25.476 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = OBTAINING_IPADDR
08-28 02:49:25.476 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
8、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,请求分配ip,估计是ip分配完成
08-28 02:49:26.095 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 02:49:26.096 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
9、wifi连接变化广播,CONNECTED 状态,已连接状态
08-28 02:49:26.135 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 02:49:26.138 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
10、网络变化广播,ip变化
08-28 02:49:26.312 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 02:49:26.323 8820 8820 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = 192.5.1.8
从上面日志可以看到比较详细的wifi连接完成过程:
wifi连接过程的广播:连接中-->校验密码-->分配ip-->连接完成 --> ip 变化
密码正确的wifi连接过程中,在 wifi.STATE_CHANGE 广播已经包含了 supplicant广播内容。
并且 supplicant广播内容没有包含wifi连接完成的广播,只是一些校验过程的广播;
总的来说,wifi.STATE_CHANGE 广播 包含的状态比较全面,从开始到完成的。
最后的网络变化广播,不是没啥action,是调用系统api获取了wifi的ip地址。
②密码错误情况的日志
1、wifi连接变化广播,DISCONNECTED/SCANNING 状态,断开
08-28 03:26:37.559 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:26:37.561 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: DISCONNECTED/SCANNING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,SCANNING 状态
08-28 03:26:37.563 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:26:37.564 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = SCANNING
08-28 03:26:37.565 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = 1 //返回密码错误的情况
3、wifi连接变化广播,CONNECTING状态,连接中不验证
08-28 03:26:40.297 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:26:40.300 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
4、wifi连接变化广播 supplicant,CONNECTING 状态,多次
08-28 03:26:40.300 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:26:40.301 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 03:26:40.301 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 03:26:40.505 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:26:40.506 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 03:26:40.506 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 03:26:40.532 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:26:40.534 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 03:26:40.535 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
5、wifi连接变化广播,CONNECTING/AUTHENTICATING 状态,连接中+验证中
08-28 03:26:40.563 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:26:40.564 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
6、wifi连接变化广播 supplicant,AUTHENTICATING 状态,验证中
08-28 03:26:40.573 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:26:40.573 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 03:26:40.574 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
7、wifi连接变化广播,DISCONNECTED 状态,断开
08-28 03:26:44.758 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:26:44.759 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
8、wifi连接变化广播 supplicant,DISCONNECTED 状态,断开
08-28 03:26:44.770 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:26:44.771 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = DISCONNECTED
08-28 03:26:44.771 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = 1 //返回密码错误的情况
08-28 03:26:47.604 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.SCAN_RESULTS
从上面日志可以看到比较详细的wifi密码错误连接完成过程:
wifi密码错误连接过程的广播:断开连接+扫描,密码错误-->连接中-->校验密码-->断开连接,密码错误
密码错误的wifi连接过程中,supplicant 广播内容可以判断出wifi 密码错误的情况;
之前我发现这个wifi连接状态的 supplicant 广播,怎么有两次密码错误的,
所以一直没有这个广播判断密码错误的情况,使用的是原生系统Setings的不断判断连接状态的方法。
现在看来是可以用这个wifi连接状态的 supplicant 广播,判断密码错误的,加上 detailedState 的判断就可以了;
最好是判断 detailedState = DISCONNECTED 的情况,因为这个时候已经是完成了整个校验过程。
所以说查看详细日志还是有作用的。
③ 无密码情况的连接
1、wifi连接变化广播,CONNECTING状态,连接中
08-28 06:16:33.045 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:16:33.047 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,CONNECTING状态,多次(3次)
08-28 06:16:33.055 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:16:33.057 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:16:33.057 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:16:33.584 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:16:33.585 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:16:33.585 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:16:33.626 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:16:33.627 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:16:33.627 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
3、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,连接中+请求ip分配
08-28 06:16:33.640 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:16:33.642 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
4、wifi连接变化广播 supplicant,OBTAINING_IPADDR 状态,请求ip分配
08-28 06:16:33.668 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:16:33.669 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = OBTAINING_IPADDR
08-28 06:16:33.669 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
5、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,连接中+请求ip分配,应该是ip分配完成
08-28 06:16:33.790 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:16:33.791 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
6、wifi连接变化广播,CONNECTED 状态,已连接状态
08-28 06:16:33.828 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:16:33.833 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
7、网络变化广播,ip变化
08-28 06:16:34.032 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 06:16:34.054 3984 3984 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = 192.168.31.47
可以看到,和有密码的wifi连接的广播是差不多的,但是没有验证过程的广播 CONNECTING/AUTHENTICATING;
估计是不用验证密码所以跳过了 AUTHENTICATING 的过程。
过程:
wifi无密码连接过程的广播:连接中-->分配ip-->连接完成 --> ip 变化
(2)保存状态连接的日志
1、wifi连接变化广播,CONNECTING状态,连接中
08-28 03:40:55.506 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:40:55.507 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,CONNECTING状态,多次(3次)
08-28 03:40:55.510 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:40:55.511 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 03:40:55.512 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 03:40:55.570 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:40:55.571 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 03:40:55.571 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 03:40:55.645 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:40:55.646 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 03:40:55.646 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
3、wifi连接变化广播,CONNECTING/AUTHENTICATING状态,连接中+验证中
08-28 03:40:55.745 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:40:55.746 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
4、wifi连接变化广播 supplicant,AUTHENTICATING 状态,多次
08-28 03:40:55.747 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:40:55.748 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 03:40:55.748 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 03:40:55.769 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:40:55.770 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 03:40:55.770 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
5、wifi连接变化广播 ,CONNECTING 状态,
08-28 03:40:55.776 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:40:55.777 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
6、wifi连接变化广播 ,CONNECTING/OBTAINING_IPADDR 状态,请求分配ip
08-28 03:40:55.783 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:40:55.783 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
7、wifi连接变化广播 supplicant,OBTAINING_IPADDR 状态,请求分配ip
08-28 03:40:55.792 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:40:55.793 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = OBTAINING_IPADDR
08-28 03:40:55.793 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
8、wifi连接变化广播 ,CONNECTING/OBTAINING_IPADDR 状态,请求分配ip完成
08-28 03:40:55.875 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:40:55.876 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
9、wifi连接变化广播 ,CONNECTED 状态,完成连接
08-28 03:40:55.907 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:40:55.913 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
10、网络变化广播,ip变化
08-28 03:40:56.055 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 03:40:56.059 8820 8820 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = 192.168.31.79
可以看到连接已保存的wifi和第一次连接wifi过程基本是一样的。
过程:
连接保存的wifi过程的广播:连接中-->校验密码-->分配ip-->连接完成 --> ip 变化
后续有测试保存的wifi,修改密码后,再选择连接,这种情况和连接错误密码的wifi情况是差不多的,也是有wifi密码错误广播的。
(3)断开连接的wifi 的日志
1、wifi连接变化广播,DISCONNECTED 状态,断开
08-28 06:48:29.778 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:48:29.780 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,DISCONNECTED 状态,断开
08-28 06:48:29.785 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:48:29.786 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = DISCONNECTED
08-28 06:48:29.786 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
3、网络变化广播,ip变化
08-28 06:48:29.787 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 06:48:29.790 3984 3984 I WifiReceiverDemo: FunctionUtil.java(128)::getNetIpAddress - hardWareAddress = null
08-28 06:48:29.790 3984 3984 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = null
可以看到wifi断开的过程比较简单,断开后是没有wifi ip的。
过程:
断开wifi过程的广播:断开wifi--> ip 变化
并且可以看到断开wifi不会像连接wifi那样磨磨唧唧,没有断开中的状态,直接就是断开。
只有打开、关闭、连接wifi 有进行中的状态,断开wifi是没有进行中状态的。
(4)忘记连接的wifi的日志
1、wifi连接变化广播,DISCONNECTED 状态,断开
08-28 03:42:25.246 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 03:42:25.247 8820 8820 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,DISCONNECTED 状态,断开
08-28 03:42:25.259 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 03:42:25.260 8820 8820 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = DISCONNECTED
08-28 03:42:25.260 8820 8820 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
3、网络变化广播,ip变化
08-28 03:42:25.382 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 03:42:25.385 8820 8820 I WifiReceiverDemo: FunctionUtil.java(128)::getNetIpAddress - hardWareAddress = null
08-28 03:42:25.385 8820 8820 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = null
可以看到忘记wifi的关闭和断开wifi的广播情况是一样的。
过程:
忘记wifi过程的广播:断开wifi--> ip 变化
(5)已连接一个wifi的情况连接另外一个wifi的日志
1、wifi连接变化广播,DISCONNECTED 状态,断开
08-28 06:29:15.242 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:15.244 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
2、wifi连接变化广播 supplicant,DISCONNECTED 状态,断开
08-28 06:29:15.264 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.265 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = DISCONNECTED
08-28 06:29:15.265 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
3、wifi连接变化广播,CONNECTING 状态,连接中
08-28 06:29:15.268 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:15.269 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
4、wifi连接变化广播 supplicant,CONNECTING 状态,连接中,多次(三次)
08-28 06:29:15.269 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.270 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:29:15.271 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
5、网络变化广播,ip变化
08-28 06:29:15.340 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 06:29:15.344 3984 3984 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = null
wifi连接变化广播 supplicant,CONNECTING 状态
08-28 06:29:15.371 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.372 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:29:15.373 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:29:15.420 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.421 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:29:15.422 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
6、wifi连接变化广播,CONNECTING/AUTHENTICATING 状态,验证中
08-28 06:29:15.433 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:15.434 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
6、wifi连接变化广播 supplicant,AUTHENTICATING 状态,验证中,多次
08-28 06:29:15.440 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.441 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 06:29:15.441 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:29:15.451 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.452 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 06:29:15.452 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
7、wifi连接变化广播,CONNECTING 状态,连接中
08-28 06:29:15.506 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:15.507 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
8、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,连接中+请求ip分配
08-28 06:29:15.513 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:15.514 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
9、wifi连接变化广播 supplicant,OBTAINING_IPADDR 状态,请求ip分配
08-28 06:29:15.523 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:29:15.524 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = OBTAINING_IPADDR
08-28 06:29:15.524 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
10、wifi连接变化广播,CONNECTING/OBTAINING_IPADDR 状态,ip分配完成
08-28 06:29:16.212 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:16.213 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
11、wifi连接变化广播,CONNECTED 状态,连接完成
08-28 06:29:16.233 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:29:16.234 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
12、网络变化广播,ip变化
08-28 06:29:16.401 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 06:29:16.404 3984 3984 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = 192.168.1.39
从上面广播可以看出,已连接wifi的情况连接另外一个wifi的过程,比普通连接的wifi:
(1)多了断开wifi的;
(2)过程中间也会收到 ip变化的广播。
过程:
连接第二个wifi过程的广播:断开wifi--> ip 变化 --> 连接中 --> 校验密码-->分配ip-->连接完成 --> ip 变化
3、wifi打开、关闭的日志
(1)wifi打开但是未连接wifi的日志:
这个情况是打开wifi之前没有保存的wifi情况。
1、wifi开关变化广播,ENABLING 状态,正在打开
08-28 06:35:44.779 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:35:44.780 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 2 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
2、wifi开关变化广播,ENABLED 状态,已经打开
08-28 06:35:44.918 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:35:44.920 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 3 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
3、wifi开关变化广播 supplicant,ENABLED 状态,已经打开
08-28 06:35:44.932 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.CONNECTION_CHANGE
08-28 06:35:44.932 3984 3984 I WifiReceiverDemo: MainActivity.java(69)::onReceive - isWifiOn = true
上面的广播可以看出,wifi打开的日志中 wifi.WIFI_STATE_CHANGED 和 supplicant.CONNECTION_CHANGE 效果是一样的。
wifi.WIFI_STATE_CHANGED 的过程相对来说详细一点。
(2)wifi打开后自动连接上wifi的日志:
这个情况是打开wifi前有已保存的wifi情况。
1、wifi开关变化广播,ENABLING 状态,正在打开
08-28 06:34:10.886 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:34:10.887 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 2 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
2、wifi开关变化广播,ENABLED 状态,已经打开
08-28 06:34:11.037 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:34:11.038 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 3 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
3、wifi网络变化广播 supplicant,ENABLED 状态,已经打开
08-28 06:34:11.047 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.CONNECTION_CHANGE
08-28 06:34:11.048 3984 3984 I WifiReceiverDemo: MainActivity.java(69)::onReceive - isWifiOn = true
//上面三个广播信息和上面打开wifi后未自动连接wifi是一样的。
08-28 06:34:13.673 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.SCAN_RESULTS
4、wifi连接变化广播 ,CONNECTING 状态,正在连接
08-28 06:34:13.723 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:34:13.724 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
5、wifi连接变化广播 supplicant,CONNECTING 状态,正在连接,多次
08-28 06:34:13.727 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:34:13.727 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:34:13.728 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:34:13.745 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:34:13.746 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:34:13.747 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:34:13.766 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:34:13.767 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = CONNECTING
08-28 06:34:13.767 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
6、wifi连接变化广播 ,CONNECTING/AUTHENTICATING 状态,验证密码
08-28 06:34:13.771 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:34:13.779 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
7、wifi连接变化广播 supplicant,AUTHENTICATING 状态,验证密码
08-28 06:34:13.780 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:34:13.781 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 06:34:13.783 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
08-28 06:34:13.783 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:34:13.784 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = AUTHENTICATING
08-28 06:34:13.784 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
8、wifi连接变化广播 ,CONNECTING 状态,正在连接
08-28 06:34:13.791 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:34:13.792 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
9、wifi连接变化广播 ,CONNECTING/OBTAINING_IPADDR 状态,正在连接+请求分配ip
08-28 06:34:13.796 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:34:13.797 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
10、wifi连接变化广播 supplicant,OBTAINING_IPADDR 状态,请求分配ip
08-28 06:34:13.808 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:34:13.809 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = OBTAINING_IPADDR
08-28 06:34:13.812 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
11、wifi连接变化广播 ,CONNECTING/OBTAINING_IPADDR 状态,请求分配ip 完成
08-28 06:34:13.998 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:34:13.999 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
12、wifi连接变化广播 ,CONNECTED 状态,连接完成
08-28 06:34:14.030 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:34:14.031 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
13、网络变化广播,ip变化
08-28 06:34:14.218 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 06:34:14.221 3984 3984 I WifiReceiverDemo: FunctionUtil.java(115)::getNetIpAddress - iF.getName() = wlan0 , ipAddress = 192.168.1.39
08-28 06:34:14.237 3984 3984 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = 192.168.1.39
上面日志可以看到,wifi打开后自动连接上wifi的情况,其实就是wifi单独打开的日志 加上 wifi单独连接上的日志情况。
过程:
打开并连接上wifi过程的广播:wifi正在打开--> wifi已打开 --> 连接中 --> 校验密码-->分配ip-->连接完成 --> ip 变化
(3)wifi未连接情况下关闭日志:
1、wifi开关变化广播,DISABLING 状态,正在关闭
08-28 03:45:02.249 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 03:45:02.250 8820 8820 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 0 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
wifi开关变化广播,DISABLING 状态,正在关闭
08-28 03:45:02.259 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 03:45:02.259 8820 8820 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 0 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
2、wifi开关变化广播,DISABLED 状态,完成关闭
08-28 03:45:02.347 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 03:45:02.348 8820 8820 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 1 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
3、wifi开关变化广播 supplicant,DISABLED 状态,完成关闭
08-28 03:45:02.366 8820 8820 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.CONNECTION_CHANGE
08-28 03:45:02.366 8820 8820 I WifiReceiverDemo: MainActivity.java(69)::onReceive - isWifiOn = false
上面的广播可以看出,wifi关闭的日志中 wifi.WIFI_STATE_CHANGED 和 supplicant.CONNECTION_CHANGE 效果是一样的。
wifi.WIFI_STATE_CHANGED 广播的过程相对来说详细一点。
过程:
关闭未连接的wifi过程的广播:wifi正在关闭--> wifi已关闭
(4)wifi已连接情况下关闭wifi 的日志:
1、wifi开关变化广播,DISABLING 状态,正在关闭
08-28 06:31:24.935 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:31:24.936 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 0 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
wifi开关变化广播,DISABLING 状态,正在关闭
08-28 06:31:24.944 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:31:24.944 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 0 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
2、wifi状态变化广播,DISCONNECTED 状态,断开
8-28 06:31:25.163 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.STATE_CHANGE
08-28 06:31:25.165 3984 3984 I WifiReceiverDemo: MainActivity.java(57)::onReceive - info = [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), failover: false, available: false, roaming: false]
3、wifi状态变化广播 supplicant,DISCONNECTED 状态,断开
08-28 06:31:25.175 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.STATE_CHANGE
08-28 06:31:25.176 3984 3984 I WifiReceiverDemo: MainActivity.java(61)::onReceive - detailedState = DISCONNECTED
08-28 06:31:25.177 3984 3984 I WifiReceiverDemo: MainActivity.java(63)::onReceive - error = -1
4、wifi开关变化广播,DISABLED 状态,完成关闭
08-28 06:31:25.182 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.WIFI_STATE_CHANGED
08-28 06:31:25.182 3984 3984 I WifiReceiverDemo: MainActivity.java(53)::onReceive - wifiState = 1 (ex: 0=DISABLING, 1=DISABLED, 2=ENABLING, 3=ENABLED)
5、网络变化广播,ip变化
08-28 06:31:25.258 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.conn.CONNECTIVITY_CHANGE
08-28 06:31:25.263 3984 3984 I WifiReceiverDemo: MainActivity.java(49)::onReceive - wifiIp = null
4、wifi开关变化广播 supplicant,完成关闭
08-28 06:31:25.269 3984 3984 I WifiReceiverDemo: MainActivity.java(45)::onReceive - action = android.net.wifi.supplicant.CONNECTION_CHANGE
08-28 06:31:25.270 3984 3984 I WifiReceiverDemo: MainActivity.java(69)::onReceive - isWifiOn = false
从上面广播日志可以看到关闭WiFi的时候会,断开WiFi,还会有网络ip变化广播,之后是关闭完成广播。
过程:
关闭已连接的wifi过程的广播:wifi正在关闭--> 断开wifi网络 -->wifi已关闭 -->ip变化
上面这些广播日志对于大部分人没啥用,但是对于研究wifi连接过程是比较用作用的,很有参考价值。
根据项目需求,可以在对应的广播时间判断一些逻辑,比如wifi打开状态等,wifi连接状态,wifi密码验证情况等等。
广播的过程不仅仅是可以监听action 的Extra数据,还可以通过系统api,获取当前wifi或者ip情况进行确认。
四、其他
1、wifi常用广播小结
(1)wifi连接过程的广播:连接中-->校验密码-->分配ip-->连接完成 --> ip 变化
wifi断开、wifi开关的过程都比较简单,没啥好说的
(2)连接上wifi或者断开wifi都会有ip变化的广播
(3)wifi 打开和关闭状态的广播中,supplicant相关状态的广播没啥使用价值。
(4)wifi 连接状态的广播中,supplicant 状态的广播对于判断密码错误是有价值的。
(5)supplicant相关的广播毕竟是过时的广播,如果没有判断密码错误情况,就不要用supplicant相关的广播了
值得注意的是,连接上wifi后ip变化的时候马上获取ip,有出现过,获取到ip未空的情况;
规避这个问题可以可以通过WifiManager获取当前连接的wifi信息获取ip;(这个需要系统权限);
规避这个问题还可以延长一秒再获取ip。
最后再看一下本文开始说的容易混淆的几个广播和对应含义:
CONNECTIVITY_ACTION:网络ip变化广播
WIFI_STATE_CHANGED_ACTION :wifi开关变化广播
NETWORK_STATE_CHANGED_ACTION :wifi网络变化广播
SUPPLICANT_STATE_CHANGED_ACTION :wifi连接验证过程变化广播
SUPPLICANT_CONNECTION_CHANGE_ACTION :wifi开关变化广播
上面是wifi的还比较好区分的,
WIFI_STATE_CHANGED 这个是wifi开关,STATE_CHANGED 是wifi连接状态变化,多个wifi的就是开关的。
但是supplicant 这个确实不好记,和语义有点不符,
CONNECTION_CHANGE 的是开关变化,STATE_CHANGED是验证状态发生改变。
Connection就是连接的意思,但是CONNECTION_CHANGE居然是开关变化的广播;
不过这个 CONNECTION_CHANGE 尽量不要用了,没啥太大意义,wifi开关变化广播使用 WIFI_STATE_CHANGED_ACTION 就可以了。
2、wifi密码错误最正确的判断
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION: //wifi信息验证变化广播
NetworkInfo.DetailedState detailedState = WifiInfo.getDetailedStateOf((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
LogUtil.inform("detailedState = " + detailedState);
int error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
LogUtil.inform("error = " + error);
if (error == WifiManager.ERROR_AUTHENTICATING && detailedState == NetworkInfo.DetailedState.DISCONNECTED) { // 1 密码错误
Toast.makeText(MainActivity.this, "wifi 密码错误!" ,Toast.LENGTH_LONG).show();
}
break;
网上很多文章只判断了error ==1 的情况,所以会多次弹框密码错误的情况。
所以网上的知识大多是仅供参考,还是要实际验证才可以。
3、如果查看wifi相关广播的Extra数据?
wifi广播携带了啥数据,这个怎么看?很多很多人都会有疑问?
这个其实不难,懂的都懂,学过一次后面就懂了,其实就是看api定义广播的地方,会写明该广播携带什么数据的。
这个不仅仅是针对wifi广播,其他任何广播其实都可以参考这样查看广播携带的数据。
比如wifi的某个广播:
WifiManager.java :
wifi连接变化广播 WifiManager.NETWORK_STATE_CHANGED_ACTION :
看源码的定义:
/**
* Broadcast intent action indicating that the state of Wi-Fi connectivity
* has changed. An extra provides the new state
* in the form of a {@link android.net.NetworkInfo} object. No network-related
* permissions are required to subscribe to this broadcast.
*
* <p class="note">This broadcast is not delivered to manifest receivers in
* applications that target API version 26 or later.
* @see #EXTRA_NETWORK_INFO // (1)Extra 数据,这里可能看不出啥类型的,继续找具体定义的地方就可以了
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String NETWORK_STATE_CHANGED_ACTION = "android.net.wifi.STATE_CHANGE";
//(2)getParcelableExtra 返回的是NetworkInfo类型是数据, NetworkInfo 有啥数据可以自己看api或者打印出所有数据
/**
* The lookup key for a {@link android.net.NetworkInfo} object associated with the //返回类型
* Wi-Fi network. Retrieve with
* {@link android.content.Intent#getParcelableExtra(String)}. //获取额外数据的方法
*/
public static final String EXTRA_NETWORK_INFO = "networkInfo";
这样就能看到广播返回的数据了吧,
主要思路:
1、查看广播携带的额外数据的字符串关键字
2、查看广播写得额外数据的类型
case WifiManager.NETWORK_STATE_CHANGED_ACTION: //wifi连接变化广播,断开或者断开某个wifi
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
LogUtil.inform("info = " + info);
再看一个其他类和其他类型的广播数据,ip变化广播:
ConnectivityManager.java
wifi连接变化广播 ConnectivityManager.CONNECTIVITY_ACTION :
// (1)Extra 数据,这里这里已经说明了是boolean类型,继续找具体定义的地方查看如何获取
* For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
* is set to {@code true} if there are no connected networks at all.
*
* @deprecated apps should use the more versatile {@link #requestNetwork},
* {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
* functions instead for faster and more detailed updates about the network
* changes they care about.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
@Deprecated
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
//(2)getBooleanExtra 返回的是 boolean 类型是数据,返回的是是否没有连接网络
/**
* The lookup key for a boolean that indicates whether there is a
* complete lack of connectivity, i.e., no network is available.
* Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
*/
public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
上面的代码示例中,未添加这个数据打印,
这个数据也是今天看到的,代码中可以打印这个数据:
case ConnectivityManager.CONNECTIVITY_ACTION: //网络变化广播,一般是ip地址变化
boolean isNetNotConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
//是否网络未连接
LogUtil.inform("isNetNotConnectivity = " + isNetNotConnectivity); //如果有ip,返回就是false;也就是说断开wifi是返回 true,连接wifi是返回 false
String wifiIp = FunctionUtil.getNetIpAddress("wlan0");
LogUtil.inform("wifiIp = " + wifiIp);
学到了这个,其他任何广播都参考这样就可以找到广播携带的数据了。
是否可以在一个广播里面判断其他Extra 数据?
可能还有人会问,是否可以在广播中获取其他的 Extra 数据。
比如在 ConnectivityManager.CONNECTIVITY_ACTION 广播中是否可以获取wifi开关和wifi连接状态的 Extra 数据。
其实这个问题,学过基础知识的都知道,他没发过来的数据,你获取到的就是空的。所以没啥好疑问的。
case ConnectivityManager.CONNECTIVITY_ACTION: //网络变化广播,一般是ip地址变化
boolean isNetNotConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
LogUtil.inform("isNetNotConnectivity = " + isNetNotConnectivity);
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); //这个数据就是会 null
LogUtil.inform("info = " + info);
所以说广播里面携带了相关Extra的数据才能获取到,未携带的是获取不到的。
具体的过程,发送数据的具体信息,可以看源码里面发送广播的地方。
4、wifi其他相关广播
wifi相关广播,除了上面介绍的开关和连接相关的广播,
还有很多其他广播,直接在WifiManager.java里面搜索关键字“Broadcast intent action ”可以搜到里面所有的广播action。
其他广播:
//wifi 验证信息变化
/**
* Broadcast intent action indicating that the credential of a Wi-Fi network
* has been changed. One extra provides the ssid of the network. Another
* extra provides the event type, whether the credential is saved or forgot.
* @hide
*/
@SystemApi
public static final String WIFI_CREDENTIAL_CHANGED_ACTION = "android.net.wifi.WIFI_CREDENTIAL_CHANGED";
//"Deauth Imminent" 是一种特定于 Wi-Fi 网络的管理帧,主要用于 Passpoint 认证过程
/**
* Broadcast intent action indicating that a Passpoint Deauth Imminent frame has been received.
*
* Included extras:
* {@link #EXTRA_BSSID_LONG}
* {@link #EXTRA_ESS}
* {@link #EXTRA_DELAY}
* {@link #EXTRA_URL}
*
* Receiver Required Permission: android.Manifest.permission.ACCESS_WIFI_STATE
*
* <p>Note: The broadcast is only delivered to registered receivers - no manifest registered
* components will be launched.
*
* @hide
*/
public static final String ACTION_PASSPOINT_DEAUTH_IMMINENT =
"android.net.wifi.action.PASSPOINT_DEAUTH_IMMINENT";
//热点开关变化
@SystemApi
public static final String WIFI_AP_STATE_CHANGED_ACTION =
"android.net.wifi.WIFI_AP_STATE_CHANGED";
@SystemApi
@RequiresPermission(android.Manifest.permission.NETWORK_CARRIER_PROVISIONING)
public static final String ACTION_NETWORK_SETTINGS_RESET =
"android.net.wifi.action.NETWORK_SETTINGS_RESET";
//wifi信息重置?(恢复出厂设置那里,有个wif重置)
@SystemApi
@RequiresPermission(android.Manifest.permission.NETWORK_CARRIER_PROVISIONING)
public static final String ACTION_NETWORK_SETTINGS_RESET =
"android.net.wifi.action.NETWORK_SETTINGS_RESET";
//wifi信息修改
@SystemApi
public static final String CONFIGURED_NETWORKS_CHANGED_ACTION ="android.net.wifi.CONFIGURED_NETWORKS_CHANGE";
//常用的wifi开关广播,其他常用广播就不再罗列了。
/**
* Broadcast intent action indicating that Wi-Fi has been enabled, disabled,
* enabling, disabling, or unknown. One extra provides this state as an int.
* Another extra provides the previous state, if available. No network-related
* permissions are required to subscribe to this broadcast.
*
* <p class="note">This broadcast is not delivered to manifest receivers in
* applications that target API version 26 or later.
*
* @see #EXTRA_WIFI_STATE
* @see #EXTRA_PREVIOUS_WIFI_STATE
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String WIFI_STATE_CHANGED_ACTION =
"android.net.wifi.WIFI_STATE_CHANGED";
还有些广播,没使用过,并且看里面的注释也不清楚具体用途,就不一一罗列了。
可以看到很多不常用的广播都是需要系统权限才能监听到。
5、本文用到的获取ip代码
Android 获取ip地址多种方式介绍:
https://blog.csdn.net/wenzhi20102321/article/details/141673195
5、Android 蓝牙相关广播介绍
主要是开关,配对,取消配对,蓝牙名称修改等广播;
详细介绍:
https://blog.csdn.net/wenzhi20102321/article/details/134956116