0

I want to get list of all wifi connections available in my app but i am not getting any list. My wifi connection is getting on and also connecting to my wifi but I am not able to get the list of wifi connections in my application. Can anyone tell me why so? Here is my code:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    WifiManager wm;
    ToggleButton tb;
    ArrayAdapter<String> aa;
    ListView l;
    private static final int ENABLE_REQUEST=1;
    private static final int DISCOVERABLE_REQUEST=2;
    private static final int DISCOVERABLE_DURATION=120;

    BroadcastReceiver br=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
                List<ScanResult> hotspot=  wm.getScanResults();

                for(int i=0;i<hotspot.size();i++) {
                    aa.add(hotspot.get(i).toString());
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tb= (ToggleButton) findViewById(R.id.toggle);
        aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1);
        l= (ListView) findViewById(R.id.list);
        l.setAdapter(aa);

        wm= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        tb.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        tb= (ToggleButton) v;

        if(wm==null) {
            Toast.makeText(this, "This feature is not supported", Toast.LENGTH_SHORT).show();
            tb.setChecked(false);
        } else {
            if (tb.isChecked()) {
                wm.setWifiEnabled(true);
                discover();
            } else {
                wm.setWifiEnabled(false);
            }
        }
    }

    private void discover() {

        wm.startScan();
        IntentFilter filter=new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        this.registerReceiver(br,filter);
    }
}

AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Michael
  • 3,093
  • 7
  • 39
  • 83
Ankit Srivastava
  • 135
  • 4
  • 11

1 Answers1

0

I think you might be looking for this answer in another stackoverflow post.

As the answer suggests, you can get your list in mScanResults which you can apply to your ArrayAdapter and finally call setAdapter method on the listview.

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
        List<ScanResult> mScanResults = mWifiManager.getScanResults();
        // add your logic here
        }
    }
}

Please follow the answer for more details.

Guy Luz
  • 3,372
  • 20
  • 43
ravi
  • 899
  • 8
  • 31