views:

272

answers:

5

I am trying to execute a command from within my code, the command is "echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness" and I can run it without problems from adb shell

I am using Runtime class to execute it :

Runtime.getRuntime().exec("echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness");

However I get a permissions error since I am not supposed to access the sys directory. I have also tried to place the command in a String[] just in case spaces caused a problem but it didn't make much differense.

Does anyone know any workaround for this ?

A: 

I think the device will need to be "rooted" for this to work. Have a search on google and see how other developers have dont this, there is no shortage of flashlight apps.

thomasfedb
I've only seen one app that can turn on the flash led on my Desire and I don't have the shource code for it, still waiting for the developer to give me a hint.Thanks for your answer
ee3509
+1  A: 

Agreed you probably need to root the phone to write to system files. I'm surprised the brightness isn't exposed through the SDK.

For details on running shell commands from code, check out this project: http://code.google.com/p/android-log-collector/source/browse/trunk/android-log-collector/src/com/xtralogic/android/logcollector/SendLogActivity.java

Brad Hein
Thanks for your comment, this is the approach I am using and I still get the permission error.When I try to send the same command from an adb shell the command executes properly which makes me think it might not be necessary to have a rooted phone - not sure though.
ee3509
That file/device does not exist on my handset. By what means does the flashlight.0 directory come to be? I searched around using `adb shell` and found that I can `cd` all the way up to `/sys/devices/platform` but I do not have any file named flashlight.0. Furthermore, everything in that directory is owned by root with mode 755, which means only root can write to the files. This permission seems to be applied to everything underneith `/sys` so if your `flashlight.0/leds/flashlight/brightness` does exist, and is owned by root, and has mode 755 permissions (`drwxr-xr-x`) thenyou must have root.
Brad Hein
I'm using HTC Desire and the file is there, I have also executed the command succesfully (the led on the back will turn on) from adb shell without having rooted my device.The permissions for brightness are (-rw-rw-rw-)
ee3509
+1  A: 

The phone needs to be rooted, afterwards you can do something like:

public static void doCmds(List<String> cmds) throws Exception {
    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());

    for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd+"\n");
    }

    os.writeBytes("exit\n");
    os.flush();
    os.close();

    process.waitFor();
}    
Loxley
I've already tried this on my non routed device and didn't work as anticipated. However I feel I might be missing something here because I can execute the command from adb shell succesfully, and I've found software that turns the led on somehow.Thanks for your answer.
ee3509
+1  A: 

If you're just trying to set brightness, why don't you do so through the provided API (AKA, is there a reason you are trying to do it the way you are).

int brightness = 125; 
Settings.System.putInt(
      ftaContext.getContentResolver(), 
      Settings.System.SCREEN_BRIGHTNESS, 
      brightness); 
JasCav
It's not the screen brightness I am intertesd in but the flash led next to the camera lense.There is a FLASH_MODE_TORCH property in the camera settings which is not supported on desire (didn't do much on mine when I tried it), but apparently I've seen software capable of doing that so I guess there is a workaround.Thanks
ee3509
@ee3509 - Ah, my mistake. I just saw 'brightness' as the last param, and didn't see the flashlight part of the path.
JasCav
A: 

Did you find a solution to this?

It's certainly possible. I have found at least one app that could do it. I have been searching a way to do it all night too, without success.

Peter