I am coding an android app using android studio(Java) that controls an LED(for now) via Bluetooth using Arduino.
Goal: I want the button (btn5) to be locked/ disabled after first click while executing its code then automatically unlock/enable again after execution. btn5 must process its code once only when clicked even if there are multiple clicks on the button.
Problem: The LED blinks 5 times on one click, but if clicked multiple times it blinks 10, 15... times. I've tried to implement what was advised on other similar threads but it does not get me to my goal. it only enables the button again when I reload the app.
The closest I got was disabling btn5 once it is clicked which remains disabled until btn4 is pressed as below:
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
btn5.setEnabled(true);
sendSignal("0");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
int i= 0;
int p= 5;
@Override
public void onClick (View v) {
while (i!=p)
{
sendSignal("1");
btn5.setEnabled(false);
i=i+1;
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
sendSignal("0");
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
sendSignal("0");
i=0;
}
});
Please, any assistance would be appreciated as I am new to java and app building.