A professional, high-performance Android library for USB Serial communication. It provides a clean API for general serial I/O and includes a specialized utils suite for Modbus-based industrial sensors, such as 8-in-1 Soil Sensors.
- Universal Serial Support: Works with CH34x, CP210x, FTDI, Prolific, and more.
- Modular Architecture: General USB logic is decoupled from sensor-specific logic.
- Sensor Utilities: Built-in support for 8-in-1 Soil Sensors via
usbManager.utils. - Permission Manager: Simplified workflow for handling USB device permissions.
- Asynchronous Ready: Designed for background processing (Modbus, Serial I/O).
Add it in your root settings.gradle.kts:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}Add the following to your module's build.gradle.kts:
dependencies {
implementation("com.github.DeveloperRejaul:usb-serial:v0.0.1")
}val usbManager = UsbManager(context)
fun setupUsb() {
val devices = usbManager.getDeviceList()
if (devices.isNotEmpty()) {
val device = devices.first()
usbManager.requestUsbPermission(device) { granted ->
if (granted) {
usbManager.connect(device, baudRate = 9600)
}
}
}
}// Sending data
usbManager.write(byteArrayOf(0x01, 0x02, 0x03))
// Reading data
val buffer = usbManager.read(bufferSize = 1024)The library provides a utils namespace for specialized hardware.
Read Temperature, Humidity, EC, Salinity, N, P, K, and pH with a single call.
// Perform on background thread (e.g., Dispatchers.IO)
val data = usbManager.utils.readSoilData()
data?.let {
val temp = it["temperatureC"]
val ph = it["ph"]
val nitrogen = it["nitrogenMgKg"]
// ... use other values
}To make your app open automatically when the hardware is plugged in:
Create res/xml/device_filter.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Empty tag matches any USB Serial device -->
<usb-device />
</resources>Update your MainActivity in AndroidManifest.xml:
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>- Android device with USB Host support.
- OTG Cable or USB-C Hub.
- Industrial 8-in-1 Soil Sensor (RS485/Modbus) + RS485-to-USB Converter.
Copyright 2026 DeveloperRejaul. Licensed under the Apache License 2.0. See LICENSE for details.