scan(options)

Use this function to start scanning barcodes.

Syntax 

1import { getBarcodeScanner } from 'lightning/mobileCapabilities';
2scan(options: BarcodeScannerOptions): Promise<Object>

Parameters 

Returns 

A Promise object that resolves as an array of Barcode objects with the scanned barcode details.

A rejected promise returns a BarcodeScannerFailure.

Usage 

This function allocates scanner resources. To release the resources when scanning is complete, pair it with dismiss().

1myScanner
2  .scan(scanningOptions)
3  .then((resultsArray) => {
4    // Do something with the result of the scan
5    for (let singleResult in resultsArray) {
6      console.log(singleResult);
7      this.scannedBarcodes.push(singleResult.value);
8    }
9  })
10  .catch((error) => {
11    // Handle cancellation and scanning errors here
12    console.error(error);
13  })
14  .finally(() => {
15    myScanner.dismiss();
16  });

When this function is called, a mobile OS scanning user interface is displayed on the mobile device. The user can point their camera at a barcode and wait for the barcode to be recognized.

  • When a barcode is successfully scanned, the result is returned via a fulfilled promise. Handle successful results in a then clause.
  • When a barcode scan fails, the error is returned via a rejected promise. Handle errors in a catch clause.
  • When the user clicks the Cancel button during a scan, the promise is rejected with a BarcodeScannerFailure.code value of userDismissedScanner.
  • The mobile OS scanner interface remains displayed after a successful scan or an error. To close the scanner interface and return to your component’s interface, call dismiss().
  • Handle any final cleanup, such as calling dismiss(), in a finally clause.

See Also