hi ich bin ein android noob und versuche das w-lan nach access points zu durchsuchen
so sieht meine app aus
my MainActivity:
Code
1package com.wifi;
2
3
4import android.app.Activity;
5import android.content.BroadcastReceiver;
6import android.content.Context;
7import android.content.Intent;
8import android.content.IntentFilter;
9import android.net.ConnectivityManager;
10import android.net.wifi.WifiManager;
11import android.os.Bundle;
12import android.provider.Settings;
13import android.view.View;
14import android.widget.Toast;
15
16import android.widget.AdapterView;
17import android.widget.AdapterView.OnItemSelectedListener;
18import android.widget.ArrayAdapter;
19import android.widget.Spinner;
20import android.widget.TextView;
21import android.widget.Button;
22import android.widget.ProgressBar;
23
24
25
26public class MainActivity extends Activity
27{
28 public TextView LaTestText;
29 private Button BtScanWiFi;
30 private Button BtSelectAccessPoint;
31 private ProgressBar ProgressBar1;
32 public Spinner WiFiChannels;
33
34 WifiManager WiFiManager;
35 BroadcastReceiver WiFireceiver;
36 WiFiReceiver TextReceiver;
37
38 /** Called when the activity is first created. */
39 @Override
40 public void onCreate(Bundle savedInstanceState)
41 {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.main);
44 initControls();
45 WiFiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
46 if (WiFireceiver == null)
47 WiFireceiver = new WiFiReceiver(this);
48 registerReceiver(WiFireceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
49 }
50
51 @Override
52 public void onStop(){
53 super.onStop();
54 finish();
55 }
56
57
58 private void initControls()
59 {
60 LaTestText = (TextView)findViewById(R.id.LaTestText);
61 BtSelectAccessPoint = (Button)findViewById(R.id.BtSelectAccessPoint);
62 BtScanWiFi = (Button)findViewById(R.id.BtScanWiFi);
63 ProgressBar1 = (ProgressBar)findViewById(R.id.ProgressBar1);
64 WiFiChannels = (Spinner)findViewById(R.id.WiFiChannels);
65
66 ProgressBar1.setVisibility(ProgressBar.INVISIBLE);
67
68 BtScanWiFi.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ ScanWiFi (); }});
69 BtSelectAccessPoint.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ SelectWiFi (); }});
70 WiFiChannels.setOnItemSelectedListener(
71 new OnItemSelectedListener()
72 {
73 public void onItemSelected(AdapterView parent,View View, int pos, long id)
74 {
75 // something
76 }
77 public void onNothingSelected(AdapterView parent)
78 {
79 // Do nothing.
80 }
81 }
82 );
83
84 }
85
86
87
88
89 private void ScanWiFi()
90 {
91 ConnectivityManager manager = (ConnectivityManager)getSystemService(MainActivity.CONNECTIVITY_SERVICE);
92
93 Boolean is3G = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
94 Boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
95
96 String ConnectionInfo;
97
98 if(is3G)
99 {
100 ConnectionInfo = "Your current InternetConnectionType is ''3G'' please set to WiFi !!!";
101 Toast.makeText(getApplicationContext(), ConnectionInfo, Toast.LENGTH_LONG).show();
102 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
103 }
104 else
105 if(isWifi)
106 {
107 ConnectionInfo = "Your current InternetConnectionType is ''WiFi/W-Lan''" ;
108 Toast.makeText(getApplicationContext(), "Scanning for AccessPoints ...", Toast.LENGTH_LONG).show();
109 WiFiManager.startScan();
110
111
112 }
113 else
114 {
115 ConnectionInfo = "Your are currently not connectet to WiFi/W-Lan" ;
116
117 Toast.makeText(getApplicationContext(), ConnectionInfo, Toast.LENGTH_LONG).show();
118 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
119
120 }
121
122
123 }
124
125 private void SelectWiFi()
126 {
127 //
128 }
129
130
131 }
Alles anzeigen
WiFiReceiver.java:
Java
1package com;
2
3import java.util.List;
4
5import android.content.BroadcastReceiver;
6import android.content.Context;
7import android.content.Intent;
8import android.net.wifi.ScanResult;
9import android.util.Log;
10import android.widget.ArrayAdapter;
11import android.widget.Spinner;
12
13
14public class WiFiReceiver extends BroadcastReceiver{
15 private MainActivity mainactivity;
16 public StringBuilder sb;
17 public List APList;
18 public WiFiReceiver(MainActivity mainactivity)
19 {
20 super();
21 this.mainactivity = mainactivity;
22 }
23 @Override
24 public void onReceive(Context c, Intent intent)
25 {
26 APList = mainactivity.WiFiManager.getScanResults();
27 for(int i = 0; i < APList.size(); i++)
28 {
29 sb.append(new Integer(i+1).toString() + ".");
30 sb.append(( APList.get(i)).toString());
31 sb.append("\\n");
32 }
33 String info=" "+APList.size();
34 Log.i("wifiScan", info);
35 mainactivity.LaTestText.setText(info);
36
37
38
39 //mainactivity.LaTestText.setText(sb);
40 }
41
42
43}
Alles anzeigen
AndroidManifest:
wenn ich nun die app auf meinem GalaxySII ausführe und auf "scann wifi" drücke bekomme ich nach ca. 2-5 sekunden ein fenster "Sorry! The application stopped unexpected." und meine app schließt sich -.-
wenn ich "startscan()" auskommentire läuft alles wunderbar
was mache ich falsch ?
danke das ich eure zeit in anspruch nehmen darf