Search This Blog

Thursday, December 29, 2011

Android application - A GUI turning on the flash

In this post the goal is to just to build a GUI with buttons that turn on and off the camera flash.  

The way I see it, there are generally two things for a hardware guy to do with an android platform.
  1. Use it as the human interface device to control something else.  Normally through USB, Bluetooth, 3G, etc.  Human pushes buttons, app sends commands to an Android.
  2. Use it as the brain for a micro controller like application, such as the portable computer for a robot.  Then use it's compute power, sensors, and network interface ability to talk to a remote user.
I'll be exploring the second type of application.  In that case I have to get the Android to talk to the outside world and control things.  It's ability to do that is somewhat limited.  It has a screen where it can display lights, patterns, etc.  It has a couple LEDs, most notably the camera flash LED.   There is a headphone jack that can be used to create waveforms up to maybe 20kHz, and finally a USB jack.   

What I'm eventually going to attempt to do is flash the LED in some sort of modulated fashion that an Arduino or other serial device can recognize and respond to.  I could use the USB port, but that requires a lot of code writing on the Arduino to decode.  Using only the LED I can send binary codes directly to the serial interface of the Arduino, with only a photo transistor for hardware.   I believe the phone may already have a light sensor because it dims the screen in response to ambient light, so communication could be two way.

Once I get the flash working, I will explore how precise the timing of the flashing can be, and what frequency I can modulate data onto it.   There are many other possibilities that I will share once I learn what the capabilities are.

First I want to use the camera flash LED, Some links I found

Stole code here, I pretty much copied out the setOn() functions verbatim and assigned them to my buttons

This is the juicy bits, the rest is all the buttons and GUI that evolved from the skeleton app

// code largely copied from
    private Object mManager;
    public boolean isOn(Context context) {
        try {
            Object manager = getManager();
            if (manager != null) {
                Method getFlashlightEnabledMethod = manager.getClass()
                return (Boolean) getFlashlightEnabledMethod
        } catch (Exception e) {
            Log.e(TAG, "", e);
        return false;
    public void setOn(boolean on, Context context) {
        try {
            Object manager = getManager();
            if (manager != null) {
                Method setFlashlightEnabledMethod = manager.getClass()
                setFlashlightEnabledMethod.invoke(manager, on);
        } catch (Exception e) {
            Log.e(TAG, "", e);
    private Object getManager() {
        if (mManager == null) {
            try {
                Class<?> managerClass = Class
                Method methodGetService = managerClass.getMethod(
                        "getService", String.class);
                IBinder hardwareService = (IBinder) methodGetService
                        .invoke(managerClass, "hardware");
                Class<?> stubClass = Class
                Method asInterfaceMethod = stubClass.getMethod(
                        "asInterface", IBinder.class);
                mManager = asInterfaceMethod.invoke(stubClass,
            } catch (Exception e) {
                Log.e(TAG, "", e);
        return mManager;

That almost worked right off but I got an error that I needed HARDWARE_TEST or FLASHLIGHT permission was needed.

Found an example of setting permissions here:

Later found that the manifest GUI knows the whole list of permissions, you just click on them.
I have to update the AndroidManifest.xml to add the uses-permission statements

Also found that the phone knows what permissions you asked for in the Settings->Applications menu.  Apparently when you load an app this way it doesn't ask if you want to grant permission.  It just takes it.

W00t!  Partial Success!  The flash button on my app now turns on the camera light!   I was missing the FLASHLIGHT permissions in my manifest.  Added an off button too and played around with the GUIs to clean things up.

At this point I have done all the basic GUI things and hardware fundamentals in only a few hours work!   Anyone could do this.  My code is a kluge of the highest order, but I've demonstrated making buttons, menus, displaying text responses, showing a gif, and making the hardware do something.  Awesome.

Here is the emulator running the app, taking a picture of the phone isn't worth the effort.   I showed that in my last post, and because my phone is my camera, it involves borrowing wifey's phone.    Too bad the emulator doesn't have the flashlight.   

Next step is to explore flashing the light like a strobe, and learning what timing sources are available!

No comments:

Post a Comment