Friday, April 4, 2014

Sending touch event to the android device by using monkeyrunner



Using the monkeyrunner, you can control the device with connected on a pc.
It is contained in the android sdk from google.

http://developer.android.com/tools/help/monkeyrunner_concepts.html


Preparation

  1. Install android sdk (recent version, maybe?)
  2. Turn on the debugging option of the device

  3. If DOWN_AND_UP is not working well,
    Settings >  Pointer speed > Lower the speed


Write following python for the first try.

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
# Touches (x, y) position
device.touch(x, y, MonkeyDevice.DOWN_AND_UP)

Connect your device to your pc, confirm it is well being debugging.
Type the following script.
$ adb devices

If it is well connected, it will reply device's some string.

To confirm that the touch is working well, I suggest you run some paint app first such as Sketchbook express, Evernote skitch.

With the device connected to the pc, run monkeyrunner with your python script.

<android sdk directory>/tools/monkeyrunner <your script>.py

(x, y) position is dependent on your device's resolution (pixel coordination).
(500, 500) is ok, I think.

If you want to know exact position of the device, turn some settings on.

  1. Developer options > Input > Show touches CHECK
  2. Developer options > Input > Pointer location CHECK
With this setting, you can check the exact coordinate of the location when you touch somewhere.
Location will be shown on the top of the screen.