Search code examples
androidlistviewandroid-wifi

Android Wifi Scanner NOT SHOWING RESULTS IN LISTVIEW


I am coding a wifi scanner to scan available networks and connect to them. My code compiles without an issue and the app runs, BUT the scanned results do not show in the list view. Please help. Even the button click which triggers a toast runs as expected.

Main.java

package com.example.wifiscanner;

import androidx.appcompat.app.AppCompatActivity;
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.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private WifiManager wifiManager;
    private ListView listView;
    private Button button;
    private int size = 0;
    private List<ScanResult> results;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ArrayAdapter arrayAdapter;

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

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                scanWifi();
            }
        });

        listView = findViewById(R.id.listview);
        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (!wifiManager.isWifiEnabled()) {

            Toast.makeText(this,"Wifi is disabled",Toast.LENGTH_SHORT).show();
            wifiManager.setWifiEnabled(true);

        }

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);
        scanWifi();
    }

    private void scanWifi() {

        arrayList.clear();
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();
        Toast.makeText(this,"Scanning for Networks", Toast.LENGTH_SHORT).show();
    }


    BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            results = wifiManager.getScanResults();
            unregisterReceiver(this);

            for (ScanResult scanResult : results){
                arrayList.add(scanResult.SSID + scanResult.capabilities);
                arrayAdapter.notifyDataSetChanged();
            }
        }
    };
}

I am coding a wifi scanner to scan available networks and connect to them. My code compiles without an issue and the app runs, BUT the scanned results do not show in the list view. Please help. Even the button click which triggers a toast runs as expected.


Solution

  • @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.