https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

Travis Griggs

12/13/2023, 7:02 PM
I have some legacy code that operates the BLE chip using a special HandlerThread or two. Basically, you want to do most things BLE off of the main thread, but they have to be serialized carefully. Being somewhat new to kotlin coroutines, I thought I'd have a go at reworking this to use coroutines. Currently my design uses a bunch of dedicated post/postDelayed on a single thread. I get that I can replace my posts with launches on a given dispatcher. What's less clear, is how I specify a specific dispatcher with serial semantics. Am I trying to do apples/oranges too much here?
s

streetsofboston

12/13/2023, 7:05 PM
You could create a single-threaded Executor(Service) and create a coroutine Dispatcher from that.
plus1 2
d

Daniel Pitts

12/13/2023, 9:09 PM
Coroutine Channels might be a better approach, depending on what you're trying to do. If it's just passing data back-and-forth, they might provide what you want.
a

Albert Chang

12/14/2023, 1:35 AM
There is also a
Handler.asCoroutineDispatcher()
function.
p

Patrick Steiger

12/14/2023, 2:50 PM
^ best option IMO since it’s already using handler threads