Android:如何检测WiFi是否完全连接?

时间:2018-05-21 09:03:59

标签: java android wifi wifimanager

我在Android WiFi API中发现了一些奇怪的行为。 在建立WiFi连接以连接到路由器上的套接字后,我需要获取远程IP。但令我遗憾的是,我得到了一些不确定的行为。

假设我有三个接入点:

  • NET1
  • NET2
  • NET3

目前我已连接到NET1,我想切换到NET3。

我连接到wifi:

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "NET3";
...
int netId = m_manager.addNetwork(wifiConfig);
IntentFilter intentFilter = new IntentFilter();                
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
m_context.registerReceiver(mWifiConnectReceiver, intentFilter);
m_manager.disconnect();
m_manager.enableNetwork(networkID, true);

m_manager这里是WifiManager

要接收WiFi事件,我有以下代码:

private final BroadcastReceiver mWifiConnectReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
    NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            Log.i("MyApp","--------------------------");
            Log.i("MyApp", info.getDetailedState() + ", " + info.getExtraInfo());            
            String ssid = m_manager.getConnectionInfo().getSSID();
            ssid = ssid.replaceAll("^\"(.*)\"$", "$1");
            Log.i("MyApp","SSID: " + ssid);
    }
}); 

连接后输出如下:

05-21 11:36:02.081 : CONNECTED, "NET1"
05-21 11:36:02.094 : SSID: NET3
05-21 11:36:02.095 : --------------------------
05-21 11:36:02.095 : CONNECTED, <unknown ssid>
05-21 11:36:02.098 : SSID: NET3
05-21 11:36:02.098 : --------------------------
05-21 11:36:02.098 : DISCONNECTED, <unknown ssid>
05-21 11:36:02.100 : SSID: NET3
05-21 11:36:02.100 : --------------------------
05-21 11:36:02.100 : DISCONNECTED, <unknown ssid>
05-21 11:36:02.104 : SSID: NET3
05-21 11:36:02.104 : --------------------------
05-21 11:36:02.104 : DISCONNECTED, <unknown ssid>
05-21 11:36:02.105 : SSID: NET3
05-21 11:36:02.107 : --------------------------
05-21 11:36:02.107 : DISCONNECTED, "NET1"
05-21 11:36:02.112 : SSID: NET3
05-21 11:36:02.112 : --------------------------
05-21 11:36:02.112 : CONNECTING, "NET1"
05-21 11:36:02.115 : SSID: NET3
05-21 11:36:02.240 : --------------------------
05-21 11:36:02.240 : CONNECTING, "NET3"
05-21 11:36:02.244 : SSID: NET3
05-21 11:36:02.245 : --------------------------
05-21 11:36:02.245 : CONNECTING, "NET3"
05-21 11:36:02.248 : SSID: NET3
05-21 11:36:02.308 : --------------------------
05-21 11:36:02.308 : OBTAINING_IPADDR, "NET3"
05-21 11:36:02.311 : SSID: NET3
05-21 11:36:07.885 : --------------------------
05-21 11:36:07.885 : CONNECTED, "NET3"
05-21 11:36:07.887 : SSID: NET3
05-21 11:36:07.903 : --------------------------
05-21 11:36:07.903 : CONNECTED, "NET3"
05-21 11:36:07.906 : SSID: NET3

在这里我看到很多不一致的地方:

  • 第一个CONNECTED令人费解 - 为什么会在这里? NET1已经连接。为什么当我连接到NET3时,它会为NET1触发CONNECTED?
  • 为什么NetworkInfo报告NET1(那是对的)和getConnectionInfo()报告NET3 ??
  • 最让我担心的问题是 - 如何以一种明确的方式检测与建议网络的连接?是否可以从NetworkInfo
  • 中提取SSID

更新

我发现以下代码返回正确的SSID:

WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
if(wifiInfo != null) {
    String ssid = wifiInfo.getSSID();
    ssid = ssid.replaceAll("^\"(.*)\"$", "$1");
}

可能是用户而不是m_manager.getConnectionInfo().getSSID()

0 个答案:

没有答案