I want my program to send 0 only if all buttons are released.
The code now sends 0 when i release the button, but if i hold to button and release one of them it will send 0 but i dont want it to do that i only want when all of the button are released.
Thanks.
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()){
case R.id.commend1B:
if (motionEvent.getAction() == ACTION_DOWN) {
commend1BHold = true;
Log.i("sending", "sending 1");
bluetooth.write("1#");
} else if (motionEvent.getAction() == ACTION_UP){
Log.i("sending", "sending 0");
bluetooth.write("0#");
}
break;
case R.id.commend2B:
if (motionEvent.getAction() == ACTION_DOWN) {
Log.i("sending", "sending 2");
bluetooth.write("2#");
} else if (motionEvent.getAction() == ACTION_UP){
Log.i("sending", "sending 0");
bluetooth.write("0#");
}
break;
case R.id.commend3B:
if (motionEvent.getAction() == ACTION_DOWN) {
Log.i("sending", "sending 3");
bluetooth.write("3#");
} else if (motionEvent.getAction() == ACTION_UP){
Log.i("sending", "sending 0");
bluetooth.write("0#");
}
break;
case R.id.commend4B:
if (motionEvent.getAction() == ACTION_DOWN) {
Log.i("sending", "sending 4");
bluetooth.write("4#");
} else if (motionEvent.getAction() == ACTION_UP){
Log.i("sending", "sending 0");
bluetooth.write("0#");
}
break;
default:
throw new IllegalStateException("Unexpected value: " + view.getId());
}
return false;
}
2
Answers
I made Boolean for each button to check if it's pressed or not when I press it will be true and when I release it will turn false. Instead of sending 0 when I release it will check if every button is not pressed (check that all the Boolean are false) and only then send a 0.
Every time you release a button (any button), call a method that checks if all the buttons are released. If they are, send
0
.There is also a possibility to count the pressed buttons: every time you press a button, you increase the counter and vice versa. If the counter is zero, send
0
.