• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with Wifi Scanning via app


Feb 23, 2021
The function wifiManager.startScan() does not return any results when tested on a device instead of in the emulator. After searching around I found posts from 2 years ago stating that for API 26+ the wifiManager was changed to work differently. My question is: how to get it to work, aka to return me the list of available wifi networks to connect?

I have a button and a listview and on create I enable the wifi. When the button is pressed I scan for wifi networks and populate the listview with their names. Upon clicking a list item a pop asking for the wifi passwords appears. Afterwards it attempts to connect via the network name and the entered password.

Here is my current code (although I have a lot of experience in programming, I have little experience in Android Studio, so everything is crammed in one script). It works in the emulator (I get the predefined wifi network and successfully connect to it). On a device, however, it doesn't find any wifi networks despite seeing 4-5 when opening my wifi settings from the top navbar and not from the app. The three commented out lines are what I found I'm supposed to do to get the wifiManager to get scan results on API 26+ (though I probably put it in the wrong place due to my limited knowledge of Android Studio)

package com.example.bla;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiNetworkSuggestion;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private WifiManager wifiManager;
    private ListView listView;
    private Button buttonScan;
    private int size = 0;
    private List<ScanResult> results;
    private ArrayList<String> arrayList = new ArrayList<String>();
    private ArrayAdapter adapter;
    private int REQUEST_LOCATION = 101;
    private EditText input;
    private AlertDialog ad;

    private int ItemNumb;

    protected void onCreate(Bundle savedInstanceState) {
        buttonScan = findViewById(R.id.scanBtn);
        buttonScan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

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

        if (!wifiManager.isWifiEnabled()) {
            Toast.makeText(this, "Wifi is disabled... Enabling it", Toast.LENGTH_LONG).show();

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);

        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
        alertBuilder.setTitle("Connect to network?");
        alertBuilder.setMessage("Please enter network password");
        input = new EditText(MainActivity.this);
        alertBuilder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                connectToNetwork("\"" + results.get(ItemNumb).SSID.toString() + "\"", input.getText().toString());
        alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {

        ad = alertBuilder.create();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemNumb = I;

        //IntentFilter intentFilter = new IntentFilter();
        //registerReceiver(wifiReceiver, intentFilter);

    private void scanWifi() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE}, REQUEST_LOCATION);
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        Toast.makeText(this, "Scanning Wifi...", Toast.LENGTH_SHORT).show();

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(), permissions[0] + " permission refused", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), permissions[0] + " permission granted", Toast.LENGTH_SHORT).show();

    BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            results = wifiManager.getScanResults();

            for (ScanResult scanResult : results) {
                arrayList.add(scanResult.SSID + " - " + scanResult.capabilities);

    private void connectToNetwork(String networkSSID,  String networkPass)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            WifiNetworkSuggestion networkSuggestion1 =
                    new WifiNetworkSuggestion.Builder()

            WifiNetworkSuggestion networkSuggestion2 =
                    new WifiNetworkSuggestion.Builder()

            List<WifiNetworkSuggestion> suggestionsList = new ArrayList<>();

            WifiConfiguration wifiConfiguration = new WifiConfiguration();
            wifiConfiguration.SSID = String.format("\"%s\"", networkSSID);
            wifiConfiguration.preSharedKey = String.format("\"%s\"", networkPass);
            int wifiID = wifiManager.addNetwork(wifiConfiguration);
            wifiManager.enableNetwork(wifiID, true);

I'm developing an app for the less technologically capable. For that purpose the app itself enables wifi, lists networks and lets you connect to a preferred network via the app rather than having you manually do it from the top android navbar. This is a non-negotiable product requirement, so I'm searching for solutions on how to change my code to get the wifi list successfully for API 26+ and not suggestions for alternate designs.


We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.