Back to Demos List

GSW Zebra Scanner

This demonstration will show the ability to access Zebra scanner from the soft button in the browser.




DataMatrix Format Legend

This is the expected format of the DataMatrix barcode using GS1 Application Identifiers:

Example:
010123456789012810LOT123␟1712312123456789
(␟ represents the invisible ASCII Group Separator (GS or \x1D) character after variable-length fields)







Debug Output


        

View JavaScript Source

Click to expand
        
        document.addEventListener('gswscannerready', function () {
            console.log('Scanner ready');
            navigator.gswzebrascanner.initialize(function(data) {
                console.log('initialize OK');
                document.getElementById('scan_now').addEventListener("touchstart", GSW.Tests.scanPressed);
                document.getElementById('scan_now').addEventListener("touchend", GSW.Tests.scanReleased);
                document.getElementById('scan_now').addEventListener("touchmove", GSW.Tests.scanReleased);
                document.getElementById('scan_now').addEventListener("touchcancel", GSW.Tests.scanReleased);
                GSW.Tests.disable();
            }, function(error) {
                if(error === "Zebra scanner decode service is unavailable on this device") {
                    document.getElementById("noticeContainer").style.display = "block";
                    document.getElementById("error").innerHTML = 
                        "This device was not manufactured by Zebra and is not compatible with this demo.";
                } else {
                    console.error("Initialize error:", error);
                }
            });
        });
        
        GSW.Tests.scanPressed = function() {
            navigator.gswzebrascanner.enableTrigger(function(triggerResult) {
                navigator.gswzebrascanner.softwareTriggerStart(function(result) {
                    if (!result || !result.data) {
                        document.getElementById('rawDataDump').textContent = "No data received from scanner.";
                        return;
                    }
        
                    const rawData = result.data;
                    const debugDump = {
                        raw: rawData,
                        character_set: result.character_set,
                        code_id: result.code_id,
                        aim_id: result.aim_id,
                        timestamp: result.timestamp,
                        label_type: result.label_type,
                        code_type: result.code_type
                    };
                    document.getElementById('rawDataDump').textContent = JSON.stringify(debugDump, null, 2);
        
                    const regex = /^01(.{13})\d10(.+)\x1D17(.{6})21(.+)$/;
                    const match = rawData.match(regex);
        
                    if (match) {
                        document.getElementById('gtin').value = match[1];
                        document.getElementById('customer').value = match[2];
                        document.getElementById('date').value = match[3];
                        document.getElementById('serial').value = match[4];
                    } else {
                        console.warn("Regex did not match rawData:", rawData);
                    }
        
                }, function(error) {
                    document.getElementById('rawDataDump').textContent = "Error starting software trigger: " + error;
                });
            }, function(error) {
                document.getElementById('rawDataDump').textContent = "Error enabling trigger: " + error;
            });
        };
        
        GSW.Tests.scanReleased = function() {
            navigator.gswzebrascanner.disableTrigger(function(result) {
                navigator.gswzebrascanner.softwareTriggerStop();
            }, function(error) {
                console.log('Unable to disable trigger: ' + error);
            });
        };
        
        GSW.Tests.disable = function() {
            navigator.gswzebrascanner.disableTrigger(function(result) {
                console.log('Trigger disabled');
            }, function(error) {
                console.log('Unable to disable trigger: ' + error);
            });
        };