我正在尝试以编程方式使用 Android 连接到 wifi,但是当我以十六进制输入我的 WEP 密钥时 - Logcat 表示它太长了。当我尝试使用纯文本时 - 它表示它太短(并且永远不会连接)。当我在我构建的应用程序外部手动输入时(只需输入密码:superman),它就会连接!
附言
我正在尝试使用以下 StackOverflow 示例:
如何以编程方式连接到 Android 中的特定 Wi-Fi 网络?
使用十六进制:
String networkSSID = "ANDRE-PC_NETWORK";
String networkPass = "73:75:70:65:72:6d:61:6e";
日志猫:
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";
日志猫:
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;
}
}
}}