Skip to content

How to toggle flashlight/torch? #39

@Damercy

Description

@Damercy

Is there any possibility to toggle the flashlight via code?

Maybe expose a function in QRActivity class that can allow us to toggle the flashlight programmatically instead of clicking in the camera preview? 😅

I've tried to switch on the flashlight using a utility flashlight class before launching the QRActivity (camera preview), however it always turns off the flashlight.

My flashlight util class:

class FlashlightUtil(private val context: Context) {

    fun toggleFlashlight(isOn: Boolean){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val camManager: CameraManager? = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager?
            var cameraId: String? = null
            try {
                cameraId = camManager?.cameraIdList?.get(0)
                camManager?.setTorchMode(cameraId!!, isOn) 
            } catch (e: CameraAccessException) {
                e.printStackTrace()
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions