# Turn on/off Android Phone Flashlight

**URL:** <https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496>\
**Category:** Processing for Android\
**Created:** [August 25, 2020, 11:49pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496 "2020-08-25T23:49:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoLi](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@JoLi](https://discourse.processing.org/u/JoLi)\
**Post date:** [August 25, 2020, 11:49pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/1 "2020-08-25T23:49:29Z")

</div>

Hi,  
I have a simple problem:  
I want a simple sketch to turn on/off the flashlight on my phone.  
it would be nice, if it stays on, even if I turn off the screen of my phone.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 26, 2020, 2:48am UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/2 "2020-08-26T02:48:29Z")

</div>

Hi. Check [this code](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/Flash%20light) if it works for you. Do not forget to give CAMERA permission.  
If I remember well @akenaton pointed out that above Lollipop you have to ask for permission at runtime. I will look into that tomorrow, or maybe he himself can provide the code to add.

---

<div class="post-metadata">

**Author:** ![Bobby54](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bobby54/32/9302_2.png) [@Bobby54](https://discourse.processing.org/u/Bobby54)\
**Post date:** [August 26, 2020, 5:17pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/3 "2020-08-26T17:17:20Z")

</div>

I ran the code on my Android 6 Samsung Galaxy S5 mini and it ran fine.  
Then I tried it on my Sony XA2 with Android 9 and it doesn’t work.  
So I added different colors for flashlight on/off(works fine on Android 6 as well) and it shows me the fitting colors on Android 9 but still no light.  
Has anyone got any ideas on how to fix this?

PS: Camera permission is given in both cases.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [August 27, 2020, 8:13am UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/4 "2020-08-27T08:13:32Z")

</div>

@Bobby54 === how have you asked for permissions?

---

<div class="post-metadata">

**Author:** ![Bobby54](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bobby54/32/9302_2.png) [@Bobby54](https://discourse.processing.org/u/Bobby54)\
**Post date:** [August 27, 2020, 9:33am UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/5 "2020-08-27T09:33:45Z")

</div>

Not in the sketch code but in the drop down menu I ticked Camera and the file which is created for every android sketch(manifest I believe) also said `<uses-permission android:name="android.permission.CAMERA"`(hope the spelling and syntax is correct, don’t have the code currently but it is definitely the right one).  
Also I gave the sketch Camera permission after installing it on my phone.

---

<div class="post-metadata">

**Author:** ![Bobby54](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bobby54/32/9302_2.png) [@Bobby54](https://discourse.processing.org/u/Bobby54)\
**Post date:** [August 28, 2020, 4:09pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/6 "2020-08-28T16:09:20Z")

</div>

I now tried it by adding:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9ce3b7c417670bd30e5ac83b106099ac88d2e922.png)

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3548337db7d0a66086945a011188edfd49d46e89.png)  
The console logs that the sketch has camera permission ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1620c8f68fee571b53c818c7b29d956341662e3c.png) but the flashlight still doesn’t turn on.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 28, 2020, 4:21pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/7 "2020-08-28T16:21:42Z")

</div>

Unfortunately I can only test up to Lollipop, so it make no sence for me posting code.  
You’ll have to wait for @akenaton.  
Did you try [these](https://stackoverflow.com/questions/38552144/how-get-permission-for-camera-in-android-specifically-marshmallow) codes?

---

<div class="post-metadata">

**Author:** ![Bobby54](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bobby54/32/9302_2.png) [@Bobby54](https://discourse.processing.org/u/Bobby54)\
**Post date:** [August 28, 2020, 4:28pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/8 "2020-08-28T16:28:27Z")

</div>

No not yet but I will, thanks.

---

<div class="post-metadata">

**Author:** ![JoLi](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@JoLi](https://discourse.processing.org/u/JoLi)\
**Post date:** [August 29, 2020, 11:00am UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/9 "2020-08-29T11:00:06Z")

</div>

I tried the original code on android 8.1, but it doesn’t work  
the preview (i tried installing as app too) instantly crashes when trying to open  
i gave the permission to camera and flashlight in the APDE sketch properties menu

---

<div class="post-metadata">

**Author:** ![Bobby54](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bobby54/32/9302_2.png) [@Bobby54](https://discourse.processing.org/u/Bobby54)\
**Post date:** [August 30, 2020, 5:48pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/10 "2020-08-30T17:48:46Z")

</div>

Ha, turns out `Camera` is deprecated since Android 5 which means that I probably only got lucky with `Camera` working on Android 6 because it wasn’t completely disabled yet.

However there is a package named `camera2` (see here: [https://developer.android.com/reference/android/hardware/camera2/package-summary](https://developer.android.com/reference/android/hardware/camera2/package-summary)).  
In this package there is a class CameraManager.TorchCallback(see here: [https://developer.android.com/reference/android/hardware/camera2/CameraManager.TorchCallback](https://developer.android.com/reference/android/hardware/camera2/CameraManager.TorchCallback)) with the function `setTorchMode(String camerID, boolean enabled)` to control if the camera flashlight is enabled (then ´enabled = true´) or if the flashlight is turned off and available to be turned back on  
(`enabled = false`).

Addition: You can get the CameraId which is required by `setTorchMode()` by using the `CameraCharacteristics` class which is also included in the package.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c291adf91d9f54d78bf18e0ffaedefc006877aff.png)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 30, 2020, 7:56pm UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/11 "2020-08-30T19:56:12Z")

</div>

The permission worked as you described above?

---

<div class="post-metadata">

**Author:** ![Bobby54](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bobby54/32/9302_2.png) [@Bobby54](https://discourse.processing.org/u/Bobby54)\
**Post date:** [August 31, 2020, 5:49am UTC](https://discourse.processing.org/t/turn-on-off-android-phone-flashlight/23496/12 "2020-08-31T05:49:53Z")

</div>

Haven’t done it yet but found this in the mean time:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/513cbe73a61f525e22c1c609d7bdee0800d25616.png)  
According to this you only need to give the Camera permission in the Manifest file which can be done by typing it in the manifest manually or by ticking the Camera permission in Processing.
