EMDK initialization fail

// Expert user has replied.
M Martin Mrazik 3 years 6 months ago
246 2 0

Hi,
 
I have two activities using EMDK. In the first one I initialize EMDK in onCreate method, this works properly. In this first activity I have a button, that starts another activity like this:
 
public void btnDoneOnClick(View view) {
        Intent intent = new Intent(this, LocationUnitsListActivity.class);
        intent.putExtra(Constants.LocationKey, barcode);
        intent.putExtra(Constants.LocationOption, selectedOption);
        startActivity(intent);
        //finish();
    }
 
In onDestroy method of this first activity I do this:
 
@Override
    protected void onDestroy() {
        super.onDestroy();
        if (emdkManager != null) {
 
 
            // Clean up the objects created by EMDK manager
            emdkManager.release();
            emdkManager = null;
        }
    }
 
In the new started activity (the second one) I initialize EMDK again. No error occurs during initialization but barcode scanner stays inactive. So I tried to uncomment last line of the btnDonOnClick method. This made a little progress. Barcode reader is active now also in second activity but in this second activity listener's event DoScannerDataPostExecute is not fired. So the scanned barcodes goes to nowhere and I am not able to catch them in my activity.
 
Here is architecture of my application:
 
class EMDKBaseActivity{
     protected void initializeEMDK(){
          EMDKResults results = EMDKManager.getEMDKManager(
                getApplicationContext(), this);
 
             if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS) {
                 Toast t = Toast.makeText(this, "EMDKManager Request Failed", Toast.LENGTH_LONG);
                 t.show();
             }
     }
 
     public void onOpened(EMDKManager emdkManager){
      ....
     }
 
     public void onClosed(){
          ...
     }
 
     public void onStatus(StatusData sd){
          ...
     }
 
     public void onData(ScanDataCollection scanDataCollection){
          new AsyncScannerDataUpdate().execute(scanDataCollection);
     }
    
     protected void onStop(){
          ...
     }
 
     @Override
    protected void onDestroy() {
        super.onDestroy();
        if (emdkManager != null) {
 
 
            // Clean up the objects created by EMDK manager
            emdkManager.release();
            emdkManager = null;
        }
    }
}
 
    private class AsyncScannerDataUpdate extends AsyncTask {
 
 
        @Override
        protected ArrayList doInBackground(ScanDataCollection... params) {
            ArrayList result = new ArrayList();
 
 
            try {
                // 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();
 
 
                ScanDataCollection scanDataCollection = params[0];
 
 
                // The ScanDataCollection object gives scanning result and the
                // collection of ScanData. So check the data and its status
                if (scanDataCollection != null
                        && scanDataCollection.getResult() == ScannerResults.SUCCESS) {
 
 
                    ArrayList scanData = scanDataCollection
                            .getScanData();
 
 
                    // Iterate through scanned data and prepare the statusStr
                    for (ScanData data : scanData) {
                        // Get the scanned data
                        String barcodeData = data.getData();
                        result.add(barcodeData);
                    }
                }
 
 
            } catch (ScannerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 
 
            // Return result to populate on UI thread
            return result;
        }
 
 
        @Override
        protected void onPostExecute(ArrayList result) {
            DoScannerDataPostExecute(result);
        }
    }
 
 
class FirstActivity extends EMDKBaseActivity{
     public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
 
          ....
 
        initializeEMDK();
    }
 
@Override
 
    protected void DoScannerDataPostExecute(ArrayList result) {
          //this works correctly
    }
 
     public void btnDoneOnClick(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
        finish();
    }
}
 
class SecondActivity extends EMDKBaseActivity{
     public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
 
          ....
 
        initializeEMDK(); //no error occurs
    }
 
    protected void DoScannerDataPostExecute(ArrayList result) {
          //this DOES NOT work at all. This is not even fired
    }
 
}

Please Register or Login to post a reply

2 Replies

Y Yanis Dalabiras

Hi, your class needs to implement Scanner.DataListener in order to receive the onData event.  There could also be a timing issue releasing the EMDKManager in your first activity and initialising it in your second activity, you might want to make use of the EMDK StatusListener to ensure the EMDK is ready.

P Pietro Francesco Maggi

Hi Martin,
if you're using the Barcode API and the scanner object you should release it in the onStop callback, before jumping into the second activity.

You can take a look at this tutorial and use a similar deInitScanner method:

private void deInitScanner() {
if (scanner != null) {    try {        scanner.cancelRead();        scanner.removeDataListener(this);        scanner.removeStatusListener(this);        scanner.disable();    } catch (ScannerException e) {        // TODO Auto-generated catch block        statusTextView.setText("Status: " + e.getMessage());    }    scanner = null;}~Pietro

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