2013-04-04 693 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