Flash Events
The FlashView can be customized to report and/or set the torch state and mode values.
This allows the integrator to implement a functionality to read, persist, and set the torch state/mode between multiple usages of the Mobile SDK for Android.
To do so, you need to attach a listener to subscribe to the events when the torch state/mode changes and you can set the torch state/mode programmatically.
Persisting these settings can easily be implemented using Android DataStore (recommended) or SharedPreferences (legacy).
At the time of writing, this functionality is available in the Mobile SDK for Android only. |
Torch Mode Events - get and set torch mode
The Mobile SDK for Android allows you to watch torch mode changes as well as set the torch mode (auto/on/off).
How to subscribe to Flash Mode Change Events and set the Flash Mode programmatically
-
Kotlin
-
Java
companion object {
private var lastFlashMode = FlashControl.Mode.AUTO
}
fun setupScanView() {
scanView.init("viewconfig.json")
scanView.flashView?.let { flashView ->
flashView.modeChangedEvent = Event { newMode -> lastFlashMode = newMode } // Subscribe to receiving Events when the flash mode changes
flashView.cameraAttachedEvent = Event { cameraReady -> flashView.setMode(lastFlashMode) } // Set the flash mode programmatically
}
}
private static FlashControl.Mode lastFlashMode = FlashControl.Mode.AUTO;
private void setupScanView() {
scanView.init("viewconfig.json");
FlashView flashView = scanView.getFlashView();
if (flashView != null) {
flashView.cameraAttachedEvent = cameraReady -> {flashView.setMode(lastFlashMode);}; // Subscribe to receiving Events when the flash mode changes
flashView.modeChangedEvent = newMode -> {lastFlashMode = newMode;}; // Set the flash mode programmatically
}
}
Torch State Events - get and set torch state
When using the FlashView on Mode.AUTO, by default the torch on the device is automatically turned on and off based on the measured brightness. Because this is determined based on the evaluated images, this functionality can take a few moments to react.
To mitigate this, the Mobile SDK for Android allows you to set an initial state (torch on or off), which will be set until evaluated images allow Mode.AUTO to kick in and then set the torch automatically (on or off).
How to subscribe to Flash State Change Events and set the Flash State programmatically
-
Kotlin
-
Java
companion object {
private var lastFlashState = false
}
fun setupScanView() {
scanView.init("viewconfig.json")
scanView.flashView?.let { flashView ->
flashView.stateChangedEvent = Event { newState -> lastFlashState = newState } // Subscribe to receiving Events when the flash state changes
flashView.cameraAttachedEvent = Event { cameraReady -> flashView.setFlashOnIfAuto(lastFlashState) } // Set the flash state programmatically
}
}
private static boolean lastFlashState = false;
private void setupScanView() {
scanView.init("viewconfig.json");
FlashView flashView = scanView.getFlashView();
if (flashView != null) {
flashView.cameraAttachedEvent = cameraReady -> {flashView.setFlashOnIfAuto(lastFlashState);}; // Subscribe to receiving Events when the flash state changes
flashView.stateChangedEvent = newState -> {lastFlashState = newState;}; // Set the flash state programmatically
}
}