No onData/onStatus when writing a Module for React Native

S Stanislav Izotov 3 years 6 months ago
69 1 0

I'm following the Basic Tutorial but don't get any data feedback at all. What am I doing wrong?
 
I tried a lot of test projects and ways but can't get it to work. This is the most basic test from react-native for writing a module and the test from your documentation for basic scanning. Also, calling AsyncTask kills and reloads react-native. So dunno what to do about that. Can probably just run them in this thread.
 
package com.somescanner.SHEScanner;   import android.app.AlertDialog; import android.content.DialogInterface; import android.widget.Toast;   import com.symbol.emdk.EMDKManager; import com.symbol.emdk.EMDKManager.EMDKListener; import com.symbol.emdk.EMDKResults; import com.symbol.emdk.barcode.BarcodeManager; import com.symbol.emdk.barcode.ScanDataCollection; import com.symbol.emdk.barcode.Scanner; import com.symbol.emdk.barcode.Scanner.DataListener; import com.symbol.emdk.barcode.Scanner.StatusListener; import com.symbol.emdk.barcode.ScannerException; import com.symbol.emdk.barcode.ScannerResults; import com.symbol.emdk.barcode.StatusData;   import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod;   import java.util.ArrayList; import java.util.Map; import java.util.HashMap;   public class SHEScannerModule extends ReactContextBaseJavaModule implements EMDKListener, StatusListener, DataListener {       private EMDKManager emdkManager = null;     private BarcodeManager barcodeManager = null;     private Scanner scanner = null;       private static final String DURATION_SHORT_KEY = "SHORT";     private static final String DURATION_LONG_KEY = "LONG";       public SHEScannerModule(ReactApplicationContext reactContext) {         super(reactContext);     }       @Override     public String getName() {         return "ToastExample";     }       @Override     public void initialize() {         Toast.makeText(getReactApplicationContext(), "initialize", Toast.LENGTH_SHORT).show();         EMDKResults results = EMDKManager.getEMDKManager(                 getReactApplicationContext(), this);     }       @Override     public Map getConstants() {         final Map constants = new HashMap();         constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);         constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);         return constants;     }       @ReactMethod     public void show(String message, int duration) {         Toast.makeText(getReactApplicationContext(), message, duration).show();     }       @Override     public void onClosed() {         // TODO Auto-generated method stub     }       @Override     public void onOpened(EMDKManager emdkManager) {         this.emdkManager = emdkManager;           try {             // Call this method to enable Scanner and its listeners             initializeScanner();         } catch (ScannerException e) {             e.printStackTrace();         }           // Toast to indicate that the user can now start scanning         Toast.makeText(getReactApplicationContext(),                 "Press Hard Scan Button to start scanning...",                 Toast.LENGTH_SHORT).show();     }       @Override     public void onData(ScanDataCollection scanDataCollection) {         Toast.makeText(getReactApplicationContext(), "Got Data", Toast.LENGTH_SHORT).show();     }       @Override     public void onStatus(StatusData statusData) {         Toast.makeText(getReactApplicationContext(), "onStatus", Toast.LENGTH_SHORT).show();     }       // Method to initialize and enable Scanner and its listeners     private void initializeScanner() throws ScannerException {         if (scanner == null) {             // Get the Barcode Manager object             barcodeManager = (BarcodeManager) this.emdkManager                     .getInstance(EMDKManager.FEATURE_TYPE.BARCODE);             // Get default scanner defined on the device             scanner = barcodeManager.getDevice(BarcodeManager.DeviceIdentifier.DEFAULT);             // Add data and status listeners             scanner.addDataListener(this);             scanner.addStatusListener(this);             // Hard trigger. When this mode is set, the user has to manually             // press the trigger on the device after issuing the read call.             scanner.triggerType = Scanner.TriggerType.HARD;             // Enable the scanner             scanner.enable();             // Starts an asynchronous Scan. The method will not turn ON the             // scanner. It will, however, put the scanner in a state in which             // the scanner can be turned ON either by pressing a hardware             // trigger or can be turned ON automatically.             scanner.read();         }     } }

Please Register or Login to post a reply

1 Replies

Y Yanis Dalabiras

Is onOpened() ever getting called?  I suspect the lack of EMDKManager.getEMDKManager(...) is causing the issue: samples-emdkforandroid-6_6/MainActivity.java at BarcodeSample1 · Zebra/samples-emdkforandroid-6_6 · GitHub 

I have not used the Barcode API with ReactNative myself but I don't see why it shouldn't work as long as the EMDK library is referenced (which it must be or you'd get build errors) and you are running on a Zebra device.

CONTACT
Can’t find what you’re looking for?