6

I am developing an app, in which I want to blink the flash light in specific interval.

Below are the steps I have followed.

1) Set the Timer for specific interval.

2) In run() method i did the code for TurnOn and TurnOff flash.

But the interval of flash blinking is different on different devices. The timer time is same for all devices, I have also put a Log in between, I am getting same values but, still the problem is there.

Is it a Hardware issue, because the hardware is different for different devices. I have also tested in iPhone 5s (By converting same code in iOS) but, the flash blinking is much faster than Android.

For Android, I have tested on Nexus 4, Motorola G2, Sony Xperia Neo and it is working fine.

Problem is with Nexus 5 and Samsung Galaxy S4.

EDIT

Code of Timer :

long delayLong = 200;
long timerValueLong = 500;
Timer timer;

timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        if (!mLightOn) {
            turnOnFlash();
        } else {
            turnOffFlash();
        }
    }
}, delayLong, timerValueLong);
15
  • What is your timer period or PWM duty cycle? May 25, 2015 at 12:49
  • AFAIK it depends on hardware, Timer implements runable and runnable causes messages to be pushed in queue. May 25, 2015 at 13:35
  • @MurtazaKhursheedHussain: So basically it is hardware issue? May 25, 2015 at 13:35
  • probably, when a message is queued its upon the kernel to execute. P.S I could be wrong. May 25, 2015 at 13:36
  • @MurtazaKhursheedHussain: Ok, i will try to look more into it. But, i have searched on google though i haven't found any feasible solution. Is this issues doesn't come up to anyone till now. :( May 25, 2015 at 13:42

4 Answers 4

1

This is an older problem, but the problem still persists today so I'll post how I solved this.

The problem is that the call to turn the LED on or off takes variable amount of time to traverse through the Android operating system. The way these calls are handled are phone dependent.

First off you need to measure the time it takes for the LED to turn on and off starting from the time the call to do so. Use the input from the camera, keep the phone close to a surface and measure the change in brightness in the frame. You can use glReadPixels if working with OpenGL and read out the center line only each frame. You will need to make multiple measurements, as the call can be shorter or longer depending on the state of the OS. Preferably you'd want to have no buffer or a fixed buffer of frames, so timing of the frames is reliable (which might not be the case with compression). I used OpenGL and a SurfaceTexture and this is a snappy way.

You now know the minimum(1) and maximum(2) time it takes for the call to traverse the OS. Using this information you can make the LED blink as fast as possible on the phone. To truly get the most out of it, start the second call to the flash before maximum(2) time has passed; maximum(2) - minium(1).

Using the last trick, the speed of the flashing is mostly dependent on the difference in minimum and maximum time of the call traversal. This is typically very different per phone, from 10ms to 100ms+.

Also note that because the measuring of the call traversal time happens with the camera, times are rounded up/down to 33ms segments (@30fps).

1

I had the same issue with the flashlight and the problem is not related to the Timer. It is about how you are turning the flash on and off. On some devices like Nexus 5, you have to have and use a SurfaceView inside of your layout. It would be useful to show us the methods you are using for turning the flashlight on and off.

2
  • I have used surfaceview for Nexus 5. But, still the flashing of light is having different intervals on different devices. Jun 2, 2015 at 4:57
  • 1
    There is a new awesome android library that fixes this problem github.com/Abhi347/NoobCameraFlash
    – David
    May 1, 2017 at 13:31
1
    long delayLong = 20;
    long timerValueLong = 100;
    Timer timer;
    final Parameters p = camera.getParameters();
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            if (!isLighOn) {
                p.setFlashMode(Parameters.FLASH_MODE_ON);
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                isLighOn = true;
            } else {
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                isLighOn = false;
            }
        }
    }, delayLong, timerValueLong);
1
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. Aug 14, 2015 at 16:44
0

Maybe you can try to use Alarm functions like:

  1. https://developer.android.com/training/scheduling/alarms.html

  2. https://developer.android.com/reference/android/provider/AlarmClock.html

your can set repeat period for alarm. make 2 alarms, one for On and one for Off. Even phone has hard working, alarm will work depending on clock time. so maybe it will not Off and twice On by error, but solution will be same.

and also you are using 200 milliseconds. it's hard for device catch such small time interval. maybe you'll try to increase you time intervals?

5
  • Thank you for the reply. Actually I have frequency from 0 to 100. So i need to give time to the Alarm Clock accordingly, which will display the torch at same interval on all the devices. I am trying using AlarmManager will let you about my outputs. Jun 1, 2015 at 10:06
  • In that case, what about making a queue, that will work for blinking, and one Rannable to add blick type (on or off) to queue. If device works slow of other program, queue will collect several commands (on/offs) and when it work normal, normal. And Rannable will be a infinite loop, with Thread.sleep(ms). Did you try this one?
    – Saidolim
    Jun 1, 2015 at 10:26
  • Yes, i am using the same. Thread.sleep(); for specific time interval. But, the flashing time varies from device to device. Jun 1, 2015 at 10:57
  • did you try LockSupport.parkNanos(nanos) as in stackoverflow.com/a/20729245/4953620
    – Saidolim
    Jun 1, 2015 at 11:49
  • No. i haven't used. Let me use that. Jun 1, 2015 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.