Wifi连接到接入点

时间:2012-04-13 11:34:33

标签: android

目前我正在尝试建立一个与接入点的连接...我找到了很好的教程(http://windev-us.net/WXUS_WEB/pdf/LSTArticle.pdf)。我已经修改了一下并尝试运行应用程序,但当我选择一个网络连接到我面临一个错误。也许你们中的某些人可以帮助我......

public class WifiActivity extends Activity {

static WifiManager WifiManager;
ArrayAdapter <String> net_array_adapter;
List <ScanResult >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 <String> (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<?> 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 --> 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 < 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 <WifiConfiguration> listConfig = WifiManager.getConfiguredNetworks();

    for (int i = 0; i<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);
}

}

这是日志:

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)

顺便说一下,我正在更改Key值manuel,但它也不能使用值1。非常感谢所有帖子......安德烈

1 个答案:

答案 0 :(得分:0)

我想问题出现在 onItemClick int Key = 0;。与方法 connectToNetwork 中的 Switch 语句一样,您只检查 WPA WEP 1 < / em>和 2 分别。它会导致默认符合您的switch语句,并在 tmpConfig 中留下一些空白。

因此,将设置为 0 ,您应该将其设置为 1 2 并制作确保您的密码长度符合所选的加密标准