Preamble and
Postamble
This example shows how to set a preamble and a postamble to scan data. The program performs the following steps:

This example assumes that at least one driver has been installed. It also assumes this driver supports all the attributes that are being used.
...
1. Implements the constructor for the class. It opens a connection to the scanner in scan aware mode, activates the Code 39 symbology, and sets an event listener.
2. Implements the onError method from ScannerListener.
3. Implements the onDataReceived method from ScannerListener.
4. Declares and implements a method called scanBarcode. This method starts the scanning engine, waits for the application to read a barcode, and stops the scanning engine.
5. Declares and implements the setPreamble method. It sets the preamble for the barcode data.
6. Declares and implements the setPostamble method. It sets the postamble for the barcode data.
7. Declares and implements the close method. It closes the connection to the scanner.
8. Creates an instance of the class. Then sets the preamble and postamble.
9. Scans a barcode and displays the scanned data.
10. Closes the connection to the scanner.
package scanner_api_examples;
import com.sap.ip.me.api.pios.PIOSException;
import com.sap.ip.me.api.pios.connection.*;
import com.sap.ip.me.api.pios.scanner.*;
import com.sap.ip.me.api.pios.symbology.*;
public class PreAmblePostAmble implements ScannerListener {
private ScannerConnection scannerConnection = null;
private String barcodeData = null;
private boolean barcodeScanned = false;
private Exception lastException = null;
//-----------------------(1)-----------------------
public PreAmblePostAmble() throws PIOSException {
Connector connector = Connector.getInstance();
DriverInfo[] scanners = connector.listDrivers(ConnectionType.SCANNER);
ScannerParameters parameters = new ScannerParameters(scanners[0]);
parameters.setMode(ScannerParameters.SCAN_AWARE);
scannerConnection = (ScannerConnection) connector.open(parameters);
scannerConnection.addSymbology(new Code39(Code39.FULLASCII));
scannerConnection.setEventListener(this);
}
//-----------------------(2)-----------------------
public void onError(ScannerException e) {
lastException = e;
barcodeScanned = true;
}
//-----------------------(3)-----------------------
public void onDataReceived(ScannerData scannerData) {
try {
barcodeData = new String(scannerData.toByteArray(), "ASCII");
} catch (Exception ex) {
lastException = ex;
}
barcodeScanned = true;
}
//-----------------------(4)-----------------------
public void scanBarcode() throws Exception {
scannerConnection.startRead();
while (!barcodeScanned) {
Thread.sleep(500);
}
if (lastException != null) {
throw lastException;
}
scannerConnection.endRead();
barcodeScanned = false;
}
//-----------------------(5)-----------------------
public void setPreamble(String preamble) throws PIOSException {
scannerConnection.setPreamble(preamble.getBytes());
}
//-----------------------(6)-----------------------
public void setPostamble(String postamble) throws PIOSException {
scannerConnection.setPostamble(postamble.getBytes());
}
//-----------------------(7)-----------------------
public void close() {
try {
scannerConnection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
PreAmblePostAmble preamblePostambleExample = null;
try {
//-----------------------(8)-----------------------
preamblePostambleExample = new PreAmblePostAmble();
preamblePostambleExample.setPreamble("<-Preamble->");
preamblePostambleExample.setPostamble("<-Postamble->\t");
//-----------------------(9)-----------------------
preamblePostambleExample.scanBarcode();
System.out.println("The data with preamble and postamble is:\n" + preamblePostambleExample.barcodeData);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
//-----------------------(10)-----------------------
preamblePostambleExample.close();
}
}
}