我首先扫描所有可用的WiFi网络,然后输出信号强度和名称。但是我的标签没有按我希望的方式响应。我希望在第一个选项卡上进行扫描,但是当我选择第二个选项卡时,我希望它更加详细,我希望您可以获得所有信息,如mac地址,协议,加密等。为什么我的代码不能执行那我该怎么办?
public class MainActivity extends TabActivity {
WifiManager wifiManager;
WifiScanReceiver wifiReciever;
String bssid;
String ssid;
INT channel;
int level;
ArrayAdapter adapter;
ListView list;
ArrayList<String> wifis;
WifiInfo wifiInfo;
TabHost tabHost;
// TabSpec Names
PRIVATE static final String TAB1 = "Tab1";
private static final String TAB2 = "Tab2";
private static final String TAB3 = "Tab3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
final TabHost.TabSpec inboxSpec = tabHost.newTabSpec(TAB1);
inboxSpec.setIndicator(TAB1);
inboxSpec.setContent(new Intent(this, Tab1.class));
tabHost.addTab(inboxSpec);
TabHost.TabSpec PriceSpec = tabHost.newTabSpec(TAB2);
PriceSpec.setIndicator(TAB2);
PriceSpec.setContent(new Intent(this,Tab2.class));
tabHost.addTab(PriceSpec);
TabHost.TabSpec DistanceSpec = tabHost.newTabSpec(TAB3);
DistanceSpec.setIndicator(TAB3);
DistanceSpec.setContent(new Intent(this,Tab3.class));
tabHost.addTab(DistanceSpec);
tabHost.setCurrentTab(0);
list = (ListView) findViewById(R.id.text);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
wifiInfo = wifiManager.getConnectionInfo();
wifis = new ArrayList<String>(); //initialize wifis
wifis.add("loading...");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, wifis);
list.setAdapter(adapter);
wifiManager.startScan(); //make sure this is the last call
list.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, INT position, long id) {
switchTab(1);
}
});};
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
public static int convertFrequencyToChannel(int freq) {
if (freq >= 2412 && freq <= 2484) {
return (freq - 2412) / 5 + 1;
} else if (freq >= 5170 && freq <= 5825) {
return (freq - 5170) / 5 + 34;
} else {
return -1;
}
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
public void tabs() {
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifiManager.getScanResults();
//wifis = new String[wifiScanList.size()]; //remove this
wifis.clear(); //add this
for (int i = 0; i < wifiScanList.size(); i++) {
ssid = wifiScanList.get(i).SSID; //Get the SSID
level = wifiScanList.get(i).level;
channel =wifiScanList.get(i).frequency;
//use add here:
wifis.add(ssid + " " + level + " " + convertFrequencyToChannel(channel)); //append to the other data
wifis.toString();
}
adapter.notifyDataSetChanged(); //add this
wifiManager.startScan(); //start a new scan to update values faster
//ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),
// android.R.layout.simple_list_item_1, wifis)
//list.setAdapter(adapter);
}
}
}