skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    @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#");
            }
        }
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search