0

I'm attempting to connect to wifi using Android programatically however when I input my WEP key in Hex - Logcat states it's too long. When I attempt to use plaintext - it states it is too short (and never connects). When I type it in manually outside the app I've built (by simply typing the password: superman) it connects!

P.S.

I'm attempting to use the following StackOverflow example:

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

With Hex:

    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

Without Hex:

  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

SOURCE:

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;
            }           
         }

    }}
Community
  • 1
  • 1
RobTheBuilder
  • 37
  • 5
  • 11

1 Answers1

0

Try:

String networkPass = "73757065726d616e";
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • BTW - there is one additional problem I'm experiencing... Now when executing the source code above - while using the password format you suggested - It creates a new network profile for ANDRE-PC_Network as a WEP network instead of WPA/WPA2 (and never connects - since there is not WEP network titled "Andre-PC_NETWORK [it is using WPA/WPA2]) – RobTheBuilder Apr 04 '13 at 17:06
  • If you look at the tutorial you referenced, there are different configuration parameters you need to set depending on whether the network you want to connect to is WEP, WPA, etc. You have specified the config for **both** WEP and WPA. If the network is WPA then you should **only** specify the configuration for WPA (ie: only `preSharedKey` not `wepKeys`, `allowedKeyManagement` and `allowedGroupCiphers`) – David Wasser Apr 04 '13 at 17:16
  • There isn't a simple way to differentiate between the two (and select accordingly) is there? – RobTheBuilder Apr 04 '13 at 17:53
  • Sorry, I don't know. I'd have to invest some effort to research that myself. Maybe someone else knows. Good luck! – David Wasser Apr 04 '13 at 18:14