現在、アクセスポイントへの接続を確立しようとしています... 非常に優れたチュートリアル (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でも機能しません。すべての投稿に感謝します... アンドレ