Search code examples
javaandroidkotlinandroid-wifiwifimanager

How to filter a specific SSID from scan result in android?


I am new in android development and I am working on an application that scan for all available WI-FI and then show them in a list and its working fine here is the code:

private var wifiManager: WifiManager? = null
private val arrayList = ArrayList<String>()
private var adapter: ArrayAdapter<*>? = null

private var wifiReceiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val results = wifiManager!!.scanResults
        unregisterReceiver(this)

        for (scanResult in results) {
            arrayList.add(scanResult.SSID)
            adapter!!.notifyDataSetChanged()

        }
    }
}

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

    wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList)

    val wifi = AlertDialog.Builder(this)
    wifi.setTitle("Wifi scan list")
    wifi.setAdapter(adapter) { dialog, which ->
    }
    wifi.create()
    wifi.show()

}

But now I want to hide some ssid from wifi scan list that contains "_". How can I hide these SSID from wifi scan list?


Solution

  • Just change to this:

    for (scanResult in results) {
        if (!scanResult.SSID.contains("_") {
            arrayList.add(scanResult.SSID)
            adapter!!.notifyDataSetChanged()
        }
    }