I was trying a solution on the Huawei P20 Lite but did not work.
I found the issue can be reproduced 100% on Huawei P20 Lite when quickly turn ON/OFF torch. The torch will flash twice while turn OFF.
But unfortunately it seems to be a device specific timing issue on Huawei P20 Lite because I cat get the same result by implementing a test app with camera2 API. (CameraX base on camera2 API.)
There is a workaround for this device, that is to wait for the completion of ListenableFuture of enableTorch() request and then issue next request.
For example by modifying the test project,
// Create a single thread executor to execute the torch requests sequentially.
private val executor = Executors.newSingleThreadExecutor()
private fun switchFlash(cam: Camera, on: Boolean) {
isFlashOn = on
executor.execute
{
try {
cam.cameraControl.enableTorch(on).get() // blocking until request complete.
} catch (e: ExecutionException) {
// log error
}
}
}
Not sure if the workaround is feasible for your application since it may delay for a while if quickly switching torch.
I currently do not have Redmi Note 4 and other devices you pasted, so I am not able to confirm are they also device specific issue.
Would you mind to test if the workaround feasible on the device you test?