简体   繁体   中英

Android Timer instead of Thread.sleep for sending binary code via LED

I'm working on an app to transmit binary code via switching on/off the LED of my smartphone. In a for-loop the app goes from char to char of the binary code and if there is a "1" it flashes the LED for 100ms and if there is a "0" it's just off for 100ms. Also the binary code is being send 3 times. Here some part of the code I used:

public void turnOnOff() {
    if (button) {
        button = false;
        Camera.Parameters parametersOn = camera.getParameters();
        Camera.Parameters parametersOff = parameters;
        parametersOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        parametersOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

        try {
            for (int a = 0; a < 3; a++){
                for (int i = 0; i < data.length(); i++){

                            if (data.charAt(i) == '1') {
                                    camera.setParameters(parametersOn);
                                    camera.startPreview();
                                    Thread.sleep(100);
                            } else {
                                    camera.setParameters(parametersOff);
                                    camera.startPreview();
                                    Thread.sleep(100);
                                    }
                }
            }
        } catch (Exception e) {

        }
    }
    button = true;
}

It does work but unfortunatly the Thread.sleep() function is not very exact in terms of time. When I type in Thread.sleep(100) it is always more than 100ms. So I tried to change the code and use a timer with timer.schedule() method because I hope that it is more accurate. But I just couldn't figure it out how to make it work with a timer.

Can someone show me how to make this task work with a timer, so the "ones" and "zeros" will be send in an intervall of 100ms and repeat it 3 times?

Thread.sleep caused your current Thread to sleep at least 100ms . This doesn't mean that in 100 ms your next statement will be executed. Timer uses another thread, so if you need to perform some action on UI thread, you have to post the message to UI thread. If you need delayed message without blocking the UI thread - use Handler.post message:

Handler handler = new Handler();
try {
    for (int a = 1; a <= 3; a++) {
        for (int i = 0; i < data.length(); i++) {

            if (data.charAt(i) == '1') {
                handler.postDelayed(new Runnable() {
                    @Override public void run() {
                        camera.setParameters(parametersOn);
                        camera.startPreview();
                    }
                }, i * 100);
            } else {
               handler.postDelayed(new Runnable() {
                    @Override public void run() {
                        camera.setParameters(parametersOff);
                        camera.startPreview();
                    }
                }, i * 100);
            }
        }
    }
} catch (Exception e) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM