5

I want to send a character with Bluetooth. The code works perfectly when there is only a single character.But I want to use a delay function between the two codes. I want to enter any number with EditText and the app will take that number and do EditText/44. That is what I want to wait between 2 codes

Finally work.. Thanks guys. :)

I moved a,b,c inside setOnClick.. ;

kileri = (Button) findViewById(R.id.kileri);
final EditText value1 = (EditText) findViewById(R.id.textkont);
assert value1 != null;
value1.setText("0");


btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();

kileri.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int a = Integer.parseInt(value1.getText().toString());
            int b = a / 44;
            int c = b * 1000;

            sendData("F");

            try {
                Thread.sleep(c);

            } catch (Exception e) {
                e.printStackTrace();
            }
VishnuVS
  • 1,055
  • 2
  • 14
  • 29
R.Sahin Altunbas
  • 53
  • 1
  • 1
  • 8

6 Answers6

14

You can use handler

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                //do something
            }
        }, 2000 );//time in milisecond
Sanjeev S
  • 1,113
  • 2
  • 13
  • 33
Krishna
  • 343
  • 5
  • 22
5
 try {
       //set time in mili
        Thread.sleep(3000);

    }catch (Exception e){
        e.printStackTrace();
    }

edited as your code

 kileri.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            sendData("F");
            try {
                //set time in mili
                Thread.sleep(3000);

            }catch (Exception e){
                e.printStackTrace();
            }
            sendData("S");
        }
    });
Vanraj Ghed
  • 1,261
  • 11
  • 23
2

Use handler like this:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // do something after 2s = 2000 miliseconds
        }
    }, 2000); //Time in milisecond
Jay
  • 21
  • 7
0

You can do like this

kileri = (Button) findViewById(R.id.kileri);
final EditText value1 = (EditText) findViewById(R.id.textkont);
assert value1 != null;
value1.setText("0");
final int a = Integer.parseInt(value1.getText().toString());
final int b = a/22;
final int c = b/2; // It will take a int from Edittext and do this operation on that.


btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();

kileri.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        sendData("F");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                sendData("S");    
            }
        }, c);    
    }
});
Mohit Charadva
  • 2,555
  • 1
  • 22
  • 30
0

just use runOnUiThread on button click and post Handler after time delay..

  Button button = new Button(this);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendData("F");                   // send
            delay(2000);
        }
    });

UPDATE

delay()..

 public void delay(final int c){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(c);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            sendData("S");           //send
        }
    }, c);

}
Iamat8
  • 3,888
  • 9
  • 25
  • 35
0

It's not recommended to use Thread.Sleep because it stops execution of main thread if it is called in main thread execution.So,if we want to set delay we can use CountDownTimer.

In below snippet,we gave 2 seconds delay.So,after 2s onFinish() callback comes and we can have our operation there.

new CountDownTimer(2000, 1000) {
     public void onFinish() {
         
     }

 public void onTick(long millisUntilFinished) {
          
 }.start();
} 
Tarun Anchala
  • 2,232
  • 16
  • 15