Zebra Printer Code
Zebra Printer Code
Preparation
Using the Print APIs requires inclusion of individual print modules eb.printer.js and eb.printerzebra.js, which ar
1. Confirm that the following JavaScript is in the HEAD section of the sample app's HTML file:
<script type="text/javascript" charset="utf-8" src="ebapi-modules.js"></script>
For more information about how to include API modules, please refer to the Printing API.
Next we'll add a Print button to the main HTML form add a placeholder for displaying print status and log messages
2. To add a Print button, insert the following two lines of JavaScript as indicated in the comments below:
<button onClick="EB.Application.quit()">Quit</button>
// ...and the </BODY> tag, which is near the very bottom of the file.
</BODY>
When finished, this portion of the HTML should look like this:
//Here's roughly how the code should look after adding the 'Print' button:
</BODY>
3. Save the change and reload the page. A new 'Print' button will appear next to 'Reload' and 'Quit' buttons, simila
myPrinter.connect(connect_callback)
myPrinter.print...(<data>)
function print_ticket() {
EB.PrinterZebra.searchPrinters (<search params here>, function(cb) {
// If connection is successful,
// check printer state and set options and parameters.
// Upload images and format files.
// If printer is ready, print.
myPrinter.printRawString('^XA^FO20,20^AD^FDTest^XZ',{},function(cb) {
{"deviceAddress":"1.2.3.4","devicePort":6101,"connectionType":EB.Printer.CONNECTION_TYPE_TCP}
If the IP address is not specified, the device will sweep the entire /8 (255.0.0.0) subnet, and generally cause a long d
Bluetooth with specific MAC Address:
{"deviceAddress":"00225898D8CB","connectionType":EB.Printer.CONNECTION_TYPE_BLUETOOTH}
If no Bluetooth MAC address is specified, all devices within range--headsets, laptops, TVs and printers-–will be retur
NOTE: The Android security model does provide a simple means of pairing programmatically; pairing must
USB:
{"connectionType":EB.Printer.CONNECTION_TYPE_USB}
For USB connections, device must be in 'USB Host mode' before the app is started. For more more information
If unable to print, please check the following assumptions:
Only a single printer is connected
Printer is accessible and/or 'discoverable' (over BT, etc.)
Printer connection is always successful
Printer is loaded with media and ready to print
Media is of the right type, size, etc.
Printer is correctly pre-configured to work with the data we want to print (all the settings and options, correct comma
Adding flesh to the bones
In the previous section we created the “skeleton” code for printing. The problems in this proof-of-concept app can be
1. Numerous assumptions
2. Does not reflect real-world logic and flow
3. Nested inline functions make inflexible and hard to manage
Let’s deal with problems two and three first. Currently, we have three inline callbacks:
function print_ticket() {
EB.PrinterZebra.searchPrinters (<search params here>, function(cb) {
var myPrinter = EB.PrinterZebra.getPrinterByID(cb.printerID)
myPrinter.connect(function(cb){
myPrinter.printRawString('^XA^FO20,20^AD^FDTest^XZ',{},function(cb) {
})
})
})
}
Aside from being inflexible and hard to read, the code also has a fatal flaw; the first callback (for searchPrinters) will
So let's disentangle everything:
function print_ticket() {
EB.PrinterZebra.searchPrinters (<search params here>, search_callback)
}
function search_callback(cb) {
var myPrinter = EB.PrinterZebra.getPrinterByID(cb.printerID)
myPrinter.connect(connect_callback)
}
function connect_callback(cb) {
myPrinter.printRawString(<string>,{},print_callback)
}
function print_callback(cb) {
}
Next, let’s think logically about adding printing capability to an app:
Does it make sense to begin searching for printers only after user has pressed 'Print'?
If not, when is the best time to search for and connect to a printer?
How much time will the search for a printer require?
How will that delay affect the user experience?
Should the Print button be disabled until a printer is connected?
What should be displayed before a printer is selected?
When a single printer is connected via USB cable, most of these questions become irrelevant. But when Wi-Fi or Blu
One approach is to search for printers as soon as the app loads and determine actions based on how many are foun
1. If no printers are found, disable print functionality (if the business process allows it).
2. If a single printer is found, connect to it.
3. If more than one printer is found, prompt the user to select the desired printer.
In our example, we decided to disable the Print button upon page load, enabling it only if a suitable printer is found.
// will hold all the printers found if more than one is expected
// global var used for simplicity
var printers_array = []
var printers_array = [] // will hold all the printers found, if we expect more than one
var myPrinter = null // this will be the printer to use
function onLoad () { // called when the page loads
$('#PrintBtn').disabled = true
v_out('#print_status', "Searching for printers")
EB.PrinterZebra.searchPrinters ({"timeout":10000, <other params>}, search_callback)
var PrinterTimeout = setTimeout(process_found_printers, 10500)
}
function process_found_printers() {
EB.PrinterZebra.stopSearch()
myPrinter.connect(connect_callback)
}
Now our code actually can search for printers and connect to the chosen one. Once a successful connection is mad
function print_ticket() { myPrinter.printRawString(<string>,{},print_callback) }
Our code now connects to a printer one time and can print information from different parts of the app without additio
Working with the printer
Now that a printer is found and connected, we want to ensure it’s properly set up and ready to print. Typical tasks an
1. Check that the printer is correctly configured in terms of control language, printer settings, media settings, etc
getAllProperties(), getProperties(), getProperty()
setProperties(), setProperty()
enumerateSupportedControlLanguages()
2. If printing images and/or ZPL templates (aka 'formats'), check that they're resident in the printer (and push if n
retrieveFileNames(), retrieveFileNamesWithExtensions()
storeImage()
3. Check for printer readiness (i.e. media present, all panels closed, etc.):
requestState()
4. Did the job print successfully (print callback)?
Consider which of these operations must be performed:
Just once when the app loads
Prior to sending every print job
After sending every print job
Unless reconfiguring the printer for varied tasks, the first two will most likely be required just when the app initially lo
Conclusion
This completes the Ente
as well as some of the typical considerations when working with USB and wireless printers.
of the main training module and several additional EB tutorials is recommended (see below). This tutorial enables the sample a
ent knowledge)
calls are callback-based and the process follows the Search → Connect → Initialize → Act formula used by other Zebra APIs th
mpleted that training or didn't save the app, please download the sample app now.
t status and log messages and alerts. Such feedback is important for letting the user know of printing progress and potential co
the comments below:
code will actually print something to the connected printer. Below are the printer connection types and the sample search param
CTION_TYPE_TCP}
nd generally cause a long delay.
s and printers-–will be returned. EB cannot identify a printing device until after it has paired with one.
mmatically; pairing must be performed manually.
For more more information about this and other connection types, defaults and [parameters], please refer to the Printer API refe
ack (for searchPrinters) will be called for every printer. While it will work perfectly well for a single printer, it might waste time atte
vant. But when Wi-Fi or Bluetooth are involved, or if the app is intended for more general purposes, these issues become impo
ased on how many are found (0, 1 or >1):
a suitable printer is found. Here’s what that sample code might look like:
es that we need. Now, how would we actually know that the search is over and choose the printer to use? Since the code is asy
ccessful connection is made, we can enable printing functionality. Then our print_ticket() function will be simplified to the followi
s of the app without additional overhead. However, we still have several assumptions about the printer state and its configuratio
ust when the app initially loads, and the latter two before and after every job.
utorial enables the sample app from the MBS1018 lesson to print a ticket with a barcode that contains a summary of error data
nter, it might waste time attepmting to connect and print if more than one printer is found.
these issues become important to the user experience.
o use? Since the code is async, we will need to specify search timeout as part of parameters, and then trigger an async timed fu
ll be simplified to the following, which makes a lot more sense:
ter state and its configuration. We’ll deal with those next.
ns a summary of error data to a printer attached directly to the mobile device.
endly than bombarding them with alerts and pop-ups. We will these objects right after the quit button.
en trigger an async timed function (using JS standard SetTimeout() call). The default timeout is 30 seconds. We’ll reduce it to 1
seconds. We’ll reduce it to 10.