Android的WifiManager制造幻象连接? [英] Android WifiManager making a phantom connection?

查看:291
本文介绍了Android的WifiManager制造幻象连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WifiManager测试特定SSID的presence并验证一个给定的WPA密码,但我发现了一个奇怪的结果。

在code是这样的:

  WifiConfiguration WC =新WifiConfiguration();//初始化SSID和密码字符串...
wc.SSID =\\+ SSID +\\;
。WC2 preSharedKey =\\+密码+\\;wc.hiddenSSID = TRUE;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);INT解析度= wifi.addNetwork(WC);
布尔B = wifi.enableNetwork(RES,真);

怪异的是,enableNetwork()返回true,即使无线网络SSID不在线(?!)。但请注意,如果目标Wifi网络是present和密码是正确的,code成功建立连接。

Android的文件说enableNetwork(),如果操作成功返回true。我的问题是:

1)如何,如果它甚至不存在网络已启用?

2)有我初始化参数WifiConfiguration,厕所,正确?

3)这是拨打/测试到WiFi网络连接的正确方法?


解决方案

  

1)如何,如果它甚至不存在网络已启用?


  
  

Android的文件说enableNetwork()返回true,如果操作成功了。


他们实际上意味着让操作已经成功启动,它has't坠毁......这可能会返回false如果WiFi实例没有任何链接更多的Wi-Fi请求者。


  

2)有我初始化参数WifiConfiguration,厕所,正确?


我不知道,但它看起来没事。


  

3)这是拨打/测试到WiFi网络连接的正确方法?


使网络之前,您应该检查网络是使用 startScan()方法实际上可用。 (您将获得SSID的回调列表)。

您也应该使用一个BroadcastReceiver从意向行动 NETWORK_STATE_CHANGED_ACTION ,在那里你可以检查您的接入点连接成功与否得到的结果。

I'm using WifiManager to test for the presence of a particular SSID and verify a given WPA password, but I'm getting a weird result.

The code looks like this:

WifiConfiguration wc = new WifiConfiguration();

// init ssid and password as Strings ...
wc.SSID = "\"" + ssid + "\"";
wc2.preSharedKey  = "\"" + password + "\"";

wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

int res = wifi.addNetwork(wc);
boolean b = wifi.enableNetwork(res, true);

The weird part is that enableNetwork() returns true, even if the wifi network with SSID is not online (?!). Note, however, that if the target Wifi network is present and the password is correct, the code successfully makes the connection.

The Android documentation says enableNetwork() "returns true if the operation succeeded." My questions are:

1) How can the network have been enabled if it's not even there?

2) Have I initialized the WifiConfiguration parameter, wc, incorrectly?

3) Is this the right way to make/test a connection to a Wifi network?

解决方案

1) How can the network have been enabled if it's not even there?

The Android documentation says enableNetwork() "returns true if the operation succeeded."

They actually mean that the enabling operation has been successfully initiated, it has't crashed... This could return false if the wifi instance is not linked any more to the wifi supplicant.

2) Have I initialized the WifiConfiguration parameter, wc, incorrectly?

I am not sure, but it looks alright.

3) Is this the right way to make/test a connection to a Wifi network?

Before enabling the network, you should check if the network is actually available by using the startScan() method. (You will get a list of SSID in the callback).

You should also use a BroadcastReceiver to get the result from the Intent action NETWORK_STATE_CHANGED_ACTION, where you could check if your connection to the access point is successful or not.

这篇关于Android的WifiManager制造幻象连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆