Android连接到托管网络

时间:2014-04-01 20:46:55

标签: java android networking

我正在开展一个项目,我正在尝试将移动设备连接到Windows托管网络。 Android设备扫描主机Windows系统上的QR码以获取网络SSID和密钥,连接,然后与Windows系统上的服务器软件进行通信,以做其他有趣的事情。我遇到的问题是,当连接到托管网络时,应用程序表现得非常不稳定;很多时候(甚至可能是大多数)它连接没有任何问题。有时我最终连接到一个不同的,预先存在的Wifi配置文件。其他时候,我的代码在addNetwork WifiConfigurationWifiManager时失败,有时我看似连接但在创建套接字时会抛出超时异常。

为冗长而道歉,这是我连接的代码。 Windows托管网络是WPA2,问题肯定不是来自不正确的SSID和/或密钥。在此先感谢您的帮助,这让我很生气!

String contSSID = "SomeSSID";
String contKEY = "SomePassword";

WifiManager wifiManager = (WifiManager) (ontext.getSystemService(Context.WIFI_SERVICE);
boolean addNetwork = true;
int netId = -1;

for (WifiConfiguration tmp : wifiManager.getConfiguredNetworks()) {
    if (tmp.SSID.equals( "\""+contSSID+"\"")) {
        addNetwork = false;
        netId = tmp.networkId;
    }
}
if (addNetwork) {
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"".concat(contSSID).concat("\"");
    conf.preSharedKey = "\"".concat(contKEY).concat("\"");
    conf.priority = 40;
    conf.hiddenSSID = false;
    conf.status = WifiConfiguration.Status.ENABLED;
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    for (int i=0; i<10; i++) {
        netId = wifiManager.addNetwork(conf);
        if (netId != -1) {
            Log.d("CAT", "Added network configuration to WifiManager");
            break;
        } else if (i==9) {
            Log.e("CAT", "Error adding network configuration!");
            return "NETCONFIGERROR";
        }
    }
} else {
    Log.d("CAT", "Hosted network has existing profile");
    if (netId == -1) {
        Log.e("CAT", "Existing profile is invalid!");
        return "NETCONFIGERROR2";
    }
}
for (int i=0; i<10; i++) {
    if (wifiManager.setWifiEnabled(true)) {
        Log.d("CAT", "Successfully enabled WiFi");
        break;
    } else if (i==9) {
        Log.e("CAT","Could not enable WiFi!");
        return "WIFIERROR";
    }
}
for (int i=0; i<10; i++) {
    if (wifiManager.disconnect()) {
        Log.d("CAT", "Successfully disconnected from existing networks");
        break;
    } else if (i==9) {
        Log.e("CAT", "Could not disconnect from current WiFi!");
        return "DISCONNECTERROR";
    }
}
boolean enabled = wifiManager.enableNetwork(netId, true);;
for (int i=0; i<10; i++) {
    if (enabled = true) {
        Log.d("CAT", "Network Enabled");
        break;
    }
    enabled = wifiManager.enableNetwork(netId, true);
}
if (!enabled) {
    Log.e("CAT","Could not enable network!");
    return "ENABLEERROR";
}
int j = 0;
while (wifiManager.reconnect() == false) {
    j++;
    if (j>10) {
        Log.e("CAT","Could not reconnect network!");
        return "RECONNECTERROR";
    }
}

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
long startTime = System.currentTimeMillis();
while (networkInfo == null || (networkInfo.isConnected()==false) || networkInfo.getType() != ConnectivityManager.TYPE_WIFI ) {
    networkInfo = connectivityManager.getActiveNetworkInfo();
    if ((System.currentTimeMillis() - startTime) > (30 * 1000)) {
        Log.e("CAT","Error confirming network connection!");
        return "DETECTCONNECTERROR";
    }
            try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        Log.d("CAT", e.getLocalizedMessage());
        Thread.currentThread().interrupt();
    }
}
// Now would come creating the socket and all that.

我还有一些其他的睡眠,我为了尝试管理代码的丑陋而删除了,但我很确定那些不是问题。此外,这是通过AsyncTask在后​​台运行的。我认为有关它的内容,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

所以看起来我的问题一直都在行

conf.status = WifiConfiguration.Status.ENABLED;

完全省略之后,我的代码似乎没有问题。我不认为这个值是手动设置的,但我并不积极。对于遇到类似问题的人,我发现如果发生错误,打印conf.toString()非常有帮助;在WifiConfiguration的源代码中,有一个名为disableReason的漂亮的公共int,我无法访问,但是当你使用toString()时,除非你当前连接到网络,否则它包括在内。来源就是这样:

if (this.status == WifiConfiguration.Status.CURRENT) {
    sbuf.append("* ");
} else if (this.status == WifiConfiguration.Status.DISABLED) {
    sbuf.append("- DSBLE: ").append(this.disableReason).append(" ");
}

这实际上是第一行打印,并提供了许多其他有用的信息。如果您首先手动连接到网络(使用android创建网络配置文件),然后使用toString()获取配置的所有细节,这也很有帮助;这样你就可以确认你在WifiConfiguration中正确地做到了。

希望这有助于某人,请发表任何评论/更正!

编辑:我意识到这可能有点不清楚;如果你有一个WifiConfiguration对象,例如WifiConfiguration conf在我的情况下,您只需使用conf.toString()