Find a complete working example below:
The code by @Android is very good but has few issues, namely:
size = size -1
seems unnecessary.Find below the modified code of @Android as a working example:
WifiScanner.java which is the Main Activity
package com.arjunandroid.wifiscanner;
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.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class WifiScanner extends Activity implements View.OnClickListener{
WifiManager wifi;
ListView lv;
Button buttonScan;
int size = 0;
List<ScanResult> results;
String ITEM_KEY = "key";
ArrayList<String> arraylist = new ArrayList<>();
ArrayAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getActionBar().setTitle("Widhwan Setup Wizard");
setContentView(R.layout.activity_wifi_scanner);
buttonScan = (Button) findViewById(R.id.scan);
buttonScan.setOnClickListener(this);
lv = (ListView)findViewById(R.id.wifilist);
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false)
{
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
this.adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arraylist);
lv.setAdapter(this.adapter);
scanWifiNetworks();
}
public void onClick(View view)
{
scanWifiNetworks();
}
private void scanWifiNetworks(){
arraylist.clear();
registerReceiver(wifi_receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
Log.d("WifScanner", "scanWifiNetworks");
Toast.makeText(this, "Scanning....", Toast.LENGTH_SHORT).show();
}
BroadcastReceiver wifi_receiver= new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
Log.d("WifScanner", "onReceive");
results = wifi.getScanResults();
size = results.size();
unregisterReceiver(this);
try
{
while (size >= 0)
{
size--;
arraylist.add(results.get(size).SSID);
adapter.notifyDataSetChanged();
}
}
catch (Exception e)
{
Log.w("WifScanner", "Exception: "+e);
}
}
};
}
activity_wifi_scanner.xml which is the layout file for the Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/wifilist"
android:layout_width="match_parent"
android:layout_height="312dp"
android:layout_weight="0.97" />
<Button
android:id="@+id/scan"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:layout_margin="15dp"
android:background="@android:color/holo_green_light"
android:text="Scan Again" />
</LinearLayout>
Also as mentioned above, do not forget to add Wifi permissions in the AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />