- Home /
turn on/off the flash of the android phone
I have this code to turn the flash on / off but it doesn't work, what am I wrong?
AndroidJavaObject _camera = null;
public void setFlashMode(string mode, Boolean enabled)
{
if (_camera != null) {
AndroidJavaObject cameraParameters = _camera.Call<AndroidJavaObject>("getParameters");
cameraParameters.Call("setFlashMode",mode);
_camera.Call("setParameters",cameraParameters);
}
}
public void LEDOn() {
setFlashMode("torch", true);
}
public void LEDOff() {
setFlashMode("off", false);
}
}
Answer by Bunny83 · Mar 12, 2020 at 08:33 PM
Uhm where and when do you actually initialize your _camera variable? Did you actually use the static open method on the Camera class retrieve the first backfacing Camera object? Your code doesn't show this. If the code you've posted is all you have, of course it won't work.
You would need something like this:
using(var camClass = new AndroidJavaClass("android.hardware.Camera"))
{
_camera = camClass.CallStatic("open");
}
I have never used the Camera class. Maybe you can do all this inside your "setFlashMode" method because you should call release on the camera object once you don't use it anymore. So I would suggest one of those two approaches:
public void setFlashMode(bool aEnabled)
{
using(var camClass = new AndroidJavaClass("android.hardware.Camera"))
using(var cam = camClass.CallStatic("open"))
{
if (cam == null)
return;
var cameraParameters = cam.Call<AndroidJavaObject>("getParameters");
cameraParameters.Call("setFlashMode", aEnabled?"torch":"off");
cam.Call("setParameters", cameraParameters);
cam.Call("release");
}
}
If that doesn't work (because you might need to keep the camera object alive) you would do something like
public void setFlashMode(bool aEnabled)
{
if (aEnabled)
{
if (_camera == null)
{
using(var camClass = new AndroidJavaClass("android.hardware.Camera"))
_camera = camClass.CallStatic("open"))
}
var cameraParameters = _camera.Call<AndroidJavaObject>("getParameters");
cameraParameters.Call("setFlashMode", "torch");
_camera.Call("setParameters", cameraParameters);
}
else if (_camera != null)
{
var cameraParameters = _camera.Call<AndroidJavaObject>("getParameters");
cameraParameters.Call("setFlashMode", "off");
_camera.Call("setParameters", cameraParameters);
_camera.Call("release");
_camera.Dispose();
_camera = null;
}
}
The first approach would be the better one if it works.
Answer by stecavalli · Mar 13, 2020 at 01:30 PM
the two codes entered are executed but the torch does not turn on Does not work. I will have to look for a different solution. @Bunny83
SOLVED
I created a plugin with Android studio.
With the code I recalled the functions of the flash light.
This is my code:
using UnityEngine;
using UnityEngine.UI;
public class PluginWrapper : $$anonymous$$onoBehaviour
{
private AndroidJavaObject javaClass;
void Start()
{
javaClass = new AndroidJavaObject("com.stecavalliofficial.flashlightlibrary.flashlibraryplugin");
javaClass.Call("getCamera");
}
public void LedON()
{
javaClass.Call("setFlashlightOn");
}
public void LedOFF()
{
javaClass.Call("setFlashlightOff");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612224833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GameObject and Camera 2 Answers
How to get smooth device camera in gearvr like passthrough option? 0 Answers
How to set resolution for android 0 Answers
Best Setting Size Orthographic Camera? 1 Answer
Check permissions on Android 2 Answers