I'm new in stackoverflow. I created a simple bluetooth app to controll a relay board.
The Relay is "on" for 1s after clicking the a button. It works so far but if I click the button during the 1s the relay gets "on" for another 1s.
So I want to disable the button while the relay is "on". I used .postDelayed(). This works as well, but it is not possible to generate the outputStream to clear the relay in the .postDelayed() routine.
Does anybody have an idea for me?
regards Joe
// Rel1 btn click
mRel1Btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
mRel1Btn.setEnabled(false);
mRel1Btn.setClickable(false);
if (mBlueAdapter.isEnabled()) {
try {
OutputStream mOutputStream = mBtSocket.getOutputStream();
mOutputStream.write(RELAY_ON);
new Handler().postDelayed(new Runnable()
{
public void run()
{
mRel1Btn.setEnabled(true);
mRel1Btn.setClickable(true);
mOutputStream.write(RELAY_OFF); // sending bytes to serial COM (BT)
}
}, 1000 //Specific time in milliseconds
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
Following error occured
error: unreported exception IOException; must be caught or declared to be thrown
mOutputStream.write(RELAY_OFF); (marker on opening brace)