Scanning
Point, scan, done. Let's turn those black and white lines into data.
Getting the Scanner Adapter
First, grab the scanner adapter from your connected device:
import com.averydennison.addevicemanager.adapters.ScannerAdapter;
import com.averydennison.addevicemanager.callbacks.ScanCallback;
import com.averydennison.addevicemanager.models.ScannedData;
import com.averydennison.addevicemanager.models.ScannerSymbology;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final Logger logger = LoggerFactory.getLogger(getClass());
// Get scanner adapter (throws exception if device doesn't support scanning)
ScannerAdapter scannerAdapter = deviceAdapter.getScannerAdapter();
Note: Not all devices have scanners. If
getScannerAdapter()throws an exception, the connected device doesn't support scanning.
Receiving Scan Data
The scanner works on callbacks. Register once, receive every scan:
scannerAdapter.setScanCallback(new ScanCallback() {
@Override
public void onScanSuccess(ScannedData scannedData) {
logger.info("Scanned: {} (type: {})", scannedData.data, scannedData.symbology);
// Do something with the data
processBarcode(scannedData.data);
}
@Override
public void onScanFailure(String errorMessage) {
logger.warn("Scan failed: {}", errorMessage);
// Show a "try again" message
}
});
The ScannedData object provides:
| Property | Description |
|---|---|
data | The actual barcode content (the string of numbers/letters) |
symbology | What type of barcode it was (EAN-13, Code 128, QR, etc.) |
Triggering a Scan
Two ways to initiate a scan:
Option 1: Physical Button
The user presses the scan button on the Pathfinder Edge device. Your callback fires automatically.
Option 2: Programmatically
Trigger a scan from your app:
scannerAdapter.triggerScan()
.thenRun(() -> logger.debug("Scan triggered"))
.exceptionally(error -> {
logger.error("Couldn't trigger scan: {}", error.getMessage());
return null;
});
Stopping a Scan
If you're in continuous scan mode or need to cancel:
scannerAdapter.stopScan()
.thenRun(() -> logger.debug("Scan stopped"))
.exceptionally(error -> {
logger.error("Couldn't stop scan: {}", error.getMessage());
return null;
});
Configuring Symbologies
Symbologies are barcode types. By default, the scanner recognizes common formats. You might want to:
- Narrow it down: Only accept EAN-13 barcodes (faster, fewer false reads)
- Expand it: Enable 2D codes like QR or DataMatrix
Supported Symbologies
Supported symbologies are defined in the enum Symbology.
Retrieving and Configuring Symbologies
Fetching and updating symbology configuration is done asynchronously. First, get the current settings, then modify and send them back:
// retrieve current symbology settings
scannerAdapter.getSymbologySettings()
.thenApply(symbologySettings -> {
// symbology settings retrieved successfully, we can now access individual symbology configs
SymbologyConfig symbologyConfig = symbologySettings.getSymbology(Symbology.QR_CODE);
QrCodeConfig qrConfig = (QrCodeConfig) symbologyConfig;
// Symbology config fetched; now we can get or set its properties
qrConfig.setEnabled(false); // disable QR Code symbology
symbologyConfig = symbologySettings.getSymbology(Symbology.EAN_13);
Ean13Config ean13Config = (Ean13Config) symbologyConfig;
ean13Config.setEnabled(true); // enable EAN-13 symbology
ean13Config.setTransmitCheckDigit(false); // update other properties as needed
return symbologySettings;
})
.thenApply(updatedSettings -> {
// send updated settings back to the scanner
return scannerAdapter.setSymbologySettings(updatedSettings);
})
.exceptionally(throwable -> {
// handle any errors that occurred during the process
logger.error("Failed to update symbology settings", throwable);
return null;
});
Best Practices
1. Set Up Callbacks Early
Register your ScanCallback as soon as you have the adapter. Don't wait until the user tries to scan.
private void setupScanner() {
try {
scannerAdapter = deviceAdapter.getScannerAdapter();
scannerAdapter.setScanCallback(scanCallback);
logger.info("Scanner ready");
} catch (Exception e) {
logger.warn("Device doesn't support scanning");
}
}
2. Handle Both Success and Failure
Always implement onScanFailure. Scans fail (barcode damaged, moved too fast, wrong angle). Give users feedback.
@Override
public void onScanFailure(String errorMessage) {
runOnUiThread(() -> {
// Visual feedback
showErrorAnimation();
showToast("Couldn't read barcode. Try again.");
});
logger.warn("Scan failed: {}", errorMessage);
}
Troubleshooting
| Problem | Solution |
|---|---|
| Scanner doesn't respond | Is the device connected? Check with ConnectionStateCallback. Does this device have a scanner? |
| Barcode not recognized | Is that symbology enabled? Check your configuration. Is the barcode damaged or poorly printed? Try different angles/distances. |
| Getting wrong barcode type | Some barcodes look similar (UPC-A and EAN-13 are related). If strict type matching is needed, check scannedData.symbology. |
| Scan triggers but no callback | Ensure setScanCallback() was called. Check that callback isn't being overwritten. |
| Slow scan response | Reduce enabled symbologies. Only enable what you need. |
Next Steps
| Guide | What You'll Learn |
|---|---|
| Printing | Print labels with scanned data |
| Callbacks | Monitor scan events |
| Logging | Debug scanning issues |
| Getting Started | Connection basics |