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 ?

ee3509
  • 171
  • 2
  • 2
  • 6

6 Answers6

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();
}    
Loxley
  • 1,781
  • 17
  • 20
  • 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 Jun 16 '10 at 18:47
  • 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. – Chris Stratton Nov 04 '14 at 16:07
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:

DeltaCap019
  • 6,532
  • 3
  • 48
  • 70
Brad Hein
  • 10,997
  • 12
  • 51
  • 74
  • 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 Jun 16 '10 at 18:43
  • 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 Jun 16 '10 at 22:50
  • 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 Jun 17 '10 at 11:18
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); 
JasCav
  • 34,458
  • 20
  • 113
  • 170
  • 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 Jun 16 '10 at 18:46
  • @ee3509 - Ah, my mistake. I just saw 'brightness' as the last param, and didn't see the flashlight part of the path. – JasCav Jun 16 '10 at 20:10
1

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

Kemal
  • 11
  • 1
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.

Vlad
  • 735
  • 2
  • 10
  • 19
  • 2
    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. – Ralf Nov 30 '12 at 16:10
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.

thomasfedb
  • 5,990
  • 2
  • 37
  • 65
  • 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 Jun 16 '10 at 18:41