How can I get Android Wifi Scan Results into a list?
Try this code
public class WiFiDemo extends Activity implements OnClickListener
{
WifiManager wifi;
ListView lv;
TextView textStatus;
Button buttonScan;
int size = 0;
List<ScanResult> results;
String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
lv = (ListView)findViewById(R.id.list);
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 SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
lv.setAdapter(this.adapter);
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public void onClick(View view)
{
arraylist.clear();
wifi.startScan();
Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
try
{
size = size - 1;
while (size >= 0)
{
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities);
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}
}
catch (Exception e)
{ }
}
}
WiFiDemo.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/textStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Status" />
<Button
android:id="@+id/buttonScan"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Scan" />
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"></ListView>
</LinearLayout>
For ListView- row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/list_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp" />
</LinearLayout>
Add these permission in 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" />
Android Wifi Scanner NOT SHOWING RESULTS IN LISTVIEW
@AfterPermissionGranted(123)
public void scanWifi() {
String[] perm = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.CHANGE_WIFI_STATE};
if(EasyPermissions.hasPermissions(this,perm)){
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
boolean scanStarted = wifiManager.startScan();
if (scanStarted) {
Toast.makeText(this, "Scanning true", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Scanning false", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this,"PERMISSIONS HAVE BEEN GRANTED",Toast.LENGTH_SHORT).show();
}else{
EasyPermissions.requestPermissions(this,"WE REQUIRE THESE PERMISSIONS FOR THE APP TO FUNCTION",123,perm);
}
Gradle dependencies add
"implementation 'pub.devrel:easypermissions:2.0.1'" and compile
Using the easy permissions library I was able to solve the problem and introduce a more maintainable code.
Get WiFi Scan Results List With Kotlin
You forgot to start scanning. Add wifiManager.startScan()
call in your startScanning
method.
How to get the true wifi scan results updated with the time
The problem comes from android 9.0
The latest version of android (android 9 (Pie)) had a new restrictions and limitations for WIFI scanning, as it is documented by android. They add throttling for the scan intervals for both of foreground and background scenarios to save the power consumption.
Android 9 and later: Each foreground app can scan four times in a 2-minute period. This allows for a burst of scans in a short time. All background apps combined can scan one time in a 30-minute period.
android wifi scan
The following links represent the response of this throttling:
links represent the response of this throttling, link 2, link 3, link 4
Get available wi-fi scan result in customized listview
Add the following after your loop
String filtered[] = new String[wifiScanList.size()];
int counter = 0;
for (String eachWifi : wifis) {
String[] temp = eachWifi.split(",");
filtered[counter] = temp[0] +temp[2] +temp[3];//0->SSID, 2->Key Management 3-> Strength
counter++;
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, filtered));
To remove extra words you can do the following:
temp[0].substring(5).trim()
This removes the first 5 characters and trims any whitespace. Do similar to others as well. And use \n
to get on next line.
Or best idea would be use custom adapter.
See my answer here
Good luck.
How to filter a specific SSID from scan result in android?
Just change to this:
for (scanResult in results) {
if (!scanResult.SSID.contains("_") {
arrayList.add(scanResult.SSID)
adapter!!.notifyDataSetChanged()
}
}
Related Topics
Gson: How to Exclude Specific Fields from Serialization Without Annotations
How to Use Swingworker in Java
Get Driving Directions Using Google Maps API V2
How to Return an Array from Jni to Java
Add Custom Headers to Webview Resource Requests - Android
How to Find a Java Thread Running on Linux with Ps -Axl
How to Open a Unc Path from Linux in Java
Amazon Ec2 Ftp Write Permission
Object Header Size in Java on 64Bit Vm with <4Gb Ram
Remote Debugging a Java Application
Java.Lang.Outofmemoryerror: Java Heap Space
Set Margins in a Linearlayout Programmatically
Error:(23, 17) Failed to Resolve: Junit:Junit:4.12
Android - Span_Exclusive_Exclusive Spans Cannot Have a Zero Length
How to Use Chef to Update-Alternatives for Java Using Execute