This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
App.js
426 lines (387 loc) · 13.9 KB
/
App.js
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, ScrollView, FlatList, TouchableHighlight, Alert} from 'react-native';
import {CheckBox, Button} from 'react-native-elements'
import { DeviceEventEmitter } from 'react-native';
import DataWedgeIntents from 'react-native-datawedge-intents';
type Props = {};
export default class App extends Component<Props> {
constructor(Props)
{
super(Props)
this.state = {
ean8checked: true,
ean13checked: true,
code39checked: true,
code128checked: true,
lastApiVisible: false,
lastApiText: "Messages from DataWedge will go here",
checkBoxesDisabled: true,
scanButtonVisible: false,
dwVersionText: "Pre 6.3. Please create and configure profile manually. See the ReadMe for more details",
dwVersionTextStyle: styles.itemTextAttention,
activeProfileText: "Requires DataWedge 6.3+",
enumeratedScannersText: "Requires DataWedge 6.3+",
scans: [],
};
//this.scans = [{decoder: 'label', timeAtDecode: 'time', data: '123'},
// {decoder: 'label', timeAtDecode: 'time', data: '321'},
// {decoder: 'label', timeAtDecode: 'time', data: '123'}];
this.sendCommandResult = "false";
}
componentDidMount()
{
this.state.deviceEmitterSubscription = DeviceEventEmitter.addListener('datawedge_broadcast_intent', (intent) => {this.broadcastReceiver(intent)});
this.registerBroadcastReceiver();
this.determineVersion();
}
componentWillUnmount()
{
this.state.deviceEmitterSubscription.remove();
}
_onPressScanButton()
{
this.sendCommand("com.symbol.datawedge.api.SOFT_SCAN_TRIGGER", 'TOGGLE_SCANNING');
}
determineVersion()
{
this.sendCommand("com.symbol.datawedge.api.GET_VERSION_INFO", "");
}
setDecoders()
{
// Set the new configuration
var profileConfig = {
"PROFILE_NAME": "ZebraReactNativeDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "BARCODE",
"PARAM_LIST": {
//"current-device-id": this.selectedScannerId,
"scanner_selection": "auto",
"decoder_ean8": "" + this.state.ean8checked,
"decoder_ean13": "" + this.state.ean13checked,
"decoder_code128": "" + this.state.code128checked,
"decoder_code39": "" + this.state.code39checked
}
}
};
this.sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig);
}
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});
}
registerBroadcastReceiver()
{
DataWedgeIntents.registerBroadcastReceiver({
filterActions: [
'com.zebra.reactnativedemo.ACTION',
'com.symbol.datawedge.api.RESULT_ACTION'
],
filterCategories: [
'android.intent.category.DEFAULT'
]
});
}
broadcastReceiver(intent)
{
// 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);
this.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 >= "06.3")
this.datawedge63();
if (datawedgeVersion >= "06.4")
this.datawedge64();
if (datawedgeVersion >= "06.5")
this.datawedge65();
this.setState(this.state);
}
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'];
this.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'];
this.activeProfile(activeProfileObj);
}
else if (!intent.hasOwnProperty('RESULT_INFO')) {
// A barcode has been scanned
this.barcodeScanned(intent, new Date().toLocaleString());
}
}
datawedge63()
{
console.log("Datawedge 6.3 APIs are available");
// Create a profile for our application
this.sendCommand("com.symbol.datawedge.api.CREATE_PROFILE", "ZebraReactNativeDemo");
this.state.dwVersionText = "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.
this.sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");
// Enumerate the available scanners on the device
this.sendCommand("com.symbol.datawedge.api.ENUMERATE_SCANNERS", "");
// Functionality of the scan button is available
this.state.scanButtonVisible = true;
}
datawedge64()
{
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.
this.state.dwVersionText = "6.4.";
this.state.dwVersionTextStyle = styles.itemText;
//document.getElementById('info_datawedgeVersion').classList.remove("attention");
// Decoders are now available
this.state.checkBoxesDisabled = 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": ["*"]
}]
};
this.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"
}
}
};
this.sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig2);
// Give some time for the profile to settle then query its value
setTimeout(() => {
this.sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");
}, 1000);
}
datawedge65()
{
console.log("Datawedge 6.5 APIs are available");
this.state.dwVersionText = "6.5 or higher.";
// Instruct the API to send
this.sendCommandResult = "true";
this.state.lastApiVisible = true;
}
commandReceived(commandText)
{
this.state.lastApiText = commandText;
this.setState(this.state);
}
enumerateScanners(enumeratedScanners)
{
var humanReadableScannerList = "";
for (var i = 0; i < enumeratedScanners.length; i++)
{
console.log("Scanner found: name= " + enumeratedScanners[i].SCANNER_NAME + ", id=" + enumeratedScanners[i].SCANNER_INDEX + ", connected=" + enumeratedScanners[i].SCANNER_CONNECTION_STATE);
humanReadableScannerList += enumeratedScanners[i].SCANNER_NAME;
if (i < enumeratedScanners.length - 1)
humanReadableScannerList += ", ";
}
this.state.enumeratedScannersText = humanReadableScannerList;
}
activeProfile(theActiveProfile)
{
this.state.activeProfileText = theActiveProfile;
this.setState(this.state);
}
barcodeScanned(scanData, timeOfScan)
{
var scannedData = scanData["com.symbol.datawedge.data_string"];
var scannedType = scanData["com.symbol.datawedge.label_type"];
console.log("Scan: " + scannedData);
this.state.scans.unshift({ data: scannedData, decoder: scannedType, timeAtDecode: timeOfScan });
console.log(this.state.scans);
this.setState(this.state);
}
render() {
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={this.state.dwVersionTextStyle}>{this.state.dwVersionText}</Text>
<Text style={styles.itemHeading}>Active Profile</Text>
<Text style={styles.itemText}>{this.state.activeProfileText}</Text>
{ this.state.lastApiVisible &&
<Text style={styles.itemHeading}>Last API message</Text>
}
{ this.state.lastApiVisible &&
<Text style={styles.itemText}>{this.state.lastApiText}</Text>
}
<Text style={styles.itemHeading}>Available scanners:</Text>
<Text style={styles.itemText}>{this.state.enumeratedScannersText}</Text>
<View style={{flexDirection: 'row', flex: 1}}>
<CheckBox
title='EAN 8'
checked={this.state.ean8checked}
disabled={this.state.checkBoxesDisabled}
onPress={() => {this.state.ean8checked = !this.state.ean8checked;this.setDecoders(); this.setState(this.state)}}
/>
<CheckBox
title='EAN 13'
checked={this.state.ean13checked}
disabled={this.state.checkBoxesDisabled}
onPress={() => {this.state.ean13checked = !this.state.ean13checked;this.setDecoders(); this.setState(this.state)}}
/>
</View>
<View style={{flexDirection: 'row', flex: 1}}>
<CheckBox
title='Code 39'
checked={this.state.code39checked}
disabled={this.state.checkBoxesDisabled}
onPress={() => {this.state.code39checked = !this.state.code39checked;this.setDecoders(); this.setState(this.state)}}
/>
<CheckBox
title='Code 128'
checked={this.state.code128checked}
disabled={this.state.checkBoxesDisabled}
onPress={() => {this.state.code128checked = !this.state.code128checked;this.setDecoders(); this.setState(this.state)}}
/>
</View>
{this.state.scanButtonVisible &&
<Button
title='Scan'
color="#333333"
buttonStyle={{
backgroundColor: "#ffd200",
height: 45,
borderColor: "transparent",
borderWidth: 0,
borderRadius: 5,
}}
onPress={() => {this._onPressScanButton()}}
/>
}
<Text style={styles.itemHeading}>Scanned barcodes will be displayed here:</Text>
<FlatList
data={this.state.scans}
extraData={this.state}
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',
}
});