80,471
社区成员




private void test()
{
if(mWifiManager.isWifiEnabled()==false)
{
System.out.println("打开wifi...");
if(OpenWifi())
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
showInfo("wifi已打开");
}
if (mWifiManager.startScan()) //扫描可用的无线网络
{
List<ScanResult> scanResultList = mWifiManager.getScanResults();
for (int i = 0; i < scanResultList.size(); i++)
{
ScanResult scanRet = scanResultList.get(i);
if (scanRet.SSID.equalsIgnoreCase(wifi_siid)) //找到需要的网络
{
System.out.println("配置网络...");
mWifiConfiguration=new WifiConfiguration();
mWifiConfiguration.SSID= "\""+wifi_siid+ "\"";
mWifiConfiguration.preSharedKey="\""+wifi_password+"\"";
mWifiConfiguration.SSID="\"fjq\"";
mWifiConfiguration.preSharedKey="\"zjuttxz321\"";
mWifiConfiguration.hiddenSSID=false;
mWifiConfiguration.status=WifiConfiguration.Status.ENABLED;
mWifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
// mWifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
mWifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
mWifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
mWifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netId=mWifiManager.addNetwork(mWifiConfiguration);
Log.v(Tag,"******************id= ******************"+netId);
if(netId==-1){
showInfo("wifi添加失败!");
return;
}
//mWifiManager.saveConfiguration();
//mWifiManager.updateNetwork(mWifiConfiguration);
boolean b=mWifiManager.enableNetwork(netId, true);//该函数返回值为true了,但为什么连接不上去呢?
Log.v(Tag,"b= "+b);
if(b){
System.out.println("wifi连接成功!");
showInfo("当前连接信息:");
wifiInfo=mWifiManager.getConnectionInfo();
showInfo(wifiInfo.toString());
}else{
showInfo("wifi连接失败!");
}
break;
}
if(i==scanResultList.size()-1){
showInfo("没有查询到指定网络!");
System.out.println("没有查询到指定网络!");
return ;
}
}
}