gpt4 book ai didi

android - 连接到开放式 WiFi

转载 作者:可可西里 更新时间:2023-11-01 19:06:05 25 4
gpt4 key购买 nike

我使用下面的代码连接到加密网络。但是,如果网络不安全并且我将 key 留空(“”)那么它就会失败。有谁知道如何解决这个问题?此外,是否可以使用 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 - 连接到开放式 WiFi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12789788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com