Leave low-level to Kotlin! Mastering language features even for hardware control
mhidakaSoftware Engineer, Representative of TechBookFest
# Overview Developing solutions that connect Android apps to real-world hardware sometimes involves legacy and unglamorous work. In this session, using the control of USB thermal printers used in stores as an example, we will introduce how to rewrite low-level hardware communication into a modern and declarative development experience using Kotlin language features and Jetpack Compose. Most hardware control protocols, down to their core, consist of command systems made of byte sequences and magic numbers. With conventional naive implementations, a lot of time is spent dealing with defects like connection failures or missed sequence handling and analyzing them. As a practical example, starting from the specifications of ESC/POS commands used for receipt printing, we will make full use of modern app development techniques: wrapping them in a type-safe way with Kotlin DSL, and finally creating a testable environment through off-screen rendering in Compose UI. We will introduce hardware prototyping techniques that will excite you with modern technologies. This session demonstrates the possibilities of Android and hardware. # Technical topics included in the talk - Fighting with mature specifications (USB Host API and ESC/POS) ESC/POS commands, the de facto standard for thermal printers, are legacy specifications continuing from the 1980s. We will explain low-level control design practices for directly sending these byte sequences (such as `0x1B 0x61 0x01` for center alignment) from Android, as well as common pitfalls when using the USB Host API. - Creating domain-specific languages with Kotlin DSL To break away from poorly readable byte sequence operations, we make full use of Kotlin's Type-Safe Builders (higher-order functions, function literals with receiver, extension functions). You will learn the know-how to abstract cipher-like command sets into a declarative and safe DSL with completion working in Android Studio, building a developer-friendly hardware control foundation that anyone on the team can understand intuitively. - Utilizing Compose UI as a general-purpose rendering engine (overcoming hardware constraints) The biggest obstacle in hardware control is that "it cannot be verified without a physical device" and "it depends on physical device specs (such as lack of Japanese font support)." In this session, we achieve a fast verification cycle for hardware behavior using Compose preview features. Furthermore, through off-screen rendering that hacks internal Compose behavior, we demonstrate an approach to directly convert UI built with Compose into Bitmaps and print them. Feel the new possibilities of Android breaking through hardware constraints with modern software technology. (Translated by the DroidKaigi Committee)
Intended audience
- Those who are interested in hardware integration and prototyping - Those who perform low-level control at work and want to increase development efficiency - Those who are looking for ways to share hardware knowledge within a team - Those who want to utilize Kotlin language features, or want to know specific design and application methods for Kotlin DSL (Type-Safe Builders) - Those who want to deepen their understanding of Jetpack Compose, or are interested in the drawing pipeline and non-UI uses (utilization as a rendering engine)