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 5
/
index.js
271 lines (243 loc) · 10.9 KB
/
index.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
var sendCommandResults = "false";
var scans = [];
var app = {
// Application Constructor
initialize: function () {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function () {
app.receivedEvent('deviceready');
document.getElementById("scanButton").addEventListener("touchstart", startSoftTrigger);
document.getElementById("scanButton").addEventListener("touchend", stopSoftTrigger);
document.getElementById('scanButton').style.display = "none";
document.getElementById('header_lastApiMessage').style.display = "none";
document.getElementById('info_lastApiMessage').style.display = "none";
document.getElementById('chk_ean8').disabled = true;
document.getElementById('chk_ean13').disabled = true;
document.getElementById('chk_code39').disabled = true;
document.getElementById('chk_code128').disabled = true;
registerBroadcastReceiver();
determineVersion();
},
onPause: function () {
console.log('Paused');
unregisterBroadcastReceiver();
},
onResume: function () {
console.log('Resumed');
registerBroadcastReceiver();
},
// Update DOM on a Received Event
receivedEvent: function (id) {
console.log('Received Event: ' + id);
}
};
app.initialize();
function startSoftTrigger() {
sendCommand("com.symbol.datawedge.api.SOFT_SCAN_TRIGGER", 'START_SCANNING');
}
function stopSoftTrigger() {
sendCommand("com.symbol.datawedge.api.SOFT_SCAN_TRIGGER", 'STOP_SCANNING');
}
function determineVersion() {
sendCommand("com.symbol.datawedge.api.GET_VERSION_INFO", "");
}
function setDecoders() {
var ean8Decoder = "" + document.getElementById('chk_ean8').checked;
var ean13Decoder = "" + document.getElementById('chk_ean13').checked;
var code39Decoder = "" + document.getElementById('chk_code39').checked;
var code128Decoder = "" + document.getElementById('chk_code128').checked;
// Set the new configuration
var profileConfig = {
"PROFILE_NAME": "ZebraCordovaDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "BARCODE",
"PARAM_LIST": {
//"current-device-id": this.selectedScannerId,
"scanner_selection": "auto",
"decoder_ean8": "" + ean8Decoder,
"decoder_ean13": "" + ean13Decoder,
"decoder_code128": "" + code128Decoder,
"decoder_code39": "" + code39Decoder
}
}
};
sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig);
}
function sendCommand(extraName, extraValue) {
console.log("Sending Command: " + extraName + ", " + JSON.stringify(extraValue));
var broadcastExtras = {};
broadcastExtras[extraName] = extraValue;
broadcastExtras["SEND_RESULT"] = sendCommandResults;
window.plugins.intentShim.sendBroadcast({
action: "com.symbol.datawedge.api.ACTION",
extras: broadcastExtras
},
function () { },
function () { }
);
}
function registerBroadcastReceiver() {
window.plugins.intentShim.registerBroadcastReceiver({
filterActions: [
'com.zebra.cordovademo.ACTION',
'com.symbol.datawedge.api.RESULT_ACTION'
],
filterCategories: [
'android.intent.category.DEFAULT'
]
},
function (intent) {
// Broadcast received
console.log('Received Intent: ' + JSON.stringify(intent.extras));
if (intent.extras.hasOwnProperty('RESULT_INFO')) {
var commandResult = intent.extras.RESULT + " (" +
intent.extras.COMMAND.substring(intent.extras.COMMAND.lastIndexOf('.') + 1, intent.extras.COMMAND.length) + ")";// + JSON.stringify(intent.extras.RESULT_INFO);
commandReceived(commandResult.toLowerCase());
}
if (intent.extras.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.extras['com.symbol.datawedge.api.RESULT_GET_VERSION_INFO'];
console.log('Version Info: ' + JSON.stringify(versionInfo));
var datawedgeVersion = versionInfo['DATAWEDGE'];
datawedgeVersion = datawedgeVersion.padStart(5, "0");
console.log("Datawedge version: " + datawedgeVersion);
// Fire events sequentially so the application can gracefully degrade the functionality available on earlier DW versions
if (datawedgeVersion >= "006.3")
datawedge63();
if (datawedgeVersion >= "006.4")
datawedge64();
if (datawedgeVersion >= "006.5")
datawedge65();
}
else if (intent.extras.hasOwnProperty('com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS')) {
// Return from our request to enumerate the available scanners
var enumeratedScannersObj = intent.extras['com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS'];
enumerateScanners(enumeratedScannersObj);
}
else if (intent.extras.hasOwnProperty('com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE')) {
// Return from our request to obtain the active profile
var activeProfileObj = intent.extras['com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE'];
activeProfile(activeProfileObj);
}
else if (!intent.extras.hasOwnProperty('RESULT_INFO')) {
// A barcode has been scanned
barcodeScanned(intent, new Date().toLocaleString());
}
}
);
}
function unregisterBroadcastReceiver() {
window.plugins.intentShim.unregisterBroadcastReceiver();
}
function datawedge63() {
console.log("Datawedge 6.3 APIs are available");
// Create a profile for our application
sendCommand("com.symbol.datawedge.api.CREATE_PROFILE", "ZebraCordovaDemo");
document.getElementById('info_datawedgeVersion').innerHTML = "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
document.getElementById('scanButton').style.display = "block";
}
function 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.
document.getElementById('info_datawedgeVersion').innerHTML = "6.4.";
document.getElementById('info_datawedgeVersion').classList.remove("attention");
// Decoders are now available
document.getElementById('chk_ean8').disabled = false;
document.getElementById('chk_ean13').disabled = false;
document.getElementById('chk_code39').disabled = false;
document.getElementById('chk_code128').disabled = false;
// Configure the created profile (associated app and keyboard plugin)
var profileConfig = {
"PROFILE_NAME": "ZebraCordovaDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "BARCODE",
"RESET_CONFIG": "true",
"PARAM_LIST": {}
},
"APP_LIST": [{
"PACKAGE_NAME": "com.zebra.datawedgecordova",
"ACTIVITY_LIST": ["*"]
}]
};
sendCommand("com.symbol.datawedge.api.SET_CONFIG", profileConfig);
// Configure the created profile (intent plugin)
var profileConfig2 = {
"PROFILE_NAME": "ZebraCordovaDemo",
"PROFILE_ENABLED": "true",
"CONFIG_MODE": "UPDATE",
"PLUGIN_CONFIG": {
"PLUGIN_NAME": "INTENT",
"RESET_CONFIG": "true",
"PARAM_LIST": {
"intent_output_enabled": "true",
"intent_action": "com.zebra.cordovademo.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(function () {
sendCommand("com.symbol.datawedge.api.GET_ACTIVE_PROFILE", "");
}, 1000);
}
function datawedge65() {
console.log("Datawedge 6.5 APIs are available");
document.getElementById('info_datawedgeVersion').innerHTML = "6.5 or higher.";
// Instruct the API to send
sendCommandResults = "true";
document.getElementById('header_lastApiMessage').style.display = "block";
document.getElementById('info_lastApiMessage').style.display = "block";
}
function commandReceived(commandText) {
document.getElementById('info_lastApiMessage').innerHTML = commandText;
}
function 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 += ", ";
}
document.getElementById('info_availableScanners').innerHTML = humanReadableScannerList;
}
function activeProfile(theActiveProfile) {
document.getElementById('info_activeProfile').innerHTML = theActiveProfile;
}
function barcodeScanned(scanData, timeOfScan) {
var scannedData = scanData.extras["com.symbol.datawedge.data_string"];
var scannedType = scanData.extras["com.symbol.datawedge.label_type"];
console.log("Scan: " + scannedData);
scans.unshift({ "data": scannedData, "decoder": scannedType, "timeAtDecode": timeOfScan });
console.log(scans);
var scanDisplay = "";
for (var i = 0; i < scans.length; i++)
{
scanDisplay += "<b><small>" + scans[i].decoder + " (" + scans[i].timeAtDecode + ")</small></b><br>" + scans[i].data + "<br><br>";
}
document.getElementById('scannedBarcodes').innerHTML = scanDisplay;
}