2 Replies Latest reply on Feb 16, 2016 5:09 PM by Douglas Christensen

    BarcodeManager null reference

    Douglas Christensen
      • TC55CH
        • 01.08.01G_3G build
        • Non GSM
        • Enterprise manager installed
        • 4.0 EMDK installed
      • Android Studio 1.5.1

       

      Trying to establish a simple project that performs basic scanning using platform 23 as in Pietro's example http://pietromaggi.com/2015/11/25/building-zebras-emdk-project-using-sdk-platform-23/.

      Adjusted the gradle file as the example indicates.  Compiles and transfers to handheld however the BarcodeManager is not instantiated for some reason.  Maybe you can see the error.

       

      Null reference here in initializeScanner method which is called during onOpened

       

      barcodeManager = (BarcodeManager) emdkManager.getInstance(FEATURE_TYPE.BARCODE);
      

       

      There is an object reference to emdkManager.  Created a "libs' folder and copied the v16 of com.symbol.emdk.jar into it.

      gradle file:

      
      apply plugin: 'com.android.application'
      
      
      android {
          compileSdkVersion 'Google Inc.:Google APIs:23'
          buildToolsVersion "23.0.2"
      
      
          defaultConfig {
              applicationId "com.goroute.barcodescanning"
              minSdkVersion 16
              targetSdkVersion 23
              versionCode 1
              versionName "1.0"
          }
          buildTypes {
              release {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
          }
      }
      
      
      dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar'], exclude: ['com.symbol.emdk.jar'] )
          testCompile 'junit:junit:4.12'
          compile 'com.android.support:appcompat-v7:23.1.1'
          compile 'com.android.support:design:23.1.1'
          provided fileTree(dir: 'libs', include: ['com.symbol.emdk.jar'])
      }
      
      
      
      
      
      
      
      

       

       

      Main code.  Null reference occurrs after onOpened calls initializeScanner. 

      Any suggestions appreciated.  Project is attached.

       

      Thanks.

       

      package com.example.barcodescanning;
      
      
      import android.os.AsyncTask;
      import android.os.Bundle;
      import android.support.design.widget.FloatingActionButton;
      import android.support.design.widget.Snackbar;
      import android.support.v7.app.AppCompatActivity;
      import android.support.v7.widget.Toolbar;
      import android.view.View;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.widget.EditText;
      import android.widget.TextView;
      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.StatusData;
      import com.symbol.emdk.barcode.ScannerResults;
      import com.symbol.emdk.barcode.ScanDataCollection.ScanData;
      import com.symbol.emdk.barcode.ScanDataCollection.LabelType;
      import com.symbol.emdk.barcode.StatusData.ScannerStates;
      import com.symbol.emdk.EMDKManager.FEATURE_TYPE;
      
      
      import java.util.ArrayList;
      
      
      public class MainActivity extends AppCompatActivity implements EMDKListener, StatusListener, DataListener {
        // Declare a variable to store EMDKManager object
        private EMDKManager emdkManager = null;
          // Declare a variable to store Barcode Manager object
          private BarcodeManager barcodeManager = null;
          // Declare a variable to hold scanner device to scan
          private Scanner scanner = null;
          // Text view to display status of EMDK and Barcode Scanning Operations
          private TextView statusTextView = null;
          // Edit Text that is used to display scanned barcode data
          private EditText dataView = null;
          boolean isScanning = false;
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
      
      
              // Reference to UI elements
              statusTextView = (TextView) findViewById(R.id.textViewStatus);
              dataView = (EditText) findViewById(R.id.editText1);
      
      
      
      
        // The EMDKManager object will be created and returned in the callback.
              EMDKResults results = EMDKManager.getEMDKManager(
                      getApplicationContext(), this);
              // Check the return status of getEMDKManager and update the status Text
              // View accordingly
              if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS) {
                  statusTextView.setText("EMDKManager Request Failed");
              }
      
      
              FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
              fab.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                              .setAction("Action", null).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) emdkManager.getInstance(FEATURE_TYPE.BARCODE);
                  // Get default scanner defined on the device
                  scanner = barcodeManager.getDevice(BarcodeManager.DeviceIdentifier.INTERNAL_LASER1);
                  // 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();
              }
          }
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
      
              // Inflate the menu; this adds items to the action bar if it is present.  
              getMenuInflater().inflate(R.menu.menu_main, menu);
              return true;
          }
      
      
          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
      
      
              //noinspection SimplifiableIfStatement
              if (id == R.id.action_settings) {
                  return true;
              }
      
      
              return super.onOptionsItemSelected(item);
          }
      
      
          @Override
          public void onData(ScanDataCollection scanDataCollection) {
      
      
          }
      
      
          @Override
          protected void onDestroy() {
              super.onDestroy();
              if (emdkManager != null) {
                  // Clean up the objects created by EMDK manager
                  emdkManager.release();
                  emdkManager = null;
              }
          }
      
      
      
      
          @Override
          protected void onStop() {
              super.onStop();
              try {
                  if (scanner != null) {
                      // releases the scanner hardware resources for other application
                      // to use. You must call this as soon as you're done with the
                      // scanning.
                      scanner.disable();
                      scanner = null;
                  }
              } catch (ScannerException e) {
                  e.printStackTrace();
              }
          }
      
      
          @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(MainActivity.this,
                      "Press Hard Scan Button to start scanning...",
                      Toast.LENGTH_SHORT).show();
          }
      
      
          @Override
          public void onClosed() {
              // The EMDK closed abruptly. // Clean up the objects created by EMDK
              // manager
              if (this.emdkManager != null) {
                  this.emdkManager.release();
                  this.emdkManager = null;
              }
          }
      
      
          @Override
          public void onStatus(StatusData statusData) {
              // process the scan status event on the background thread using
              // AsyncTask and update the UI thread with current scanner state
              new AsyncStatusUpdate().execute(statusData);
          }
      
      
          // Update the scan data on UI
          int dataLength = 0;
      
      
          // AsyncTask that configures the scanned data on background
          // thread and updated the result on UI thread with scanned data and type of
          // label
          private class AsyncDataUpdate extends
                  AsyncTask<ScanDataCollection, Void, String> {
      
      
              @Override
              protected String doInBackground(ScanDataCollection... params) {
      
      
                  // Status string that contains both barcode data and type of barcode
                  //  that is being scanned   
                  String statusStr = "";
      
      
                  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 barcodeDate = data.getData();
                              // Get the type of label being scanned
                              LabelType labelType = data.getLabelType();
                              // Concatenate barcode data and label type
                              statusStr = barcodeDate + " " + labelType;
                          }
                      }
      
      
                  } catch (ScannerException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
      
      
                  // Return result to populate on UI thread
                  return statusStr;
              }
      
      
              @Override
              protected void onPostExecute(String result) {
                  // Update the dataView EditText on UI thread with barcode data and
                  // its label type
                  if ( dataLength++ > 50) {
                      // Clear the cache after 50 scans
                      dataView.getText().clear();
                      dataLength = 0;
                  }
                  dataView.append(result + "\n");
              }
          }
      
      
      
      
          private class AsyncStatusUpdate extends AsyncTask<StatusData, Void, String>{
      
      
              @Override
              protected String doInBackground(StatusData... params) {
                  String statusStr = "";
                  // Get the current state of scanner in background
                  StatusData statusData = params[0];
                  ScannerStates state = statusData.getState();
                  // Different states of Scanner
                  switch (state) {
                      // Scanner is IDLE
                      case IDLE:
                          statusStr = "The scanner enabled and its idle";
                          break;
                      // Scanner is SCANNING
                      case SCANNING:
                          statusStr = "Scanning..";
                          break;
                      // Scanner is waiting for trigger press
                      case WAITING:
                          statusStr = "Waiting for trigger press..";
                          break;
                      // Scanner is not enabled
                      case DISABLED:
                          statusStr = "Scanner is not enabled";
                          break;
                      default:
                          break;
                  }
      
      
                  // Return result to populate on UI thread
                  return statusStr;
              }
      
      
              @Override
              protected void onPostExecute(String s) {
                  super.onPostExecute(s);
                  statusTextView.setText(s);
              }
          }
      }
      
          
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

      Message was edited by: Douglas Christensen It appears that when the EMDK 4.0 is installed on the device, DataWedge is also removed.  Is this by design?