ArticlesAndroidZebra Scanner Integration

Integrating Zebra Phone Scanners with Android Applications using DataWedge

Introduction

Zebra’s enterprise Android devices are equipped with powerful scanning capabilities, critical in sectors like logistics, retail, and manufacturing. With Zebra’s DataWedge tool, integrating barcode scanning functionality into an Android app is straightforward, eliminating the need for complex SDKs.

This guide walks through adding DataWedge-based scanning to a Kotlin-based Android application using Broadcast Intents, ensuring minimal disruption to your existing codebase.

What is DataWedge?

DataWedge is a utility pre-installed on Zebra Android devices that allows barcode scanning and data capture without the need for developers to manage the scanner hardware directly. It can send the scanned data to an application via Intents or as keystrokes.

DataWedge enables:

  • Scanning without SDK dependencies.
  • Sending barcode data to applications through intents.
  • Managing profiles to define how data is captured and where it is sent.

Key benefit: DataWedge simplifies capturing and handling scan data in your Android app.

Setting Up DataWedge in Your Application

To receive scan data from a Zebra scanner, configure a DataWedge profile that specifies how the scanner behaves and how the captured data is sent to your app.

Steps to Set Up DataWedge:

Open the DataWedge App: This is pre-installed on Zebra devices.

Create a Profile: Create a new profile and associate it with your application by selecting your app’s package name.

Enable Barcode Input: Go to the Barcode input section and enable it, allowing DataWedge to capture barcode scans.

Configure Intent Output:

  • Enable Intent Output in the profile.
  • Set the Intent Action to something unique like com.yourapp.SCAN_ACTION.
  • Set the Intent Delivery to Broadcast Intent.

This setup ensures your app can receive barcode data via broadcast intents without interacting with the scanner hardware.

Receiving Scanned Data in Your Android App

Once the DataWedge profile is set up, configure your Android app to handle the incoming barcode scan data.

BroadcastReceiver Setup

Use a BroadcastReceiver to listen for intents sent by DataWedge. When a barcode is scanned, DataWedge sends the scan data as part of an intent, which your app can capture.

Here’s a basic implementation of a BroadcastReceiver in Kotlin:

class BarcodeReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        intent?.let {
            if (it.action == "com.yourapp.SCAN_ACTION") {
                // Extract the barcode data from the intent
                val barcodeData = it.getStringExtra("com.symbol.datawedge.data_string")
                // Process the scanned data (e.g., pass it to a ViewModel, log it, etc.)
                Log.d("BarcodeReceiver", "Scanned data: $barcodeData")
            }
        }
    }
}

This receiver listens for intents with the action com.yourapp.SCAN_ACTION, configured in the DataWedge profile. The barcode data is extracted from the intent and can be processed in your app.

Registering the Receiver in AndroidManifest.xml

To ensure your app can listen for these intents, declare the BroadcastReceiver in your AndroidManifest.xml file:

<receiver
    android:name=".utils.BarcodeReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.yourapp.SCAN_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

This ensures your app is ready to receive barcode data via broadcast intents.

Handling Scanned Data in Your UI

Once the scan data is captured by the BroadcastReceiver, you might want to update the UI with the scanned information. Here’s an example using Jetpack Compose, though this can be adapted to any preferred UI framework.

@Composable
fun ScannerScreen(
    event: (String) -> Unit
) {
    val context = LocalContext.current
 
    DisposableEffect(Unit) {
        val receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                if (intent?.action == "com.yourapp.SCAN_ACTION") {
                    val scanData = intent.getStringExtra("com.symbol.datawedge.data_string")
                    scanData?.let {
                        event(it) // Pass scanned data to the event handler
                    }
                }
            }
        }
        val filter = IntentFilter("com.yourapp.SCAN_ACTION")
        context.registerReceiver(receiver, filter)
 
        onDispose {
            context.unregisterReceiver(receiver)
        }
    }
 
    // Your UI components here (e.g., displaying the scanned data)
    Text(text = "Scan a barcode to see the result here.")
}

This ScannerScreen listens for scanned data, and once received, it triggers the event function to update the UI accordingly.

Best Practices for Intent Output

When configuring Intent Output in DataWedge, follow these best practices:

  1. Use Broadcast Intents for seamless communication between DataWedge and your app, especially for background operations.
  2. Ensure the Intent Action used in DataWedge is unique to your app to avoid conflicts with other apps.
  3. Check if your Zebra device’s DataWedge version supports the features you need (newer versions offer enhanced scanning capabilities).

Linking Your App to the DataWedge Profile

Finally, ensure your app is linked to the DataWedge profile. In DataWedge:

  • Go to the Applications section.
  • Click Associated Apps.
  • Select your application from the list.

This ensures DataWedge will deliver scan data to your app through the profile you configured.

Conclusion

Integrating Zebra’s DataWedge with your Android application is a simple and effective way to add barcode scanning functionality without directly managing scanner hardware.

By setting up a DataWedge profile, configuring intents, and using a BroadcastReceiver to capture the data, your app can easily process scanned data.

Whether you’re building a barcode scanning app or adding scanning to an existing one, DataWedge minimizes development complexity, offering flexibility with intent-based communication.


Written by @bhoratiu