<?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 Dulanja Kristhombu on Medium]]></title>
        <description><![CDATA[Stories by Dulanja Kristhombu on Medium]]></description>
        <link>https://medium.com/@dulanja-kristhombu?source=rss-f0fd2698cc8e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*r0t3VGCx8ElxGCCxqc-LwA.png</url>
            <title>Stories by Dulanja Kristhombu on Medium</title>
            <link>https://medium.com/@dulanja-kristhombu?source=rss-f0fd2698cc8e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:30:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dulanja-kristhombu/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[Build an iOS Printer Client Without an SDK]]></title>
            <link>https://medium.com/@dulanja-kristhombu/build-an-ios-printer-client-without-an-sdk-9bce1b42e171?source=rss-f0fd2698cc8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bce1b42e171</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[thermal-printer]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[escpos]]></category>
            <dc:creator><![CDATA[Dulanja Kristhombu]]></dc:creator>
            <pubDate>Mon, 10 Aug 2026 15:20:21 GMT</pubDate>
            <atom:updated>2026-08-10T15:28:28.016Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bqk4aiKLfLSGBuJ0vaA15A.png" /></figure><p>Every thermal printer vendor ships an iOS SDK. Epson has one, Zebra has one, Rongta has one. If your app has to support more than one brand, you end up bundling several of them, each with its own API, its own threading model, and its own bugs.</p><p>There’s a simpler path. Almost every network printer sold today listens on <strong>TCP port 9100</strong> (JetDirect / raw printing). You open a socket, write bytes, close the socket. That’s the whole protocol. The only thing that changes between brands is <em>which</em> bytes.</p><p>This is how I replaced four vendor SDKs with three Swift files.</p><h3>The architecture</h3><p>Three layers, each independent:</p><p>Layer Job <strong>Transport</strong> Open a TCP socket, write bytes, close it <strong>Languages</strong> Turn “print this text/image” into command bytes <strong>Discovery</strong> Find printers on the LAN and guess their language</p><h3>1. Transport</h3><p>Network.framework gives you everything. Wrap NWConnection in async/await and you have a printer client:</p><pre>func connect(host: String, port: UInt16 = 9100, timeout: TimeInterval = 10) async throws {<br>    let conn = NWConnection(host: NWEndpoint.Host(host), port: nwPort, using: .tcp)<br>    connection = conn</pre><pre>    try await withCheckedThrowingContinuation { cont in<br>        conn.stateUpdateHandler = { state in<br>            switch state {<br>            case .ready:            finish(.success(()))<br>            case .failed(let e):    finish(.failure(PrinterError.connectionFailed(e.localizedDescription)))<br>            case .waiting(let e):   finish(.failure(PrinterError.connectionFailed(e.localizedDescription)))<br>            default:                break<br>            }<br>        }<br>        conn.start(queue: queue)<br>        queue.asyncAfter(deadline: .now() + timeout) { finish(.failure(...)) }<br>    }<br>}</pre><p>Two details worth stealing:</p><p><strong>Treat </strong><strong>.waiting as a failure.</strong> NWConnection sits in .waiting on an unreachable host or refused port, retrying forever. For a bounded connect you want it to fail fast.</p><p><strong>Close the socket after every job.</strong> The FIN is the end-of-job signal many printers wait for. Leave the connection open and some cutterless models over-feed paper trying to &quot;present&quot; the receipt.</p><pre>private func sendJob(_ data: Data) async throws {<br>    try await connect(host: host, port: port, timeout: 35)<br>    try await send(data)<br>    let drainSeconds = min(6.0, 0.3 + Double(data.count) / 8000.0)<br>    try? await Task.sleep(nanoseconds: UInt64(drainSeconds * 1_000_000_000))<br>    disconnect()  // FIN = end of job<br>}</pre><p>That 35-second timeout isn’t paranoia. An idle Zebra sleeps between jobs and can take ~30s to accept a connection. A 10s timeout dies before it wakes up.</p><p>The drain wait matters too. A directly-networked printer accepts a job in ~200ms, but a generic Ethernet→serial print-server clocks bytes out at the serial baud rate. Close too early and the job truncates.</p><h3>2. Languages</h3><p>The transport is language-agnostic, so the same socket drives eight command languages: ESC/POS, ZPL, EPL, CPCL, TSPL, DPL, PCL, PostScript. Each is just a byte builder:</p><pre>static func text(_ text: String, language: PrinterLanguage, widthDots: Int = 576, cut: Bool = true) -&gt; Data {<br>    switch language {<br>    case .escpos:     return escposText(text, cut: cut)<br>    case .zpl:        return zplText(lines)<br>    case .tspl:       return tsplText(lines)<br>    // …<br>    }<br>}</pre><p>Images need a 1-bit raster. One shared Floyd–Steinberg dither feeds all of them — then each language wraps it in its own header. ESC/POS uses GS v 0, ZPL uses ^GFA, TSPL uses BITMAP.</p><p>Three gotchas cost me real days:</p><p><strong>TSPL bitmap polarity is inverted.</strong> In ESC/POS a 1 bit is black. In TSPL a 0 bit is black. Every byte gets bit-flipped:</p><pre>let inverted = m.bytes.map { ~$0 }</pre><p><strong>Band your ESC/POS rasters.</strong> Many controllers can’t accept a single full-height GS v 0. Split into bands of ≤255 dots.</p><p><strong>Tell label printers the media is continuous.</strong> ZPL ^MNN, TSPL GAP 0 mm,0 mm. Otherwise the printer hunts for a label gap it will never find on a receipt roll and feeds 30cm of blank paper looking for it.</p><h3>3. Discovery</h3><p>Two mechanisms, run concurrently:</p><p><strong>Bonjour</strong> for printers with real firmware. NetService browsing _pdl-datastream._tcp, _ipp._tcp, _printer._tcp gives you the model name (TXT ty) and supported languages (TXT pdl) for free.</p><p><strong>A port sweep</strong> for everything else. Cheap raw-TCP boxes advertise nothing, so probe every host on the /24 and see who accepts on 9100:</p><pre>async let sweepTask   = discover(port: port)<br>async let bonjourTask = BonjourPrinterBrowser().browse()<br>let ips     = await sweepTask<br>let bonjour = await bonjourTask</pre><p>For swept hosts with no Bonjour record, fetch their embedded web page on port 80 and fingerprint it — &lt;title&gt;, Server: header, brand keywords. &quot;ZEBRA&quot; → ZPL, &quot;RONGTA&quot; → TSPL, &quot;EPSON&quot; → ESC/POS.</p><p>Two bonuses fall out of the sweep:</p><p>The subnet sweep also warms the <strong>ARP cache</strong>, so you get every printer’s MAC address for free. Read it with sysctl(CTL_NET, PF_ROUTE, …) — no entitlement, no private API. Store the MAC instead of the IP and a DHCP address change stops breaking your saved printers.</p><p>You also have to <strong>filter out non-printers</strong>. Phones, TVs, and speakers all answer on the LAN, so a name blocklist keeps the scan list clean.</p><h3>What you need in Info.plist</h3><pre>NSLocalNetworkUsageDescription<br>NSBonjourServices  →  _pdl-datastream._tcp, _ipp._tcp, _printer._tcp<br>NSAllowsLocalNetworking  (for HTTP fingerprinting on port 80)</pre><h3>Was it worth it?</h3><p>Three files, zero dependencies, eight printer languages, one API.</p><p>You own the protocol quirks now — every over-feed and truncated job is yours to debug. But those bugs were in the SDKs too; you just couldn’t fix them.</p><p><strong>Thank you for reading! If you have any questions or feedback, feel free to reach out.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bce1b42e171" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simplifying Android UI Access with Kotlin Synthetic Properties]]></title>
            <link>https://medium.com/@dulanja-kristhombu/simplifying-android-ui-access-with-kotlin-synthetic-properties-1b712a6ee803?source=rss-f0fd2698cc8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b712a6ee803</guid>
            <category><![CDATA[android-ui]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[kotlin-synthetic]]></category>
            <dc:creator><![CDATA[Dulanja Kristhombu]]></dc:creator>
            <pubDate>Sat, 11 May 2024 13:14:49 GMT</pubDate>
            <atom:updated>2024-05-11T13:34:22.610Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qfd7jlqiJyjHSn-tb9YMfw.png" /></figure><p>In Android development, Kotlin Synthetic Properties provide a convenient way to directly access UI elements from XML layouts in Kotlin code. This eliminates the need for repetitive findViewById() calls, making the code cleaner and more concise. In this article, we’ll explore how Kotlin Synthetic Properties simplify UI access and improve the development experience for Android developers.</p><p><strong>1.Understanding Kotlin Synthetic Properties:</strong></p><ul><li>Kotlin Synthetic Properties is a feature that allows direct access to UI elements declared in XML layouts.</li><li>It eliminates the need for findViewById() calls by generating synthetic properties for each UI element with an ID.</li></ul><p><strong>2. Enabling Kotlin Synthetic Properties:</strong></p><ul><li>To use Kotlin Synthetic Properties, ensure that the Kotlin Android Extensions plugin is added to your project.</li><li>In the app-level build.gradle file, include the plugin: apply plugin: id&#39;kotlin-android-extensions&#39;.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*a6_gbNZIhAAG1_Cr7ln8Cg.png" /></figure><p><strong>3. Declaring UI Elements in XML Layouts:</strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;androidx.constraintlayout.widget.ConstraintLayout <br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    android:padding=&quot;12dp&quot;<br>    tools:context=&quot;.MainActivity&quot;&gt;<br><br>    &lt;EditText<br>        android:id=&quot;@+id/nameEditText&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:hint=&quot;Name&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintHorizontal_bias=&quot;0.5&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>    &lt;EditText<br>        android:id=&quot;@+id/ageEditText&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:hint=&quot;Age&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintHorizontal_bias=&quot;0.5&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toBottomOf=&quot;@+id/nameEditText&quot; /&gt;<br><br>    &lt;EditText<br>        android:id=&quot;@+id/countryEditText&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:hint=&quot;Country&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintHorizontal_bias=&quot;0.5&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toBottomOf=&quot;@+id/ageEditText&quot; /&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/saveButton&quot;<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Save&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintHorizontal_bias=&quot;0.5&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toBottomOf=&quot;@+id/countryEditText&quot; /&gt;<br>    <br>&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;</pre><p><strong>4. Accessing UI Elements in Kotlin Code:</strong></p><pre>package com.example.synthuikotlintutorial<br><br>import androidx.appcompat.app.AppCompatActivity<br>import android.os.Bundle<br>import kotlinx.android.synthetic.main.activity_main.*<br><br>class MainActivity : AppCompatActivity() {<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.activity_main)<br><br>        saveButton.setOnClickListener{<br>            val userName = nameEditText.text.toString()<br>            val userAge = ageEditText.text.toString().toInt()<br>            val userCountry = countryEditText.text.toString()<br>        }<br><br>    }<br>}</pre><p>5. <strong>Benefits of Kotlin Synthetic Properties:</strong></p><ul><li>Reduced boilerplate: Eliminates repetitive findViewById() calls, resulting in cleaner and more concise code.</li><li>Improved readability: Directly accessing UI elements in Kotlin code makes it easier to understand and maintain.</li><li>Type safety: Kotlin Synthetic Properties ensure type safety by generating properties with appropriate types for UI elements.</li></ul><p><strong>Thank you for reading! If you have any questions or feedback, feel free to reach out.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b712a6ee803" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>