I'm developing an app bluetooth to communicate with a device. I have to send to that device a certain packet and that device respond to me in notify mode after a certain time. All works well, but I have to check if the response arrive to me in 3 seconds, and if it doesn't arrive in time, I have to send another command without waiting anymore to previous packet.
In what way Can I implement a timer in Android ?
I have thought to : 1- start timer when I send a packet 2- stop timer when the response arrive to me or if the msg was sended more 3 seconds ago the timer triggers function and it enable to resend other msg
I think to do it with handler and postdelay.
What do you think ? are there a better ways to implement this ?
Maybe a TimerTask ?
You can use CountDownTimer and start the timer when you send the packet to the device and in the onFinish() of CountDownTimer, you can check the response. If the response is not yet arrived, You can send another command to the device without waiting for the response anymore.