Hi guys, I am making an android app to control a L...
# juul-libraries
g
Hi guys, I am making an android app to control a LED from my phone through BLE. This is my first time using Kable and I would appreciate having your opinion on the way I am using it. I have prepared a small gist where you can see my implementation: https://gist.github.com/glsubri/ac280b594d0c68b3dc1b3bcfdc7b7b7b. I also had a small question: is it normal that it takes something like a minute to connect to my Bluetooth device ? Am I missing something with kable or is it my micro-controller ? Thanks!
t
Overall, seems like reasonable/good usage of Kable. I do have some small pointers that might improve a few things: Kable doesn't care what Dispatcher you use for all its functions (internally it uses either a dedicated thread or a
Handler
, depending on the Android version, to manage threading for I/O operations), so your use of
<http://Dispatchers.IO|Dispatchers.IO>
to interact with Kable doesn't hurt anything, but also isn't necessary (and may be resulting in unnecessary context switches). The same is also true for your usages of
observe
, you don't need
flowOn
for those. I'd recommend you make
updateTime
suspend and then you can remove the
ioScope.launch
within that function. We plan to later either introduce another
State
or make
State.Connected
emit after service discovery, so it will likely remove your need to manage your own
Ready
state. https://github.com/JuulLabs/kable/issues/75 By having
updateTime
suspend also means that failures in that function will propagate through Kable's
connect
method, which IMHO will make your error handling simpler. Since your
disconnect
method is `launch`ing, it may cause issues in your
reconnect
function. Since you should "wait" for disconnect to complete before attempting another connection. So it would be better if your
reconnect
called either suspend version of your
disconnect
function or just used
Peripheral.disconnect
directly. Re: it taking a minute to connect; that does seem like a really long time, I haven't see something like that. I'm used to connections taking less than 10 seconds (assuming peripheral is near the Android phone). We'll be adding logging to Kable soon, which might help diagnose issues related to your connections taking a long time. 🤞 https://github.com/JuulLabs/kable/issues/130
g
@travis Thanks for the insights, I will probably switch to
suspend
functions. Also, thanks for the library, it's really nice 👍
👍 1
t
Also, thanks for the library, it's really nice 👍
Thanks for the kind words, glad it is working well for you. 😄