8 Replies Latest reply on Aug 15, 2016 8:08 AM by Kha Tran

    How to read barcode in child activity

    Kha Tran

      Hello,

       

      I has setup the barcode reading in parent form (List Activity) which search particular item based on barcode value and open the child form (detail activity). All are working!

       

      public class BarcodeActivity
         extends BaseActivity
         implements EMDKManager.EMDKListener, Scanner.DataListener, Scanner.StatusListener, BarcodeManager.ScannerConnectionListener
      {
      ...
      @Override
      public void Search(String code){
         try {
        ParcelModel model = ParcelService.Get(code);
         if (model==null){
        showToast("Parcel " + code + " is missing");
        }
         else{
        openParcel(model);
        }
        }
         catch (Exception ex){
        showToast(ex.getMessage());
        }
      }
      ...
      }
      

       

      In the child form, I would like to read the barcode to its text boxes or process its own Search function. However Search function in parent form is handling the event instead.

       

      Any idea please?

        • Re: How to read barcode in child activity
          Bill Hecox

          Hello Kha,

          I'm not sure I completely follow what you are trying to accomplish. Please provide a more complete sample of what you are trying to solve ( sample of both activities )

           

          What I think you are trying to solve is passing data collected in one activity to another. If that's the case, have a look at the "Starting Activities and Getting Results" section of the following google api guide. The code example shows how to bundle data when starting an activity.

           

          Activity | Android Developers

            • Re: How to read barcode in child activity
              Kha Tran

              Hi Bill. It is not the problem of passing data from 1 activity to another. Both activities are inherited from BarcodeActivity which is implementing Zebra's Barcode interfaces. we just override function Search in inherited activities. Please find the Barcode Activity as follow:

               

              public class BarcodeActivity
                 extends BaseActivity
                 implements EMDKManager.EMDKListener, Scanner.DataListener, Scanner.StatusListener, BarcodeManager.ScannerConnectionListener
              {
                 //barcode
                 private EMDKManager emdkManager = null;
                 private BarcodeManager barcodeManager = null;
                 private Scanner scanner = null;
                 private int dataLength;
                 private List<ScannerInfo> deviceList;
                 private int defaultIndex;
                 private int scannerIndex;
              
                 public void InitBarcoder(){
                EMDKResults results = EMDKManager.getEMDKManager(getApplicationContext(), this);
                 if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS) {
                showToast("Status: " + "EMDKManager object request failed!");
                }
                }
              
                 @Override
                 public void onOpened(EMDKManager emdkManager) {
                 this.emdkManager = emdkManager;
              
                 // Acquire the barcode manager resources
                 barcodeManager = (BarcodeManager) emdkManager.getInstance(EMDKManager.FEATURE_TYPE.BARCODE);
              
                 // Add connection listener
                 if (barcodeManager != null) {
                 barcodeManager.addConnectionListener(this);
                }
              
                 // Enumerate scanner devices
                 enumerateScannerDevices();
              
                 // Set default scanner
                //spinnerScannerDevices.setSelection(defaultIndex);
                 selectScannerDevices(defaultIndex);
                }
              
                 void selectScannerDevices(int index){
                 scannerIndex = index;
              
                deInitScanner();
                initScanner();
                 //setTrigger();
                //setDecoders();
              
                 startScan();
                }
              
              
              
                 private void startScan() {
              
                 if(scanner == null) {
                initScanner();
                }
              
                 if (scanner != null) {
                 try {
              
                 // Submit a new read.
                 scanner.read();
              
              // if (checkBoxContinuous.isChecked())
              // bContinuousMode = true;
              // else
              // bContinuousMode = false;
              
                //new AsyncUiControlUpdate().execute(false);
              
                 } catch (ScannerException e) {
              
                showToast("Status: " + e.getMessage());
                }
                }
              
                }
              
                 private void stopScan() {
              
                 if (scanner != null) {
              
                 try {
              
                 // Reset continuous flag
                //bContinuousMode = false;
              
                // Cancel the pending read.
                 scanner.cancelRead();
                 scanner.disable();
                 //new AsyncUiControlUpdate().execute(true);
              
                 } catch (ScannerException e) {
              
                showToast("Status: " + e.getMessage());
                }
                }
                }
              
                 private void initScanner() {
              
                 if (scanner == null) {
              
                 if ((deviceList != null) && (deviceList.size() != 0)) {
                 scanner = barcodeManager.getDevice(deviceList.get(scannerIndex));
                }
                 else {
                showToast("Status: " + "Failed to get the specified scanner device! Please close and restart the application.");
                 return;
                }
              
                 if (scanner != null) {
              
                 scanner.addDataListener(this);
                 scanner.addStatusListener(this);
              
                 try {
                 scanner.enable();
                } catch (ScannerException e) {
              
                showToast("Status: " + e.getMessage());
                }
                }else{
                showToast("Status: " + "Failed to initialize the scanner device.");
                }
                }
                }
              
              
              
                 @Override
                 public void onClosed() {
              
                 if (emdkManager != null) {
              
                 // Remove connection listener
                 if (barcodeManager != null){
                 barcodeManager.removeConnectionListener(this);
                 barcodeManager = null;
                }
              
                 // Release all the resources
                 emdkManager.release();
                 emdkManager = null;
                }
                showToast("Status: " + "EMDK closed unexpectedly! Please close and restart the application.");
                }
              
                 @Override
                 protected void onDestroy() {
                 super.onDestroy();
              
                 // De-initialize scanner
                 deInitScanner();
              
                 // Remove connection listener
                 if (barcodeManager != null) {
                 barcodeManager.removeConnectionListener(this);
                 barcodeManager = null;
                }
              
                 // Release all the resources
                 if (emdkManager != null) {
                 emdkManager.release();
                 emdkManager = null;
              
                }
              
                }
              
                 @Override
                 public void onStatus(StatusData statusData) {
              
                StatusData.ScannerStates state = statusData.getState();
              
                 switch(state) {
                 case IDLE:
                String statusString = statusData.getFriendlyName()+" is enabled and idle...";
                 //new AsyncStatusUpdate().execute(statusString);
                //if (bContinuousMode)
                 {
                 try {
                 // An attempt to use the scanner continuously and rapidly (with a delay < 100 ms between scans)
                // may cause the scanner to pause momentarily before resuming the scanning.
                // Hence add some delay (>= 100ms) before submitting the next read.
                 try {
                Thread.sleep(100);
                } catch (InterruptedException e) {
                e.printStackTrace();
                }
              
                 scanner.read();
                } catch (ScannerException e) {
                showToast(e.getMessage());
                 //statusString = e.getMessage();
                //new AsyncStatusUpdate().execute(statusString);
                 }
                }
                 //new AsyncUiControlUpdate().execute(true);
                 break;
                 case WAITING:
                statusString = "Scanner is waiting for trigger press...";
                 //new AsyncStatusUpdate().execute(statusString);
                //new AsyncUiControlUpdate().execute(false);
                 break;
                 case SCANNING:
                statusString = "Scanning...";
                showToast(statusString);
                 //new AsyncStatusUpdate().execute(statusString);
                //new AsyncUiControlUpdate().execute(false);
                 break;
                 case DISABLED:
                statusString = statusData.getFriendlyName()+" is disabled.";
                showToast(statusString);
                 //new AsyncStatusUpdate().execute(statusString);
                //new AsyncUiControlUpdate().execute(true);
                 break;
                 case ERROR:
                statusString = "An error has occurred.";
                showToast(statusString);
                 //new AsyncStatusUpdate().execute(statusString);
                //new AsyncUiControlUpdate().execute(true);
                 break;
                 default:
                 break;
                }
                }
              
              
              
                 @Override
                 public void onData(ScanDataCollection scanDataCollection) {
              
                 if ((scanDataCollection != null) && (scanDataCollection.getResult() == ScannerResults.SUCCESS)) {
                ArrayList<ScanDataCollection.ScanData> scanData = scanDataCollection.getScanData();
                 for(ScanDataCollection.ScanData data : scanData) {
              
                String dataString = data.getData();
              
                 new AsyncDataUpdate().execute(dataString);
                }
                }
                }
              
              
                 private void enumerateScannerDevices() {
              
                 if (barcodeManager != null) {
              
              
                List<String> friendlyNameList = new ArrayList<String>();
                 int spinnerIndex = 0;
              
                 deviceList = barcodeManager.getSupportedDevicesInfo();
              
                 if ((deviceList != null) && (deviceList.size() != 0)) {
              
                Iterator<ScannerInfo> it = deviceList.iterator();
                 while(it.hasNext()) {
                ScannerInfo scnInfo = it.next();
                friendlyNameList.add(scnInfo.getFriendlyName());
                 if(scnInfo.isDefaultScanner()) {
                 defaultIndex = spinnerIndex;
                }
                ++spinnerIndex;
                }
                }
                 else {
                showToast("Status: " + "Failed to get the list of supported scanner devices! Please close and restart the application.");
                }
              
                 //ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_spinner_item, friendlyNameList);
                //spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //spinnerScannerDevices.setAdapter(spinnerAdapter);
                 }
                }
              
              
              
              
                 private void deInitScanner() {
              
                 if (scanner != null) {
              
                 try {
              
                 scanner.cancelRead();
                 scanner.disable();
              
                } catch (ScannerException e) {
              
                showToast("Status: " + e.getMessage());
                }
                 scanner.removeDataListener(this);
                 scanner.removeStatusListener(this);
                 try{
                 scanner.release();
                } catch (ScannerException e) {
              
                showToast("Status: " + e.getMessage());
                }
              
                 scanner = null;
                }
                }
              
                 @Override
                 public void onConnectionChange(ScannerInfo scannerInfo, BarcodeManager.ConnectionState connectionState) {
              
                String status;
                String scannerName = "";
                String statusString="";
              
                String statusBT = connectionState.toString();
                String scannerNameBT = scannerInfo.getFriendlyName();
              
                 if (deviceList.size() != 0) {
                scannerName = deviceList.get(scannerIndex).getFriendlyName();
                }
              
                 if (scannerName.equalsIgnoreCase(scannerNameBT)) {
              
                status = scannerNameBT + ":" + statusBT;
                 //new AsyncStatusUpdate().execute(status);
              
                 switch(connectionState) {
                 case CONNECTED:
                initScanner();
                 //setTrigger();
                //setDecoders();
                 break;
                 case DISCONNECTED:
                deInitScanner();
                 //new AsyncUiControlUpdate().execute(true);
                 break;
                }
                }
                 else {
                status = statusString + " " + scannerNameBT + ":" + statusBT;
                 //new AsyncStatusUpdate().execute(status);
                 }
                }
              
                 private class AsyncDataUpdate extends AsyncTask<String, Void, String> {
              
                 @Override
                 protected String doInBackground(String... params) {
              
                 return params[0];
                }
              
                 protected void onPostExecute(String result) {
              
                 if (result != null) {
                 //showToast(result);
                 Search(result);
                }
                }
                }
              
                 public void Search(String code){
              
                }
              }
              

               

              In parent Activity, the function Search will get the barcode and open the child activity:

               

              @Override
              public void Search(String code){
                 try {
                ParcelModel model = ParcelService.Get(code);
                 if (model==null){
                showToast("Parcel " + code + " is missing");
                }
                 else{
                openParcel(model);
                }
                }
                 catch (Exception ex){
                showToast(ex.getMessage());
                }
              }
              

               

              When child activity is open, i want to show a toast in when barcoder is reading:

               

              @Override
              public void Search(String code){
                showToast(code);
              }
              

              However, the function Search has never been called as expecting. The search in parent activity is processing all barcode event.

               

              Any idea to allow to receive the barcode only when the actitvity is active?