2 ssummeraw ssummeraw 于 2012.12.06 17:35 提问

在无线模式存在的情况下,如何连接无线网络

我使用下面的代码在android手机上创建一个无线模式。出于某种原因它每次都会创建一个新模式,因此对于相同的无线网络手机将会有多个模式。是否有办法能实现当一个模式已经存在时,就不要再创建一个新的模式?

public class IVMOBILEActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);       
    TextView status = new TextView(this);                  


    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration wc = new WifiConfiguration();  
    wc.SSID = "\"MyNetwork\""; //IMP! This should be in Quotes!! 

    wc.hiddenSSID = true; 
    wc.status = WifiConfiguration.Status.ENABLED;      
    wc.priority = 10; 
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    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.preSharedKey = "\"Password\""; 
    WifiManager  wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE); 
    boolean res1 = wifiManag.setWifiEnabled(true); 
    int res = wifi.addNetwork(wc); 
    Log.d("WifiPreference", "add Network returned " + res ); 
    boolean es = wifi.saveConfiguration(); 
    Log.d("WifiPreference", "saveConfiguration returned " + es ); 
    boolean b = wifi.enableNetwork(res, true);    
    Log.d("WifiPreference", "enableNetwork returned " + b );   

    tv.setText("You are now connected!  " +
            "Version 1.0");

    status.setText("The was an error connecting, please try again.");

    try {

        Thread.sleep(5000);

         ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

         if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){  
                //You are connected, do something online. 
                setContentView(tv);

            }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {              
                //Not connected.         
               setContentView(status);
            }  

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

2个回答

curie_87
curie_87   2012.12.07 17:47
已采纳
public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
    List<WifiConfiguration configs = wifi.getConfigurations();
    for(WifiConfiguration config : configs) {
        if(config.SSID.equals(wc.SSID)) return config;
    }
    return wc;
}

//......
int res = wifi.addNetwork(checkPreviousConfiguration(wc));
mao520741111
mao520741111   2013.06.13 15:55

int res = wifi.addNetwork(wc); 三星手机测试发现,返回-1 LZ 请教一下

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!