public class LabelPrinter extends Printer
PrintProgressEvent
. These events inform the application
of when the printing is started and when the printing is completed.
The LabelPrinter
class is a subclass of the Printer
class. The inherited methods throw LabelPrinterException
on errors if
they are called from a LabelPrinter
object. The LabelPrinterException
class is a subclass of PrinterException
so you may catch the exceptions
with either class.
Note: It is generally a good practice to create a separate thread for printing
because the UI may not be responsive if the printing is done in the main UI
thread. On the Android platform, the application must call the LabelPrinter
constructors and methods in a separate thread from the main UI thread;
otherwise, a LabelPrinterException
will be thrown. The only exceptions
to this requirement are the methods that add or remove an event listener.
This requirement is to prevent the Android "Application Not Responding"
dialog from being displayed because communicating with the printer via
network connection may invoke unpredictable delays.
In order to print a label, the printer settings need to include the label definition. Please refer to the API Guide for more information on label printing.
The following is an example LINEPRINTERCONTROL JSON object that defines a printer "PB32_Fingerprint" which includes a label definition group called "3in_FingerprintLabels". The 3in_FingerprintLabels label group contains a label definition called "ItemLabel". Please refer to the Printer Commands and Attributes User Guide for more information.
{ "LINEPRINTERCONTROL": { "FormatVersion": "1.0.0.0", "DEFAULTS": { "NullsBeforeClose": 0, "PreCloseDelay": 0, "BtConnectRetries" : 3, "BtconnectRetryDelay" : 100, "BtWriteDataReadyTimeout" : 10000, "BtWriteIntervalTimeout" : 10000, "BtMaxSegWrite" : 1024, "BtLinger" : 10 }, "PRINTERS": { "PB32_Fingerprint": { "DisplayName":"PB32 Bt Label Printer", "LABEL_01": "3in_FingerprintLabels", "FormFeed": "[0x46,0x4F,0x52,0x4D,0x46,0x45,0x45,0x44]", "Initialize": [], "NormalFont": [], "NullsBeforeClose": 0, "PreCloseDelay": 1000 } }, "LABELS": { "3in_FingerprintLabels": { "ItemLabel": { "LabelDataStream": "DIR 4:AN 7:PP 40, 180:FT \"Swiss 721 Bold Condensed BT\",20:PT \"ItemName$$\":PP 150,180:BARSET \"CODE128\",3,1,4,240:PB \"ItemNo$$\":PP 400, 330:FT \"Letter Gothic 12 Pitch BT\",14:PT \"ItemNo$$\":PF\r\n", "VarPostfix": "$$" } } } } }
The following is a sample code snippet that demonstrates calling the LabelPrinter API on the Android platform to print the ItemLabel defined in the above LINEPRINTERCONTROL JSON object.
import android.os.AsyncTask; import android.app.Activity; import com.honeywell.mobility.print.*;public class PrintActivity extends Activity { private void createPrintTask() { PrintTask task = new PrintTask(); // Executes PrintTask with the specified parameters which // will be passed to the PrintTask.doInBackground method. // In this case, the doInBackground method does not expect // any parameter. task.execute(); } class PrintTask extends AsyncTask<Void, Integer, Void> { @Override protected Void doInBackground(Void... args) { doPrint(); return null; } private void doPrint() { LabelPrinter lp = null; LabelPrinter.ExtraSettings exSettings = new LabelPrinter.ExtraSettings(); exSettings.setContext(PrintActivity.this); try { // Assumes the printer_profiles.JSON file exists in the external storage // and it contains the PB32_Fingerprint settings and the ItemLabel definition. File profiles = new File (getExternalFilesDir(null), "printer_profiles.JSON"); lp = new LabelPrinter(profiles.getAbsolutePath(), "PB32_Fingerprint", "bt://00:02:5B:CF:B3:20", exSettings); lp.connect(); // Connects to the printer // Sets up the variable dictionary. LabelPrinter.VarDictionary varDataDict = new LabelPrinter.VarDictionary(); varDataDict.put("ItemName", "Honeywell AirGenius 4"); varDataDict.put("ItemNo", "92926003104"); // Prints the ItemLabel as defined in the printer_profiles.JSON file. lp.writeLabel("ItemLabel", varDataDict); // You may print more labels. } catch (LabelPrinterException ex) { // Handles LabelPrinter exceptions } catch (Exception ex) { // Handles other exceptions } finally { if (lp != null) { try { // Note: The PB32_Fingerprint printer settings in the printer_profiles.JSON // file specify a PreCloseDelay setting to ensure the label data stream // is transmitted to the printer before the connection is closed. lp.disconnect(); // Disconnects from the printer lp.close(); // Releases resources } catch (Exception ex) {} } } } } }
Modifier and Type | Class and Description |
---|---|
static class |
LabelPrinter.ExtraSettings
This class defines extra settings that may be required for the LabelPrinter.
|
static class |
LabelPrinter.VarDictionary
This class defines the dictionary entries that are used to replace
variables in a label definition with the specified values.
|
Constructor and Description |
---|
LabelPrinter(java.lang.String aCmdAttribFilePath,
java.lang.String aPrinterID,
java.lang.String aPrinterURI,
LabelPrinter.ExtraSettings extraSettings)
Constructs a LabelPrinter object with the specified settings.
|
Modifier and Type | Method and Description |
---|---|
void |
writeLabel(java.lang.String aFormat,
LabelPrinter.VarDictionary aDictionary)
Prints a label with the specified layout definition and data.
|
addPrintProgressListener, close, connect, disconnect, endDoc, flush, formFeed, getBytesWritten, removePrintProgressListener, sendCustomCommand, setSettingBool, setSettingBytes, setSettingNum, setSettingString, startFileEcho, stopFileEcho, write, write
public LabelPrinter(java.lang.String aCmdAttribFilePath, java.lang.String aPrinterID, java.lang.String aPrinterURI, LabelPrinter.ExtraSettings extraSettings) throws LabelPrinterException
aCmdAttribFilePath
parameter may contain an absolute
file path or a string. The file or string contents must conform to
the commands and attributes format specified in the Printer Commands
and Attributes User Guide.
The aPrinterID
parameter specifies an identifier which
is used to load the printer settings from the commands and attributes
specified in the aCmdAttribFilePath
parameter.
The aPrinterURI
parameter specifies how to connect to the
printer. As of version 1.30, it only supports Bluetooth printing and the URI
format is "bt://MacAddress where MacAddress specifies the
Bluetooth MAC address in the format of "nn:nn:nn:nn:nn:nn" where each n
is a hexadecimal digit, for example, * "00:06:AB:3C:25:8F".
The extraSettings
parameter is a required parameter on the
Android platform and may be optional on other platforms. The application
needs to call the LabelPrinter.ExtraSetting.setContext(Object)
method with a valid android.content.Context
object. It can
be an instance of an Android Activity class.
aCmdAttribFilePath
- A string specifying the absolute path of the
commands and attributes file, or a string containing the printer commands
and attributes.aPrinterID
- A string containing the printer identifier that is
used to load the specific printer settings from the specified commands
and attributes file.aPrinterURI
- A string in URI format that specifies how to connect
to the printer.extraSettings
- A LabelPrinter.ExtraSettings
object. On
Android, the LabelPrinter.ExtraSettings.getContext()
method
needs to return a valid android.content.Context object.LabelPrinterException
- if the parameters are invalid or an unexpected
error occurs.public void writeLabel(java.lang.String aFormat, LabelPrinter.VarDictionary aDictionary) throws LabelPrinterException
The aFormat
parameter specifies an identifier to locate
a label definition in the printer settings specified in the constructor.
The aDictionary
parameter specifies a collection of key-value
pairs to provide data to the variables defined in the label definition.
The key of a dictionary entry should specify the variable name (without
prefix or postfix) and the value should specify the data to replace the
variable with. If the label definition in the commands and attributes
JSON object specifies the VarPrefix
and/or the VarPostfix
settings, the prefix and/or the postfix will be added to form the complete
variable name before the variable substitution logic is applied.
The class description provides an example label definition called "ItemLabel".
The following code snippet defines dictionary entries to replace
ItemName$$ and ItemNo$$ in the LabelDataStream
setting
of ItemLabel
.
// Sets up the variable dictionary. LabelPrinter.VarDictionary varDataDict = new LabelPrinter.VarDictionary(); varDataDict.put("ItemName", "Honeywell AirGenius 4"); varDataDict.put("ItemNo", "92926003104");// Prints the ItemLabel. lp.writeLabel("ItemLabel", varDataDict);
When the writeLabel
method is called, the following data
is sent to the printer.
DIR 4 AN 7 PP 40, 180 FT "Swiss 721 Bold Condensed BT",20 PT "Honeywell AirGenius 4" PP 150,180 BARSET "CODE128",3,1,4,240 PB "92926003104" PP 400, 330 FT "Letter Gothic 12 Pitch BT",14 PT "92926003104" PF
aFormat
- A string that identifies a label definition.aDictionary
- A collection of key-value pairs to provide data to
the variables defined in the label definition.LabelPrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.Copyright (c) 2013-2015 Honeywell International Inc. All rights reserved.