https://kotlinlang.org logo
#juul-libraries
Title
# juul-libraries
a

A

04/19/2022, 9:08 PM
This is my program on my Raspberry pi pico:
Copy code
import machine
import utime
led_onboard = machine.Pin(25, machine.Pin.OUT)
uart = machine.UART(0, baudrate=9600)
while True:
    led_onboard.value(1)
    utime.sleep(0.05)
    led_onboard.value(0)
    uart.write("test")
    utime.sleep(0.05)
This is my program on my Android:
Copy code
@OptIn(ExperimentalCoroutinesApi::class)
    fun connect(device: Device) {
        connectingCoroutine = viewModelScope.launch {
            try {
                val scanner = Scanner {
                    filters = null
                    logging {
                        engine = SystemLogEngine
                        level = Logging.Level.Data
                        format = Logging.Format.Multiline
                    }

                }

                scanner.advertisements.collect {
                    if (it.address == device.deviceUUID) {
                        val p = peripheral(it)
                        p.connect()
                        dataTransferState = DataTransferState.CONNECTED(device)
                        val services = p.services!!
                        val char = services
                            .first { service -> service.serviceUuid == uuidFrom("0000ffe0-0000-1000-8000-00805f9b34fb") }
                            .characteristics
                            .first { char -> char.characteristicUuid == uuidFrom("0000ffe1-0000-1000-8000-00805f9b34fb") }
                        Log.d("TAG", "serv=${char}")
                        dataTransferState = DataTransferState.WAITING
                        p.observe(char).catch { er -> Log.d("TAG", "$er") }
                            .collect { data ->
                                dataTransferState =
                                    DataTransferState.TRANSFERING_FROM_DEVICE
                                Log.d("TAG", "data1=${data.contentToString()}")
                            }
                        dataTransferState = DataTransferState.NOT_CONNECTED
                        Log.d("TAG", "data1=Done")
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        connectingCoroutine?.invokeOnCompletion {
            connectingCoroutine = null
        }
    }
t

travis

04/21/2022, 3:38 AM
The problem is your nested
collect
. If you only want to connect to a single peripheral, then use:
Copy code
val p = scanner.advertisements.first {
    it.address == device.deviceUUID
}.let { peripheral(it) }
// ..
p.observe(char).catch { /* .. */ }
    .collect {
        // ..
    }
// ..
If you want to connect to every matching peripheral, then you'll need to wrap the
observe
in `launch`s. Essentially, by having the nested
collect
(
observe(char).collect
) means it is suspending at that point while collecting characteristic changes, but that is applying "back-pressure" to the
advertisements.collect
.
a

A

04/21/2022, 4:02 AM
Thank you!
👍 1
37 Views