Android,Wifi重新连接无法正常工作

时间:2017-05-25 04:16:37

标签: android android-wifi

我有一个Wifi路由器,我尝试建立这样的连接:

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + wifiSSID + "\"";
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfig.preSharedKey = "\"" + wifiPassword + "\"";


WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

int netId = wifiManager.addNetwork(wifiConfig);
Log.d("WIFI_RECONNECT", netId + "");
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
boolean recon = wifiManager.reconnect();
Log.d("WIFI_RECONNECT", recon + "");

当我启动此代码时,我会在日志中收到此信息:

D/WIFI_RECONNECT: "My_WIFI_SSID" 1234567890
D/WIFI_RECONNECT: 67
D/WIFI_RECONNECT: true

但是,我的设备显示我已断开所有可访问的wifi路由器,我的路由器wifi网络显示在我的可用wifi列表中但未连接模式。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

这是因为WifiManager.reconnect()返回true if the operation succeeded而不是连接完成时。您应该在reconnect()

之后使用广播接收器捕获Wi-Fi状态