Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample app? #27

Open
qxdevelop opened this issue Sep 6, 2023 · 2 comments
Open

Sample app? #27

qxdevelop opened this issue Sep 6, 2023 · 2 comments

Comments

@qxdevelop
Copy link

Please could you put a sample app? Im trying to use the sample at the presentation page:

        val obdConnection = ObdDeviceConnection(socket.inputStream, socket.outputStream)
        val response = obdConnection.run(SpeedCommand())

But it looks like wrong by compiler for the "obdConnection.run()". Error showed is:
Suspend function 'run' should be called only from a coroutine or another suspend function

@MartinLukacSTUBA
Copy link

@qxdevelop hello, did you move on ? or did you find any sample application for that ?

@AugustinVoiMa
Copy link

AugustinVoiMa commented Feb 12, 2024

Hello,
As shown by the error message, you are trying to call a function that blocks the execution thread. I assume that you are trying to call it from the main UI Thread. It is not recommanded to block the UI thread as it would cause a freeze of the UI. That's why even the compiler prevent you to do that.

I am not very skilled in Kotlin but I found a simple way of achieving an async call to the obd.run that later produce a result on the UI thread:

fun custom_run(command: ObdCommand, use_cache: Boolean = false): CompletableFuture<ObdResponse> {
        val response = CompletableFuture<ObdResponse>()

        runBlocking {
            launch {
                // What is inside "launch" block will be executed on a corouting, this code can block without blocking the calling thread
                val res = obd_co.run(command, use_cache)
                response.complete(res)
            }
        }
        return response
    }

When the result will be ready, it will be placed in a promise on which you can register a main thread

custom_run(VINCommand(), use_cache = true).thenAccept {
  // Do what you want with "it", an ObdResponse object
  // This code will run on the current execution thread
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants