9

At home I often connect my android to some speakers, to listen to internet-radio. It would like to be able to change the sound-level through a command on my laptop.

I can open an ssh shell to the phone, but I am stuck finding a command to change the volume on the android.

Is there a command to change the audio-volume on the android-shell? Or maybe there is a different approach without ssh that I missed completely?

I am running Android 4.1.

Edit

The accepted answer should work for rooted devices. I am still keen on getting an answer for an unrooted device.

1 Answer 1

13

Have a look at simulating keyevents on Android and search for "KEYCODE_VOLUME_UP" and "KEYCODE_VOLUME_DOWN".

Events can be generated by executing "input":

#local shell:
input keyevent 24 #vol up
input keyevent 25 #vol down
#remotely using ssh
ssh your-phone input keyevent 24 #vol up
ssh your-phone input keyevent 25 #vol down

# 'su -c command' to make it run as root (if not already). EDIT: now escaped properly.
ssh your-phone su -c "input\ keyevent\ 24" #vol up
ssh your-phone su -c "input\ keyevent\ 25" #vol down"

Your shell needs appropriate permissions most probably to fire input events I guess. If you do public key authentication and keep your private key loaded in memory (ssh-add) you can make a wrapper script and just execute this without beeing asked for a passphrase. Note: There's a considerable overhead for both 'su' and 'input', all in all one single volume adjust takes 2.5s to complete on my HTC Desire.

EDIT2:

shell@android:/ $ id
uid=2000(shell) gid=2000(shell) groups=1003(graphics),1004(input),[...]
shell@android:/ $ time input keyevent 24                                       
    0m0.92s real     0m0.29s user     0m0.06s system

Seems like you have to be member of the input group (here GID=2004) to have the capability to generate keyevents using 'input'. Also, I noticed that the screen has to not be locked (and/or off), because the lock would consume all keyevents eventually. Note also that the command takes almost 1 second.

6
  • This looks very promising. The commands are accepted by the shell without failure, but the volume does not change. I will probably have to play a bit more with my permissions.
    – mirk
    Jul 14, 2012 at 17:19
  • it works for me (using a locally connected phone via "adb shell" with root). Most probably it's a question of permissions. Maybe look at logcat if there's some output about rejected 'input' calls
    – ce4
    Jul 14, 2012 at 17:33
  • Nope: I don't think I have root access. Additionally, It tells me 'su not found' when running as android user. Logcat gives the following output: I/Input ( 2502): InjectKeyEvent: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_VOLUME_UP, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=399847, downTime=399847, deviceId=-1, source=0x101 } I/Input ( 2502): InjectKeyEvent: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_VOLUME_UP, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=399847, downTime=399847, deviceId=-1, source=0x101 }. I have to add that I am new to android.
    – mirk
    Jul 15, 2012 at 19:01
  • Ok, I am not a member of the input(1004) group. I will root my device one day, and try again.
    – mirk
    Jul 17, 2012 at 17:21
  • There must be an option to not needing root, like (mis)using tasker with intents for that (tasker most likely has the necessary rights for almost everything...)
    – ce4
    Jul 17, 2012 at 23:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .