Sorry about my English, I dont speak very well.
I have an app that comumnicates with an arduino device by blinking flash. I notice that both Camera1 and Camera2 have issues to work into all Android devices, so I builded a settings screen that user can test both and choose the one that works fine.
I'm now trying to build the same communication with CameraX, hoping that works fine into more devices, but I cant find examples to just toggle the flash. I'm new at android develop, and the material I found is just about taking pictures and stuff, but I dont want even open a camera screen, just turn on and off the flash, like lantern.
Can someone help with this, or send a documentation that helps?
edit1
I did this in onCreate and I see the logs into logcat but flash dont toggle. Maybe I need to create case ?
lateinit var cameraControl: CameraControl
val cameraProcessFuture = ProcessCameraProvider.getInstance(this)
cameraProcessFuture.addListener(Runnable {
val cameraProvider = cameraProcessFuture.get()
val lifecycleOwner = this
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector)
cameraControl = camera.cameraControl
val listenableFuture = cameraControl!!.enableTorch(true)
// cameraControl.enableTorch(false)
Log.d("MurilloTesteCamera", "listener")
listenableFuture.addListener(Runnable {
Log.d("MurilloTesteCamera", "listener 2")
}, ContextCompat.getMainExecutor(this))
}, ContextCompat.getMainExecutor(this))
Log.d("MurilloTesteCamera", "oncreate")
edit2
this code I tried to create a case, but no solve my problem and flash stil dont turn on ( my activity implements CameraXConfig.Provider:
val context = this
Log.d("MurilloTesteCamera", "before initialize")
CameraX.initialize(context, cameraXConfig).addListener(Runnable {
Log.d("MurilloTesteCamera", "inside initialize")
CameraX.unbindAll()
val preview = Preview.Builder()
.apply {
setTargetResolution(Size(640, 480))
}
.build()
lateinit var cameraControl: CameraControl
val cameraProcessFuture = ProcessCameraProvider.getInstance(context)
cameraProcessFuture.addListener(Runnable {
val cameraProvider = cameraProcessFuture.get()
val lifecycleOwner = context
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector)
cameraControl = camera.cameraControl
camera.cameraInfo.hasFlashUnit()
Log.d("MurilloTesteCamera", "info before -> " + camera.cameraInfo.torchState)
Log.d("MurilloTesteCamera", "has flash -> " + camera.cameraInfo.hasFlashUnit())
val listenableFuture = cameraControl.enableTorch(true)
Log.d("MurilloTesteCamera", "listener")
listenableFuture.addListener(Runnable {
Log.d("MurilloTesteCamera", "info after -> " + camera.cameraInfo.torchState)
Log.d("MurilloTesteCamera", "listener 2")
}, ContextCompat.getMainExecutor(context))
CameraX.bindToLifecycle(context, cameraSelector, preview)
}, ContextCompat.getMainExecutor(context))
}, ContextCompat.getMainExecutor(context))
Log.d("MurilloTesteCamera", "after initialize")
while (!CameraX.isInitialized()){}
Log.d("MurilloTesteCamera", "after while")