I know there has to be a simple way to do what I am trying to do. In short, I want to call a function, and have that function listen for values for a specified period of time, then return the items it found during that time. Here is the code I have so far:
fun returnAllDevicesStartingWith(devicePrefix: String): List<String> {
Logger.d(TAG, "About to scan for devices. Will return those starting with $devicePrefix")
val mutableList = mutableListOf<String>()
val result = scanForDevices()
.take(4, TimeUnit.SECONDS, timeoutScheduler)
.subscribe{scanResult ->
val name = scanResult.bleDevice.name
Logger.d(TAG, "Potential device match named $name")
if(name != null) {
if(name.startsWith(prefix = devicePrefix)) {
Logger.d(TAG, "Match found $name")
mutableList.plus(name)
}
}
}
return mutableList
}
So I call the above function something like:
val deviceList = returnAllDevicesStartingWith("ABC")
The scanForDevices function returns an Observable<ScanResult> as it finds each device. That function works fine.
How to I keep from returning the mutableList until the take(4, TimeUnit.SECONDS, timeoutScheduler) expires?