DEV Community

Bratik Mukherjee
Bratik Mukherjee

Posted on

Zero Internet? No Problem: How I Built Offline P2P Sharing Using QR Codes in Android

This is a great strategy for Dev.to and Hashnode. Articles that solve a specific technical problem—like offline data transfer—perform much better than general "I made an app" posts because they provide immediate value to other developers.

DEMO

Here is a ready-to-publish Markdown draft for your technical article.


Zero Internet? No Problem: How I Built Offline P2P Sharing Using QR Codes in Android

Imagine you’re in a basement lab, a subway, or a remote area with zero bars of signal. You find a vital piece of documentation or a complex algorithm on your phone that your teammate needs right now. Without an internet connection, how do you send it?

This was the challenge I faced while building bDoci, an open-source documentation hub for developers. I didn't want to rely on Bluetooth pairing or local Wi-Fi hotspots, which can be finicky.

Instead, I built a Zero-Network P2P Sync system using nothing but JSON serialization, QR codes, and Android Deep Links. Here is how I did it.

The Logic Flow

The goal is to move a structured Doc object from Device A's database to Device B's database using the camera as the data bridge.

  1. Serialize: Convert the Room Entity (Kotlin Data Class) to a JSON string.
  2. Encode: Convert that JSON to a Base64 string to keep the URI safe.
  3. Generate: Convert the Base64 string into a QR code.
  4. Broadcast: Embed the QR data into a custom URI scheme (bdoci://share/...).
  5. Receive: Use an Android Intent Filter to catch the link and inject the data.

🛠 The Code: From Object to Image

To handle the heavy lifting, I used GSON for serialization and ZXing for QR generation.

1. Serializing the Document

First, we turn our documentation object into a format that can be embedded in a URL.

fun generateShareUrl(doc: Doc): String {
    // 1. Convert Object to JSON
    val json = Gson().toJson(doc)

    // 2. Encode to Base64 to handle special characters in the URL
    val encodedData = Base64.encodeToString(json.toByteArray(), Base64.URL_SAFE or Base64.NO_WRAP)

    // 3. Return the custom Deep Link
    return "bdoci://share/$encodedData"
}
Enter fullscreen mode Exit fullscreen mode

2. Generating the QR Code

Once we have our URL, we need to draw it as a QR code that the recipient can scan.

fun generateQRCode(content: String): Bitmap {
    val writer = MultiFormatWriter()
    val matrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512)
    val encoder = BarcodeEncoder()
    return encoder.createBitmap(matrix)
}
Enter fullscreen mode Exit fullscreen mode

📡 The Intent: Catching the Data

The "magic" happens on the receiving device. When the user scans the QR code with their native camera, Android needs to know that bDoci is the app responsible for that specific link.

I configured the AndroidManifest.xml with an <intent-filter> to catch the custom bdoci:// scheme:

<activity android:name=".Dashboard">
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="bdoci" android:host="share" />
    </intent-filter>
</activity>
Enter fullscreen mode Exit fullscreen mode

In the Dashboard.kt activity, I added a listener to handle the incoming data:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val data: Uri? = intent?.data
    if (data != null && data.scheme == "bdoci") {
        val encodedData = data.lastPathSegment
        val decodedJson = String(Base64.decode(encodedData, Base64.URL_SAFE))

        // Convert back to Object and save to local Room Database
        val sharedDoc = Gson().fromJson(decodedJson, Doc::class.java)
        viewModel.insert(sharedDoc)

        Toast.makeText(this, "Received: ${sharedDoc.title}", Toast.LENGTH_SHORT).show()
    }
}
Enter fullscreen mode Exit fullscreen mode

Why this works so well

By using Base64 encoding and Deep Links, we eliminate the need for a server middleman. The data never touches the cloud—it moves at the speed of light from one screen to another lens.

For developer tools, this type of reliability is crucial. Whether you're in a high-security server room with no Wi-Fi or a rural area with poor 4G, your knowledge base remains collaborative.

Check out the project

I implemented this entire system (along with a floating window PiP mode and Gruvbox aesthetics) in my open-source app, bDoci.

If you want to see the full implementation or contribute to the project:

💻 Source Code: GitHub - bimbok/bdoci-app
📱 Download the APK: Latest Release

I'd love to hear your thoughts on this offline-sync approach! What other creative ways have you used QR codes in your apps?

Android #Kotlin #OpenSource #SoftwareArchitecture #MobileDev


Pro-Tips for Dev.to / Hashnode:

  1. The Cover Image: Use a tool like Canva to make a simple thumbnail. Put a QR code on one side and the Android logo on the other with the text "Offline P2P Sharing."
  2. Interactive Code: If you use Gists, you can embed them directly for better syntax highlighting.
  3. Engagement: After you post, check the comments! People might ask about the character limit of QR codes (which is about 2,953 bytes for alphanumeric data)—be ready to explain that you keep your document objects lightweight!

Top comments (0)