<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tia Sarwoedhi Pratama on Medium]]></title>
        <description><![CDATA[Stories by Tia Sarwoedhi Pratama on Medium]]></description>
        <link>https://medium.com/@tiasarwoedhip?source=rss-64b9226079f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qUGZ5ZJOra15BJMI</url>
            <title>Stories by Tia Sarwoedhi Pratama on Medium</title>
            <link>https://medium.com/@tiasarwoedhip?source=rss-64b9226079f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:43:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tiasarwoedhip/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simplifying Serial Communication in Android Kotlin with JSerialComm]]></title>
            <link>https://medium.com/@tiasarwoedhip/simplifying-serial-communication-in-android-kotlin-with-jserialcomm-3dbdbb65e4d7?source=rss-64b9226079f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dbdbb65e4d7</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Tia Sarwoedhi Pratama]]></dc:creator>
            <pubDate>Fri, 12 Jul 2024 06:21:09 GMT</pubDate>
            <atom:updated>2024-07-12T06:21:09.601Z</atom:updated>
            <content:encoded><![CDATA[<p>Serial communication is a vital aspect of many Android applications, particularly those interfacing with external hardware like sensors, microcontrollers, or industrial devices. Managing serial ports efficiently while ensuring compatibility across different platforms can be challenging. However, with the help of JSerialComm, an open-source Java library, integrating serial communication into Android applications becomes straightforward, especially when using Kotlin.</p><h3>What is JSerialComm?</h3><p><a href="https://github.com/Fazecast/jSerialComm">JSerialComm</a> simplifies serial communication by offering a platform-independent API for Java-based applications, including those developed in Kotlin. It handles the complexities of serial port communication, making it easier for developers to manage data exchange with external devices seamlessly.</p><h3>Key Features of JSerialComm</h3><ol><li><strong>Cross-Platform Compatibility:</strong> JSerialComm supports multiple operating systems, ensuring consistent serial communication across Android, Windows, macOS, and Linux.</li><li><strong>Simple API:</strong> The library provides a straightforward API that abstracts the complexities of serial communication, making it accessible even for developers new to hardware interaction.</li><li><strong>Event-Driven Architecture:</strong> JSerialComm utilizes an event-driven model, allowing applications to respond asynchronously to events such as data arrival or connection status changes.</li><li><strong>Configurability:</strong> Developers can easily configure baud rates, data bits, stop bits, and parity settings, ensuring compatibility with a wide range of serial devices.</li><li><strong>Efficient Buffer Management:</strong> It includes efficient buffer management mechanisms to handle incoming and outgoing data streams, optimizing performance and reliability.</li></ol><h3>Integrating JSerialComm into Android Kotlin</h3><p>Integrating JSerialComm into an Android Kotlin project involves several steps:</p><h4>Step 1: Adding JSerialComm to Your Project</h4><p>Follow these instructions:</p><pre>for some bug in AndroidKotlin jSerialComm https://github.com/Fazecast/jSerialComm/issues/128<br>i give this configuration in kotlin:<br><br>1. Download the .jar file in this https://github.com/Fazecast/jSerialComm<br>2. create &quot;jniLibs/Android&quot; folder in &quot;main&quot; folder and copy your .so file from .jar file (extract your jar file) to Jnifolder<br>3. create &quot;lib&quot; folder in &quot;apps&quot; folder and copy .jar file to &quot;lib&quot; folder<br>4. write this configuration to Build App Gradle.<br><br>configurations {<br>    create(&quot;repackage&quot;)<br>}<br><br>dependencies {<br><br>  //...your gradle implementation<br>  implementation(files(&quot;libs/jSerialComm-2.11.0.jar&quot;))<br>  &quot;repackage&quot;(&quot;com.googlecode.jarjar:jarjar:1.3&quot;)<br>  &quot;repackage&quot;(&quot;com.fazecast:jSerialComm:2.+&quot;)<br>}<br><br>tasks.register&lt;Copy&gt;(&quot;copySharedLibraries&quot;) {<br>    from(zipTree(&quot;libs/repackagedLibs.jar&quot;)) {<br>        include(&quot;Android/*/libjSerialComm.so&quot;)<br>    }<br>    into(&quot;src/main/jniLibs/&quot;)<br>    eachFile {<br>        path = path.replace(Regex(&quot;Android/(.*?)/libjSerialComm.so&quot;), &quot;$1/libjSerialComm.so&quot;)<br>    }<br>    doLast {<br>        delete(&quot;src/main/jniLibs/Android/&quot;)<br>    }<br>}<br><br>//here some configuration from the link above.<br>tasks.register(&quot;repackageJavaModuleJars&quot;) {<br>    doLast {<br>        ant.withGroovyBuilder {<br>            &quot;taskdef&quot;(<br>                &quot;name&quot; to &quot;jarjar&quot;,<br>                &quot;classname&quot; to &quot;com.tonicsystems.jarjar.JarJarTask&quot;,<br>                &quot;classpath&quot; to configurations[&quot;repackage&quot;].asPath<br>            )<br>            &quot;jarjar&quot;(<br>                &quot;jarfile&quot; to &quot;libs/repackagedLibs.jar&quot;,<br>                &quot;filesetmanifest&quot; to &quot;merge&quot;<br>            ) {<br>                configurations[&quot;repackage&quot;].forEach {<br>                    if (!it.name.contains(&quot;jarjar-&quot;)) {<br>                        &quot;zipfileset&quot;(&quot;src&quot; to it, &quot;excludes&quot; to &quot;module-info.class&quot;)<br>                    }<br>                }<br>            }<br>        }<br>    }<br>}</pre><p>Sync your project with Gradle files to import the library into your project.</p><h4>Step 2: Requesting Serial Port Permissions</h4><p>Since Android restricts direct access to serial ports for security reasons, you need to declare the necessary permissions in your AndroidManifest.xml file:</p><pre>&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;<br>&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;<br>&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot;/&gt;<br>&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot;/&gt;<br>&lt;uses-permission android:name=&quot;android.permission.USB_PERMISSION&quot; /&gt;</pre><h4>Step 3: Initializing and Using JSerialComm</h4><p>Here’s a basic example demonstrating how to initialize and use JSerialComm in Kotlin:</p><pre>import com.fazecast.jSerialComm.SerialPort</pre><pre>// Initialize a serial port object<br>val portName = &quot;/dev/ttyUSB0&quot; // Example port name<br>val baudRate = 9600 // Example baud rate</pre><pre>val serialPort = SerialPort.getCommPort(portName)<br>serialPort.setBaudRate(baudRate)</pre><pre>// Open the serial port<br>if (serialPort.openPort()) {<br>    println(&quot;Port opened successfully.&quot;)</pre><pre>    // Setup a listener for data reception<br>    serialPort.addDataListener(object : SerialPortDataListener {<br>        override fun serialEvent(event: SerialPortEvent) {<br>            if (event.eventType == SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {<br>                val bytesAvailable = serialPort.bytesAvailable()<br>                val newData = ByteArray(bytesAvailable)<br>                serialPort.readBytes(newData, bytesAvailable.toLong())</pre><pre>                // Handle the received data (e.g., update UI)<br>                println(&quot;Received data: ${String(newData)}&quot;)<br>            }<br>        }<br>    })</pre><pre>    // Send data through the serial port<br>    val dataToSend = &quot;Hello, Serial!&quot;<br>    serialPort.writeBytes(dataToSend.toByteArray(), dataToSend.length.toLong())<br>} else {<br>    println(&quot;Failed to open the port.&quot;)<br>}</pre><pre>// Close the serial port when done<br>serialPort.closePort()</pre><h3>Example Use Case: IoT and Embedded Systems</h3><p>Imagine developing an Android application that communicates with IoT devices or embedded systems via serial ports. JSerialComm enables seamless integration, making it ideal for applications in home automation, industrial control, and more.</p><h3>Conclusion</h3><p>JSerialComm simplifies serial communication in Android Kotlin applications, providing a robust solution for interacting with external hardware. Whether you’re a beginner or an experienced developer, integrating serial communication using Kotlin and JSerialComm opens up new possibilities for your Android projects.</p><p>By leveraging JSerialComm’s cross-platform compatibility, simplicity, and event-driven architecture, developers can focus on building innovative applications without being burdened by low-level communication details. Explore the power of JSerialComm and enhance your Android Kotlin applications today!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dbdbb65e4d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>