Skip to content

Commit 72f2b94

Browse files
chore: add example code
1 parent 09aa5ae commit 72f2b94

File tree

4 files changed

+235
-57
lines changed

4 files changed

+235
-57
lines changed

android/src/main/java/com/datawedgeintents/DatawedgeIntentsModule.kt

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.facebook.react.bridge.WritableArray
2121
import com.facebook.react.bridge.WritableMap
2222
import com.facebook.react.bridge.WritableNativeArray
2323
import com.facebook.react.bridge.WritableNativeMap
24+
import com.facebook.react.modules.core.DeviceEventManagerModule
2425
import org.json.JSONArray
2526
import org.json.JSONException
2627
import org.json.JSONObject
@@ -30,8 +31,10 @@ import java.util.Observer
3031
class DatawedgeIntentsModule(private val reactContext: ReactApplicationContext) :
3132
ReactContextBaseJavaModule(
3233
reactContext
33-
),
34-
Observer, LifecycleEventListener {
34+
)
35+
// Observer,
36+
// LifecycleEventListener
37+
{
3538

3639
override fun getName(): String {
3740
return "DatawedgeIntents"
@@ -49,6 +52,25 @@ class DatawedgeIntentsModule(private val reactContext: ReactApplicationContext)
4952
private var registeredAction: String? = null
5053
private var registeredCategory: String? = null
5154

55+
// Broadcast receiver for the response to the Enumerate Scanner API
56+
// THIS METHOD IS DEPRECATED, you should enumerate scanners as shown in https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js
57+
var myEnumerateScannersBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
58+
override fun onReceive(context: Context, intent: Intent) {
59+
Log.v(TAG, "Received Broadcast from DataWedge API - Enumerate Scanners")
60+
ObservableObject.getInstance().updateValue(intent)
61+
}
62+
}
63+
64+
// Broadcast receiver for the DataWedge intent being sent from Datawedge.
65+
// Note: DW must be configured to send broadcast intents
66+
// THIS METHOD IS DEPRECATED, you should enumerate scanners as shown in https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js
67+
var scannedDataBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
68+
override fun onReceive(context: Context, intent: Intent) {
69+
Log.v(TAG, "Received Broadcast from DataWedge API - Scanner")
70+
ObservableObject.getInstance().updateValue(intent)
71+
}
72+
}
73+
5274
override fun onHostResume() {
5375
val filter = IntentFilter()
5476
filter.addAction(ACTION_ENUMERATEDLISET)
@@ -307,33 +329,15 @@ class DatawedgeIntentsModule(private val reactContext: ReactApplicationContext)
307329
}
308330
}
309331

310-
// Broadcast receiver for the response to the Enumerate Scanner API
311-
// THIS METHOD IS DEPRECATED, you should enumerate scanners as shown in https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js
312-
var myEnumerateScannersBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
313-
override fun onReceive(context: Context, intent: Intent) {
314-
Log.v(TAG, "Received Broadcast from DataWedge API - Enumerate Scanners")
315-
ObservableObject.getInstance().updateValue(intent)
316-
}
317-
}
318-
319-
// Broadcast receiver for the DataWedge intent being sent from Datawedge.
320-
// Note: DW must be configured to send broadcast intents
321-
// THIS METHOD IS DEPRECATED, you should enumerate scanners as shown in https://github.com/darryncampbell/DataWedgeReactNative/blob/master/App.js
322-
var scannedDataBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
323-
override fun onReceive(context: Context, intent: Intent) {
324-
Log.v(TAG, "Received Broadcast from DataWedge API - Scanner")
325-
ObservableObject.getInstance().updateValue(intent)
326-
}
327-
}
328-
329332
// Sending events to JavaScript as defined in the native-modules documentation.
330333
// Note: Callbacks can only be invoked a single time so are not a suitable interface for barcode scans.
331334
private fun sendEvent(
332335
reactContext: ReactContext,
333336
eventName: String,
334337
params: WritableMap
335338
) {
336-
reactContext.getJSModule<BridgeReactContext.RCTDeviceEventEmitter>(BridgeReactContext.RCTDeviceEventEmitter::class.java)
339+
reactContext
340+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
337341
.emit(eventName, params)
338342
}
339343

example/src/App.tsx

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
1-
// import { useState, useEffect } from 'react';
2-
// import { StyleSheet, View, Text } from 'react-native';
3-
// import { multiply } from 'react-native-datawedge-intents';
1+
import { useEffect } from 'react';
2+
import { NativeEventEmitter, StyleSheet, Text, View } from 'react-native';
3+
import { ScannerInit, ScannerReceiver } from './libs/scanner';
44

5-
// export default function App() {
6-
// const [result, setResult] = useState<number | undefined>();
5+
const eventEmitter = new NativeEventEmitter();
6+
let isSuccessScan = false;
77

8-
// useEffect(() => {
9-
// multiply(3, 7).then(setResult);
10-
// }, []);
11-
12-
// return (
13-
// <View style={styles.container}>
14-
// <Text>Result: {result}</Text>
15-
// </View>
16-
// );
17-
// }
18-
19-
// const styles = StyleSheet.create({
20-
// container: {
21-
// flex: 1,
22-
// alignItems: 'center',
23-
// justifyContent: 'center',
24-
// },
25-
// box: {
26-
// width: 60,
27-
// height: 60,
28-
// marginVertical: 20,
29-
// },
30-
// });
8+
export default function App() {
9+
useEffect(() => {
10+
isSuccessScan = false;
11+
ScannerInit();
12+
const subscription = eventEmitter.addListener(
13+
'datawedge_broadcast_intent',
14+
_broadcastReceiverHandler
15+
);
16+
return () => {
17+
subscription.remove();
18+
};
19+
}, []);
3120

32-
import { StyleSheet, Text, View } from 'react-native';
33-
// import React from 'react';
21+
const _broadcastReceiverHandler = (intent: any) => {
22+
const result = ScannerReceiver(intent);
23+
if (result && !isSuccessScan) {
24+
isSuccessScan = true;
25+
console.log('result', result);
26+
setTimeout(() => {
27+
isSuccessScan = false;
28+
}, 500);
29+
}
30+
};
3431

35-
export default function App() {
3632
return (
3733
<View style={styles.container}>
3834
<Text>App</Text>

example/src/libs/scanner.ts

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import {
2+
registerBroadcastReceiver,
3+
sendBroadcastWithExtras,
4+
} from 'react-native-datawedge-intents';
5+
6+
const config_scanner = {
7+
name: 'Example',
8+
package: 'datawedgeintents.example',
9+
};
10+
11+
let ean8checked = true;
12+
let ean13checked = true;
13+
let code39checked = true;
14+
let code128checked = true;
15+
let isSendResult = false;
16+
17+
export function ScannerInit() {
18+
registerBroadcastReceiver({
19+
filterActions: [
20+
'com.zebra.reactnativedemo.ACTION',
21+
'com.symbol.datawedge.api.RESULT_ACTION',
22+
],
23+
filterCategories: ['android.intent.category.DEFAULT'],
24+
});
25+
sendCommand('com.symbol.datawedge.api.GET_VERSION_INFO', '');
26+
ScannerDecoder();
27+
}
28+
29+
export function ScannerTrigger() {
30+
sendCommand('com.symbol.datawedge.api.SOFT_SCAN_TRIGGER', 'TOGGLE_SCANNING');
31+
}
32+
33+
export function ScannerDecoder() {
34+
// NOTE: Set the new configuration
35+
const body = {
36+
PROFILE_NAME: config_scanner.name,
37+
PROFILE_ENABLED: 'true',
38+
CONFIG_MODE: 'UPDATE',
39+
PLUGIN_CONFIG: {
40+
PLUGIN_NAME: 'BARCODE',
41+
PARAM_LIST: {
42+
scanner_selection: 'auto',
43+
decoder_ean8: '' + ean8checked,
44+
decoder_ean13: '' + ean13checked,
45+
decoder_code128: '' + code128checked,
46+
decoder_code39: '' + code39checked,
47+
},
48+
},
49+
};
50+
sendCommand('com.symbol.datawedge.api.SET_CONFIG', body);
51+
}
52+
53+
function sendCommand(extraName: string, extraValue: any) {
54+
console.log(
55+
'Sending Command: ' + extraName + ', ' + JSON.stringify(extraValue)
56+
);
57+
var broadcastExtras: any = {};
58+
broadcastExtras[extraName] = extraValue;
59+
broadcastExtras.SEND_RESULT = '' + isSendResult;
60+
sendBroadcastWithExtras({
61+
action: 'com.symbol.datawedge.api.ACTION',
62+
extras: broadcastExtras,
63+
});
64+
}
65+
66+
export function ScannerReceiver(intent: any): any {
67+
console.log('Received Intent: ' + JSON.stringify(intent));
68+
if (
69+
intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_GET_VERSION_INFO')
70+
) {
71+
// NOTE: The version has been returned (DW 6.3 or higher). Includes the DW version along with other subsystem versions e.g MX
72+
const versionInfo =
73+
intent['com.symbol.datawedge.api.RESULT_GET_VERSION_INFO'];
74+
console.log('Version Info: ' + JSON.stringify(versionInfo));
75+
const datawedgeVersion = versionInfo.DATAWEDGE;
76+
console.log('Datawedge version: ' + datawedgeVersion);
77+
78+
// NOTE: Fire events sequentially so the application can gracefully degrade the functionality available on earlier DW versions
79+
if (datawedgeVersion >= '06.3') {
80+
datawedge63();
81+
}
82+
if (datawedgeVersion >= '06.4') {
83+
datawedge64();
84+
}
85+
if (datawedgeVersion >= '06.5') {
86+
datawedge65();
87+
}
88+
} else if (
89+
intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS')
90+
) {
91+
// NOTE: Return from our request to enumerate the available scanners
92+
const enumeratedScannersObj =
93+
intent['com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS'];
94+
enumerateScanners(enumeratedScannersObj);
95+
} else if (!intent.hasOwnProperty('RESULT_INFO')) {
96+
// NOTE: A barcode has been scanned
97+
const scannedData = intent['com.symbol.datawedge.data_string'];
98+
const scannedType = intent['com.symbol.datawedge.label_type'];
99+
if (scannedData) {
100+
return {
101+
data: scannedData,
102+
decoder: scannedType,
103+
timeAtDecode: new Date().toISOString(),
104+
};
105+
} else {
106+
return null;
107+
}
108+
}
109+
}
110+
111+
function datawedge63() {
112+
sendCommand('com.symbol.datawedge.api.CREATE_PROFILE', config_scanner.name);
113+
sendCommand('com.symbol.datawedge.api.GET_ACTIVE_PROFILE', ''); // NOTE: Although we created the profile we can only configure it with DW 6.4.
114+
sendCommand('com.symbol.datawedge.api.ENUMERATE_SCANNERS', ''); // NOTE: Enumerate the available scanners on the device
115+
}
116+
117+
function datawedge64() {
118+
// NOTE: Configure the created profile (associated app and keyboard plugin)
119+
const bodyAssociate = {
120+
PROFILE_NAME: config_scanner.name,
121+
PROFILE_ENABLED: 'true',
122+
CONFIG_MODE: 'UPDATE',
123+
PLUGIN_CONFIG: {
124+
PLUGIN_NAME: 'BARCODE',
125+
RESET_CONFIG: 'true',
126+
PARAM_LIST: {},
127+
},
128+
APP_LIST: [
129+
{
130+
PACKAGE_NAME: config_scanner.package,
131+
ACTIVITY_LIST: ['*'],
132+
},
133+
],
134+
};
135+
sendCommand('com.symbol.datawedge.api.SET_CONFIG', bodyAssociate);
136+
137+
// NOTE: Configure the created profile (intent plugin)
138+
const bodyIntent = {
139+
PROFILE_NAME: config_scanner.name,
140+
PROFILE_ENABLED: 'true',
141+
CONFIG_MODE: 'UPDATE',
142+
PLUGIN_CONFIG: {
143+
PLUGIN_NAME: 'INTENT',
144+
RESET_CONFIG: 'true',
145+
PARAM_LIST: {
146+
intent_output_enabled: 'true',
147+
intent_action: 'com.zebra.reactnativedemo.ACTION',
148+
intent_delivery: '2',
149+
},
150+
},
151+
};
152+
sendCommand('com.symbol.datawedge.api.SET_CONFIG', bodyIntent);
153+
154+
// NOTE: Give some time for the profile to settle then query its value
155+
setTimeout(() => {
156+
sendCommand('com.symbol.datawedge.api.GET_ACTIVE_PROFILE', '');
157+
}, 1000);
158+
}
159+
160+
function datawedge65() {
161+
// NOTE: Instruct the API to send
162+
isSendResult = true;
163+
}
164+
165+
function enumerateScanners(enumeratedScanners: any) {
166+
let humanReadableScannerList = '';
167+
for (let i = 0; i < enumeratedScanners.length; i++) {
168+
console.log(
169+
'Scanner found: name= ' +
170+
enumeratedScanners[i].SCANNER_NAME +
171+
', id=' +
172+
enumeratedScanners[i].SCANNER_INDEX +
173+
', connected=' +
174+
enumeratedScanners[i].SCANNER_CONNECTION_STATE
175+
);
176+
humanReadableScannerList += enumeratedScanners[i].SCANNER_NAME;
177+
if (i < enumeratedScanners.length - 1) {
178+
humanReadableScannerList += ', ';
179+
}
180+
}
181+
return humanReadableScannerList;
182+
}

src/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ const DatawedgeIntents = NativeModules.DatawedgeIntents
1717
}
1818
);
1919

20-
export function multiply(a: number, b: number): Promise<number> {
21-
return DatawedgeIntents.multiply(a, b);
22-
}
23-
2420
export default {
2521
ACTION_SOFTSCANTRIGGER: DatawedgeIntents.ACTION_SOFTSCANTRIGGER,
2622
ACTION_SCANNERINPUTPLUGIN: DatawedgeIntents.ACTION_SCANNERINPUTPLUGIN,

0 commit comments

Comments
 (0)