2 Replies Latest reply on Jun 22, 2016 3:06 AM by Pietro Maggi

    EMDK initialization fail

    Martin Mrazik

      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<ScanDataCollection, Void, ArrayList<String>> {

       

       

              @Override

              protected ArrayList<String> doInBackground(ScanDataCollection... params) {

                  ArrayList<String> result = new ArrayList<String>();

       

       

                  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> 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<String> result) {

                  DoScannerDataPostExecute(result);

              }

          }

       

       

      class FirstActivity extends EMDKBaseActivity{

           public void onCreate(Bundle icicle) {

              super.onCreate(icicle);

       

                ....

       

              initializeEMDK();

          }

       

      @Override

       

          protected void DoScannerDataPostExecute(ArrayList<String> 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<String> result) {

                //this DOES NOT work at all. This is not even fired

          }

       

      }

        • Re: EMDK initialization fail
          Darryn Campbell

          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.

          • Re: EMDK initialization fail
            Pietro 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