Reputation: 313
I'm trying to display a list of ALL the wifi AP's in range i.e. including those with same SSID's as these are not shown in the default wifi settings.
This is my first attempt at Android SDK and I've gotten so far:
package com.iiitd.wifistats;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WiFiStats extends Activity implements View.OnClickListener {
WifiManager wifi;
List<ScanResult> scanResults;
ArrayList<Map<String, Integer>> list;
SimpleAdapter adapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
Toast.makeText(getApplicationContext(), "Turning on WiFi...", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
carryON();
}
private void carryON(){
final Button button = (Button) findViewById(R.id.buttonScan);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wifi = (WifiManager) v.getContext().getSystemService(Context.WIFI_SERVICE);
scanResults = wifi.getScanResults();
Toast.makeText(getApplicationContext(), "Scanning...", Toast.LENGTH_SHORT).show();
list = buildData(scanResults);
//Toast.makeText(getApplicationContext(), list.toString(), Toast.LENGTH_SHORT).show();
adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});
//Toast.makeText(getApplicationContext(), adapter.toString(), Toast.LENGTH_SHORT).show();
listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
}
});
}
private ArrayList<Map<String, Integer>> buildData(java.util.List<ScanResult> s) {
ArrayList<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
for (ScanResult result : s) {
list.add(putData(result.BSSID, result.level));
}
return list;
}
private HashMap<String, Integer> putData(String BSSID, int level) {
HashMap<String, Integer> item = new HashMap<String, Integer>();
item.put(BSSID, level);
return item;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.wi_fi_stats, menu);
return true;
}
@Override
public void onClick(View view) {
}
}
This is my activity_xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"
android:id="@+id/buttonScan"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_above="@+id/buttonScan"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"/>
</RelativeLayout>
And this is listitem.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<TextView
android:id="@+id/strength"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp"/>
<TextView
android:id="@+id/BSSID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/strength"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp"/>
</RelativeLayout>
There is no list displayed when the I start a scan. However, if I use a toast, the I can see that the list is forming correctly. Can someone guide me here? Thanks.
Upvotes: 0
Views: 897
Reputation: 51
Take a look at the code here
adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});
The String array is telling the adapter the keys to be used when accessing the map that you define. In your code, you are assigning the values as such
private HashMap<String, Integer> putData(String BSSID, int level) {
HashMap<String, Integer> item = new HashMap<String, Integer>();
item.put(BSSID, level);
return item;
}
This is assigning the int for level to the key set by the String BSSID. You need to change all occurrences of HashMap in your code to take a String value and a String key HashMap< String, String >
and replace the method with the following
private HashMap<String, String> putData(String BSSID, int level) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("BSSID", BSSID);
item.put("strength", Integer.toString(level));
return item;
}
Upvotes: 1
Reputation: 410
have you tried extending ListActivity instead of a plain activity? you could need to change the ID of the ListView in your XML to android:id="@id/android:list"
Then in your caryON() method you would call setListAdapter(adapter);
after initialising the new Simple Adapter
adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});
setListAdapter(adapter);
Upvotes: 0