【发布时间】:2015-05-20 23:16:10
【问题描述】:
我是 android 开发新手,正在尝试使用 Android SDK 连接到 WiFi 网络。断开连接的代码工作正常,但重新连接失败。这是我的代码
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain SSID in quotes
conf.wepKeys[0] = password; //WEP password is in hex, we do not need to surround it with quotes.
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
WifiManager wifiManager = (WifiManager)ba.applicationContext.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
//WiFi Connection success, return true
return true;
} catch (Exception ex) {
throw ex;
}
我将此代码包装在我在不同应用程序中使用的 jar 文件中。当我调用此方法并尝试使用 SSID 和密码连接到 WEP 网络时,我不断收到以下错误:
android.system.ErrNoException: recvfrom failed: ETIMEDOUT(连接超时)。
该错误确实表明某处存在连接超时,但我无法解决这个问题来修复我的代码。我可以在代码中引入任何指针和更改以使其工作吗??
帕里托什
【问题讨论】:
-
我不清楚你是在什么时候收到错误的。如果要连接,为什么要断开连接?
-
我基本上使用列表视图列出了所有可用的网络,用户可以从列表中选择一个并输入密码以连接到新网络。因此与现有网络断开连接并与新网络连接。
-
It is unclear to me at which moment you get the error. -
如果您只是使用此代码而不是来自 jar 文件,那么它的行为如何?
-
您确定此 SSID 使用 WEP 编码吗?
标签: android android-wifi wifimanager