Skip to main content
Version: 1.1.8.4

Callbacks

Things happen. The connection drops. The printer runs out of paper. A print job finishes. Callbacks let your app respond in real-time.


Available Callbacks​

CallbackTells You When...
ConnectionStateCallbackDevice connects, disconnects, or is connecting
PrinterStatusCallbackPrinter state changes (paper out, cover open, ready)
PrintProgressCallbackEach label in a multi-label job completes
ScanCallbackA barcode is scanned (covered in Scanning Guide)

ConnectionStateCallback​

Know instantly when your connection changes:

import com.averydennison.addevicemanager.callbacks.ConnectionStateCallback;
import com.averydennison.addevicemanager.connection.DeviceConnection;
import com.averydennison.addevicemanager.connection.DeviceConnectionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private final Logger logger = LoggerFactory.getLogger(getClass());

deviceAdapter.setConnectionStateCallback(new ConnectionStateCallback() {
@Override
public void onConnectionStateChanged(
DeviceConnection connection,
DeviceConnectionState newState,
String reason) {

switch (newState) {
case CONNECTING:
logger.info("Connecting to {}...", connection.getName());
runOnUiThread(() -> showStatus("Connecting..."));
break;

case CONNECTED:
logger.info("Connected to {}", connection.getName());
runOnUiThread(() -> {
showStatus("Connected");
enablePrintButton(true);
});
break;

case DISCONNECTING:
logger.info("Disconnecting...");
runOnUiThread(() -> showStatus("Disconnecting..."));
break;

case DISCONNECTED:
logger.info("Disconnected: {}", reason);
runOnUiThread(() -> {
showStatus("Disconnected");
enablePrintButton(false);
// Maybe attempt reconnection?
offerReconnect(reason);
});
break;
}
}
});

Connection States​

StateDescription
CONNECTINGConnection in progress
CONNECTEDReady to use
DISCONNECTINGGraceful disconnect in progress
DISCONNECTEDNot connected (check reason for why)

PrinterStatusCallback​

The printer tells you when something needs attention:

import com.averydennison.addevicemanager.callbacks.PrinterStatusCallback;
import com.averydennison.addevicemanager.models.PrinterStatus;

PrinterAdapter printerAdapter = deviceAdapter.getPrinterAdapter();

printerAdapter.setPrinterStatusCallback(new PrinterStatusCallback() {
@Override
public void onPrinterStatusChanged(PrinterStatus status) {
logger.info("Printer status: {}", status.getDescription());

// React based on code
if ("NORMAL".equalsIgnoreCase(status.rawCode)) {
// Printer is ready — enable print button
} else if ("PAPER_JAM".equalsIgnoreCase(status.rawCode)) {
// Alert user to clear jam
} else if ("OUT_OF_PAPER".equalsIgnoreCase(status.rawCode)) {
// Alert user to load paper
} // else if ... handle other statuses
}
});

PrintProgressCallback​

When printing multiple labels, track each one:

import com.averydennison.addevicemanager.callbacks.PrintProgressCallback;

printerAdapter.setPrintProgressCallback(new PrintProgressCallback() {
@Override
public void onPrintProgressUpdate(int currentLabel, int totalLabels) {
logger.debug("Printed {} of {}", currentLabel, totalLabels);

runOnUiThread(() -> {
// Update progress bar
int percent = (currentLabel * 100) / totalLabels;
progressBar.setProgress(percent);
progressText.setText(currentLabel + " / " + totalLabels);

// Check if done
if (currentLabel == totalLabels) {
showToast("All labels printed!");
hideProgressBar();
}
});
}
});

Then print multiple copies:

// Print 50 labels — progress callback fires after each one
printerAdapter.print("ShelfTag.ngt", "PRICE=9.99", 5);

Best Practices​

1. Register Callbacks Early​

Set up callbacks right after getting your adapters:

private void onDeviceConnected() {
printerAdapter = deviceAdapter.getPrinterAdapter();
scannerAdapter = deviceAdapter.getScannerAdapter();

// Set up callbacks immediately
deviceAdapter.setConnectionStateCallback(...);
printerAdapter.setPrinterStatusCallback(...);
printerAdapter.setPrintProgressCallback(...);
scannerAdapter.setScanCallback(...);
}

2. Always Update UI on Main Thread​

Callbacks may fire on background threads. Use runOnUiThread():

@Override
public void onPrinterStatusChanged(PrinterStatus status) {
// This might be on a background thread
runOnUiThread(() -> {
// UI updates here
statusText.setText(status.getDescription());
});
}

3. Handle All States​

Don't ignore edge cases:

@Override
public void onConnectionStateChanged(DeviceConnection conn, DeviceConnectionState state, String reason) {
switch (state) {
case CONNECTING:
showLoadingIndicator();
break;
case CONNECTED:
hideLoadingIndicator();
enableFeatures();
break;
case DISCONNECTING:
showLoadingIndicator();
disableFeatures();
break;
case DISCONNECTED:
hideLoadingIndicator();
handleDisconnect(reason);
break;
}
}

4. Log Callback Events​

Callbacks are great for debugging:

@Override
public void onConnectionStateChanged(DeviceConnection conn, DeviceConnectionState state, String reason) {
logger.info("Connection state: {} -> {} ({})", conn.getName(), state, reason);
// ... handle state
}

Troubleshooting​

ProblemSolution
Callback never firesEnsure you registered it before the event occurs
UI doesn't updateUse runOnUiThread() for UI changes
Callback stops workingCheck if adapter was recreated without re-registering callback
Missing status updatesVerify device is connected and supports the feature

Next Steps​

GuideWhat You'll Learn
Getting StartedBasic setup and connection
PrintingPrint labels
ScanningBarcode scanning callbacks
LoggingDebug callback issues
Error HandlingHandle failures from callbacks