Use the Scandit Scan Case

This guide show how to scan barcodes with the Scandit scan case using the Titanium scan case API.

Using the scan case API is simple: all you need to do is to create an scan case object and implement a few callbacks.

Creating an instance of scan case

In order to create an instance of ScanCase, you first need to set the license key (if you haven't already), and enable the symbologies you need.

var scanditsdk = require("com.mirasense.scanditsdk");
scanditsdk.appKey = "-- ENTER YOUR SCANDIT LICENSE KEY HERE --";
var scanCase = scanditsdk.createScanCase({
scanSettings : {
symbologies : ["ean13", "upca", "qr", "data-matrix"]
},
didInitialize : function() {
// see below
},
didChangeState : function(event) {
// see below
},
didScan : function(session) {
// see below
}
});
scanCase.initialize();

For a description of available options, take a look at the symbology settings documentation.

Scanning with scan case is driven by it's state property. The scan case initially starts in 'standby' state and can be set to one of the following three states:

  • 'off': camera is off, torch is off.
  • 'stanby': camera is on but with throttled frame-rate, scanner is off, torch is off.
  • 'active': camera is on, scanner is on, torch is on.

In order to start scanning, you just need to change the state to 'active':

scanCase.state = 'active';

Implementing the scan case callbacks

The scan case supports 3 different callbacks. Typically you provide implementations for these callbacks when creating the scan case. It's however also possible to provide the callbacks by setting the properties directly. Let's check them one by one.

The first method is called when the scan case finished the initialization process and it's ready to be used.

scanCase.didInitialize = function() {
// scan case is ready to be used.
};

Whenever a new code is scanned the didScan callback is invoked. You can switch to another state by returning a the new desired state from the callback. If you want to continue scanning codes, return 'active' (the default), if you want to temporarily pause the scanner return 'standby'. You can also return 'off' if you don't plan to scan new codes relatively soon.

Please note that changing from 'off' to 'active' takes more time than switching from 'standby' to 'active'.

scanCase.didScan = function(session) {
var firstCode = session.newlyRecognizedCodes[0];
console.log(firstCode.data, firstCode.symbology);
return 'standby';
};

Whenever the state of scan case changes the following method is called. Take a look at the ScanCase.didChangeState documentation for details.

scanCase.didChangeState = function(event) {
};

Enabling the volume button to start scanning

It is possible to use the volume button to scan codes. Keeping the volume button pressed will keep the scan case in 'active' state, while releasing the button will change the state to 'standby'. This feature is disabled by default. To enable this feature, all you need to do is:

scanCase.volumeButtonToScanEnabled = true;

You know when the state changes, because of the volume button, in ScanCase.didChangeState, the reason will be 'volumeButton'.

Using timeouts to switch state

It is possible to switch from one state to another one automatically after a specific timeout. This could be useful, for instance, to switch the scanner off after a long time of inactivity in order to save power.

The following code changes the state from 'standby' to 'off' after approximately 20 seconds.

scanCase.setTimeout('standby', 'off', 20.0);

You know when the state changes because of a timeout in ScanCase.didChangeState, the reason will be 'timeout'.