if (portIdentifier.isCurrentlyOwned()) {
System.out.println("SerialPortConnection.listenForPort() port already owned, excluding it, owner: " + portIdentifier.getCurrentOwner());
continue;
}
SerialPort serialPort = null;
// Open the port
try {
serialPort = (SerialPort) portIdentifier.open(
"BTGPSSimulator.listenForPort()", 2000);
} catch (PortInUseException pe) {
System.out
.println("SerialPortConnection.listenForPort() port is already used by another application, excluding it: "
+ name);
continue;
} catch (ClassCastException cc) {
System.out
.println("SerialPortConnection.listenForPort() port was not a serial port after all, excluding it: "
+ name);
continue;
}
// Start listening for events on the port
if (serialPort != null) {
try {
serialPort.addEventListener(this); // This is first call in this
// section in case of
// exception
serialPort.notifyOnDataAvailable(true); // This is the event
// that is specific
// when receiving a
// connection.
portsOpenWhileListening.add(serialPort);
} catch (TooManyListenersException te) {
System.out
.println("SerialPortConnection.listenForPort() there was already another listener for the port, excluding it: "
+ name);
serialPort.close();
continue;
}
}
}
// If no ports found, then return
if (portsOpenWhileListening.size() == 0) {
System.out
.println("SerialPortConnection.listenForPort() No available ports found");
ErrorDialog errorDialog = new ErrorDialog(caller, "No Ports Found");
errorDialog.addText("No available ports found.");
errorDialog.show();
return;
}
// Show a wait message
cancelDialog = new CancelDialog(caller, "Waiting for Connections");
cancelDialog
.addText("Please connect a device, e.g. select your computer as the Bluetooth GPS in WF Navigator.");
cancelDialog.show();
isWaitingOnEvent = false;
// Close all temporary ports
for (int i = 0; i < portsOpenWhileListening.size(); i++) {
SerialPort serialPort = (SerialPort) portsOpenWhileListening.get(i);
serialPort.close();
}
portsOpenWhileListening.clear();
// Confirm that a connection has been made
if (portOpenAndReadyForUse) {