title: React Native Android Module
body: <p>I am trying to implement a React Native Android Module for a Honeywell CT50 device with no prior experience. The module will listen for scans from the in-build laser scanner on the device.</p>
  
  <p>I have gone through the official walkthrough on the React Native site and I have managed to setup a basic module that I can receive a simple value in a RN component. My code looks like this so far:</p>
  
  <p>HoneywellCT50 Package:</p>
  
  <pre><code>public class HoneywellCT50Package implements ReactPackage {
      @Override
      public List&lt;NativeModule&gt; createNativeModules(ReactApplicationContext reactContext) {
          List&lt;NativeModule&gt; modules = new ArrayList&lt;&gt;();
          modules.add(new HoneywellCT50Module(reactContext));
          return modules; 
      }
  
      @Override
      public List&lt;Class&lt;? extends JavaScriptModule&gt;&gt; createJSModules() {
          return Collections.emptyList();
      }
  
      @Override
      public List&lt;ViewManager&gt; createViewManagers(ReactApplicationContext reactContext) {
          return Collections.emptyList();
      }
  }
  </code></pre>
  
  <p>HoneywellCT50 Module:</p>
  
  <pre><code>public class HoneywellCT50Module extends ReactContextBaseJavaModule {
      private ReactApplicationContext reactContext;
  
      private Callback successCallback;
  
      public HoneywellCT50Module(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
      }
  
  
      @ReactMethod
      public void doSomething(
        int a,
        int b,
        Callback successCallback
      ) {
        boolean equal = a == b;
        successCallback.invoke(equal);
      }
  
  
      @Override
      public String getName() {
        return "HoneywellCT50";
      }
  }
  </code></pre>
  
  <p>React Native Component (in componentDidMount)</p>
  
  <pre><code>NativeModules.HoneywellCT50.doSomething(
      5,
      10,
      (equal) =&gt; {
          ToastAndroid.show(`Result: ${equal}`, ToastAndroid.LONG);
      }
  );
  
  returns false
  </code></pre>
  
  <p>I am struggling to understand how I can now map the methods in the native Android class (contained in .jar) to React Native. I am not looking for a finished solution but would be grateful if someone could help explain how I would begin to do this using the following:</p>
  
  <p>Android class</p>
  
  <pre><code>public class MainActivity extends Activity implements BarcodeReader.BarcodeListener {
      private AidcManager manager;
      private BarcodeReader reader;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // create the AidcManager providing a Context and an
          // CreatedCallback implementation.
          AidcManager.create(this, new AidcManager.CreatedCallback() {
  
              @Override
              public void onCreated(AidcManager aidcManager) {
                  manager = aidcManager;
                  // use the manager to create a BarcodeReader with a session
                  // associated with the internal imager.
                  reader = manager.createBarcodeReader();
  
                  try {
                      // apply settings
                      reader.setProperty(BarcodeReader.PROPERTY_CODE_39_ENABLED, false);
                      reader.setProperty(BarcodeReader.PROPERTY_DATAMATRIX_ENABLED, true);
  
                      // set the trigger mode to automatic control
                      reader.setProperty(BarcodeReader.PROPERTY_TRIGGER_CONTROL_MODE,
                          BarcodeReader.TRIGGER_CONTROL_MODE_AUTO_CONTROL);
                  } catch (UnsupportedPropertyException e) {
                      Toast.makeText(MainActivity.this, "Failed to apply properties",
                          Toast.LENGTH_SHORT).show();
                  }
  
                  // register bar code event listener
                  reader.addBarcodeListener(MainActivity.this);
              }
          });
      }
  
      @Override
      public void onResume() {
          super.onResume();
          if (reader != null) {
              try {
                  reader.claim();
              } catch (ScannerUnavailableException e) {
                  e.printStackTrace();
                  Toast.makeText(this, "Scanner unavailable", Toast.LENGTH_SHORT).show();
              }
          }
      }
  
      @Override
      public void onPause() {
          super.onPause();
          if (reader != null) {
              // release the scanner claim so we don't get any scanner
              // notifications while paused.
              reader.release();
          }
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          if (reader != null) {
              // unregister barcode event listener
              reader.removeBarcodeListener(this);
  
              // close BarcodeReader to clean up resources.
              // once closed, the object can no longer be used.
              reader.close();
          }
          if (manager != null) {
              // close AidcManager to disconnect from the scanner service.
              // once closed, the object can no longer be used.
              manager.close();
          }
      }
  
      @Override
      public void onBarcodeEvent(final BarcodeReadEvent event) {
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  String barcodeData = event.getBarcodeData();
                  String timestamp = event.getTimestamp();
  
                  // update UI to reflect the data
              }
          });
      }
  
      @Override
      public void onFailureEvent(final BarcodeFailureEvent event) {
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  Toast.makeText(MainActivity.this, "Barcode read failed",
                      Toast.LENGTH_SHORT).show();
              }
          });
      }
  }
  </code></pre>
  
  <p>Many thanks in advance.</p>
  
  <p>EDIT</p>
  
  <p>I have looked at the following tutorials to get to this point:</p>
  
  <ul>
  <li><a href="https://shift.infinite.red/native-modules-for-react-native-android-ac05dbda800d#.bl0zke73p" rel="nofollow noreferrer">https://shift.infinite.red/native-modules-for-react-native-android-ac05dbda800d#.bl0zke73p</a></li>
  <li><a href="http://facebook.github.io/react-native/docs/native-modules-android.html" rel="nofollow noreferrer">http://facebook.github.io/react-native/docs/native-modules-android.html</a></li>
  <li><a href="https://medium.com/@sejoker/writing-android-component-for-react-native-e34802bf3377#.ia8jwh6to" rel="nofollow noreferrer">https://medium.com/@sejoker/writing-android-component-for-react-native-e34802bf3377#.ia8jwh6to</a></li>
  <li><a href="https://www.sitepoint.com/access-platform-apis-with-react-native-modules/" rel="nofollow noreferrer">https://www.sitepoint.com/access-platform-apis-with-react-native-modules/</a></li>
  </ul>
  
  <p>FURTHER EDIT</p>
  
  <p>After looking a bit further I have found a Cordova plugin that has been developed for this purpose:</p>
  
  <ul>
  <li><a href="https://github.com/icsfl/cordova-honeywell/blob/master/src/android/HoneywellScannerPlugin.java" rel="nofollow noreferrer">https://github.com/icsfl/cordova-honeywell/blob/master/src/android/HoneywellScannerPlugin.java</a></li>
  </ul>
  
  <p>I did consider using the React Native Cordova Bridge:</p>
  
  <ul>
  <li><a href="https://github.com/axemclion/react-native-cordova-plugin" rel="nofollow noreferrer">https://github.com/axemclion/react-native-cordova-plugin</a></li>
  </ul>
  
  <p>but unfortunately this no longer works with the latest version of React Native (&lt; 0.29) which I need for other parts of my application.</p>
  
  <p>I have tried playing around with some of the methods in the Cordova plugin but again I don't have enough experience with React Native / Cordova / Java to figure out how to get this working.</p>
  
  <p>To further explain what I am looking:</p>
  
  <ul>
  <li><p>Do I need a ReactMethod for each of the corresponding Honeywell methods?</p></li>
  <li><p>Can I simply create a ReactMethod for the onBarcodeEvent?</p></li>
  <li><p>Which React Bridge methods must I use to get a value from the scanner?</p></li>
  </ul>