Skip to content

DeveloperRejaul/usb-serial

Repository files navigation

UsbSerial-Android

Release License

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.

Features

  • 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).

Installation

1. Add JitPack repository

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") }
    }
}

2. Add Dependency

Add the following to your module's build.gradle.kts:

dependencies {
    implementation("com.github.DeveloperRejaul:usb-serial:v0.0.1")
}

Core Usage

1. Initialize & Connect

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)
            }
        }
    }
}

2. General Serial Read/Write

// Sending data
usbManager.write(byteArrayOf(0x01, 0x02, 0x03))

// Reading data
val buffer = usbManager.read(bufferSize = 1024)

Sensor Utilities (utils)

The library provides a utils namespace for specialized hardware.

8-in-1 Soil Sensor (Modbus RTU)

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
}

Professional Setup: Auto-Launch App

To make your app open automatically when the hardware is plugged in:

1. Define Device Filter

Create res/xml/device_filter.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Empty tag matches any USB Serial device -->
    <usb-device />
</resources>

2. Configure Manifest

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>

Hardware Requirements

  • Android device with USB Host support.
  • OTG Cable or USB-C Hub.
  • Industrial 8-in-1 Soil Sensor (RS485/Modbus) + RS485-to-USB Converter.

License

Copyright 2026 DeveloperRejaul. Licensed under the Apache License 2.0. See LICENSE for details.

About

A professional, high-performance Android library for USB Serial communication.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages