0

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)

Joe
  • 1
  • 1
  • "it is not possible to generate the outputStream to clear the relay in the .postDelayed() routine" Why is it not? – Torima Mar 14 '22 at 15:11
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 14 '22 at 18:55
  • Ok, I will simplify this part of code and add the error statement. – Joe Mar 14 '22 at 18:59

1 Answers1

0

Solved:

I created a subroutine "sendMessage(message)" and called this subroutine out of my listener.

// Rel1 btn click
        mRel1Btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                    if (mBlueAdapter.isEnabled()) {

                        sendMessage(RELAY1_JDY30_ON);
                        mRel1Btn.setEnabled(false);
                        mRel1Btn.setClickable(false);

                        new Handler().postDelayed(new Runnable()
                                                  {
                                                      public void run()
                                                      {
                                                          sendMessage(RELAY1_JDY30_OFF);
                                                          mRel1Btn.setEnabled(true);
                                                          mRel1Btn.setClickable(true);
                                                      }
                                                  }, 1000    //Specific time in milliseconds
                        );
                    }
            }
        });
.
.
.
    private void sendMessage(byte[] message) {
        OutputStream mOutputStream;
        try {
            mOutputStream = mBtSocket.getOutputStream();
            mOutputStream.write(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Joe
  • 1
  • 1