WiFi Scanning

I’m at my wit’s end. I’ve already read a book about Kotlin for Android Studio, and I’m 25% into an online course.

I’m trying to build a list of strings of the WiFi access points available to my smartphone (Samsung S8). I’ve followed to code and structure suggested at the official website.
Whatever I do, the code always reports “[0, 1, ]” at the textResults text view component.

Here’s the code. What am I doing wrong?

package com.example.prueba02

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.wifi.ScanResult
import android.net.wifi.WifiManager
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
lateinit var apScan: WifiManager

val broadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(contxt: Context?, intent: Intent?) {
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    textResults.text="Waiting for button"


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
        || ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) == PackageManager.PERMISSION_GRANTED
        )  {
         apScan = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        }
}


fun scanForNetworks(view: View) {
    registerReceiver(broadcastReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
    apScan.startScan()
    Toast.makeText(this,"Scanning for Networks",Toast.LENGTH_SHORT).show()
    Handler().postDelayed({
       stopScanning()
    }, 2000)
}

fun stopScanning() {
    var apList = ArrayList<String>()
    apList.add("0") //test
    apList.add("1") //test
    apList.add(apScan.scanResults.toString())


    textResults.text = apList.toString()
}

}

I also try to make a WiFi scanner without success. In the internet are some tutorials but they are all more than 4 years old and in java. I try to ask chat gpt but it gives me no corect code. Maybe I find it out in a few days and can answer again. (I try to make a WiFi bruteforcer app. So I need the WiFi list. And it must know that is the right network if the user click on it. Many hard and difficult things to solve) but maybe I get it

I question the legality of such app, but anyway.

Did you ask for location permission? It’s needed to scan for wifi