/**
* Main method for execution
*/
public void run()
{
System.out.println("RUN!");
/** StreamConnection to Device. */
StreamConnection streamConnection = null;
/** InputStream for StreamConnection. */
InputStream inputStream = null;
/** Timer for detecting connection loss */
Timer connectionTimer = null;
/** Indicates that a new valid sentence was received */
boolean fix = false;
while (reconnect)
{
//
// establish connection
//
while ((!this.connected) && (this.reconnect))
{
try {
parent.indicateConnection(TitleWidget.CONNECTION_STATUS_INIT);
System.out.print("Connector open...");
streamConnection = (StreamConnection) Connector.open(this.BTsppURL, Connector.READ_WRITE, true);
System.out.println(" [OK]");
System.out.print("open inputstream...");
inputStream = streamConnection.openInputStream();
System.out.println(" [OK]");
System.out.print("open outputstream...");
outputStream = streamConnection.openOutputStream();
System.out.println(" [OK]");
this.connected = true;
parent.indicateConnection(TitleWidget.CONNECTION_STATUS_ONLINE);
this.ping = 0l;
// this.receiver.reset();
// Set up Timer from now
System.out.print("open timer...");
connectionTimer = new Timer();
connectionTimer.schedule(new BTConnectionTask(this), 10000, 2000);
System.out.println(" [OK]");
}
catch (IOException e)
{
e.printStackTrace();
parent.indicateConnection(TitleWidget.CONNECTION_STATUS_OFFLINE);
// kill all connections
try {inputStream.close();} catch (Exception er) {}
try {outputStream.close();} catch (Exception er) {}
try {streamConnection.close();} catch (Exception er) {}
inputStream = null;
streamConnection = null;
// sleep for 5 seconds
try {Thread.sleep(5000);} catch (InterruptedException er) {}
}
}
//
// receive
//
System.out.println("receive " +this.connected);
Vector vector = null;
while (this.connected)
{
fix = true;
while ((this.connected))
{
try {
int ch = 0;
vector=new Vector();
// Read Inputstream char for char
while ((this.connected) & fix)
{
ch = inputStream.read();
int flag = isEndorStart(ch);
// byteArrayOutputStream.write((byte)ch);
vector.addElement(new IntegerIGD(ch));
if(flag==0xFF) fix=false;
}
last1=-1; last2=-1; last3=-1;
this.receiver.proceedLine(vector);
vector = null;
// System.out.println("Sentences|" + line + "|");
// Check for sentence
if (true)
{
fix = true;
this.ping = System.currentTimeMillis();
}
}
catch (IOException e)
{
System.out.println("EXxeption");
}
}
// parse sentence
// if (line.length() > 3) this.receiver.processLine(line);
}
//
// close connection
//
this.connected = false;
parent.indicateConnection(TitleWidget.CONNECTION_STATUS_OFFLINE);
// kill all connections/timers
try {connectionTimer.cancel();} catch (Exception er) {}
try {inputStream.close();} catch (Exception er) {}
try {streamConnection.close();} catch (Exception er) {}
connectionTimer = null;
inputStream = null;
streamConnection = null;
// reset receiver
// this.receiver.reset();
}
}