Send adb command using PERL - android

Lets take a simple example.
My android real device is connected to PC via USB and ADB is connected.
I used
D:\Test>adb shell input keyevent 29
to enter A
I want to implement a PERL script for this and want to execute same functionality using
C:\Perl\bin>perl t.pl
I tried:
t.pl content:
system("adb shell input keyevent 29");
It say adb is not recognized command

The code you have written should work, unless your ADB is configured in environment variable. Try the script using your android device id.
Once you configure the environment variable of ADB, do this.
D:\Test>adb devices
Once you get the device ID, try the following code in your perl script.
system("adb -s <device id> shell input keyevent 10");
P.S: I tried with 10 keyevent using dialer to check which key press in number 10. It presses number 3 from the dialpad.

Related

How to use Windows batch file to create a Txt file on a sony device SD card

I am trying to automate a txt file to be created and installed to an android phone.
Hi, I have created a batch file to automate a script, setting up a Sony device. The script works fine and is great, however I am now trying to create a Txt file that stores information of the version and date that the device was scripted within the internal memory.
So far, all I need to do is set the device to developer mode, enable USB debugging then double click the windows batch file and it runs as expected. the list of commands follow as such:
\
shell input tap 0358 1108 #all apps\
shell input tap 0610 0802 #settings\
shell input tap 0343 0427 #wifi\
shell input tap 0674 0101 #3 dots\
shell input tap 0437 0290 #advanced\
shell input tap 0630 0252 #network notification off \
shell input keyevent 4 #back\
shell input keyevent 4 #exit\
shell input tap 0350 0552 #bluetooth\
shell input tap 0635 0212 #on\
shell input keyevent 4 #exit\
shell input tap 0357 0871 #more\
shell input tap 0340 0410 #android beam\
shell input tap 0648 0198 #off .........
This continues further and again works great. What I am trying to achieve is that the script creates a Txt files within the SD card, so I can refer back to which version the script used in the many months to come. So far I can achieve the following by manually opening CMD.
adb shell \
shell#F3311:/ $ echo "#Sony_Script \t#Version 1.0 \n\n \t\t#08/11/2022" > /sdcard/Android/data/ScriptVersion.txt
This creates the file as expected within the directory. However I haven't got the time to do this manually for every device and would like this automated but it never seems to work.
I don't want to open adb shell every time to do this, I am hoping to implement it within the original batch file so it can run in the background. I hope this makes sense. Below is the windows batch file I have created.
setlocal ENABLEDELAYEDEXPANSION\
#echo off
REM adb command to grab all connected devices
adb devices > adb.txt
findstr /v "List of devices attached" adb.txt > clean.txt
for /f "tokens=*" %%a in (Version.txt) do (
for /f "tokens=1" %%b in (clean.txt) do (
echo adb -s %%b %%a
start adb -s %%b %%a
)
TIMEOUT /T 4
)
Maintain a script with version, something like
:
#Sony_Script
#Version 1.0
#08/11/2022
input tap 0358 1108 #all apps
input tap 0610 0802 #settings
input tap 0343 0427 #wifi
input tap 0674 0101 #3 dots
input tap 0437 0290 #advanced
input tap 0630 0252 #network notification off
...
then
adb push myscript /sdcard/
adb shell sh /sdcard/myscript
you will know exactly what was executed on each device

`adb shell input` returns `Killed` response without any input registered

I am trying to simulate touches on my Xiaomi Mi Max 2. I tried monkeyrunner but failed, so I am trying adb. However, each command is returned by Killed response and no touch/key is pressed. What does it mean?
I used to get this on a Xiaomi device with MIUI we used for development.
When sending input commands using adb I could not see any output and the adb command completed with SIGEXIT.
When I tried to manually issue it in an adb shell (on the device), I would see a killed output.
Solved by enabling USB debugging (Security setting) (Note that this is not the standard USB debugging setting, which was already enabled) in the Developer options. This required a number of confirmation steps, and once enabled, the input commands worked.
You need to root your android device before executing that command. Few adb commands need root privilege
My xiaomi max 3 works
PS D:\ADB Command> adb devices
List of devices attached
606704bf device
MKJ0117A19000186 device
PS D:\ADB Command> adb -s 606704bf shell
nitrogen:/ $ input keyevent MENU
nitrogen:/ $ input keyevent MENU
nitrogen:/ $

python and (android) adb shell

i would like to simulate touch on my anroid phone through a python code on my computer using the "adb shell tap x y" function (or any other way you may know). I have tried using
from subprocess import call
call(["adb", "kill-server"])
call(["adb", "shell"])
call(["input", "tap" , "1400" , "800"]) //example of x and y
but it just reaches the "shell" call and gets stuck.
(I know the tap function works because it works on the ordinary cmd window)
This should do it:
from subprocess import call
call(["adb", "shell", "input", "tap" , "1400" , "800"])
In your original script:
You start a remote shell on your Android device (adb shell)
After you quit the remote shell typing exit, you issue a command on your host computer shell (input tap 1400 800).
Instead you should use adb to redirect a command to the Android device's remote shell. To do that, just append the command after adb shell, for example adb shell input tap 1400 800. Take a look here.
I also removed the adb kill-process line because there is no kill-process adb command.

ADB: How to tap/close from recent apps to completely through ADB one liner command

I have tried multiple commands to close the app from App switcher but didn't through it. I even don't know if there is ány adb command which is persist to close the app from the app switcher. I more Googled for the same and even went through Android but no luck.
Does anyone know about this, how to tackle?
Kindly suggest our valuable inputs.
Thanks in advance
Finally, I got the answer when I tried combining multiple commands:
Open the app switcher
adb shell input keyevent KEYCODE_APP_SWITCH
Select or navigate to the next app in the app switcher
adb shell input keyevent 20
...
(run the above command again for each app down the list)
Remove the app from open app list
adb shell input keyevent DEL
And you are done :-) the app gone out of your open apps list.
android 5.0 and more
open recent apps
adb shell input keyevent KEYCODE_APP_SWITCH
select app
adb shell input keyevent KEYCODE_DPAD_DOWN
clear from recent apps
adb shell input keyevent DEL
Thanks to #rilwan for the answer, below command worked for me:
First enter into adb shell then execute:
$ adb shell
$ input keyevent KEYCODE_APP_SWITCH && input swipe 522 1647 522 90
or execute at once
$ adb shell input keyevent KEYCODE_APP_SWITCH && input swipe 522 1647 522 90
Here are some tips-
Open recent apps-
adb shell input keyevent KEYCODE_APP_SWITCHER
Tap the cross X or close all . Depends on your system UI you need to navigate to it and press enter.
For me below worked, you can find what works for your device using up,down,tab KEYEVENTs.
adb shell input keyevent KEYCODE_DPAD_DOWN
adb shell input keyevent KEYCODE_ENTER
#get apps
dumpsys window windows | grep -P 'topApp.* u0 ([^/]+)' | grep -P '(?<= u0 )[^/]+' | xargs -l am force-stop

How to enable Speakerphone on android through adb shell?

I am working on a testing tool and I am able to initiate and terminate voice call from default Dialer app in android using following commands.
adb shell am start -a android.intent.action.CALL -d tel:+919999999999
adb shell input keyevent KEYCODE_ENDCALL
I need to enable Speakerphone through adb interface after initiating voice call. How can I achieve this? My android device may not have root permission.

Categories

Resources