From e3ad0551553f0210d6480fb1acf1856ea1c21632 Mon Sep 17 00:00:00 2001 From: darryncampbell Date: Thu, 27 Sep 2018 11:44:45 +0100 Subject: [PATCH] Tidying code --- README.md | 48 ++++++++++++++++--- .../RNDataWedgeIntentsModule.java | 29 +++++++---- index.js | 1 + package.json | 2 +- 4 files changed, 65 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6ab2aed..3f67bc6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ Note: as of ReactNative version 0.27 automatic installation of modules is suppor ## Example usage -**Please see [RNDataWedgeIntentDemo](https://github.com/darryncampbell/RNDataWedgeIntentDemo) for a sample application that makes use of this module**, file [index.android.js](https://github.com/darryncampbell/RNDataWedgeIntentDemo/blob/master/index.android.js) +There are two samples available for this module: + +**Please see [RNDataWedgeIntentDemo](https://github.com/darryncampbell/RNDataWedgeIntentDemo) for a basic sample application that makes use of this module**, file [index.android.js](https://github.com/darryncampbell/RNDataWedgeIntentDemo/blob/master/index.android.js). This application is a little dated now and is designed to work with version 0.0.2 of this module. ```javascript import DataWedgeIntents from 'react-native-datawedge-intents' @@ -40,16 +42,50 @@ DataWedgeIntents.sendIntent(DataWedgeIntents.ACTION_SOFTSCANTRIGGER,DataWedgeInt ``` +**Please see [DataWedgeReactNative](https://github.com/darryncampbell/DataWedgeReactNative) for a more fully featured and up to date application that makes use of this module**, file [App.js](https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js). This application requires a minimum version of 0.1.0 of this module. + +```javascript +import DataWedgeIntents from 'react-native-datawedge-intents' +... +// Register a receiver for the barcode scans with the appropriate action +DataWedgeIntents.registerBroadcastReceiver({ + filterActions: [ + 'com.zebra.reactnativedemo.ACTION', + 'com.symbol.datawedge.api.RESULT_ACTION' + ], + filterCategories: [ + 'android.intent.category.DEFAULT' + ] +}); +... +// Declare a handler for broadcast intents +this.broadcastReceiverHandler = (intent) => +{ + this.broadcastReceiver(intent); +} +... +// Initiate a scan (you could also press the trigger key) +this.sendCommand("com.symbol.datawedge.api.SOFT_SCAN_TRIGGER", 'TOGGLE_SCANNING'); +... +sendCommand(extraName, extraValue) { + console.log("Sending Command: " + extraName + ", " + JSON.stringify(extraValue)); + var broadcastExtras = {}; + broadcastExtras[extraName] = extraValue; + broadcastExtras["SEND_RESULT"] = this.sendCommandResult; + DataWedgeIntents.sendBroadcastWithExtras({ + action: "com.symbol.datawedge.api.ACTION", + extras: broadcastExtras}); +} +``` + ## DataWedge -This module **requires the DataWedge service running** on the target device to be **correctly configured** to broadcast Android intents on each barcode scan with the appropriate action: +This module **requires the DataWedge service running** on the target device to be **correctly configured** to broadcast Android intents on each barcode scan with the appropriate action. This can be achieved either manually or via an API, see the sample application readme files for a more thorough explanation. ### Output Plugin -Please also ensure you disable the keyboard output plugin to avoid undesired effects on your application: https://developer.zebra.com/message/95397?et=watches.email.thread#95397 - -![Associate app](https://raw.githubusercontent.com/darryncampbell/react-native-datawedge-intents/master/screens/datawedge.png) +Please also ensure you disable the keyboard output plugin to avoid undesired effects on your application: [thread](https://developer.zebra.com/message/95397). -For more information about DataWedge and how to configure it please visit Zebra [tech docs](http://techdocs.zebra.com/). The DataWedge API that this module calls is detailed [here](http://techdocs.zebra.com/datawedge/5-0/guide/api/) +For more information about DataWedge and how to configure it please visit Zebra [tech docs](http://techdocs.zebra.com/). The DataWedge API that this module calls is detailed [here](http://techdocs.zebra.com/datawedge/latest/guide/api/) diff --git a/android/src/main/java/com/darryncampbell/rndatawedgeintents/RNDataWedgeIntentsModule.java b/android/src/main/java/com/darryncampbell/rndatawedgeintents/RNDataWedgeIntentsModule.java index 16f763c..5aeb627 100644 --- a/android/src/main/java/com/darryncampbell/rndatawedgeintents/RNDataWedgeIntentsModule.java +++ b/android/src/main/java/com/darryncampbell/rndatawedgeintents/RNDataWedgeIntentsModule.java @@ -73,7 +73,7 @@ public class RNDataWedgeIntentsModule extends ReactContextBaseJavaModule impleme private static final String KEY_ENUMERATEDSCANNERLIST = "DWAPI_KEY_ENUMERATEDSCANNERLIST"; // END DEPRECATED PROPERTIES - // Scan data receiver + // Scan data receiver - These strings are only used by registerReceiver, a deprecated method private static final String RECEIVED_SCAN_SOURCE = "com.symbol.datawedge.source"; private static final String RECEIVED_SCAN_DATA = "com.symbol.datawedge.data_string"; private static final String RECEIVED_SCAN_TYPE = "com.symbol.datawedge.label_type"; @@ -89,7 +89,7 @@ public RNDataWedgeIntentsModule(ReactApplicationContext reactContext) { reactContext.addLifecycleEventListener(this); Log.v(TAG, "Constructing React native DataWedge intents module"); - // Register a broadcast receiver for the Enumerate Scanners intent + // Register a broadcast receiver to return data back to the application ObservableObject.getInstance().addObserver(this); } @@ -100,12 +100,21 @@ public void onHostResume() { filter.addAction(ACTION_ENUMERATEDLISET); reactContext.registerReceiver(myEnumerateScannersBroadcastReceiver, filter); if (this.registeredAction != null) - registerReceiver(this.registeredAction, this.registeredCategory); + registerReceiver(this.registeredAction, this.registeredCategory); + + // Note regarding registerBroadcastReceiver: + // This module makes no attempt to unregister the receiver when the application is paused and re-registers the + // receiver when the application comes to the foreground. Feel free to fork and add this logic to your solution if + // required - I have found in the past this has led to confusion. } @Override public void onHostPause() { //Log.v(TAG, "Host Pause"); + // Note regarding registerBroadcastReceiver: + // This module makes no attempt to unregister the receiver when the application is paused and re-registers the + // receiver when the application comes to the foreground. Feel free to fork and add this logic to your solution if + // required - I have found in the past this has led to confusion. try { this.reactContext.unregisterReceiver(myEnumerateScannersBroadcastReceiver); @@ -135,11 +144,11 @@ public String getName() { return "DataWedgeIntents"; } - @Override public Map getConstants() { final Map constants = new HashMap<>(); // These are the constants available to the caller + // CONSTANTS HAVE BEEN DEPRECATED and will not stay current with the latest DW API constants.put("ACTION_SOFTSCANTRIGGER", ACTION_SOFTSCANTRIGGER); constants.put("ACTION_SCANNERINPUTPLUGIN", ACTION_SCANNERINPUTPLUGIN); constants.put("ACTION_ENUMERATESCANNERS", ACTION_ENUMERATESCANNERS); @@ -157,6 +166,7 @@ public Map getConstants() { @ReactMethod public void sendIntent(String action, String parameterValue) { + // THIS METHOD IS DEPRECATED, use SendBroadcastWithExtras Log.v(TAG, "Sending Intent with action: " + action + ", parameter: [" + parameterValue + "]"); // Some DW API calls use a different paramter name, abstract this from the caller. String parameterKey = EXTRA_PARAMETER; @@ -175,6 +185,9 @@ public void sendIntent(String action, String parameterValue) @ReactMethod public void sendBroadcastWithExtras(ReadableMap obj) throws JSONException { + // Implementation note: Whilst this function will probably be able to deconstruct many ReadableMap objects + // (originally JSON objects) to intents, no effort has been made to make this function generic beyond + // support for the DataWedge API. String action = obj.hasKey("action") ? obj.getString("action") : null; Intent i = new Intent(); if (action != null) @@ -208,7 +221,6 @@ public void sendBroadcastWithExtras(ReadableMap obj) throws JSONException i.putExtra(key, valueStr); } } - this.reactContext.sendBroadcast(i); } @@ -332,7 +344,7 @@ else if (obj.get(key) instanceof JSONObject) @ReactMethod public void registerReceiver(String action, String category) { - // THIS METHOD IS DEPRECATED + // THIS METHOD IS DEPRECATED, use registerBroadcastReceiver Log.d(TAG, "Registering an Intent filter for action: " + action); this.registeredAction = action; this.registeredCategory = category; @@ -389,10 +401,10 @@ public void registerBroadcastReceiver(ReadableMap filterObj) } } this.reactContext.registerReceiver(genericReceiver, filter); - } // Broadcast receiver for the response to the Enumerate Scanner API + // THIS METHOD IS DEPRECATED, you should enumerate scanners as shown in https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js public BroadcastReceiver myEnumerateScannersBroadcastReceiver = new BroadcastReceiver() { @Override @@ -404,6 +416,7 @@ public void onReceive(Context context, Intent intent) { // Broadcast receiver for the DataWedge intent being sent from Datawedge. // Note: DW must be configured to send broadcast intents + // THIS METHOD IS DEPRECATED, you should enumerate scanners as shown in https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js public BroadcastReceiver scannedDataBroadcastReceiver = new BroadcastReceiver() { @Override @@ -431,7 +444,7 @@ private void sendEvent(ReactContext reactContext, reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params); } - // http://stackoverflow.com/questions/28083430/communication-between-broadcastreceiver-and-activity-android#30964385 + // Credit: http://stackoverflow.com/questions/28083430/communication-between-broadcastreceiver-and-activity-android#30964385 @Override public void update(Observable observable, Object data) { diff --git a/index.js b/index.js index ec29fec..aa16b3f 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ var { Platform, NativeModules } = require('react-native'); var RNDataWedgeIntents = NativeModules.DataWedgeIntents; var DataWedgeIntents = { + // Specifying the DataWedge API constants in this module is deprecated. It is not feasible to stay current with the DW API. ACTION_SOFTSCANTRIGGER: RNDataWedgeIntents.ACTION_SOFTSCANTRIGGER, ACTION_SCANNERINPUTPLUGIN: RNDataWedgeIntents.ACTION_SCANNERINPUTPLUGIN, ACTION_ENUMERATESCANNERS: RNDataWedgeIntents.ACTION_ENUMERATESCANNERS, diff --git a/package.json b/package.json index e9a2e5c..0d5342f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-datawedge-intents", - "version": "0.1.0", + "version": "0.1.1", "description": "React Native Android module to interface with Zebra's DataWedge using Android Intents to control the barcode scanner and retrieve scanned data", "main": "index.js", "scripts": {