Using adb to play a game for you on an Android device.

May 17th, 2012 by joey | Filed under Uncategorized.

I recently flashed my Kindle Fire with the latest CyanogenMod 9 image using the awesome Kindle Fire Utility.  This put Android Ice Cream Sandwhich on my Kindle Fire.

I play a game “Defender II” which is a lot of fun.  But, you have to play it ALOT to level up and upgrade all your weapons.  (Which takes a LONG time).

I knew about Androids Debug Bridge which you use to root most android devices.  And so I did a quick google search to see if you could simulate touches on the screen with it.

YOU CAN!  So, I wrote a little php page that uses some javascript, jQuery, and ajax to send the right inputs to my kindle fire to play the game for me.  It doesn’t do any reading from the device, just screen touches, so it obviously doesn’t play the game very well.  But, it provides me with Defender II cash, and experience points every time it plays for me.  So, if I let it run for a few days, I should be able to get the next 75000 point Multi Arrow upgrade!



While somewhat of a useless excercise I did learn alot about the adb  and a little more bout Android.

The code to do this yourself is below:

(send_input.php)

<?php
if (!empty($_GET['action'])) {
    $commands = array();
    if ($_GET['action'] == 'click') {
        //Up
        $commands[] = "sendevent /dev/input/event1 3 48 0";
        $commands[] = "sendevent /dev/input/event1 0 2 0";
        $commands[] = "sendevent /dev/input/event1 0 0 0";

        //Down
        $commands[] = "sendevent /dev/input/event1 3 57 0";
        $commands[] = "sendevent /dev/input/event1 3 53 " . $_GET['y'];
        $commands[] = "sendevent /dev/input/event1 3 54 " . $_GET['x'];
        $commands[] = "sendevent /dev/input/event1 3 48 1";
        $commands[] = "sendevent /dev/input/event1 0 2 0";
        $commands[] = "sendevent /dev/input/event1 0 0 0";

        //Up
        $commands[] = "sendevent /dev/input/event1 3 48 0";
        $commands[] = "sendevent /dev/input/event1 0 2 0";
        $commands[] = "sendevent /dev/input/event1 0 0 0";
    } elseif ($_GET['action'] == 'move') {
        $commands[] = "sendevent /dev/input/event1 3 57 0";
        $commands[] = "sendevent /dev/input/event1 3 53 " . $_GET['y'];
        $commands[] = "sendevent /dev/input/event1 3 54 " . $_GET['x'];
        $commands[] = "sendevent /dev/input/event1 3 48 1";
        $commands[] = "sendevent /dev/input/event1 0 2 0";
        $commands[] = "sendevent /dev/input/event1 0 0 0";
    } elseif ($_GET['action'] == 'up') {
        $commands[] = "sendevent /dev/input/event1 3 48 0";
        $commands[] = "sendevent /dev/input/event1 0 2 0";
        $commands[] = "sendevent /dev/input/event1 0 0 0";
    }

    system('adb shell ' . implode(";", $commands) . ";");
} else {

    ?>
<link rel="stylesheet" type="text/css"
      href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>
<script>
    jQuery(function () {
        jQuery('[x]').click(function () {
            var $this = jQuery(this);
            jQuery('[name=x]').val($this.attr('x'));
            jQuery('[name=y]').val($this.attr('y'));
            jQuery('[name=action]').val(jQuery(this).attr('name'));
        });

        jQuery('[type=button]').click(function () {
            jQuery('[name=action]').val(jQuery(this).attr('name'));
            jQuery('form').submit();
        });

        jQuery('form').submit(
            function () {
                var $this = jQuery(this);
                jQuery.ajax('send_input.php?' + $this.serialize(), {async: false});
                return false;
            }
        )    ;



    });

    function play(){
        jQuery('[value=Top]').click();
        jQuery('[value=Top]').click();
        jQuery('[value=MiddleTop]').click();
        jQuery('[value=Middle]').click();
        jQuery('[value=MiddleBottom]').click();
        jQuery('[value=MiddleBottom]').click();
        jQuery('[value=Top]').click();
        jQuery('[value=Top]').click();
        jQuery('[value=MiddleTop]').click();
        jQuery('[value=Middle]').click();
        jQuery('[value=MiddleBottom]').click();
        jQuery('[value=MiddleBottom]').click();
        jQuery('[name=up]').click();
        jQuery('[value=AdClose]').click();
        jQuery('[value=ContinueButton]').click();
        setTimeout(play, 1000);
    }

</script>
<form method="get">
    X: <input type="text" name="x" value="<?=!empty($_GET['x']) ? $_GET['x'] : 0 ?>">
    <br/>
    Y: <input type="text" name="y" value="<?=!empty($_GET['y']) ? $_GET['y'] : 0 ?>">
    <br/>
    Action: <input type="text" name="action" value="<?=!empty($_GET['action']) ? $_GET['action'] : '' ?>">
    <br/>

    <input type="button" name="click" value="Down">
    <input type="button" name="move" value="Move">
    <input type="button" name="up" value="Up">
    <br/>
    <input type="button" name="move" value="Top" x="3500" y="2100">
    <input type="button" name="move" value="MiddleTop" x="3500" y="1575">
    <input type="button" name="move" value="Middle" x="3500" y="1050">
    <input type="button" name="move" value="MiddleBottom" x="3500" y="525">

    <input type="button" name="move" value="Bottom" x="3500" y="0">
    <br/>
    <input type="button" name="click" value="ContinueButton" x="3500" y="2100">
    <input type="button" name="click" value="AdClose" x="3500" y="1000">
</form>
    <input type="button" name="start" value="Auto Play" onclick="play();"/>
<?

}
?>

Leave a Reply

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette