目前我正在尝试建立与接入点的连接......我发现q非常好的教程(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)
顺便说一句,我目前正在更改键值手册,但它也不适用于值 1。非常感谢您的所有帖子......安德烈