Beep Options
This example deals with the scanner's beep options. It shows how to turn on beeping scanning, for both successful reading and a failed scan. The program executes as explained below:

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 openScanAwareConnection.
6. Declares and implements the setBeepOptions method. This method receives and sets the beep options for the scanner.
7. Declares and implements the close method. It closes the connection to the scanner.
8. Opens a connection to the scanner. Turns on the "Beep on Read" and the "Beep on Fail" options for the scanner. Then scans a barcode.
9. Turns off the beeping options for the scanner. Then scans a barcode.
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 BeepOptions implements ScannerListener {
private ScannerConnection scannerConnection = null;
private boolean barcodeScanned = false;
private Exception lastException = null;
//-----------------------(1)-----------------------
public BeepOptions() throws PIOSException {
openScanAwareConnection();
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 {
System.out.println(
"Data = " + 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)-----------------------
private void openScanAwareConnection() 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);
}
//-----------------------(6)-----------------------
public void setBeepOptions(long options) throws PIOSException {
scannerConnection.setOptions(options);
}
//-----------------------(7)-----------------------
public void close() {
try {
scannerConnection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
BeepOptions beepOptionsExample = null;
try {
beepOptionsExample = new BeepOptions();
//-----------------------(8)-----------------------
beepOptionsExample.setBeepOptions(
ScannerConnection.BEEP_ON_FAIL
| ScannerConnection.BEEP_ON_READ);
beepOptionsExample.scanBarcode();
//-----------------------(9)-----------------------
beepOptionsExample.setBeepOptions(ScannerConnection.BEEP_OFF);
beepOptionsExample.scanBarcode();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
//-----------------------(10)-----------------------
beepOptionsExample.close();
}
}
}