Wednesday, 11 September 2013

Runnable handler correct execution

Runnable handler correct execution

I've got a function with a handler where I execute other functions. These
functions are via bluetooth writing and reading functions where a string
is send and then a another string is read.
I've got the function created this way:
status= 0;
handler.post(new Runnable() {
@Override
public void run() {
try {
if (status== 0) {
initThread_send(); //Strings that send the command to
initialize transmission to the PCB
status= 1;
}
else if (status== 1) {
writeClimb_send(); //Sent string with writing command
status= 2;
}
else if (status== 2 && <Another condition>) {
readClimbConf_send(); //Received string that confirms
the writing
status= 3;
}
else if (estado == 3) {
writePlatform_send(); //Sent string with writing command
estado = 4;
}
else if (status== 4 && <Another condition>) {
readPlatformConf_send(); //Received string that
confirms the writing
status= 5;
}
else if (status== 5) {
writeFast_send(); //Sent string with writing command
status= 6;
}
else if (status== 6 && <Another condition>) {
readFastConf_send(); //Received string that confirms
the writing
status= 7;
}
else if (status== 7) {
saveReq_send();
saveStat_send();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.com_error
+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
The question that I pose here is about how to structure this function in
the right way to meet this requiriments:
* The first problem that I encounter is that when I send a string with the
write command,
in the next state I have to receive the write confirmation from the PCB
and read it,
so, I would need some kind of delay to give to the thread the need time
to process this.
* The thread must be executed linearly, and in this way i think that after
executing the
first function, it goes out of the run.

No comments:

Post a Comment