gpt4 book ai didi

android - 连接到指定的可用 wifi 网络

转载 作者:行者123 更新时间:2023-11-29 02:13:38 25 4
gpt4 key购买 nike

在我的应用程序中,我能够在我的设备中打开/关闭 WIFI 并能够扫描可用网络,但我无法连接到指定的可用网络。这是我正在使用的代码:

 if(wifi.getWifiState()==wifi.WIFI_STATE_DISABLED)
{
wifi.setWifiEnabled(true);

}
if(wifi.startScan())
{
//ls=(ArrayAdapter<ScanResult>) wifi.getScanResults();
ls=wifi.getScanResults();

Log.e("",ls.get(0).toString());
for(int i=0;i<ls.size();i++)
{ Log.e("VALUE"," "+ls.get(i).toString());
Log.e("",""+ls.get(i).SSID);
if(ls.get(i).SSID.equalsIgnoreCase("SPECTRUM-GREEN"))
{
Log.e("","SPectrum GREEN FOUND.....");

try{
String ssid="\""+ls.get(i).SSID+"\"";
Log.e("SSId"," "+ssid);
config.SSID=ssid;
}catch(Exception e){Log.e("","Error : "+e.toString());}

config.preSharedKey="\"password\"";
config.status=WifiConfiguration.Status.ENABLED;

config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

int res=wifi.addNetwork(config);
Log.e("ENABLE ",""+wifi.enableNetwork(res, false));

break;
}
}

最佳答案

虽然这是一个老问题,但万一有人遇到这个问题,以下内容对我有帮助:

void connect (String ssidName) {
boolean result = false;
List<WifiConfiguration> arraylist = wifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : arraylist) {
String wifiConfigSSID = wifiConfiguration.SSID.replace("\"", "");
if (wifiConfigSSID.equals(ssidName)) {
result = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
break;
}
}
}

如果网络已经可用,则无需提供各种参数,包括密码。

关于android - 连接到指定的可用 wifi 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5775697/

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