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

我使用下面的代码在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);


qq_遁去的一_1
浏览 473回答 2
2回答

收到一只叮咚

public&nbsp;WifiConfiguration&nbsp;checkPreviousConfiguration(WifiConfiguration&nbsp;wc)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;List<WifiConfiguration&nbsp;configs&nbsp;=&nbsp;wifi.getConfigurations(); &nbsp;&nbsp;&nbsp;&nbsp;for(WifiConfiguration&nbsp;config&nbsp;:&nbsp;configs)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(config.SSID.equals(wc.SSID))&nbsp;return&nbsp;config; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;wc; } //...... int&nbsp;res&nbsp;=&nbsp;wifi.addNetwork(checkPreviousConfiguration(wc));

暮色呼如

int res = wifi.addNetwork(wc); 三星手机测试发现,返回-1 LZ 请教一下
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java