Wifi Connect To Access Point
<p>currently i am trying to estblish a connection to an acces point ... i have found q quite good tutorial (http://windev-us.net/WXUS_WEB/pdf/LSTArticle.pdf). I have modifed it a bit and try to run the app but when i pick a network to connect to i face an error. Maybe someone of you guys can help me ...</p>

<pre><code>public class WifiActivity extends Activity {

static WifiManager WifiManager;
ArrayAdapter &lt;String&gt; net_array_adapter;
List &lt;ScanResult &gt;Wlan_list;

public static final int WPA = 1;
public static final int WEP = 2;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);

    net_array_adapter = new ArrayAdapter &lt;String&gt; (this, R.layout.device_name);
    ListView lv_net = (ListView)findViewById(R.id.lv_net);
    lv_net.setAdapter(net_array_adapter);
    lv_net.setOnItemClickListener(Net);

    IntentFilter filter = new IntentFilter(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    this.registerReceiver(Wifi, filter);

    //Scan
    Button btn_scan = (Button)findViewById(R.id.btn_scan);
    btn_scan.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            Discovery();
        }       
    });
}

protected void Discovery() {
    WifiManager.startScan();
}

private OnItemClickListener Net = new OnItemClickListener() {
    public void onItemClick(AdapterView&lt;?&gt; av, View v, int arg2, long arg3) {

        WifiInfo WifiInfo = WifiManager.getConnectionInfo();
        //Name
        String SSID = WifiInfo.getSSID();
        //MAC-Adresse
        String BSSID = WifiInfo.getBSSID();
        //Sichertheit
        int Key = 0; //!!! EDIT: Key= 1 or 2 --&gt; still dont work
        //Passwort
        String PW = "123";

        connectToNetwork(BSSID, Key, PW, SSID);
        finish();
    }
};

private final BroadcastReceiver Wifi = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        net_array_adapter.clear();
        Wlan_list = WifiManager.getScanResults();
        if (WifiManager.isWifiEnabled() == false){
            net_array_adapter.add("WLAN is currently disabled");
        } else {
            for(int i = 0; i &lt; Wlan_list.size(); i++){
                net_array_adapter.add("Name: " + Wlan_list.get(i).SSID + "\nMAC-Adresse: " + Wlan_list.get(i).BSSID);
                if (net_array_adapter.getCount() == 0){
                    net_array_adapter.add("No network avaible");
                }
            }   
        }
    }
};

//Verbindung mit gewählten AP aufbauen
public static boolean connectToNetwork(String sBSSID, int iSecurityType, String sSecurityKey, String sSSID){

    WifiConfiguration tmpConfig;

    List &lt;WifiConfiguration&gt; listConfig = WifiManager.getConfiguredNetworks();

    for (int i = 0; i&lt;listConfig.size(); i++){
        tmpConfig = listConfig.get(i);
        if (tmpConfig.BSSID.equalsIgnoreCase(sBSSID)){
            return WifiManager.enableNetwork(tmpConfig.networkId, true);
        }
    }

    tmpConfig = new WifiConfiguration();
    tmpConfig.BSSID = sBSSID;
    tmpConfig.SSID = sSSID; 
    tmpConfig.priority = 1;

    switch(iSecurityType){
    //WPA
    case WPA:
        tmpConfig.preSharedKey = sSecurityKey;
        break;
    //WEP
    case WEP:
        tmpConfig.wepKeys[0] = sSecurityKey;
        tmpConfig.wepTxKeyIndex = 0;
        break;
    default:
        break;
    }
    tmpConfig.status = WifiConfiguration.Status.ENABLED;

    int netId = WifiManager.addNetwork(tmpConfig);

    return WifiManager.enableNetwork(netId, true);
}
</code></pre>

<p>}</p>

<p>and here is the log:</p>

<pre><code>04-13 13:29:36.421: E/AndroidRuntime(2496): FATAL EXCEPTION: main
04-13 13:29:36.421: E/AndroidRuntime(2496): java.lang.NullPointerException
04-13 13:29:36.421: E/AndroidRuntime(2496): at wifi.aut.connect.WifiActivity.connectToNetwork(WifiActivity.java:117)
04-13 13:29:36.421: E/AndroidRuntime(2496): at wifi.aut.connect.WifiActivity$1.onItemClick(WifiActivity.java:85)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.widget.ListView.performItemClick(ListView.java:3513)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.os.Handler.handleCallback(Handler.java:587)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.os.Looper.loop(Looper.java:130)
04-13 13:29:36.421: E/AndroidRuntime(2496): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 13:29:36.421: E/AndroidRuntime(2496): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 13:29:36.421: E/AndroidRuntime(2496): at java.lang.reflect.Method.invoke(Method.java:507)
04-13 13:29:36.421: E/AndroidRuntime(2496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 13:29:36.421: E/AndroidRuntime(2496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 13:29:36.421: E/AndroidRuntime(2496): at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Btw i am currently changing the Key value manuel but it wont work with the value 1 either. Thank you very much for all postings ... Andre</p>
