Skip to content Skip to sidebar Skip to footer

Android Wifi Can't Get The Ssid And Bssid From Scanresult

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 th

Solution 1:

You can get the SSID and BSSID from the ScanResult Object using wifiScanList.get(i).SSID and wifiScanList.get(i).BSSID. Then just append it to the other data returned from toString().

See Documentation Here.

First, declare your ArrayAdapter as an instance variable, and call setAdapter() in onCreate():

ArrayAdapter adapter;
ListView list;
ArrayList<String> wifis;
WifiInfo wifiInfo;
publicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.text);
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiReciever = newWifiScanReceiver();
    wifiInfo = wifiManager.getConnectionInfo();

    wifis = newArrayList<String>(); //initialize wifis
    wifis.add("loading...");
    adapter = newArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_list_item_1, wifis)
    list.setAdapter(adapter);

    wifiManager.startScan(); //make sure this is the last call
}

Modified BroadcastReceiver:

classWifiScanReceiverextendsBroadcastReceiver {
        @SuppressLint("UseValueOf")
        publicvoidonReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            //wifis = new String[wifiScanList.size()]; //remove this
            wifis.clear(); //add thisfor (int i = 0; i < wifiScanList.size(); i++) {
                String ssid = wifiScanList.get(i).SSID; //Get the SSID
                String bssid =  wifiScanList.get(i).BSSID //Get the BSSID//use add here:
                wifis.add( ssid + " " + bssid + " " +((wifiScanList.get(i)).toString()) ); //append to the other data
            }

            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);
        }
    }

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 a TextView where you update the RSSI every second for the current SSID.

Post a Comment for "Android Wifi Can't Get The Ssid And Bssid From Scanresult"