Integrate the Scandit Barcode Scanner into your app

To integrate the Scandit SDK into your Xamarin.Android app, follow the simple steps below.

Get the Scandit Barcode Scanner

The Scandit Barcode Scanner is available as the Scandi.BarcodePicker.Xamarin NuGet package and can easily be installed via NuGet.org. For details on how to add a NuGet package to your project, take a look at the guides for Xamarin Studio, or Visual Studio.

Grant camera access permissions to your app

If not already done, grant your application the right to access the camera. You will need to open the project settings and go to Build->AndroidApplication where you can find a list of "Required permissions". Look for "Camera" and select it if it is not yet selected.

Add the barcode scanner namespace to easily access its classes

If you don't want to type the whole namespace each time when using the scanner you should add a using directive at the top of the class you will use the scanner in:

Prepare class to receive scanning events

To receive events from the Scandit Barcode Scanner SDK you have to implement the OnScanListener interface. This requires the following steps:

Specify that your class implements the IOnScanListener interface.

If your class is an activity called DemoActivity, it looks something like this:

public class DemoActivity : Activity, IOnScanListener {
}


Implement callback methods

Add the following callback methods to the class (as defined by the interface):

public void DidScan(IScanSession session) {
// This callback is called whenever a barcode is decoded.
}

For details on how to use the barcode data returned by the scan session consult the IOnScanListener documentation or take a look at one of the included samples.


Instantiate and configure the barcode picker

The scanning process is managed by the BarcodePicker. Before instantiating the picker, you will have to set your Scandit Barcode Scanner application key. The key is available from your Scandit Barcode Scanner SDK account at http://account.scandit.com in the downloads section. The barcode scanning is configured through an instance of ScanSettings that you pass to the BarcodePicker constructor.

// Set your app key
ScanditLicense.AppKey("-- ENTER YOUR SCANDIT LICENSE KEY HERE --");
ScanSettings settings = ScanSettings.Create();
settings.SetSymbologyEnabled(Barcode.SymbologyEan13, true);
settings.SetSymbologyEnabled(Barcode.SymbologyUpca, true);
// Instantiate the barcode picker by using the settings defined above.
BarcodePicker picker = new BarcodePicker(this, settings);
// Set the on scan listener to receive barcode scan events.
picker.SetOnScanListener(this);


Show the scan UI

Show the scanner to the user. The easiest way to do so is by setting it as the content view of your activity:

SetContentView(picker);

For more information on the different ways to add the barcode picker to your view hierarchy, consult Show the scanner.

Start the scanner

Starting and stopping should normally happen in the OnPause() and OnResume() methods of the same activity or fragment that contains the picker.

private BarcodePicker picker;
protected override void OnResume() {
picker.StartScanning();
base.OnResume();
}
protected override void OnPause() {
picker.StopScanning();
base.OnPause();
}

It does not matter whether the picker is the root view of the activity or just a subview in the hierarchy. The camera resource needs to be freed before the activity ends or goes into the background, so stopping the picker in OnPause() is a necessity. If it is not stopped your app will keep a lock on the camera which prevents any other app from opening the camera itself.


Run the project

Run your app on your Android device.


Next steps