【问题标题】:Android WifiManager - to HEX or not to HEXAndroid WifiManager - 到 HEX 或不到 HEX
【发布时间】:2013-03-26 20:20:57
【问题描述】:

我正在尝试以编程方式使用 Android 连接到 wifi,但是当我以十六进制输入我的 WEP 密钥时 - Logcat 指出它太长了。当我尝试使用纯文本时 - 它表明它太短(并且永远不会连接)。当我在我构建的应用程序外部手动输入时(只需输入密码:superman),它就会连接!

附言

我正在尝试使用以下 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;
            }           
         }

    }}

【问题讨论】:

    标签: java android wifi wpa wep


    【解决方案1】:

    试试:

    String networkPass = "73757065726d616e";
    

    【讨论】:

    • 顺便说一句 - 我遇到了一个额外的问题......现在执行上面的源代码时 - 使用您建议的密码格式 - 它为 ANDRE-PC_Network 创建了一个新的网络配置文件作为WEP 网络而不是 WPA/WPA2(并且永远不会连接 - 因为没有名为“Andre-PC_NETWORK [它正在使用 WPA/WPA2] 的 WEP 网络)
    • 如果你看你引用的教程,根据你要连接的网络是WEP,WPA等,你需要设置不同的配置参数。你已经为 WEP 和 WPA。如果网络是 WPA,那么您应该指定 WPA 的配置(即:仅 preSharedKey 而不是 wepKeysallowedKeyManagementallowedGroupCiphers
    • 没有一种简单的方法可以区分两者(并相应地选择)吗?
    • 对不起,我不知道。我必须自己投入一些精力来研究它。也许别人知道。祝你好运!
    • 谢谢先生!非常感谢您的帮助。
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2015-06-16
    • 2021-10-11
    • 2019-06-20
    • 2010-12-30
    相关资源
    最近更新 更多