JuulLabs / kable

Kotlin Asynchronous Bluetooth Low-Energy

Home Page:https://juullabs.github.io/kable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ConnectionLostException in observing

skaminskiProexe opened this issue · comments

Hi, is any option to get ConnectionLostException from Peripheral.observe(...) when I turn off device?
I think this is missing in Your library.

I have simple configuration:

                val scanner = Scanner {
                    filters = null
                }.advertisements.first()
                peripheral((scanner)).apply {
                    connect()
                    val controlFlow by lazy {
                        characteristicOf(
                            "1bc51520-0200-c991-e511-e47140b6f6ac",
                            "1bc51522-0200-c991-e511-e47140b6f6ac",
                        )
                    }
                    val dataFlow by lazy {
                        characteristicOf(
                            "1bc51520-0200-c991-e511-e47140b6f6ac",
                            "1bc51521-0200-c991-e511-e47140b6f6ac",
                        )
                    }
                    write(controlFlow, byteArrayOf(0), WriteType.WithResponse)
                    observe(dataFlow).collect {
                        println(it.joinToString())
                    }
                }

You can get connection state from Peripheral.state and collect it to know when the peripheral disconnects.