0

My target is to scan wifi access points and show its SSID AND LEVEL(in dbm), but I have an problem in programming such that I can't to display it in listview or in toast meesage. I tried to write the below code and its looks like :

public class MainActivity extends Activity implements OnClickListener{
    List<ScanResult> scanResults;
    ArrayList<String> SSID_values;
    ArrayList<Integer> level_values;
    Button bt1;
    WifiManager mWifiManager;
    int size= 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt1 = (Button) findViewById(R.id.button1);
        SSID_values = new ArrayList<String>();
        level_values= new ArrayList<Integer>();
        bt1.setOnClickListener(this);
        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }

when button clicked, the following code is :

@Override
    public void onClick(View v) {
        mWifiManager.startScan();
        // get list of the results in object format ( like an array )
        scanResults = mWifiManager.getScanResults();
        size = scanResults.size();
        // loop that goes through list
//        for (ScanResult  scanResult :  scanResults) {
        for(int i = 0;i<=size;i++){
            SSID_values.add(scanResults.get(size).SSID);
            level_values.add(scanResults.get(size).level);
        }
            Toast.makeText(this, SSID_values + "   " + level_values,
                    Toast.LENGTH_LONG).show();

    }

Can any one help me?

Thank you in advance.

MMG
  • 3,226
  • 5
  • 16
  • 43
Mohsen Ali
  • 655
  • 1
  • 9
  • 30
  • 1
    Well firstly, `startScan()` is asynchronous, so you're not going to immediately get the results of the scan you requested -- rather, you'll get the results from the last scan attempt (who knows how old that may be). You can see how to handle the asynchronous request for scan results [here](http://www.androidsnippets.com/scan-for-wireless-networks) – Kevin Coppock Sep 15 '13 at 21:44
  • i tried to run your proposed code but not working with me, also i added the following permissions: – Mohsen Ali Sep 15 '13 at 22:26
  • "Not working" isn't useful information. – Kevin Coppock Sep 15 '13 at 22:30
  • i run your code (copy and past), but not working in my device galaxy s+, i see your code is right but i don't know why it is not working with me!! – Mohsen Ali Sep 16 '13 at 22:31
  • Try [this](https://stackoverflow.com/questions/44426533/android-not-able-to-get-the-available-wifi-list-using-scanresult) one. May this will help you. – Nilesh Panchal Jun 08 '17 at 06:32
  • Try [this](https://stackoverflow.com/questions/44426533/android-not-able-to-get-the-available-wifi-list-using-scanresult) one. May it will hep you. – Nilesh Panchal Jun 08 '17 at 06:33

0 Answers0