Skip to main content

Scanner service

The scanner service provides an implementation for barcode and QR code scanning. It follows a subscription pattern where the platform can start/stop scanning and listen for scanned codes.

Registration​

export default async context => {
const listeners = [];

context.service.addService({
type: 'scannerService',
start: async () => {
// Initialize your scanner hardware
},
stop: async () => {
// Release scanner resources
},
onCodeScanned: callback => {
listeners.push(callback);
// Return a cleanup function to unsubscribe
return () => {
listeners.splice(listeners.indexOf(callback), 1);
};
},
});
};

Methods​

start()​

Called when the platform wants to activate the scanner. Use this to initialize your scanner hardware or begin listening for scan input.

stop()​

Called when the platform wants to deactivate the scanner. Use this to release resources or stop listening for scan input.

onCodeScanned(callback)​

Registers a listener that should be called whenever a code is scanned. The callback receives an object with a code property containing the scanned value.

Returns a cleanup function that removes the listener when called.

Dispatching scanned codes​

When your scanner hardware detects a code, notify all registered listeners:

function onScanDetected(code) {
for (const listener of listeners) {
listener({ code });
}
}