2013-04-04 282 views
0

我試圖用編程方式連接到使用Android的wifi,但是當我輸入我的WEP密鑰在十六進制 - Logcat狀態它太長。當我嘗試使用明文時 - 它表明它太短(並且從不連接)。當我在我創建的應用程序外部手動輸入時(通過簡單地輸入密碼:超人),它會連接!Android WifiManager - 以十六進制或不以十六進制

P.S.

我嘗試使用下面的StackOverflow例如:

How do I connect to a specific Wi-Fi network in Android programmatically?

六角:

String networkSSID = "ANDRE-PC_NETWORK"; 
    String networkPass = "73:75:70:65:72:6d:61:6e"; 

的logcat:

04-04 12:12:13.643: E/wpa_supplicant(594): Line 0: Too long WEP key 0 '"73:75:70:65:72:6d:61:6e"'. 
04-04 12:12:13.643: E/WifiConfigStore(479): failed to set wep_key0: "73:75:70:65:72:6d:61:6e" 
04-04 12:12:13.793: I/ActivityManager(479): Displayed com.nfc.linked/.Connect: +855ms 
04-04 12:12:16.283: W/GAV2(3422): Thread[Service Reconnect,5,main]: Connection to service failed 1 

沒有十六進制:

String networkSSID = "ANDRE-PC_NETWORK"; 
    String networkPass = "superman"; 

的logcat:

04-04 12:23:10.913: E/wpa_supplicant(594): Line 0: Invalid WEP key length 8 - this network block will be ignored 

來源:

import java.util.List; 

import android.app.Activity;  
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.content.Context; 

public class Connect extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.connect); 
     String networkSSID = "ANDRE-PC_NETWORK"; 
     String networkPass = "superman"; 

     WifiConfiguration conf = new WifiConfiguration(); 
     conf.SSID = "\"" + networkSSID + "\""; //ssid must be in quotes 


     conf.wepKeys[0] = "\"" + networkPass + "\""; 
     conf.wepTxKeyIndex = 0; 
     conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
     conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

     conf.preSharedKey = "\""+ networkPass +"\""; 

     conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 

     WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); 
     wifiManager.addNetwork(conf); 

     List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); 
     for(WifiConfiguration i : list) { 
      if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) { 
       wifiManager.disconnect(); 
       wifiManager.enableNetwork(i.networkId, true); 
       wifiManager.reconnect();     

       break; 
      }   
     } 

    }} 

回答

0

嘗試:

String networkPass = "73757065726d616e"; 
+0

釘它!謝謝... – RobTheBuilder 2013-04-04 17:04:18

+0

順便說一句 - 有一個額外的問題,我遇到了...現在執行上面的源代碼 - 使用您建議的密碼格式時 - 它創建一個新的網絡配置文件ANDRE-PC_Network作爲WEP網絡而不是WPA/WPA2(並且從未連接 - 因爲沒有WEP網絡標題爲「Andre-PC_NETWORK [它正在使用WPA/WPA2]) – RobTheBuilder 2013-04-04 17:06:43

+0

如果您查看您引用的教程,則需要根據需要設置不同的配置參數關於你想連接的網絡是WEP,WPA等。你已經爲** ** WEP和WPA指定了配置。如果網絡是WPA,那麼你應該只**指定WPA的配置(即:只有'preSharedKey'不是'wepKeys','allowedKeyManagement'和'allowedGroupCiphers') – 2013-04-04 17:16:13