📜 ⬆️ ⬇️

We program LED on the phone


Although the Android API does not allow you to directly turn on or off the LED indicator or camera flash, on some phones there is such an opportunity.

How to programmatically flash with multi-colored lights, how to write your own “Flashlight” or what other device LEDs can be controlled - you will learn about it below.

It all started with the fact that I, while exploring the file system of my HTC Desire using ES Explorer , accidentally stumbled upon interesting directories: /sys/class/leds/blue , /sys/class/leds/flashlight , etc.
What other blue ?! I only saw the orange and green lights. But the most interesting is that inside these directories there is a brightness file with the right to write! What I immediately took advantage of.
')
In fact, this is not a simple file, but an interface for working with the LED driver. So, having /sys/class/leds/blue/brightness positive number in the /sys/class/leds/blue/brightness file, we will turn on the blue indicator on the phone, write 0 to turn it off. Similar to the amber and green indicators. Turning two LEDs together, we get new colors: amber + blue = purple; green + blue = aqua.
Subdirectory in / sys / class / ledsWhat is responsible
lcd backlightThe brightness of the display. The brightness file is written number from 0 to 255: more - brighter.
flashlightThe brightness of the LED flash. Possible values: 0, 127, 128, 255.
button-backlightButton illumination (on / off).
amber
green
blue
Orange, green and blue indicators (on / off),
as well as the purple (amber + blue) and cyan (green + blue) indicators.

And now how this is all programmed

public void ledControl(String name, int brightness) {<br>
try {<br>
FileWriter fw = new FileWriter( "/sys/class/leds/" + name + "/brightness" );<br>
fw.write(Integer.toString(brightness));<br>
fw.close();<br>
} catch (Exception e) {<br>
// LED <br>
}<br>
}<br>
<br>
// <br>
ledControl( "amber" , 255 );<br>
ledControl( "blue" , 255 );<br>
<br>
// <br>
ledControl( "lcd-backlight" , 30 );<br>
<br>
// <br>
ledControl( "button-backlight" , 0 );<br>
<br>
// <br>
ledControl( "flashlight" , 128 );<br>

The example application with source codes can be downloaded here .


Conclusion

Everything! Now the phone glows like a Christmas tree. The code was checked only on HTC Desire running Android 2.2, but it can probably work on other devices. Email me if it works or fails to focus on your phone.

Source: https://habr.com/ru/post/104627/


All Articles