Barcodes: from the field to the IBM i thanks to NTi and .NET

04/04/2024

Rémi Rouillot

IBM i News

In this article, I'm going to show you how to quickly and easily develop an Android mobile application implementing a connection to the IBM i using .NET and NTi. This app will scan barcodes and insert them into a table on the IBM i.

Introduction

In this article, I'm going to show you how to quickly and easily develop an Android mobile application implementing a connection to the IBM i using .NET and NTi. This app will scan barcodes and insert them into a table in the IBM i.

Step 1/3: Create the IBM i table

First, we'll create a table to store the barcodes that will be scanned. I want to store the data scanned, the scan date and the type of barcode scanned. The table creation SQL is as follows:

CREATE TABLE SAMPLE.BARCODES (SCANDATE TIMESTAMP, SCANTYPE VARCHAR(256), SCANVALUE VARCHAR(1024))

We now have a BARCODES table in the SAMPLE library to store everything.

Step 2/3: Developing the mobile application

Let's create the mobile application using a .NET MAUI project template.

Using the camera to read barcodes:

To exploit the smartphone's camera, I'm using one of the libraries available for reading barcodes, in this case ZXing. This component is very simple to use, directly in the view:

<zxing:CameraBarcodeReaderView
    x:Name="cameraBarcodeReaderView"
    BarcodesDetected="BarcodesDetected"/>

We then intercept the data read from the underlying code via the BarcodesDetected method (see the ZXing doc).

Don't forget to specify in the application manifest that you wish to access the phone's camera. Otherwise, the necessary authorizations will not be granted.

Data processing:

We now have a working barcode scanner. We now need to process the data by inserting it into the IBM i. To do this, all we need to do is use NTi and Dapper in the BarcodesDetected method to perform our insertion SQL query:

protected void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
{
    Vibration.Default.Vibrate();
    NTiConnection conn = new(_connectionString);
    conn.Open();
    foreach (var barcode in e.Results)
    {
        conn.Execute(
            "INSERT INTO SAMPLE.BARCODES (SCANDATE, SCANTYPE, SCANVALUE) VALUES (@TIME, @TYPE, @VALUE)",
            new
            {
                Time = DateTime.Now,
                Type = barcode.Format.ToString(),
                Value = barcode.Value
            }
        );
    }
    conn.Close();
}

A vibration is triggered on the smartphone to confirm the reading to the user.

Code summary:

For those wishing to reproduce this example, the final code for the view (MainPage.xaml) is:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
             x:Class="BarcodeAppXaml.MainPage">
    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Image
                Source="nti.png"
                HeightRequest="185"
                Aspect="AspectFit"/>
            <Label
                Text="NTi - Scan"
                Style="{StaticResource Headline}" />
            <Label
                Text="Exemple d'application permettant de scanner des QR codes et des codes à barres, et d'envoyer les données dans l'IBM i avec NTi"
                Style="{StaticResource SubHeadline}"/>
            <zxing:CameraBarcodeReaderView
                x:Name="cameraBarcodeReaderView"
                BarcodesDetected="BarcodesDetected"/>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

And the underlying code (MainPage.xaml.cs):

using ZXing.Net.Maui;
using Aumerial.Data.Nti;
using Dapper;

namespace BarcodeAppXaml
{
    public partial class MainPage : ContentPage
    {
        private static string _connectionString = "...";
        public MainPage()
        {
            InitializeComponent();
            cameraBarcodeReaderView.Options = new BarcodeReaderOptions
            {
                Formats = BarcodeFormats.All,
                AutoRotate = true,
                Multiple = true
            };
        }

        protected void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
        {
            Vibration.Default.Vibrate();
            NTiConnection conn = new(_connectionString);
            conn.Open();
            foreach (var barcode in e.Results)
            {
                conn.Execute(
                    "INSERT INTO SAMPLE.BARCODES (SCANDATE, SCANTYPE, SCANVALUE) VALUES (@TIME, @TYPE, @VALUE)",
                    new
                    {
                        Time = DateTime.Now,
                        Type = barcode.Format.ToString(),
                        Value = barcode.Value
                    }
                );
            }
            conn.Close();
        }
    }
}

All that remains is to publish our application and test it on a smartphone.

Step 3/3: Publishing and testing

Thanks to Visual Studio, we can publish our application directly as an APK archive that can be installed on any Android device (smartphone, warehouse handheld, tablet, etc.).

💡Note: You need to be able to establish a connection between the smartphone and the IBM i via a VPN, for example. Without this, it would be necessary to develop a policy of offline input and then synchronization once the link has been regained.

Also, NTi is totally platform agnostic. So I can use it in an Android/ARM environment without any problem and without having to install a driver. I could do the same for iOS if I'd chosen an iPhone for this scenario.

So I tested by installing the app on my phone:

article 7

The app works as expected and tries to scan barcodes. I find a package of apples with an EAN8 code and scan it:

article 7

I felt the vibration of my phone, and if I go to check in the IBM i, I find the EAN8 data I just scanned on my apple box:

article 7

Conclusion

In this article, I've presented an example of a use case where NTi becomes the link between the field and the IBM i via a smartphone. It's easy to imagine this kind of application on a handheld shower in a warehouse, or on the smartphone of a maintenance technician scanning barcodes to feed traceability information back into the IBM i.

In fact, all acquisition devices can be used with the IBM i: cameras, laser scanners, Bluetooth peripherals....

Don't hesitate to contact us to try out NTi!

Rémi Rouillot

Back