Thanks to SoftTeco. I found how to send touch event directly to Android devices. However, his code works for 2.3.3, but not ICS and Jelly Bean. (I did not test 3.x).
However, his great blog not only gives you the code, but also tells you how to get it. After searching valid code for ICS without any result, I just catch the codes using
adb shell getevent | grep event2
Then convert Hex to decimal.
Here is the code to touch the center of my GS2 (240, 400) running AOKP ICS 4.0.X ( I am lazy, this is NOT the newest version of AOKP)
adb shell sendevent /dev/input/event2 3 57 29
adb shell sendevent /dev/input/event2 3 53 240
adb shell sendevent /dev/input/event2 3 54 400
adb shell sendevent /dev/input/event2 3 48 29
adb shell sendevent /dev/input/event2 3 58 2
adb shell sendevent /dev/input/event2 0 0 0
adb shell sendevent /dev/input/event2 3 57 4294967295
adb shell sendevent /dev/input/event2 0 0 0
As you can see, only 8 lines of code is needed now, versus 16 lines for 2.x.
Also, if you run this on DOS cmd, copy all lines and add one more line of something, and paste it all to execute all 8 lines. If you don’t add one more line, you only run 7 lines and result is unpredictable.
You can also put all of them in one line with no new line break:
adb shell "sendevent /dev/input/event2 3 57 29;sendevent /dev/input/event2 3 53 240;sendevent /dev/input/event2 3 54 400;sendevent /dev/input/event2 3 48 29;sendevent /dev/input/event2 3 58 2;sendevent /dev/input/event2 0 0 0;sendevent /dev/input/event2 3 57 4294967295;sendevent /dev/input/event2 0 0 0;"
I did not test it on Jelly Bean, but I think it should be same.
Again, the credit goes to SOFTTE, I am merely using his method to get the code for a different version of Android.
Revision: Tested on Jelly Bean. Different code. So your best shot is getevent.
Pingback: A simple low level test automation | Fang Mobile
Here’s a small piece of python code that works for me on a Samsung GT-I9000 with CM10 (JB):
import os
import time
DN=1
UP=2
DNUP=UP|DN
def adbshell(cmd):
return os.system(“adb shell ‘%s'” % cmd)
def adbtouch(x,y,mode=DNUP):
s = ”
if (mode & DN) == DN:
s += ‘adb shell sendevent /dev/input/event0 3 53 %d;’ % x
s += ‘adb shell sendevent /dev/input/event0 3 54 %d;’ % y
s += ‘adb shell sendevent /dev/input/event0 3 58 71;’
s += ‘adb shell sendevent /dev/input/event0 3 48 4;’
s += ‘adb shell sendevent /dev/input/event0 3 57 0;’
s += ‘adb shell sendevent /dev/input/event0 0 2 0;’
s += ‘adb shell sendevent /dev/input/event0 0 0 0;’
if (mode & UP) == UP:
s += ‘adb shell sendevent /dev/input/event0 0 2 0;’
s += ‘adb shell sendevent /dev/input/event0 0 0 0;’
if s != ”:
adbshell(s)
Sorry about the indentation ….
Thanks for the info. I already got the code for JB and am running it. I just did not paste it here and want to leave it as exercise for readers. Don’t worry about the indentation.
If you are using Python script already then you can simply use Monkeyrunner. use device.touch(,, ‘DOWN_AND_UP’) to touch the on screen. It is OS version and screen resolution independent.
Note your 4294967295 is hex 0xFFFFFFFF, which is -1. I’d guess code 3 57 x is ‘finger pressure’.
You are most likely right Tim! I only know line2 and line 3 are related to coordinates. So to touch another spot on screen , just change those two numbers. I had no idea what the other lines do. Your observation makes total sense.