连接到指定的可用 wifi 网络
从我的应用程序中,我能够打开/关闭设备中的 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;
}
}
From my application, I am able to switch on/off WIFI in my device and able to scan the available networks, but i am unable to connect to specified available network. This is the code I am using:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然这是一个老问题,但万一有人遇到这个问题,以下内容对我有帮助:
如果网络已经可用,则无需提供各种参数,包括密码。
Although this is an old question, in case someone comes across this, the following helped me:
If the network is already available, then there is no need for giving the various parameters, including the password.
只是一个评论。您是否正在尝试临时连接?我读过的“普通”Android 手机不可能做到这一点......
Just a comment. Are you trying to connect ad hoc? This is not possible with "normal" Android phones I read...
您必须禁用其他网络:
You have to disable other networks: