Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Tidying code
Browse files Browse the repository at this point in the history
  • Loading branch information
darryncampbell committed Sep 27, 2018
1 parent f4b84af commit e3ad055
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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/)


Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

Expand All @@ -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);
Expand Down Expand Up @@ -135,11 +144,11 @@ public String getName() {
return "DataWedgeIntents";
}


@Override
public Map<String, Object> getConstants() {
final Map<String, Object> 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);
Expand All @@ -157,6 +166,7 @@ public Map<String, Object> 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;
Expand All @@ -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)
Expand Down Expand Up @@ -208,7 +221,6 @@ public void sendBroadcastWithExtras(ReadableMap obj) throws JSONException
i.putExtra(key, valueStr);
}
}

this.reactContext.sendBroadcast(i);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit e3ad055

Please sign in to comment.