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;
}
CodePudding user response:
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
.
CodePudding user response:
Did something like that:
@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){
commend1BHold = false;
send0();
}
break;
case R.id.commend2B:
if (motionEvent.getAction() == ACTION_DOWN) {
commend2BHold = true;
Log.i("sending", "sending 2");
bluetooth.write("2#");
} else if (motionEvent.getAction() == ACTION_UP){
commend2BHold = false;
send0();
}
break;
case R.id.commend3B:
if (motionEvent.getAction() == ACTION_DOWN) {
commend3BHold = true;
Log.i("sending", "sending 3");
bluetooth.write("3#");
} else if (motionEvent.getAction() == ACTION_UP){
commend3BHold = false;
send0();
}
break;
case R.id.commend4B:
if (motionEvent.getAction() == ACTION_DOWN) {
commend4BHold = true;
Log.i("sending", "sending 4");
bluetooth.write("4#");
} else if (motionEvent.getAction() == ACTION_UP){
commend4BHold = false;
send0();
}
break;
default:
throw new IllegalStateException("Unexpected value: " view.getId());
}
return false;
}
public void send0(){
if(!commend1BHold &&!commend2BHold && !commend3BHold && !commend4BHold){
Log.i("sending", "sending 0");
bluetooth.write("0#");
}
}