1
REDLED.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {


        byte[] convertedBytes = convertingTobyteArray(
                IMM_MID_ALERT1);
        BluetoothLeService
                .writeCharacteristicNoresponse(
                        gattCharacteristic,
                        convertedBytes);

    }
});

GREENLED.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

         byte[] convertedBytes = convertingTobyteArray(
                IMM_MID_ALERT2);
        BluetoothLeService
                .writeCharacteristicNoresponse(
                        gattCharacteristic,
                        convertedBytes);

    }
});
TWOLED.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

       REDLED.callOnClick();
        GREENLED.callOnClick();

            }
});

I am doing a project to blink LED.I use two LEDs(RED,GREEN).REDLED button is used to blink Red LED and GREENLED button to blink Green LED.I want to work the two functions simultaneously by clicking TWOLED button. But according to my coding

"REDLED.callOnClick();
  GREENLED.callOnClick();"

only GREENLED.callOnClick(); works.
otherwise if I code
"GREENLED.callOnClick();
REDLED.callOnClick();"
only REDLED.callOnClick(); works.

How to make "GREENLED.callOnClick(); and REDLED.callOnClick();work simultaneously when clicking TWOLED button.

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
Joany
  • 11
  • 2

2 Answers2

2

I think performClick() is the right method to perform particular button's click!

Know more about what is the difference between performClick() and callOnClick() methods.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1

Just change this :

TWOLED.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

       REDLED.callOnClick();
        GREENLED.callOnClick();

            }
});

to:

TWOLED.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
             callRed();
             callGreen();
            }
});

Method definitions of callRed() and callGreen():

private void callRed() {
byte[] convertedBytes = convertingTobyteArray(
                IMM_MID_ALERT1);
        BluetoothLeService
                .writeCharacteristicNoresponse(
                        gattCharacteristic,
                        convertedBytes);
}

private void callGreen() {
byte[] convertedBytes = convertingTobyteArray(
                IMM_MID_ALERT2);
        BluetoothLeService
                .writeCharacteristicNoresponse(
                        gattCharacteristic,
                        convertedBytes);
}
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64