Scan Wifi in range in Android using code

by Sasikumar 2014-02-12 16:34:24

To scan Wi-Fi in range in Android device :
import the required packages for the following code,
package com.android.wifitester;

public class WifiTester extends Activity {
TextView messageText;
WifiManager myWifi;
WifiReceiver availableWifi;
List<ScanResult> availableWifiList;
StringBuilder sb = new StringBuilder();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageText = (TextView) findViewById(R.id.messageText);
myWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
availableWifi = new WifiReceiver();
registerReceiver(availableWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
myWifi.startScan();
messageText.setText("\nStarting Scan...\n");
}

public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);
}

public boolean onMenuItemSelected(int featureId, MenuItem item) {
myWifi.startScan();
messageText.setText("Starting Scan");
return super.onMenuItemSelected(featureId, item);
}

protected void onPause() {
unregisterReceiver(availableWifi);
super.onPause();
}

protected void onResume() {
registerReceiver(availableWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}

class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
sb = new StringBuilder();
availableWifiList = myWifi.getScanResults();
for(int i = 0; i &lt; availableWifiList.size(); i++){
sb.append(new Integer(i+1).toString() + ".");
sb.append((availableWifiList.get(i)).toString());
sb.append("\n");
}
messageText.setText(sb);
}
}
}
747
like
0
dislike
0
mail
flag

You must LOGIN to add comments