🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Barcode - on Android - with Xamarin and MDriven
This page was created by Alexandra on 2017-04-29. Last edited by Wikiadmin on 2026-07-29.

You can use this sample to scan a shelf and its items on an Android device, build a structured request from an MDriven ViewModel, and let MDrivenServer create or match the corresponding server-side objects. It is for developers extending an MDriven Xamarin application with barcode capture.

What the sample demonstrates

The sample models a small storage-registration workflow:

  1. Scan a shelf barcode.
  2. Scan one or more item barcodes for that shelf.
  3. Build JSON from the ViewModel state.
  4. Send the JSON to the server.
  5. Interpret the JSON as transient objects and match or create persistent shelf and item objects.

For example, scanning shelf ABC, then items 123 and 234, produces a request conceptually equivalent to:

{
  "Barcode": "ABC",
  "FoundShelfAboveItems": [
    { "Barcode": "123" },
    { "Barcode": "234" }
  ]
}

The exact JSON member names are defined by the ViewModel JSON configuration. Treat the example as the shape of the request, not as a fixed integration contract.

Get the sample

The Android barcode sample is included in the MDriven official samples repository. The referenced sample folder is Barcocde in MDrivenTurnkey.

The repository location and the referenced revision are historical. Review the repository structure and the dependency versions before using the sample in a current application.

Design the model first

Model and test the business workflow before implementing the phone user interface. The sample uses a ViewModel named BuildStorage and a temporary, or transient, scanned-shelf structure.

Element Purpose in the sample Example
FoundShelf Holds the barcode scanned for the shelf during the phone session. ABC
FoundShelfAboveItems Holds the item barcodes scanned under the current shelf. 123, 234
BuildStorage ViewModel Provides the actions, variables, nesting, and JSON representation used by the client. Scan shelf, scan item, and sync
JSON ViewModel configuration Selects the root object and the attributes and associations to serialize. Shelf barcode plus its scanned item barcodes

Keep the business rules in the model as OCL expressions. For instance, rules that decide whether a barcode identifies an existing shelf or whether an item may be added belong in the model, where they can be tested before the Android UI is written.

Prototype the workflow in MDriven Designer

Use the ViewModel UI to verify the workflow with simulated input:

  1. Open the BuildStorage ViewModel.
  2. Run the action that opens the scanner ViewModel.
  3. Enter a test scan result such as ABC and confirm it. The confirmation creates a FoundShelf and assigns its barcode.
  4. Repeat the scan flow for items, using values such as 123 and 234.
  5. Run the sync action and inspect the JSON variable used for debugging.
  6. Confirm that the generated JSON contains the shelf and its nested items before building the Android page.

This separates model and integration errors from camera and device concerns.

Build the Xamarin.Forms page

MDriven generates the ViewModel code, but the Xamarin.Forms user interface is authored in the mobile project. The sample uses a portable class library that contains the model and generated ViewModels; the generated code can be used from Android and iOS projects.

  1. Add a Xamarin.Forms page for the storage workflow, such as BuildStoragePage.
  2. Obtain the application's EcoSpace and create the generated BuildStorage ViewModel.
  3. Set that ViewModel as the page binding context.
  4. Add controls bound to the three actions: scan shelf, scan item, and sync.
  5. Bind an item list to the ViewModel association that holds scanned items.
  6. Bind each row's label to the item barcode attribute.
  7. During development, bind a label or editor to the JSON-to-send variable so that you can inspect the outgoing request. Remove this diagnostic UI before release.

The important binding path follows the ViewModel nesting. In this sample, the item list follows FoundShelfAboveItems; a row displays the nested item's Barcode.

Implement the scan actions

The sample opens a ZXing scanner page from each scan action and waits for the scanner result. When scanning finishes, the page closes and the application uses the returned barcode value to update the generated ViewModel objects.

Action Client-side result Example
Scan shelf Create or assign the current FoundShelf and set its Barcode from the scanner result. Scan result ABC becomes the shelf barcode.
Scan item Add a scanned item below the current found shelf and set its barcode. Scan result 123 becomes an item below shelf ABC.
Sync Serialize the configured ViewModel root as JSON and post it to the server route. Send shelf ABC with item barcodes 123 and 234.

The sample uses ZXing for barcode scanning and Newtonsoft.Json in the mobile application. Camera permissions, platform setup, scanner-page configuration, and package versions are outside the MDriven model and must be configured in the Xamarin application.

Configure the server request

The sync action is backed by an MDriven action that obtains JSON from the ViewModel. Its JSON configuration uses the found shelf as the root and includes the shelf's barcode and the barcodes of its nested items. The action posts this representation to a server route.

Do not hard-code a desktop-local server address in a phone build. A device must be able to reach the configured MDrivenServer endpoint. The walkthrough notes that a local endpoint was unsuitable for the device because of HTTP/certificate concerns; configure a reachable endpoint and validate its transport and certificate configuration for your environment.

Process the request on the server

On the server, receive the POST body, assign it to a variable, and convert the JSON to objects. The sample interprets the request into transient shelf and item objects first. These objects exist in memory for processing; they are not the persistent inventory records.

Then apply the matching and creation rules:

  1. Find an existing shelf whose barcode matches the transient shelf barcode.
  2. If a match exists, associate the transient shelf with that persistent shelf.
  3. If no shelf matches, create a persistent shelf and assign the scanned barcode.
  4. For every transient item, find a persistent item with the same barcode.
  5. Associate matching items with their persistent objects.
  6. For each item with no match, create the persistent item and assign its barcode to the selected shelf.

For the earlier example, the server receives shelf ABC and items 123 and 234. It reuses existing objects when their barcodes match and creates only the shelf or items that are missing. Adapt the matching, creation, and validation rules to your domain in the model's OCL.

Test before deploying

  1. Test the ViewModel workflow with simulated scan results in MDriven Designer.
  2. Inspect the JSON produced by sync and verify that the root and nested items are included.
  3. Send the request to a test server endpoint.
  4. Verify that existing barcode matches are reused and unmatched barcodes create the intended persistent objects.
  5. Test camera scanning and network access on an Android device.
  6. Remove diagnostic JSON display and test-only server-address settings from the production UI.

See also