Learning Kotlin through Android Studio...

wayneh

Joined Sep 9, 2010
18,104
Will do. But I was planning on doing that after the app is finished. Or would you recommend I perform that sort of test as the code progresses?
I'd start learning about interface design now, before you make a bunch of choices that will be hard to deal with later. I did it the way you're planning, and I regretted it. Tweaking the interface appearance is a much larger part of the total work than you want it to be, stuff like aligning fields, leaving space between them and the edges, and a million other things. Landscape versus portrait layout. Don't try to solve everything all at once, but DO at least try a few different screens once in a while.

Is that a real phone or a simulator? The latter may be crippled by default. In other words it might not have WiFi enabled. Your app might need an entitlement to access WiFi.

Consider also writing to your console. In Xcode this is simply "print("some text here \( x )" where "x" is some variable. The \( x ) syntax will evaluate almost anything to text if it can. Anyway, you can learn a huge amount about what's happening under the hood this way. For instance it's possible your code is working but the GUI is not being redrawn. I can't read the code well enough to judge that but it's incredibly easy to have this happen to you.
 
Last edited:

Thread Starter

cmartinez

Joined Jan 17, 2007
8,759
Is that a real phone or a simulator? The latter may be crippled by default. In other words it might not have WiFi enabled. Your app might need an entitlement to access WiFi.
It's a simulator. But once I get the program more or less running the way I want, then I install it in my real phone to see how it behaves. So far, no success with my WiFi listing app.

Consider also writing to your console. In Xcode this is simply "print("some text here \( x )" where "x" is some variable. The \( x ) syntax will evaluate almost anything to text if it can.
Say that's an excellent suggestion, it will be a huge aid during debugging. Many thanks!
 

wayneh

Joined Sep 9, 2010
18,104
It's a simulator. But once I get the program more or less running the way I want, then I install it in my real phone to see how it behaves. So far, no success with my WiFi listing app.
Does the sim have any installed apps, such as "settings" or whatever an Android phone typically uses to find and log in to wifi? See if the sim can handle that before judging your app. My iOS simulator can not show me anything about wifi, although I can use wifi to web browse and all the typical things.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,759
Does the sim have any installed apps, such as "settings" or whatever an Android phone typically uses to find and log in to wifi? See if the sim can handle that before judging your app. My iOS simulator can not show me anything about wifi, although I can use wifi to web browse and all the typical things.
I'm not sure if it does. I'll look it up, thanks for the suggestion.


In the meantime, I'm pulling my hair down here ... I can run the app in my own phone, and I've also added a few bread crumb instructions here and there to trace its progress, but so far I haven't been able to make this stupid thing effectively scan for access points. What gives? :mad:

Code:
class MainActivity : AppCompatActivity() {
     var resultList = ArrayList<ScanResult>()
     lateinit var apScan: WifiManager

    //val broadcastReceiver = object : BroadcastReceiver() {
    //    override fun onReceive(contxt: Context?, intent: Intent?) {
    //        apScan.startScan()
    //        resultList = apScan.scanResults as ArrayList<ScanResult>
    //        Log.d("TESTING", "onReceive Called")
    //    }
    //}

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

        textHello.text="12345"
        apScan = this.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    }

    fun addNumber(v:View){
        scanForNetworks()
    }

    fun scanForNetworks() {
        // registerReceiver(broadcastReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        apScan.startScan()
        resultList = apScan.scanResults as ArrayList<ScanResult>
        Log.d("TESTING", "onReceive Called") // trace

        Handler().postDelayed({
           stopScanning()
        }, 5000)
    }

    fun stopScanning() {
        // unregisterReceiver(broadcastReceiver)
        val apList = ArrayList<String>()
        apList.add("0") //test
        apList.add("1") //test

        for (result in resultList) {
            apList.add(result.BSSID)
            apList.add("2") //test
        }

        apList.add(resultList.toString())
        Log.d("TESTING", apList.toString()) //trace
        textHello.text = apList.toString()
    }
}
1585527778000.png
 

wayneh

Joined Sep 9, 2010
18,104
I'm not sure if it does. I'll look it up, thanks for the suggestion.


In the meantime, I'm pulling my hair down here ... I can run the app in my own phone, and I've also added a few bread crumb instructions here and there to trace its progress, but so far I haven't been able to make this stupid thing effectively scan for access points. What gives? :mad:

Code:
class MainActivity : AppCompatActivity() {
     var resultList = ArrayList<ScanResult>()
     lateinit var apScan: WifiManager

    //val broadcastReceiver = object : BroadcastReceiver() {
    //    override fun onReceive(contxt: Context?, intent: Intent?) {
    //        apScan.startScan()
    //        resultList = apScan.scanResults as ArrayList<ScanResult>
    //        Log.d("TESTING", "onReceive Called")
    //    }
    //}

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

        textHello.text="12345"
        apScan = this.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    }

    fun addNumber(v:View){
        scanForNetworks()
    }

    fun scanForNetworks() {
        // registerReceiver(broadcastReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        apScan.startScan()
        resultList = apScan.scanResults as ArrayList<ScanResult>
        Log.d("TESTING", "onReceive Called") // trace

        Handler().postDelayed({
           stopScanning()
        }, 5000)
    }

    fun stopScanning() {
        // unregisterReceiver(broadcastReceiver)
        val apList = ArrayList<String>()
        apList.add("0") //test
        apList.add("1") //test

        for (result in resultList) {
            apList.add(result.BSSID)
            apList.add("2") //test
        }

        apList.add(resultList.toString())
        Log.d("TESTING", apList.toString()) //trace
        textHello.text = apList.toString()
    }
}
I see where you call the WiFi manager but I don’t see how you intercept it when the system comes back with data. Maybe it’s right there but I cannot see it. In Swift parlance you need a delegate method to handle the return of the WiFi manager. I also have to specifically name my class as the delegate to return the data to. You could name another class and put the methods there, but it has to be somewhere. If you fail to specify a delegate to receive the manager, it just evaporates.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,759
It's been quite a journey. Kotlin has definitely been an eye opener regarding object oriented programming. Just when I thought I had mastered it, these guys took it to a whole different level!. I thought that the first book I bought was going to teach me kotlin (and it does, a little) but the book is actually about Android Studio itself. So what I had to do is buy yet another book (dummy-level this time) dealing solely with kotlin and pitched towards people that have no knowledge of said language, and gave it a thorough read.

1607466529158.png

The book turned out to be excellent. Almost all other books that I found out there are written rather for people that already know java.... and that's definitely not me. This book practically takes you by the hand and walks you through the whole learning process, without it ever getting dense or boring.

I think I'm getting the hang of it now... at first, I found being able to store and call functions within variables and constants themselves a little confusing, but it's also pretty cool. Another thing that I found easier to understand is Kotlin's use of lists, maps and collections. This because I've been programming in LISP for AutoCAD for many years now. So the concept was already familiar to me.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,759
I see where you call the WiFi manager but I don’t see how you intercept it when the system comes back with data. Maybe it’s right there but I cannot see it. In Swift parlance you need a delegate method to handle the return of the WiFi manager. I also have to specifically name my class as the delegate to return the data to. You could name another class and put the methods there, but it has to be somewhere. If you fail to specify a delegate to receive the manager, it just evaporates.
And by the way. I solved the problem with that first WiFi scan test a while ago too. The key was that WiFi and Location permissions have to be requested twice within the code. First at the manifest, and then also during runtime... which I find kind of stupid, but I'm sure there must be a good reason.
 
Top