程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-在無線模式存在的情況下,如何連接無線網絡

java-在無線模式存在的情況下,如何連接無線網絡

編輯:編程綜合問答
在無線模式存在的情況下,如何連接無線網絡

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

}

}

最佳回答:


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));
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved