How to search all wifi devices programmatically in android?

import java.io.File;
import java.util.ArrayList;
import java.util.List;

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.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

WifiManager mainWifi;
List<ScanResult> wifiList;
WifiReceiver receiverWifi;
Button btn_wifi;
StringBuilder sb = new StringBuilder();
Context ctx;
TextView txt_wifistatus;
ArrayList<String> wifiname,bssid,capabilities,level,frequency;
ArrayList<WifiDevice> wifidevice;
ListView lv_wifiname;
String myssid;
int myipaddress;
WifiP2pManager wifiManager;
private Channel wifichannel;
IntentFilter wifiServerReceiverIntentFilter;

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

ctx = this;

btn_wifi = (Button) findViewById(R.id.btn_wifi);
txt_wifistatus = (TextView) findViewById(R.id.txt_wifistatus);
lv_wifiname = (ListView) findViewById(R.id.lv_wifiname);

mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiList = mainWifi.getScanResults();
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
txt_wifistatus.setText(“\nStarting Scan…\n”);

btn_wifi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifidevice=new ArrayList<WifiDevice>();
String []strsplit = sb.toString().split(“[,]”);
WifiDevice wd = null;
int temp=0;
for(int i=0;i<strsplit.length;i++){
if(temp==0){
wd = new WifiDevice();
if(strsplit[i].substring(0,5).equalsIgnoreCase(“SSID:”)){
boolean status = false;
String ssid = strsplit[i].replace(“SSID: “, “”).toString();
for(int j=0;j<wifidevice.size();j++){
if(wifidevice.get(j).getWifiname().equals(ssid)){
temp=0;
status=true;
break;
}
}
if(!status){
wd.setWifiname(ssid);
temp++;
}
}
}else if(temp==1){
if(strsplit[i].substring(0,7).equalsIgnoreCase(” BSSID:”)){
String bssid = strsplit[i].replace(“BSSID: “, “”);
wd.setBssid(bssid);
temp++;
}
}else if(temp==2){
if(strsplit[i].substring(0,14).equalsIgnoreCase(” capabilities:”)){
String capabilities = strsplit[i].replace(“capabilities: “, “”);
wd.setCapabilities(capabilities);
temp++;
}
}else if(temp==3){
if(strsplit[i].substring(0,7).equalsIgnoreCase(” level:”)){
String level = strsplit[i].replace(“level: “, “”);
wd.setLevel(level);
temp++;
}
}else if(temp==4){
if(strsplit[i].substring(0,11).equalsIgnoreCase(” frequency:”)){
String frequency = strsplit[i].replace(“frequency: “, “”);
wd.setFrequency(frequency);
wifidevice.add(wd);
temp=0;
}
}

}
ListViewAdapter ladapter = new ListViewAdapter(MainActivity.this,wifidevice);
lv_wifiname.setAdapter(ladapter);

btn_wifi.setEnabled(false);
}
});
lv_wifiname.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
// TODO Auto-generated method stub
File f = new File(Environment.getExternalStorageState()+”wifi_icon.png”);

WifiConfiguration conf = new WifiConfiguration();
conf.SSID=myssid;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

String selSSID = wifidevice.get(position).getWifiname().toString();
mainWifi.disconnect();
WifiConfiguration conf2 = new WifiConfiguration();
conf2.SSID=wifidevice.get(position).getWifiname().toString();
conf2.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf2.status = WifiConfiguration.Status.ENABLED;
int netId=mainWifi.addNetwork(conf2);
mainWifi.saveConfiguration();
mainWifi.reconnect();

String secmyssid = mainWifi.getConnectionInfo().getSSID().toString();
Toast.makeText(ctx, secmyssid,Toast.LENGTH_SHORT).show();
int secmyipaddress = mainWifi.getConnectionInfo().getIpAddress();
Log.w(“Result”, “”+secmyssid);
String data = “hello”;
}
});
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}

@SuppressWarnings(“static-access”)
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(mainWifi.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}

class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++){

if(i==0)
sb.append((wifiList.get(i)).toString());

if(i>0)
sb.append(“,”+(wifiList.get(i)).toString());

sb.append(“”);
}
txt_wifistatus.setText(sb.toString());
//            txt_wifistatus.setText(“Scan completed.”);
}
}
}