I changed wait() to wait(2000), but this didn't work for me.
I expected that after 2 seconds on "no data received condition" the function comes out of blocked readline() function, but it didn't.
I even tried the timer task, it didn't work for me.I cannot make out what's going wrong.. Please help..
class Looper extends BaseIOIOLooper {
Uart uart,uart1;
InputStream in, in1;
OutputStream out, out1;
//String instance1=null, instance2=null;
BufferedReader rd,rd2;
MyThread thread_;
Timer updateTimer = new Timer("gForceUpdate");
public void setup() throws ConnectionLostException {
uart=ioio_.openUart(6,5, n, Uart.Parity.NONE, Uart.StopBits.ONE);
uart1=ioio_.openUart(r, tk, n, Uart.Parity.NONE, Uart.StopBits.ONE);
in=uart.getInputStream();
out=uart.getOutputStream();
in1=uart1.getInputStream();
out1=uart1.getOutputStream();
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
byte [] wrBuff =ed1.getText().toString().getBytes();
out.write(wrBuff);
Thread.sleep(200);
byte [] rdBuff=new byte[in.available()];
in.read(rdBuff);
ed2.setText(new String(rdBuff));
byte [] wrBuff1 =ed3.getText().toString().getBytes();
out1.write(wrBuff1);
Thread.sleep(200);
byte [] rdBuff1=new byte[in1.available()];
in1.read(rdBuff1);
ed4.setText(new String(rdBuff1));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
rd=new BufferedReader(new InputStreamReader(in));
rd2=new BufferedReader(new InputStreamReader(in1));
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_SHORT).show();
}
});
}
public void loop() throws ConnectionLostException,InterruptedException {
String instance1=null, instance2=null;
try {
iWrite("loop started");
//here i need to detect the condition of data not being received and trigger an event.
updateTimer.scheduleAtFixedRate(new TimerTask(){
public void run(){
iWrite("refreshed");
}
}, 100, 5000);
instance1 = rd.readLine();
} catch(IOException ioe) {
iWrite("timeout");
ioe.printStackTrace();
}
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void iWrite(String myValue)
{
final String crossValue = myValue;
runOnUiThread(new Runnable() {
@Override
public void run() {
ed2.setText(ed2.getText() + crossValue + "\n");
}
});
}
private void iWrite2(String myValue)
{
final String crossValue = myValue;
runOnUiThread(new Runnable() {
@Override
public void run() {
ed4.setText(ed4.getText() + crossValue + "\n");
}
});
}