Android serial comms using thread, handler, kotlin flows, suspend functions and compose

I’m trying to write an Android app which requires some serial comms with a bluetooth device. Some of this comms is command/response and some of it is event data that just gets sent from the device. All of it is packetized with packet type identifiers. I’ve setup a bluetooth serial thread which handles bluetoothSocket input/output streams. I can create command packets and send them. The thread takes a Handler as an arguement and sends messages to the main thread when a complete data packet is received (a handful of bytes in a ByteArray).

This Handler is defined within a “callbackflow” which overrides the “handleMessage” function and uses “trySend” to emit the ByteArray.

I’ve been able to get the received packets to display in realtime on the composable ui as hexstrings by converting the flow to a SharedFlow within my “bluetooth device datasource” to a StateFlow within my ViewModel.

I’d like other areas of my app to be able to subscribe to the SharedFlow and filter out the packets that they are interested in but I’d also like to be able to run chains of commands sequentially without building the logic into the Ui.

I thought I might be able to achieve this by creating suspend functions that can be called sequentially to carry out a series of operations on the external device. E.g:
Send command 1
Wait for response
Send command 2
Wait for response
etc.

I’m trying to work out how these are supposed to be implemented. I basically want to trigger sending the command and then “listen” on the received packet flow for the correct packet type, then return the packet from the suspend function.

I’m new to Kotlin on top of being fairly amateur in Android so I’m not sure if any of this is the “correct” way to do things.

the handler and callbackflow is like this:

fun btInputStreamHandler(): Flow<ByteArray> = callbackFlow {

        val btMessageHandler = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                val b = msg.data
                val rxpacket = b.getByteArray("rxpacket")
                if (rxpacket != null) {
                    //handleRxPacket(rxpacket)
                    trySend(rxpacket)
                }
            }
        }
//this messageHandler is passed into the serial comms thread when a new device is connected
        messageHandler = btMessageHandler

        awaitClose {
            messageHandler = null
        }
    }

    val btInputPacketSharedFlow: SharedFlow<ByteArray> = btInputStreamHandler()
        .shareIn(CoroutineScope(Dispatchers.IO), SharingStarted.Eagerly)

In the first instance I’m trying to end up with something like this:

suspend fun getValueFromBluetoothDevice(): ByteArray {

//this is the serial comms thread that loads packets to be sent asynchronously
        btSerialProcess?.sendCommand(DeviceCommand.getParameter1())

        var flag: Boolean = true
        var result: ByteArray = ByteArray(0)
        while( flag ) {
            //Wait for flow to emit the correct packet type
            result = btInputPacketSharedFlow
                .filterNotNull()
                .first()
            if( DeviceCommand.getPacketType(result) == CORRECT_PACKET_TYPE ) {
                flag = false
                
            }
        }
        return result
    }

Clearly there are some gaps in my understanding here because although the command does get sent and the response does comeback (the UI updates with the packet string) the suspend function doesn’t return the value and doesn’t even return.

Update
I assume this is the wrong way to do it but it sort of works:

suspend fun getCmdResult(cmd: DeviceCommand, packetType1: Int, packetType2: Int): ByteArray {
        var result: ByteArray = ByteArray(0)
        var flag: Boolean = true
        val job = CoroutineScope(Dispatchers.IO).launch {
            btInputPacketSharedFlow.collect { packet ->
                result = packet
                if (
                    (DeviceCommand.getPacketType1(result) == packetType1)
                    and
                    (DeviceCommand.getPacketType2(result) == packetType2)
                ){
                    flag = false

                }
            }
        }
        btSerialProcess?.sendCommand(cmd)
        while( flag ) {
          delay(50)
        }
        job.cancel()
        return result
    }

New contributor

Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật