public class LinePrinter extends Printer
PrintProgressEvent
. These events inform the application
of when the printing is started and when the printing is completed.
In release 1.30, the LinePrinter class became a subclass of the Printer
class. Some public methods have been moved to the Printer
class and
inherited by the LinePrinter
class. For backward compatibility,
these inherited methods still throw LinePrinterException
if they are
called from a LinePrinter
object. The LinePrinterException
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 LinePrinter
constructors and methods in a separate thread from the main UI thread;
otherwise, a LinePrinterException
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.
The following is a sample code snippet that demonstrates calling the LinePrinter API on the Android platform.
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() { LinePrinter lp = null; LinePrinter.ExtraSettings exSettings = new LinePrinter.ExtraSettings(); exSettings.setContext(PrintActivity.this); try { // Assumes the printer_profiles.JSON file exists in the external storage. File profiles = new File (getExternalFilesDir(null), "printer_profiles.JSON"); lp = new LinePrinter(profiles.getAbsolutePath(), "PR2", "bt://00:02:5B:00:02:78", exSettings); lp.connect(); // Connects to the printer lp.setBold(true); // Sets bold font. lp.write("SALES ORDER"); lp.setBold(false); // Returns to normal font. lp.newLine(2); lp.writeLine(" 1501 Timer-Md1 13.15 1 13.15"); // You may make more write or writeLine calls. } catch (LinePrinterException ex) { // Handles LinePrinter exceptions } catch (Exception ex) { // Handles other exceptions } finally { if (lp != null) { try { lp.disconnect(); // Disconnects from the printer lp.close(); // Releases resources } catch (Exception ex) {} } } } } }
Modifier and Type | Class and Description |
---|---|
class |
LinePrinter.BarcodeSymbologies
This class defines the symbology constants used for printing
bar codes.
|
static class |
LinePrinter.ExtraSettings
This class defines extra settings that may be required for the LinePrinter.
|
class |
LinePrinter.GraphicRotationDegrees
This class defines the degree constants used for graphic rotation.
|
Constructor and Description |
---|
LinePrinter(java.lang.String aCmdAttribFilePath,
java.lang.String aPrinterID,
java.lang.String aPrinterURI,
LinePrinter.ExtraSettings extraSettings)
Constructs a LinePrinter object with the specified settings.
|
Modifier and Type | Method and Description |
---|---|
void |
newLine(int numLines)
Advances the printer the specified number of lines.
|
void |
setBold(boolean enabled)
Enables/disables the bold font style on the printer.
|
void |
setCompress(boolean enabled)
Enables/disables the compressed font style on the printer.
|
void |
setDoubleHigh(boolean enabled)
Enables/disables the double high font style on the printer.
|
void |
setDoubleWide(boolean enabled)
Enables/disables the double wide font style on the printer.
|
void |
setItalic(boolean enabled)
Enables/disables the italic font style on the printer.
|
void |
setStrikeout(boolean enabled)
Enables/disables the strikeout font style on the printer.
|
void |
setUnderline(boolean enabled)
Enables/disables the underline font style on the printer.
|
void |
writeBarcode(int aSymbology,
java.lang.String aData,
int aSize,
int aXOffset)
Generates the specified bar code and prints it.
|
void |
writeGraphic(java.lang.String aGraphicFilePath,
int aRotation,
int aXOffset,
int aWidth,
int aHeight)
Prints the specified graphic file.
|
void |
writeGraphicBase64(java.lang.String aBase64Image,
int aRotation,
int aXOffset,
int aWidth,
int aHeight)
Prints the specified Base64 encoded graphic.
|
void |
writeLine(java.lang.String aStr)
Writes a string to the internal print buffer, followed by a call to
newLine(1) . |
addPrintProgressListener, close, connect, disconnect, endDoc, flush, formFeed, getBytesWritten, removePrintProgressListener, sendCustomCommand, setSettingBool, setSettingBytes, setSettingNum, setSettingString, startFileEcho, stopFileEcho, write, write
public LinePrinter(java.lang.String aCmdAttribFilePath, java.lang.String aPrinterID, java.lang.String aPrinterURI, LinePrinter.ExtraSettings extraSettings) throws LinePrinterException
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. In version 1.0, 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 LinePrinter.ExtraSettings.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 LinePrinter.ExtraSettings
object. On
Android, the LinePrinter.ExtraSettings.getContext()
method
needs to return a valid android.content.Context object.LinePrinterException
- if the parameters are invalid or an unexpected
error occurs.public void newLine(int numLines) throws LinePrinterException
numLines
- An integer value specifying the number of lines to
advance the printer media. If zero or a negative value is specified,
it will be the same as calling the flush()
method.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setBold(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable bold font or 'false'
to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setCompress(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable compressed font or
'false' to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setDoubleHigh(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable double high font or
'false' to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setDoubleWide(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable double wide font or
'false' to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setItalic(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable italic font or 'false'
to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setStrikeout(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable strikeout font or
'false' to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void setUnderline(boolean enabled) throws LinePrinterException
flush()
or newLine(int)
is made.
If the printer does not support this font style, no action is taken.
enabled
- A Boolean 'true' value to enable underline font or
'false' to disable it.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void writeBarcode(int aSymbology, java.lang.String aData, int aSize, int aXOffset) throws LinePrinterException
The unit of measurement for the aXOffset
parameter is
printhead dots. Please refer to the specific printer User Manual for the
media width in printhead dots. For the PR2 printers, the default is 384 dots.
aSymbology
- An integer value identifying the symbology of the
bar code to be printed. Valid values are defined in
LinePrinter.BarcodeSymbologies
aData
- A string specifying the data contained in the bar code.aSize
- An integer value specifying the size of the bar code. Size
can have different meanings depending on the symbology selected. For 1D
symbologies like Code 39, size is the height of the bar code in printhead dots.aXOffset
- An integer value specifying the number of dots of white
space to the left of the bar code.LinePrinterException
- if unexpected error occurs or if an
active connection to the printer does not exist.public void writeGraphic(java.lang.String aGraphicFilePath, int aRotation, int aXOffset, int aWidth, int aHeight) throws LinePrinterException
The unit of measurement for the aXOffset
, aWidth
,
and aHeight
parameters is printhead dots. Please refer to
the specific printer User Manual for the media width in printhead dots.
For the PR2 printers, the default is 384 dots.
aGraphicFilePath
- A string containing the file path of the graphic
to be printed.aRotation
- An integer value specifying the degrees to rotate the
image. Valid values are defined in LinePrinter.GraphicRotationDegrees
.aXOffset
- An integer value specifying the horizontal offset in
printhead dots from the left of the page where the graphic will be printed.aWidth
- An integer value specifying the width in printhead dots
of the printed graphic. The graphic width will be stretched or shrunk
to fit this size.aHeight
- An integer value specifying the height in printhead dots
of the printed graphic. The graphic height will be stretched or shrunk
to fit this size.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void writeGraphicBase64(java.lang.String aBase64Image, int aRotation, int aXOffset, int aWidth, int aHeight) throws LinePrinterException
The unit of measurement for the aXOffset
, aWidth
,
and aHeight
parameters is printhead dots. Please refer to
the specific printer User Manual for the media width in printhead dots.
For the PR2 printers, the default is 384 dots.
aBase64Image
- A string containing Base64 encoded image data to
be printed.aRotation
- An integer value specifying the degrees to rotate the
image. Valid values are defined in LinePrinter.GraphicRotationDegrees
.aXOffset
- An integer value specifying the horizontal offset in
printhead dots from the left of the page where the graphic will be printed.aWidth
- An integer value specifying the width in printhead dots
of the printed graphic. The graphic width will be stretched or shrunk
to fit this size.aHeight
- An integer value specifying the height in printhead dots
of the printed graphic. The graphic height will be stretched or shrunk
to fit this size.LinePrinterException
- if an unexpected error occurs or if an
active connection to the printer does not exist.public void writeLine(java.lang.String aStr) throws LinePrinterException
newLine(1)
. The string is converted to ASCII character
data before writing to the buffer. This method also flushes the data
in the internal buffer because it calls the newLine(int)
method internally.aStr
- A string to be written to the internal print buffer.LinePrinterException
- 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.