android – 连接到打开WiFi

我使用以下代码连接到加密网络.但是,如果一个网络是不安全的,并且我留下空白(“”),那么它将失败.有人有个想法如何解决这个问题?此外,可以使用ssid / bssid检测网络是否打开?还是我必须用过滤器进行扫描?
public void connectToSSID(final String ssid,final String key) {
    Log.i("wifimaster","connection to "+ssid);

    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\""+ssid+"\""; //IMPORTANT! This should be in Quotes!!  
    wc.priority = 40;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

    wc.preSharedKey = "\""+key+"\"";
    wc.wepKeys[0] = "\""+key+"\""; //This is the WEP Password
    wc.wepTxKeyIndex = 0;

    wc.preSharedKey = "\""+key+"\"";

    int res = wifiManager.addNetwork(wc);
    Log.d("WifiPreference","add Network returned " + res );
    boolean es = wifiManager.saveConfiguration();
    Log.d("WifiPreference","saveConfiguration returned " + es );
    boolean b = wifiManager.enableNetwork(res,true);   
    Log.d("WifiPreference","enableNetwork returned " + b );

    if(b)
        Toast.makeText(c,c.getString(R.string.connected),Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(c,c.getString(R.string.unconnected),Toast.LENGTH_SHORT).show();
}

解决方法

使用
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

只有它会工作.

删除所有其他分配给wc,没有必要使用它们来连接到网络的简单任务.您没有创建一个网络,您正在连接到现有网络.

有关完整解决方案,请查看此链接How do I connect to a specific Wi-Fi network in Android programmatically?它有你需要知道的一切.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...