The PrinterZebra API provides Zebra-specific API access to Zebra’s Bluetooth and Wi-Fi network printers, and to USB-capable printers for Android and Windows Mobile/CE. This API inherits all methods, properties and constants from the Printer API.
To use this API you must include the following extensions in your build.yml
:
extensions: ["printing","printing_zebra"]
Be sure to review the JavaScript API Usage guide for important information about using this API in JavaScript.
Be sure to review the Ruby API Usage guide for important information about using this API in Ruby.
Connect to a printer using default options. Works asynchronously; use callback to check the result. If connection is successful printer state and properties are automatically updated. **Note: Default options are defined by printer SDK and platform and may vary between different devices.
Parameters
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.connect(CallBackHandler callback)
Rho.PrinterZebra.connect(CallBackHandler callback)
Rho::PrinterZebra.connect(CallBackHandler callback)
Connect to a printer with user options. Works asynchronously; use callback to check the result. After this function call, the printer state is automatically updated.
Parameters
Connect options.
Connection timeout in milliseconds. Must be larger than 0.
Platforms:WM, CE, iOS, AndroidAny additional info; currently unused.
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.connectWithOptions(HASH options, CallBackHandler callback)
Rho.PrinterZebra.connectWithOptions(HASH options, CallBackHandler callback)
Rho::PrinterZebra.connectWithOptions(HASH options, CallBackHandler callback)
Disconnect from a printer and release OS resources. Works asynchronously; use callback to check the result.
Parameters
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.disconnect(CallBackHandler callback)
Rho.PrinterZebra.disconnect(CallBackHandler callback)
Rho::PrinterZebra.disconnect(CallBackHandler callback)
This method will return all of object/value pairs for the propertyNames of the API class.
Parameters
Async Callback Returning Parameters: HASH
Synchronous Return:
Map of all available properties
: this method also supports async callbacks - check the Callback tab for callback return parameters.
Method Access:
myObject.getAllProperties(CallBackHandler callback)
Rho.PrinterZebra.getAllProperties(CallBackHandler callback)
Rho::PrinterZebra.getAllProperties(CallBackHandler callback)
This method will return an object that represents the default instance of the API Class. For example Camera.getDefault will return a Camera object that represents the default camera.
Synchronous Return:
Default object of Module.
Method Access:
Rho.PrinterZebra.getDefault()
Rho::PrinterZebra.getDefault()
Returns printer instance specified by printerId. Returns null is no printers contains the ID. To get valid printerId, use searchPrinters function.
Parameters
Synchronous Return:
Method Access:
Rho.PrinterZebra.getPrinterByID(STRING printerId)
Rho::PrinterZebra.getPrinterByID(STRING printerId)
This method will return a set of object/value pairs for the list of the propertyName that is passed in. The propertyNames must be a valid property of the API class.
Parameters
List of properties I want to know about
Async Callback Returning Parameters: HASH
Synchronous Return:
Map of properties I want to know about
: this method also supports async callbacks - check the Callback tab for callback return parameters.
Method Access:
myObject.getProperties(ARRAY arrayofNames, CallBackHandler callback)
Rho.PrinterZebra.getProperties(ARRAY arrayofNames, CallBackHandler callback)
Rho::PrinterZebra.getProperties(ARRAY arrayofNames, CallBackHandler callback)
This method will return the value of the propertyName that is passed in. The propertyName must be a valid property of the API class.
Parameters
The property to return info about.
Async Callback Returning Parameters: STRING
Synchronous Return:
The property to return info about.
: this method also supports async callbacks - check the Callback tab for callback return parameters.Method Access:
myObject.getProperty(STRING propertyName, CallBackHandler callback)
Rho.PrinterZebra.getProperty(STRING propertyName, CallBackHandler callback)
Rho::PrinterZebra.getProperty(STRING propertyName, CallBackHandler callback)
Prints file. Works asynchronously; use callback to check the result. File extension is used to determine its type. Supported image types: JPEG and PNG.
Parameters
Full path to local image file on the device.
Providing no properties to this function will print with default settings. Reserved for future use.
This is currently not being used.
Currently unused.
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.printFile(STRING filePathOnDevice, HASH options, CallBackHandler callback)
Rho.PrinterZebra.printFile(STRING filePathOnDevice, HASH options, CallBackHandler callback)
Rho::PrinterZebra.printFile(STRING filePathOnDevice, HASH options, CallBackHandler callback)
Prints an image from a device’s file system to the connected printer as a monochrome image. Works asynchronously; use callback to check the result. Images larger than 1024x768 might take a long time or print incorrectly. Consult printer documentation for image printing parameters.
Parameters
Full path to local image file on the device. (The image must be either a PNG or JPG, all other types are platform depend).
Horizontal starting position in dots.
Vertical starting position in dots.
Provide a set of optional parameters.
Desired width of the printed image. Passing -1 will preserve original width.
Desired height of the printed image. Passing -1 will preserve original height.
Boolean value indicating whether this image should be printed by itself (false), or is part of a format being written to the connection (true).
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.printImageFromFile(STRING filePathOnDevice, INTEGER x, INTEGER y, HASH options, CallBackHandler callback)
Rho.PrinterZebra.printImageFromFile(STRING filePathOnDevice, INTEGER x, INTEGER y, HASH options, CallBackHandler callback)
Rho::PrinterZebra.printImageFromFile(STRING filePathOnDevice, INTEGER x, INTEGER y, HASH options, CallBackHandler callback)
Send raw string to printer. Works asynchronously, use callback to check the result. **Note: This method will not print ZPL commands from Windows Mobile/CE devices to Zebra printers; compatible with line mode or raw text only.
Parameters
Raw string to print. Could be any valid command in printer supported programming language.
Providing no properties to this function will print with default settings.
This is currently not being used.
Currently unused.
Async Callback Returning Parameters: HASH
PRINTER_STATUS_SUCCESS, PRINTER_STATUS_ERROR.
Error message, only if status = ERROR.
Response from printer if received, converted to string from byteArray(original data)
Platforms:Android, WMResponse from printer if received.
Platforms:Android
Synchronous Return:
Method Access:
myObject.printRawString(STRING command, HASH options, CallBackHandler callback)
Rho.PrinterZebra.printRawString(STRING command, HASH options, CallBackHandler callback)
Rho::PrinterZebra.printRawString(STRING command, HASH options, CallBackHandler callback)
Prints a stored format on the printer, filling in the fields specified by the array. Works asynchronously; use callback to check the result.
Parameters
The location of the file on the printer (e.g. “E:FORMAT.ZPL”).
An array of strings representing the data to fill into the format. For ZPL formats, index 0 of the array corresponds to field number 2 (FN2). For CPCL, the variables are passed in the order that they are found in the format
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.printStoredFormatWithArray(STRING formatPathOnPrinter, Array vars, CallBackHandler callback)
Rho.PrinterZebra.printStoredFormatWithArray(STRING formatPathOnPrinter, Array vars, CallBackHandler callback)
Rho::PrinterZebra.printStoredFormatWithArray(STRING formatPathOnPrinter, Array vars, CallBackHandler callback)
Prints a stored format on the printer, filling in the fields specified by the Hash. Works asynchronously; use callback to check the result.
Parameters
The location of the file on the printer (e.g. “E:FORMAT.ZPL”).
A Hash containing the key / value pairs for the stored format. For ZPL formats, the key number should correspond directly to the number of the field in the format. Number keys should be passed as Strings, i.e. ‘1’:‘field1’ etc. Only ZPL format is supported; CPCL format is not supported.
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.printStoredFormatWithHash(STRING formatPathOnPrinter, HASH vars, CallBackHandler callback)
Rho.PrinterZebra.printStoredFormatWithHash(STRING formatPathOnPrinter, HASH vars, CallBackHandler callback)
Rho::PrinterZebra.printStoredFormatWithHash(STRING formatPathOnPrinter, HASH vars, CallBackHandler callback)
Requests printer state with a list of parameters. Works asynchronously and uses the callback to check the result. Returns hash with status and parameters as keys. **
On the Windows Mobile / Windows CE platform, this method does not work in the case of a Bluetooth and USB connection. |
Parameters
List of parameters for request from printer device.
Parameter name – see constants with PRINTER_STATE_…
Async Callback Returning Parameters: HASH
PRINTER_STATUS_SUCCESS, PRINTER_STATUS_ERROR.
Error message, only if status = ERROR.
Currently unused.
Synchronous Return:
Method Access:
myObject.requestState(ARRAY listOfParameters, CallBackHandler callback)
Rho.PrinterZebra.requestState(ARRAY listOfParameters, CallBackHandler callback)
Rho::PrinterZebra.requestState(ARRAY listOfParameters, CallBackHandler callback)
Retrieves the names of the files which are stored on the printer. Works asynchronously; use callback to check the result.
Parameters
Async Callback Returning Parameters: HASH
PRINTER_STATUS_SUCCESS, PRINTER_STATUS_ERROR
Synchronous Return:
Method Access:
myObject.retrieveFileNames(CallBackHandler callback)
Rho.PrinterZebra.retrieveFileNames(CallBackHandler callback)
Rho::PrinterZebra.retrieveFileNames(CallBackHandler callback)
This method only returns files which have the extensions in “extensions”. Works asynchronously; use callback to check the result.
Parameters
The list of extensions that should be returned.
Async Callback Returning Parameters: HASH
PRINTER_STATUS_SUCCESS, PRINTER_STATUS_ERROR
Synchronous Return:
Method Access:
myObject.retrieveFileNamesWithExtensions(ARRAY extensions, CallBackHandler callback)
Rho.PrinterZebra.retrieveFileNamesWithExtensions(ARRAY extensions, CallBackHandler callback)
Rho::PrinterZebra.retrieveFileNamesWithExtensions(ARRAY extensions, CallBackHandler callback)
Searches for printers connected to the device via Bluetooth®, Wi-Fi or USB. To reduce search times, it is highly recommended that searches for wireless connections be done by specific MAC address (Bluetooth) or IP address (Wi-Fi). This method also can be used to retrieve the printerID for a known Bluetooth or network printer specifying the deviceAddress
options parameter. Result is returned asynchronously using a callback called for each discovered printer. Search is finished if printerID is not set in callback hash. Note that discovery is not guaranteed to return all the available devices. It is therefore recommended that this method run 2-3 times for maximum accuracy.
**NOTE**: On certain Android devices, calling searchPrinters() over Bluetooth or TCP can cause the application to freeze momentarily or to display an alert that "The application has stopped responding." In such cases, it is recommended that the user tap the "Continue waiting" button.
Parameters
Options for discover.
Printer types to search. Make sure that Printer type is supported by calling enumerateSupportedTypes method.
Interfaces for search (Bluetooth/TCP/All).
Maximum search interval in milliseconds, applies to network discovery. This is the maximum interval for wait during connection attempt. Note that if no printer was found, even with timeout property, status will be set to PRINTER_STATUS_SUCCESS, but without any printerId.
Printer address (MAC, device serial number or IP address) can be used for setting either subnet mask or full address of printer. For TCP if address is not specified /8 subnet will be used for search.
Override default printer port number. Applicable for network discovery only.
Async Callback Returning Parameters: HASH
Status of network discovery, can be the following: * PRINTER_STATUS_SUCCESS – when printer is discovered or operation is finished; * PRINTER_STATUS_ERROR – general error * PRINTER_STATUS_ERR_UNSUPPORTED – in case if printer type is not supported * PRINTER_STATUS_ERR_NOT_CONNECTED – in case if deviceAddress was specified in options and device was unable to connect to printer.
**Printer ID, valid only if status equals = ‘PRINTER_STATUS_SUCCESS’. If no more printers are available, printerID will be undefined.
Error message if status = ERROR**.
Synchronous Return:
Method Access:
Rho.PrinterZebra.searchPrinters(HASH options, CallBackHandler callback)
Rho::PrinterZebra.searchPrinters(HASH options, CallBackHandler callback)
Sends the contents of a file on the device to the printer. Equals to printing raw string with content of specified file. Should be used to print ZPL and CPCL commands stored inside predefined filed. Works asynchronously; use callback to check the result.
Parameters
Location of file to print. This path must be fully qualified path to local file on the device.
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.sendFileContents(STRING path, CallBackHandler callback)
Rho.PrinterZebra.sendFileContents(STRING path, CallBackHandler callback)
Rho::PrinterZebra.sendFileContents(STRING path, CallBackHandler callback)
This method allows you to set the attributes of the default object instance by passing in an object of the same class.
Parameters
An instance object that is of the same class.
Synchronous Return:
Method Access:
Rho.PrinterZebra.setDefault(SELF_INSTANCE: Rho::PrinterZebra defaultInstance)
Rho::PrinterZebra.setDefault(SELF_INSTANCE: Rho::PrinterZebra defaultInstance)
This method will set the values of a list of properties for the API class. The propertyName must be a valid property for the class and must also not be read only.
Parameters
Map of properties I want to set
Synchronous Return:
Method Access:
myObject.setProperties(HASH propertyMap)
Rho.PrinterZebra.setProperties(HASH propertyMap)
Rho::PrinterZebra.setProperties(HASH propertyMap)
This method will set the value of a property for the API class. The propertyName must be a valid property for the class and must also not be read only.
Parameters
The one property name that I want to set
The one property value that I want to set
Synchronous Return:
Method Access:
myObject.setProperty(STRING propertyName, STRING propertyValue)
Rho.PrinterZebra.setProperty(STRING propertyName, STRING propertyValue)
Rho::PrinterZebra.setProperty(STRING propertyName, STRING propertyValue)
Immediately stops all active search requests and cancels pending search requests. Does not return a result.
Synchronous Return:
Method Access:
Rho.PrinterZebra.stopSearch()
Rho::PrinterZebra.stopSearch()
Stores the specified image to the connected printer as a monochrome image. Works asynchronously; use callback to check the result. The image will be stored on the printer at printerDriveAndFileName with the extension GRF. If a drive letter is not supplied, E will be used as the default (e.g. FILE becomes E:FILE.GRF). If an extension is supplied, it is ignored (E:FILE.BMP becomes E:FILE.GRF). Note that if the image resolution is large (e.g. 1024x768) this method may take a long time to execute. There are device OS limitations on supported file formats and image sizes. iOS, Android and WM are supporting only JPG and PNG files. WM can fail to handle large (>1024x1024 pixel) images.
Parameters
Path on the printer where the image will be stored.
Path to the local image file containing the bitmap image to send.
Desired width of the printed image, in dots. Passing -1 will preserve original width.
Desired height of the printed image, in dots. Passing -1 will preserve original height.
Async Callback Returning Parameters: STRING
Synchronous Return:
Method Access:
myObject.storeImage(STRING printerDriveAndFileName, STRING imageFilePathOnDevice, INTEGER width, INTEGER height, CallBackHandler callback)
Rho.PrinterZebra.storeImage(STRING printerDriveAndFileName, STRING imageFilePathOnDevice, INTEGER width, INTEGER height, CallBackHandler callback)
Rho::PrinterZebra.storeImage(STRING printerDriveAndFileName, STRING imageFilePathOnDevice, INTEGER width, INTEGER height, CallBackHandler callback)
Unique printer ID during application lifetime.
Property Access:
myObject.ID
Rho.PrinterZebra.ID
Rho::PrinterZebra.ID
Type of connection to printer.
Property Access:
myObject.connectionType
Rho.PrinterZebra.connectionType
Rho::PrinterZebra.connectionType
IP address for Wi-Fi connection, Bluetooth® MAC address or device serial number. Note: iOS uses device serial number instead of MAC address. If you need to get device MAC you can use CPCL command ! U1 getvar “bluetooth.address”
Property Access:
myObject.deviceAddress
Rho.PrinterZebra.deviceAddress
Rho::PrinterZebra.deviceAddress
Bluetooth/Network name of printer.
Property Access:
myObject.deviceName
Rho.PrinterZebra.deviceName
Rho::PrinterZebra.deviceName
Default port when connecting with TCP/IP.
Property Access:
myObject.devicePort
Rho.PrinterZebra.devicePort
Rho::PrinterZebra.devicePort
Connection status flag. All other properties are valid only if isConnect = true. Note: This property does not guarantee a real connection, because all real device functionality is processed in separate thread asynchronously. If you want a real physical connection, use requestState()
method with receive result in the callback. This is the only guaranteed method of processing real requests to the device and analyzing answers from the device.
Property Access:
myObject.isConnected
Rho.PrinterZebra.isConnected
Rho::PrinterZebra.isConnected
Type of printer – see PRINTER_TYPE_… constants.
Property Access:
myObject.printerType
Rho.PrinterZebra.printerType
Rho::PrinterZebra.printerType
Applicator print mode
Cutter print mode
Delayed cut print mode
Kiosk print mode
Linerless peel print mode
Linerless rewind print mode
Partial cutter print mode
Peel-off print mode
Rewind print mode
RFID print mode
Tear-off print mode (this also implies Linerless Tear print mode)
Unknown print mode
Unable to create a connection to a printer
Write to a connection failed
Read from a connection failed
Unable to determine the control language of a printer
Malformed discovery address
Network error during discovery
Invalid multi-cast hop count
Malformed status response – unable to determine printer status
Invalid format name
Bad file directory entry
‘FN' integer must be between 1 and 999
Invalid printer language
Invalid file name
Invalid drive specified
Use this name for requestState(), true if the head is cold
Use this name for requestState(), true if the head is open
Use this name for requestState(), true if the head is too hot
Use this name for requestState(), true if there is a partial format in progress
Use this name for requestState(), true if the printer is paused
Use this name for requestState(), true if the receive buffer is full
Use this name for requestState(), true if the ribbon is out
Use this name for requestState(), The length of the label in dots
Use this name for requestState(), The number of labels remaining in the batch
Use this name for requestState(), The number of formats currently in the receive buffer of the printer
Use this name for requestState(), The print mode
Printer control language ZPL
Printer control language CPCL
Printer control language EPS. Not Supported in RMS 4.1
Not supported in RMS 4.1
Not supported in RMS 4.1
Not supported in RMS 4.1
Not supported in RMS 4.1
Supported in RMS 5.1, EB 1.3
Use this name for requestState(), return true if printer ready to print.
Use this name for requestState(), return true if cover is opened.
Use this name for requestState(), return true if drawer is opened.
Use this name for requestState(), return true if paper out.
Use this name for requestState(), return true if battery low.
To print to the Zebra printer we must first search for the device. In this example we are using the searchPrinters method and passing in options to limit the search to Bluetooth and also look for Zebra printers only. If we knew the printer Bluetooth address we could have added the ‘deviceAddress’ parameter. The callback function will be executed for each printer found. When a printer is found the callback object will contain a ‘printerID’ property. This ID is an internal RhoMobie ID used. If no printerID property is there for the successful callback object, then it means that the search has finished. We don’t have any API to identify whether it is zebra or non zebra printer, so we are checking as per the friendly name of the device, If friendly name contains “zebra” then we confirm printer type as PRINTER_TYPE_ZEBRA.
var printers = []; Rho.Printer.searchPrinters({ connectionType:Rho.Printer.CONNECTION_TYPE_BLUETOOTH, printerType: Rho.Printer.PRINTER_TYPE_ZEBRA }, function (cb){ if(cb.status == 'PRINTER_STATUS_SUCCESS') { if (typeof cb.printerID != "undefined") { console.log('Found: ' + cb.printerID) printers.push(cb.printerID); } else { console.log('Done Searching'); } } else { console.log(cb.status); } } );
@printers = [] Rho::Printer.searchPrinters({ connectionType => Rho::Printer::CONNECTION_TYPE_BLUETOOTH, printerType => Rho::Printer::PRINTER_TYPE_ZEBRA }, url_for(:action => :printer_callback) ) def find_printer_callback if @params['status'] == 'PRINTER_STATUS_SUCCESS' if defined? @params['printerID'] puts "Found: #{@params['printerID']}" @printers.push @params['printerID'] else puts "Done Searching" end else puts @params['status'] end end
Now that we have found a printer. The printers
array will contain the printerID
we need to create an instance object that we will use for all communications. Upon first connection to the printer via Bluetooth, you may see a prompt to enter the Bluetooth PIN. Check the manufacturers guide for details. Normally the default is ‘0000’ or ‘1111’ or ‘1234’. Once it is connected, you should see some indication in the printer (like a blue light).
var myPrinter = Rho.Printer.getPrinterByID(printers[0]); // Let's try connecting myPrinter.connect(function (cb){ console.log(cb); // This will be the Zebra's `Friendly Name` // by default it is the serial number console.log(myPrinter.deviceName); // This will be the BT MC Address since we are connecting via BlueTooth console.log(myPrinter.deviceAddress); });
@my_printer = Rho::Printer.getPrinterByID printers[0] # Let's try connecting @my_printer.connect url_for(:action => :printer_connect_callback) def printer_connect_callback puts @params.to_s # This will be the Zebra's `Friendly Name` # by default it is the serial number puts @my_printer['deviceName'] # This will be the BT MAC Address # since we are connecting via BlueTooth puts @my_printer['deviceAddress'] end
Now we can just print a test string and read some properties of the printer to make sure the communications is working.
// If my printer was in line mode I would see this text printed myPrinter.printRawString('This is a test print'); // Example of sending a Zebra CPCL Command // changing from linemode to ZPL mode myPrinter.printRawString('! U1 setvar "device.languages" "ZPL"\r\n'); // Get state - this is real-time status of printer. // reading a property of the myPrinter instance will // show last known status myPrinter.requestState(['PRINTER_STATE_IS_READY_TO_PRINT', 'PRINTER_STATE_IS_PAPER_OUT'],function (cb){ console.log(cb.status); console.log(cb.PRINTER_STATE_IS_PAPER_OUT); console.log(cb.PRINTER_STATE_IS_READY_TO_PRINT); });
# If my printer was in line mode I would see this text printed @my_printer.printRawString 'This is a test print' # Example of sending a Zebra CPCL Command # changing from linemode to ZPL mode @my_printer.printRawString '! U1 setvar "device.languages" "ZPL"\r\n' # Get state - this is real-time status of printer. # reading a property of the @my_printer instance will # show last known status @my_printer.requestState([Rho::Printer::PRINTER_STATE_IS_READY_TO_PRINT, Rho::Printer::PRINTER_STATE_IS_PAPER_OUT], url_for(:action => :request_state_callback)); def request_state_callback puts @params['status'] puts @params['PRINTER_STATE_IS_PAPER_OUT'] puts @params['PRINTER_STATE_IS_READY_TO_PRINT'] end
To use a Bluetooth connection on iOS devices you should take into account the following preconditions:
Made For iPod/iPhone
certification.<key>UISupportedExternalAccessoryProtocols</key> <array> <string>com.zebra.rawport</string> </array>
Required Background modes
to App Communicates with an accessory
in your app’s plist file
<key>UIBackgroundModes</key> <array> <string>external-accessory</string> </array>
You must set your app_type as “rhoelements” in your build.yml
as shown the build-time settings guide.
Requires a Printing Service application to be running. This is described in the Printing Guide
You should avoid navigating from the current page using the printer without disconnecting from the printer first. You can use the window.onunload event:
window.onunload = function(){ printer.disconnect(); }
When you call searchPrinters() with Bluetooth search (with CONNECTION_TYPE_ANY or CONNECTION_TYPE_BLUETOOTH) then all Bluetooth devices around you will be discovered. You may see a lot of pairing requests to non-printer devices and should just cancel or ignore them. This happens because we can not detect that the device is a printer until the device is paired. It is recommended that the BT Address or WiFi MAC Address is also used when searching for printers.
Only printers listed in supported models in Zebra SDK description (click “Compatible Printers” link) are supported for use with this API. For example, the QL 320 (old model) is not supported but, QLn320 and QL320 plus are supported.