Przemyslaw
Przemyslaw

Reputation: 11

Collect function won't execute in a coroutine in react-native bridge

I am creating a native bridge for React-Native to utilise NearbyDevice API for UWB ranging. I am fairly new to this and dont entirely understand coroutines and how bridges work, but I've found out that I cannot run a suspend function as a ReactMethod, that's why I created a separate function for that.

I successfully created a bridge and I call the Nearby API by creating a controllerSessionScope, and I receive a log from that API. But then once I try to collect a prepared session flow inside a coroutine it won't run at all.

@ReactMethod
    fun controllerInitiateSessionTest(callback: Callback) {
        CoroutineScope(Dispatchers.IO).launch {
            val result = controllerInitiateRanging()
            callback.invoke(null, result)
        }
    }

Then the controllerInitiateRanging is called ->

suspend fun controllerInitiateRainging(callback: Callback) {

    [...]

    val controllerSession = uwbManager.controllerSessionScope() 
    //This returns the log "Creating Gms Client session scope"

    [...]

    val sessionFlow = controllerSession.prepareSession(partnerParameters)

    job = CoroutineScope(Dispatchers.IO).launch {
                Log.d("Coroutine", "Started coroutine with session flow")
                sessionFlow.collect {
                    print("Collecting session")
                    when (it) {
                        is RangingResult.RangingResultPosition -> handlePositionResult(it.position)
                        is RangingResult.RangingResultPeerDisconnected -> handlePeerDisconnect(it)
                    }
                }
            }

}

It stops executing after the sessionFlow.collect

Upvotes: 0

Views: 92

Answers (0)

Related Questions