Tuesday, February 5, 2019

5.1 lollipop - Run adb command inside the terminal emulator or programmatically without root


Is there a way to run theadb backupcommand on the device itself without using a ᴘᴄ computer ?



Of course when I try to do it,adbcomplains the daemon isn’t started and it fails to find a device.


u0_a192@klimtwifi:/ $ adb connect localhost:9999
* daemon not running. starting it now on port 5038 *
* daemon started successfully *
unable to connect to localhost:9999:9999
u0_a192@klimtwifi:/ $ adb connect localhost:5038
connected to localhost:5038
u0_a192@klimtwifi:/ $ adb shell
error device not found
u0_a192@klimtwifi:/ $


Answer



Addressing Revision 4


You're still doing things incorrectly even though I noted in revision 1 of my answer that you would need in PC for once and once alone as long as no reboot or disabling of USB debugging occurs.


Regarding your attempt:


u0_a192@klimtwifi:/ $ adb connect localhost:9999  
* daemon not running. starting it now on port 5038 *
* daemon started successfully *
unable to connect to localhost:9999:9999
u0_a192@klimtwifi:/ $ adb connect localhost:5038
connected to localhost:5038

u0_a192@klimtwifi:/ $ adb shell
error device not found

adb connect localhost:9999 didn't work because you didn't do adb tcpip 9999 from a PC. The said command would set the property service.adb.tcp.port to specified port which in turn is read by adbd when it is started/restarted and used to listen for connections.


adb connect localhost:5038 didn't benefit you simply because either you got confused or failed to understand the basics I noted in revision 1 of my answer.


First, clear up any confusion. Just because the term daemon is mentioned doesn't mean that it is adbd. A daemon can be for lots of things. A description of protocol between adb clients and server is noted here. A good understanding of how adb client-server-device/emulator communication occurs is noted here. You're requested to read all the documents to gain understanding of how adb works.


Now, here are the details for your attempt: When you did adb connect localhost:9999, you actually instructed adb client in your host Android device to connect to a slave running adbd listening on 9999. Your adb client tried to look for adb server on your host Android device. Because it didn't find the server running, it started a server and bound the server to port 5038. Nowhere you instructed adbd to listen on port 9999 or even 5038. You can confirm this with root access. Do su -c 'netstat -natup' and you would find adb not adbd listening on port 5038 after execution of your command.


It should now be clear why adb shell resulted in the error.


I should stress that it is futile to even think about running adb client related command on a device without access to PC for once or without root access. I have already noted the procedure in regard to PC in revision 1. If you do have root access and still need to use adb on device then do:


su 2000 -c 'setprop service.adb.tcp.port  PORT'   # choose PORT greater than 1024.

su -c 'stop adbd && start adbd' # this would stop and start adbd.
su -c adb connect localhost:PORT # you shall now be connected and would be required to accept fingerprint to authorize USB debugging.

(Click an image to enlarge it)


IMG: IMG:




Edited Revision 1


Looking for this?


IMG:


You're doing things incorrectly. First, a bit of basics:




  • adb is a bridge which helps in communicating with a slave (an Android device) from a client on host machine (typically a PC) through a server running on host machine.

  • A slave has a daemon named adbd which has to running in order for slave to communicate back.


When you attempt to run an adb command which requires an Android device for desired output, the adb server expects a slave connected to host using USB. Since no Android device is connected to your Android device through USB, you get error: device not found. If you connect an another Android device through OTG and have USB debugging activated there, you would/might be able to run adb successfully. At least I was able to.


A simplest workaround is to have adbd listening on TCP as well. In that case, you can connect to localhost and get the job done. There is no way a process with ordinary privilege in Android can restart adbd to listen on TCP. The official way on stock Android is to do adb tcpip PORT from host machine. In this case, you would need a PC with setup there. Connect the device to PC and execute from PC, the command:


adb tcpip PORT   # PORT is port number in your device to listen for and establish connection on TCP. Default port is 5555. Choose a PORT greater than 1024 

You can now unplug the device. As long as you don't reboot the device or disable USB debugging from Developer options, adbd would continue to run and listen for connection on your supplied PORT.


Don't worry about security. While adbd would receive connection from local Wi-Fi network, one cannot successfully connect to your device unless you authorize debugging by accepting their fingerprint.



Anyhow, in your terminal emulator, execute


adb connect localhost:PORT

You would be required to accept a fingerprint. If you followed instructions carefully, you would get the output:


connected to localhost:PORT

Now you can run your backup command. But honestly, it is not clear to me what you're trying to achieve. If backup alone is the goal, then Helium app might be able to help, although it also requires root access or PC with setup.


In case you're not aware of this, not all the apps can be backed up using adb.


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...