HardwareBarcode

JourneyApps supports the use of certain devices with hardware barcode scanning capabilities.

Device Compatibility

  • Hardware barcode scanning is currently only supported on Unitech PA700 devices. Please contact JourneyApps Support if you are considering other devices for hardware barcode scanning.

  • This feature is currently only available for customers on the Enterprise plan.

Hardware barcode scanning is managed entirely through JavaScript/TypeScript and does not require any specific view components or data types.

Device Capabilities

The HardwareBarcode.getCapabilities method returns details about the hardware barcode scanning capabilities of the current device. The return object contains the following properties:

PropertyTypeDetails

description

string

Description of hardware barcode scanner.

supported

boolean

If hardware barcode scanning is supported on the current device.

softwareTriggerSupported

boolean

If software triggering of hardware barcode scanner is supported on the current device.

Example JavaScript:

var capabilities = HardwareBarcode.getCapabilities();
console.log("Scanner capabilities: " + JSON.stringify(capabilities));
if (capabilities.supported) {
    console.log("Hardware barcode scanning supported")
}
/**
* Sample output:
* > Scanner capabilities: {"description":"Unitech Hardware Barcode Scanner","supported":true,"softwareTriggerSupported":true}
* > Hardware barcode scanning supported
*/

Hardware Barcode Scanner Registration

If hardware barcode scanning is supported on the current device, then the scanning service can be started and a callback function registered.

The HardwareBarcode.register method must be called with the callback function to register as the scanning handler. Only a single callback function can be registered. This must be performed before scanning is attempted.

In the example JavaScript below, the scanning service is started and the onScan function registered as a callback:

/**
* Callback function for hardware barcode scanning.
*/
function onScan(barcode) {
    var base64 = barcode.toBase64();
    console.log("base64: " + base64);
}

function init() {
    HardwareBarcode.register(onScan);
}

The onScan function from the example will be called each time that a barcode is scanned with the barcode data as parameter. The barcode object contains the following methods:

MethodReturn TypeDetails

toBase64

string

Returns the barcode data as a base64-encoded string.

toArrayBuffer

ArrayBuffer

Returns the raw binary data of the barcode.

The hardware barcode scanning service should be stopped when no longer needed as shown in the example JavaScript:

HardwareBarcode.deregister();

This will remove the registered scanning handler function.

The callback is automatically deregistered when navigating away from the view.

Software Triggering

If the device supports software triggering then the hardware barcode scanner can be triggered manually as shown in the example JavaScript:

HardwareBarcode.trigger();

Software triggering can be used in place of a dedicated hardware scan button on the device. The scanning handler function is invoked in the same way regardless of triggering method.

Complete Example

The example below demonstrates the usage of the hardware barcode scanning functionality. The device capabilities are checked and if hardware barcode scanning is supported, then the scanner is registered when the view is loaded. The barcode data will be displayed as a Base64-encoded string when the hardware scan button is pressed.

View JavaScript:

function init() {
    autoRegisterScanner();
}

function resume(from) {
    autoRegisterScanner(); // scanner must be registered again when view is resumed
}

function onScan(barcode) {
    console.log("onScan");
    var base64 = barcode.toBase64();
    console.log("base64: " + base64);
    dialog("Barcode Base64", base64);
}

function registerScanner() {
    console.log("Registering scanner");
    HardwareBarcode.register(onScan);
}

function autoRegisterScanner() {
    console.log("Determining if scanner should be registered");
    if (typeof HardwareBarcode != 'undefined' && HardwareBarcode.getCapabilities().supported) {
        registerScanner();
    } else {
        console.log("Scanner not supported");
    }
}

Last updated