2012-03-15 53 views
0

所以我已經拖網真正的所有線程在這裏,以及谷歌會帶我到哪裏。但是我仍然無法連接到WPA PSK網絡。 這裏是我的代碼,我有2個edittext字段,我從中讀取SSID和PSK,然後選擇一個複選框來選擇SSID是否隱藏。安卓使用wifimanager連接到WPA-PSK安全網絡

EditText mSSID = (EditText) findViewById(R.id.wifiTVssidcurrent); 
    String networkSSID = mSSID.getText().toString(); 
    EditText mWPA = (EditText) findViewById(R.id.wifiTVwpacurrent); 
    String networkWPA = mWPA.getText().toString(); 

    // Update text to show that connection is pending 
    TextView wifiStatus = (TextView) findViewById(R.id.wifiTVconnectionstatus); 
    wifiStatus.setText("Connecting to " + networkSSID); 

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration wc = new WifiConfiguration(); 
    wc.SSID = "\"".concat(networkSSID).concat("\""); 
    wc.preSharedKey = "\"".concat(networkWPA).concat("\""); 

    CheckBox mSSIDHidden = (CheckBox) findViewById(R.id.wifiCBhiddenssid); 
    wc.hiddenSSID = false; 
    if (mSSIDHidden.isChecked()) { 
     wc.hiddenSSID = true; 
    } 
    wc.status = WifiConfiguration.Status.ENABLED;   
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    int res = wifi.addNetwork(wc); 
    Log.d("WifiPreference", "add Network returned " + res); 
    boolean b = wifi.enableNetwork(res, true);   
    Log.d("WifiPreference", "enableNetwork returned " + b); 
    boolean c = wifi.reconnect(); 
    Log.d("WifiPreference", "reconnect returned " + c); 

我看到手機上運行此之後是什麼,一個AP中設置創建的,但它沒有連接。如果我以後嘗試從設置中手動使用創建的AP,我無法連接。 但是,如果我從設置內創建AP,我會得到連接。

至於把SSID和WPA PSK我已經嘗試了「\」「。​​concat(networkSSID).concat(」\「」);和「\」「+ networkSSID +」\「」;結果相同。

任何提示將非常歡迎。 問候函 Lasse

+0

所以我想我已經嘗試了一切表明這裏堆棧溢出。 但這個線程實際上幫助我到底 http://stackoverflow.com/questions/8392747/setup-wifi-programatically-using-wpa-security-in-android-tablet 非常感謝RYAN提供的技巧在閱讀一個AP後再模仿它。 在我的情況下,事實證明,我需要添加 即使我設置的WPA我還需要添加 allowedGroupCiphers WEP40和WEP104 而我只用了 wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN) ; 所以我也加了 wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); – Lasse 2012-03-15 19:04:21

回答

4

再次非常感謝Ryan提出瞭如何讀取操作系統創建的設置。

所以我想我已經嘗試了一切在堆棧溢出這裏建議。 但是這個線程實際上幫助我最後Setup wifi programatically using WPA Security in android tablet非常感謝RYAN提供閱讀一個AP的提示,然後模仿它。在我的情況下,原來,我需要添加即使我設置的WPA我還需要添加allowedGroupCiphers WEP40和WEP104而我只有

`wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); //So I also added 
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);`