forked from darryncampbell/DataWedgeReactNative
-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
438 lines (397 loc) · 14.6 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, {Component, useState, useEffect, useRef, useReducer} from 'react';
import {Platform, StyleSheet, Text, View, ScrollView, FlatList, TouchableHighlight, Alert, CheckBox, Button, NativeEventEmitter} from 'react-native';
import { DeviceEventEmitter } from 'react-native';
import DataWedgeIntents from 'react-native-datawedge-intents';
import { useDataWedgeScanHandler } from './DataWedgeScanHandler';
export default function App() {
let broadcastReceiverHandler: any = useRef(null);
const [ean8checked, setean8checked] = useState(true);
const [ean13checked, setean13checked] = useState(true);
const [code39checked, setcode39checked] = useState(true);
const [code128checked, setcode128checked] = useState(true);
const [lastApiVisible, setlastApiVisible] = useState(false);
const [lastApiText, setlastApiText] = useState("Messages from DataWedge will go here");
const [enumeratedScannersText, setenumeratedScannersText] = useState("Requires DataWedge 6.3+");
const [checkBoxesDisabled, setcheckBoxesDisabled] = useState(true);
const [scanButtonVisible, setscanButtonVisible] = useState(false);
const [dwVersionText, setdwVersionText] = useState("Pre 6.3. Please create and configure profile manually. See the ReadMe for more details");
const [dwVersionTextStyle, setdwVersionTextStyle] = useState(styles.itemTextAttention);
const [activeProfileText, setactiveProfileText] = useState("Requires DataWedge 6.3+");
const [scans, setscans] = useState(Array());
//const [sendCommandResult, setsendCommandResult] = useState("false");
const [dataWedgeState, dispatch] = useDataWedgeScanHandler({
appNamespace: "com.datawedgereactnative.demo"
});
const _onPressScanButton:any = () =>
{
console.log("OnPressScanButton");
dispatch({ type: 'ToggleScan'});
}
const registerBroadcastReceiver : any = () =>
{
DataWedgeIntents.registerBroadcastReceiver({
filterActions: [
'com.zebra.reactnativedemo.ACTION',
'com.symbol.datawedge.api.RESULT_ACTION'
],
filterCategories: [
'android.intent.category.DEFAULT'
]
});
}
const broadcastReceiver:any = (intent: any) =>
{
// Broadcast received
console.log('Received Intent: ' + JSON.stringify(intent));
if (intent.hasOwnProperty('RESULT_INFO')) {
var commandResult = intent.RESULT + " (" +
intent.COMMAND.substring(intent.COMMAND.lastIndexOf('.') + 1, intent.COMMAND.length) + ")";// + JSON.stringify(intent.RESULT_INFO);
commandReceived(commandResult.toLowerCase());
}
if (intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_GET_VERSION_INFO')) {
// The version has been returned (DW 6.3 or higher). Includes the DW version along with other subsystem versions e.g MX
var versionInfo = intent['com.symbol.datawedge.api.RESULT_GET_VERSION_INFO'];
console.log('Version Info: ' + JSON.stringify(versionInfo));
var datawedgeVersion = versionInfo['DATAWEDGE'];
console.log("Datawedge version: " + datawedgeVersion);
// Fire events sequentially so the application can gracefully degrade the functionality available on earlier DW versions
if (datawedgeVersion >= "6.3")
datawedge63();
if (datawedgeVersion >= "6.4")
datawedge64();
if (datawedgeVersion >= "6.5")
datawedge65();
}
else if (intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS')) {
// Return from our request to enumerate the available scanners
var enumeratedScannersObj = intent['com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS'];
enumerateScanners(enumeratedScannersObj);
}
else if (intent.hasOwnProperty('com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE')) {
// Return from our request to obtain the active profile
var activeProfileObj = intent['com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE'];
activeProfile(activeProfileObj);
}
else if (!intent.hasOwnProperty('RESULT_INFO')) {
// A barcode has been scanned
barcodeScanned(intent, new Date().toLocaleString());
}
}
const datawedge63:any = () =>
{
console.log("Datawedge 6.3 APIs are available");
// Create a profile for our application
sendCommand("com.symbol.datawedge.api.CREATE_PROFILE", "ZebraReactNativeDemo");
setdwVersionText("6.3. Please configure profile manually. See ReadMe for more details.");
// Although we created the profile we can only configure it with DW 6.4.
sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");
// Enumerate the available scanners on the device
sendCommand("com.symbol.datawedge.api.ENUMERATE_SCANNERS", "");
// Functionality of the scan button is available
setscanButtonVisible(true);
}
const datawedge64:any = () =>
{
console.log("Datawedge 6.4 APIs are available");
// Documentation states the ability to set a profile config is only available from DW 6.4.
// For our purposes, this includes setting the decoders and configuring the associated app / output params of the profile.
setdwVersionText("6.4.");
setdwVersionTextStyle({...styles.itemText, backgroundColor: "white"});
// Decoders are now available
setcheckBoxesDisabled(false);
// Configure the created profile (associated app and keyboard plugin)
var profileConfig = {
"PROFILE_NAME": "ZebraReactNativeDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "BARCODE",
"RESET_CONFIG": "true",
"PARAM_LIST": {}
},
"APP_LIST": [{
"PACKAGE_NAME": "com.datawedgereactnative.demo",
"ACTIVITY_LIST": ["*"]
}]
};
sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig);
// Configure the created profile (intent plugin)
var profileConfig2 = {
"PROFILE_NAME": "ZebraReactNativeDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "INTENT",
"RESET_CONFIG": "true",
"PARAM_LIST": {
"intent_output_enabled": "true",
"intent_action": "com.zebra.reactnativedemo.ACTION",
"intent_delivery": "2"
}
}
};
sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig2);
// Give some time for the profile to settle then query its value
setTimeout(() => {
sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");
}, 1000);
}
const datawedge65:any = () =>
{
console.log("Datawedge 6.5 APIs are available");
setdwVersionText("6.5 or higher.");
// Instruct the API to send
//TODO: uncomment
//setsendCommandResult("true");
setlastApiVisible(true);
}
const commandReceived:any = (commandText: any) =>
{
console.log("Last api text set");
setlastApiText(commandText);
}
// TODO: enumeratedScanners should be a data type.
const enumerateScanners:any = (enumeratedScanners: any) =>
{
setenumeratedScannersText(enumeratedScanners.map((s: any) =>
{
console.log("Scanner found: name= " + s.SCANNER_NAME + ", id=" + s.SCANNER_INDEX + ", connected=" + s.SCANNER_CONNECTION_STATE);
return s.SCANNER_NAME;
}
).join(", "));
}
const activeProfile:any = (theActiveProfile: string) =>
{
setactiveProfileText(theActiveProfile);
}
const prepend = (array:any[], value:any) :any[] => {
var newArray = array.slice();
newArray.unshift(value);
return newArray;
}
const barcodeScanned:any = (scanData: any, timeOfScan: any) =>
{
var scannedData = scanData["com.symbol.datawedge.data_string"];
var scannedType = scanData["com.symbol.datawedge.label_type"];
console.log("Scan: " + scannedData);
var scanArray = scans;
scanArray;
setscans(scans => prepend(scans, { data: scannedData, decoder: scannedType, timeAtDecode: timeOfScan }));
}
const setDecoders = () => {
// Set the new configuration
var profileConfig = {
"PROFILE_NAME": "ZebraReactNativeDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "BARCODE",
"PARAM_LIST": {
"scanner_selection": "auto",
"decoder_ean8": "" + ean8checked,
"decoder_ean13": "" + ean13checked,
"decoder_code128": "" + code128checked,
"decoder_code39": "" + code39checked
}
}
};
sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig);
}
const sendCommand:any = (extraName: string, extraValue: any): any => {
console.log("Sending Command: " + extraName + ", " + JSON.stringify(extraValue));
var broadcastExtras: any = {};
broadcastExtras[extraName] = extraValue;
broadcastExtras["SEND_RESULT"] = "true";
// TODO: fix this command setting.
//broadcastExtras["SEND_RESULT"] = sendCommandResult;
DataWedgeIntents.sendBroadcastWithExtras({
action: "com.symbol.datawedge.api.ACTION",
extras: broadcastExtras});
}
const determineVersion:any = () =>
{
console.log("Determine Version");
sendCommand("com.symbol.datawedge.api.GET_VERSION_INFO", "");
}
const [eventEmitter, setEventEmitter] = useState(new NativeEventEmitter(DataWedgeIntents));
const intentHandler = useEffect(() =>
{
eventEmitter.addListener('datawedge_broadcast_intent', broadcastReceiverHandler.current);
return (() => {
eventEmitter.removeListener('datawedge_broadcast_intent', broadcastReceiverHandler.current);
})
}, ["hot"]);
if (broadcastReceiverHandler.current == null)
{
broadcastReceiverHandler.current = (intent:any) =>
{
broadcastReceiver(intent);
}
registerBroadcastReceiver();
}
const [hasCurrentVersion, sethasCurrentVersion] = useState(false);
if (hasCurrentVersion == false)
{
determineVersion();
sethasCurrentVersion(true);
}
return (
<ScrollView>
<View style={styles.container}>
<Text style={styles.h1}>Zebra ReactNative DataWedge Demo</Text>
<Text style={styles.h3}>Information / Configuration</Text>
<Text style={styles.itemHeading}>DataWedge version:</Text>
<Text style={dwVersionTextStyle}>{dwVersionText}</Text>
<Text style={styles.itemHeading}>Active Profile</Text>
<Text style={styles.itemText}>{activeProfileText}</Text>
{ lastApiVisible &&
<Text style={styles.itemHeading}>Last API message</Text>
}
{ lastApiVisible &&
<Text style={styles.itemText}>{lastApiText}</Text>
}
<Text style={styles.itemHeading}>Available scanners:</Text>
<Text style={styles.itemText}>{enumeratedScannersText}</Text>
<View style={{flexDirection: 'row', flex: 1, alignItems:'center', alignContent: "space-around", justifyContent: "space-evenly"}}>
<Text style={{alignContent: 'center', alignSelf: 'center', width: 70}}>EAN 8</Text>
<CheckBox
value={ean8checked}
onValueChange={async (val) => {
await setean8checked(val);
setDecoders();
}}
/>
<Text style={{alignContent: 'center', alignSelf: 'center', width: 70}}>EAN 13</Text>
<CheckBox
value={ean13checked}
disabled={checkBoxesDisabled}
onValueChange={async (val) => {
await setean13checked(val);
setDecoders();
}}
/>
</View>
<View style={{flexDirection: 'row', flex: 1, alignItems:'center', alignContent: "space-around", justifyContent:'space-evenly'}}>
<Text style={{alignContent: 'center', alignSelf: 'center', width: 70}}>EAN 39</Text>
<CheckBox
value={code39checked}
disabled={checkBoxesDisabled}
onValueChange={async (val) => {
await setcode39checked(val);
setDecoders();
}}
/>
<Text style={{alignContent: 'center', alignSelf: 'center', width: 70}}>EAN 128</Text>
<CheckBox
value={code128checked}
disabled={checkBoxesDisabled}
onValueChange={async (val) => {
await setcode128checked(val);
setDecoders();
}}
/>
</View>
{scanButtonVisible &&
<Button
title='Scan'
color="#333333"
onPress={() => {_onPressScanButton()}}
/>
}
<Text style={styles.itemHeading}>Scanned barcodes will be displayed here. Scanned barcodes: {scans.length}</Text>
<FlatList
data={scans}
extraData={scans}
keyExtractor={item => item.timeAtDecode}
renderItem={({item, separators}) => (
<TouchableHighlight
onShowUnderlay={separators.highlight}
onHideUnderlay={separators.unhighlight}>
<View style={{
backgroundColor: '#0077A0',
margin:10,
borderRadius: 5,
}}>
<View style={{flexDirection: 'row', flex: 1}}>
<Text style={styles.scanDataHead}>{item.decoder}</Text>
<View style={{flex: 1}}>
<Text style={styles.scanDataHeadRight}>{item.timeAtDecode}</Text>
</View>
</View>
<Text style={styles.scanData}>{item.data}</Text>
</View>
</TouchableHighlight>
)}
/>
</View>
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
// justifyContent: 'center',
// alignItems: 'center',
backgroundColor: '#F5FCFF',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
h1: {
fontSize: 20,
textAlign: 'center',
margin: 5,
fontWeight: "bold",
},
h3: {
fontSize: 14,
textAlign: 'center',
margin: 10,
fontWeight: "bold",
},
itemHeading: {
fontSize: 12,
textAlign: 'left',
left: 10,
fontWeight: "bold",
},
itemText: {
fontSize: 12,
textAlign: 'left',
margin: 10,
},
itemTextAttention: {
fontSize: 12,
textAlign: 'left',
margin: 10,
backgroundColor: '#ffd200'
},
scanDataHead: {
fontSize: 10,
margin: 2,
fontWeight: "bold",
color: 'white',
},
scanDataHeadRight: {
fontSize: 10,
margin: 2,
textAlign: 'right',
fontWeight: "bold",
color: 'white',
},
scanData: {
fontSize: 16,
fontWeight: "bold",
textAlign: 'center',
margin: 2,
color: 'white',
}
});