Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/src/main/java/com/example/dronedetect/DroneSignalDetector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,34 @@ import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Handler
import android.os.Looper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DroneSignalDetector(private val context: Context) {
private val handler = Handler(Looper.getMainLooper())
private val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
private val receiver = WifiScanReceiver()
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

fun startScan() {
// TODO: implement scanning logic
scope.launch {
fetchFlightData()
// TODO: implement scanning logic
}
}

suspend fun fetchFlightData() = withContext(Dispatchers.IO) {
// TODO: implement fetching flight data
}

fun stopScan() {
handler.removeCallbacksAndMessages(null)
context.unregisterReceiver(receiver)
scope.cancel()
Comment on lines 33 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Recreate CoroutineScope after cancellation

Because the scope is a single CoroutineScope(SupervisorJob() + Dispatchers.IO) stored as a val, calling stopScan() cancels that scope permanently. Any subsequent call to startScan() will try to scope.launch { … } on a cancelled scope and the coroutine will immediately cancel, so fetchFlightData() never runs after the first stop. If this detector is started and stopped repeatedly during the app lifecycle, scanning cannot resume. Consider creating a new scope when starting or cancelling only the active job instead of the whole scope.

Useful? React with 👍 / 👎.

}
}

Expand Down
Loading