Search code examples
androidkotlinbroadcastreceiverkotlin-coroutinesandroid-wifi

Wrap Broadcast receiver into Flow (coroutine)


I have a broadcast receiver for wifi scan results as a data source and I'd like to make it in coroutine way. I found an answer for suspend function here: https://stackoverflow.com/a/53520496/5938671

suspend fun getCurrentScanResult(): List<ScanResult> =
    suspendCancellableCoroutine { cont ->
        //define broadcast reciever
        val wifiScanReceiver = object : BroadcastReceiver() {
            override fun onReceive(c: Context, intent: Intent) {
                if (intent.action?.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) == true) {
                    context.unregisterReceiver(this)
                    cont.resume(wifiManager.scanResults)
                }
            }
        }
        //setup cancellation action on the continuation
        cont.invokeOnCancellation {
            context.unregisterReceiver(wifiScanReceiver)
        }
        //register broadcast reciever
        context.registerReceiver(wifiScanReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        //kick off scanning to eventually receive the broadcast
        wifiManager.startScan()
    }

This is fine for signle emit, but if I want to get results while scanning is going then I'll get crash because cont.resume() could be called only once. Then I decided to try Flow. And here is my code:

suspend fun getCurrentScanResult(): Flow<List<ScanResult>> =
    flow{
        val wifiScanReceiver = object : BroadcastReceiver() {
            override fun onReceive(c: Context, intent: Intent) {
                if (intent.action?.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) == true) {
                    //context.unregisterReceiver(this)
                    emit(wifiManager.scanResults)
                }
            }
        }
        //setup cancellation action on the continuation
        //register broadcast reciever
        context.registerReceiver(wifiScanReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        //kick off scanning to eventually receive the broadcast
        wifiManager.startScan()
    }

But now Android Stuidio says Suspension functions can be called only within coroutine body for function emit(wifiManager.scanResults) Is there a way to use Flow here?


Solution

  • Please take a look at the callback flow which is specifically designed for this use case. Something like this will do the job:

    callbackFlow {
      val receiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
          if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
            sendBlocking(wifiManager.scanResults) // or non-blocking offer()
          }
        }
      } 
      context.registerReceiver(receiver, intentFilter)
    
      awaitClose {
          context.unregisterReceiver(receiver)
      }
    }
    

    You also might want to share this flow with e.g. shareIn operator to avoid registering a new receiver for each flow subscriber.