#11 Recognizing a dropped connection

Bob Krause

Applications need to be able to reconize when a
connection has been dropped so that they can reconnect
if necessary, but not abandon a good connection every
time an i/o operation throws an IOException. Based on
the JSR82 spec, RemoteDevice.getRemoteDevice() should
throw an IOException when it is passed a Connection
that is closed. But this is not the current behavior.
If a ClientSession object that has lost it's connection
is passed to this method, the method returns a
RemoteDevice with its 'closed' field having a value of
'true', but no exception is thrown.

The following modification corrects this inconsistency
with the spec.

public static RemoteDevice
getRemoteDevice(Connection conn) throws IOException {
if (conn == null) throw new
NullPointerException("Connection is null.");
RemoteDevice dev=null;
if (conn instanceof BTConnection) {
if (((BTConnection)conn).isClosed())
throw new IOException("Connection Closed");
dev = ((BTConnection)conn).getRemoteDevice();
else if (conn instanceof OBEXConnection) dev =
else if (conn instanceof SessionNotifierImpl) {
(((SessionNotifierImpl)conn).getConnectionNotifier() ==
throw new IOException("Connection Closed");
dev =
else if (conn instanceof ConnectionNotifier) {
if (((ConnectionNotifier)conn).isNotifierClosed())
throw new IOException("Connection Closed");
dev = ((ConnectionNotifier)conn).getRemoteDevice();
else throw new ClassCastException ("Connection type
not supported");
if(dev==null) throw new IOException("The remote
device could not be determined!");
return dev;


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks