Programming LED on the phone


    Although the Android API does not allow you to directly turn on or off the LED indicator or the flash of the camera, some phones have this feature.

    How to programmatically blink with colorful lights, how to write your own “Flashlight”, or what other LEDs of the device can be controlled - you will learn about this below.

    It all started with the fact that I'm exploring the file system of the HTC Desire using ES explorer , stumbled upon a curious directories: /sys/class/leds/blue, /sys/class/leds/flashlightand so on
    What else is blue ?! I saw only the orange and green indicator. But the most interesting - inside these directories was a file brightnesswith write permission! What I immediately took advantage of.

    In fact, this is not a simple file, but an interface for working with the LED driver. So, writing a /sys/class/leds/blue/brightnesspositive number to the file , we turn on the blue indicator on the phone’s body, writing 0 - turn it off. Similarly with amber and green indicators. By turning on the two LEDs together, we get new colors: amber + blue = purple; green + blue = aqua.
    Subdirectory in / sys / class / ledsWhat is responsible for
    lcd-backlightDisplay backlight brightness. A number from 0 to 255 is written to the brightness file: more is brighter.
    flashlightFlash LED brightness. Possible values: 0, 127, 128, 255.
    button-backlightButton illumination (on / off).
    amber
    green
    blue
    Orange, green and blue indicators (on / off),
    as well as magenta (amber + blue) and cyan (green + blue) indicators.

    And now, how is it all programmed

    public void ledControl(String name, int brightness) {

        try {

            FileWriter fw = new FileWriter("/sys/class/leds/" + name + "/brightness");

            fw.write(Integer.toString(brightness));

            fw.close();

        } catch (Exception e) {

            // Управление LED недоступно

        }

    }



    // Включим пурпурный индикатор

    ledControl("amber", 255);

    ledControl("blue",  255);



    // Сделаем дисплей темнее

    ledControl("lcd-backlight", 30);



    // Выключим подсветку кнопок

    ledControl("button-backlight", 0);



    // Организуем фонарик средней яркости

    ledControl("flashlight", 128);


    An example application with source codes can be downloaded here .


    Conclusion

    All! Now the phone glows like a Christmas tree. The code was only tested on HTC Desire running Android 2.2, but it can probably work on other devices. Email me if focus on your phone will work or not.

    Also popular now: