I have a use case called “RegisterDeviceUseCase” with a function “execute()”.
I want this function to call a bluetooth adapter to get some information from the peripheral, and with the result, call a rest api and make execute() return the result from the rest call.
Something like:
suspend fun execute(): RestResult {
val bluetoothData = bluetoothAdapter.getData() //<-- wrong, getData() can't return anything
val restResult = api.call(bluetoothData)
return restResult
}
The problem is that the operations from the bluetoothAdapter are callback-based apis, and I can’t directly get a result from the methods, i will get them in the bluetooth callback:
class BluetoothAdapter {
fun getData() {
bluetooth.sendRequest()
}
private val gattCallback = object : BluetoothGattCallback() {
fun onDataReceived(data: Data){
//response received here, send this to usecase "execute" method
}
.
.
.
//more callback methods
}
}
Is there a way to call the bluetoothadapter, wait for the response in the callback and then make the rest call with it?
I know there is callbackFlow but I think it only works with one-method callbacks, which is not the case with the bluetooth callback.
Maybe getData() could return a flow and I could collect the result in the usecase, but at some point I would need to stop collecting, since I don’t want the use case to be collecting forever and I don’t know if that’s possible.
Thanks!