I got this code that i want to scan for the networks and then write it all to the listview. But the problem is that the ssid and bssid doesnt show. Everything else shows but not the ssid.
Also what is the best way to update the listview every second so you can see the signal strenghts actual signal?
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.wifi.R;
public class MainActivity extends Activity {
WifiManager wifiManager;
WifiScanReceiver wifiReciever;
ListView list;
String wifis[];
WifiInfo wifiInfo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.text);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
wifiInfo = wifiManager.getConnectionInfo();
wifiManager.startScan();
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifiManager.getScanResults();
wifis = new String[wifiScanList.size()];
for (int i = 0; i < wifiScanList.size(); i++) {
wifis[i] = ((wifiScanList.get(i)).toString());
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, wifis));
}
}
}
You can get the
SSID
andBSSID
from theScanResult
Object usingwifiScanList.get(i).SSID
andwifiScanList.get(i).BSSID
. Then just append it to the other data returned fromtoString()
.See Documentation Here.
First, declare your
ArrayAdapter
as an instance variable, and callsetAdapter()
inonCreate()
:Modified
BroadcastReceiver
:This will still only update on each scan result. I don't think it's recommended to update a
ListView
every second, you may want to re-think your approach for showing RSSI levels. You could have an on-click for each SSID, and have a details view with aTextView
where you update the RSSI every second for the current SSID.