I have some legacy code that operates the BLE chip...
# coroutines
t
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
You could create a single-threaded Executor(Service) and create a coroutine Dispatcher from that.
plus1 2
d
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
There is also a
Handler.asCoroutineDispatcher()
function.
p
^ best option IMO since it’s already using handler threads