4

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 ?

6 Answers 6

12

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();
}    
Sign up to request clarification or add additional context in comments.

2 Comments

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.
This will only work on a rooted device. When you use ADB from a PC, you have greater privileges than an app running on the device (and unlike on a secured device, on an emulator, ADB can actually run commands as root). Android is very specifically designed not to permit apps to execute commands with system-wide implications. The reason people hack their devices with root shims is to drill a big hole through that security model - a hole that is not supposed to exist on a stock device.
2

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); 

2 Comments

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 - Ah, my mistake. I just saw 'brightness' as the last param, and didn't see the flashlight part of the path.
2

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:

3 Comments

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.
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.
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-)
1

You also may remout /system with permissions to write..

Comments

0

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.

1 Comment

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
0

The adb shell can behave as a superuser without rooted devices. it's a debug bridge. you can do whatever you want through it.

BUT, when your calling Runtime.getRuntime().exec, you don't have the same premissions. some shell commands aren't even available from exec.

so not only you need a rooted device, you also need su premmisions.

1 Comment

Even with the adb shell you do NOT have root/superuser access on the device, although you do have a little more access than normal apps do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.