Skip to main content
Version: 8.2.2

barcodeScanner

The sos.hardware.barcodeScanner API provides methods for working with barcode scanners. It allows starting and stopping the scanner, as well as listening for scanned data and errors.

note

This API is experimental and may change in the future.

Device Barcode Scanner Capabilities
CapabilityDescription
BARCODE_SCANNERIf device supports serial communication for Barcode Scanners

If you want to check if the device supports this capability, use sos.display.supports().

Methods

getVersion()

The getVersion() method returns the version of the barcode scanner.

getVersion(): Promise<string>;

Return value

Returns a promise that resolves to the version of the barcode scanner.

Possible errors

If the version cannot be retrieved.

start()

The start() starts the barcode scanner and starts listening for scanned data.

start(userOptions?: Omit<IBarcodeScannerOptions, 'scannerId'>): Promise<IBarcodeScannerResponse>;

Params

NameTypeRequiredDescription
userOptionsOmit<IBarcodeScannerOptions, "scannerId">
No
User options to configure the scanner.
userOptions.timeoutnumber
No
The maximum time to wait for a scan before timing out.
userOptions.cancelPreviousboolean
No
If set to true, it will cancel any previous scanner instance with the same scannerId.

Return value

Returns a promise that resolves to an object with methods to stop the scanner and listen for scanned data and errors.

Possible errors

  • If the scanner cannot be started.
  • If the scanner is already running and cancelPrevious option is not set to true.
  • If any other error occurs while starting the scanner.

Example

// Start the barcode scanner with default options
const scanner = await sos.hardware.barcodeScanner.start();
// Listen for scanned data
scanner.onData((data) => {
console.log(`Scanned data: ${data}`);
});
// Listen for errors
scanner.onError((error) => {
console.error(`Scanner error: ${error.message}`);
});