Logcat works fine when device is connected to Android Studio. But how to log data when device is disconnected from Android Studio, but I still need to capture some information for later analysis?
This does NOT require rooting your device, NOT changing the code of your app, NOT subscribing to a third party service, NOT having your app in Google Play. If you can or are willing to do some of these things, there are other solutions.
However this solution DOES require to have the computer nearby, turned on and connected to the same network where the device is connected (not necessarily on the internet).
Start off by plugging the device into your computer via USB, then run
adb devices to make sure the device is there. If it finds more than one device (e.g. emulator and actual phone), disconnect/close everything but the one you want to debug to keep things simpler and not mistaken one device for another. Then, run adb tcpip 5555 to change the android to tcp mode, which will allow debugging over the network (instead of USB).
At that point you need to find the IP address of your phone, and you can do that in various ways. The easiest is running adb shell ip -f inet addr show wlan0 which will respond with something like:
11: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 192.168.x.x/yy brd 192.168.0.255 scope global wlan0
valid_lft forever preferred_lft forever
Disconnect the device from the computer's USB in the usual way. Connect it again "over the air" by typing adb connect 192.168.x.x, replacing the x with the IP address found above (right after inet). If the device and your computer are on the same network (e.g. same wifi, or mix of wifi and ethernet from the same router) it should connect. Otherwise, you might need to debug the networking, a whole different problem (but usually not necessary).
Sometimes you can connect to the device but it is shown as "offline". Most of the time just waiting one minute will automatically fix the issue. If in a minute or two the issue does not fix itself, you have to debug it, e.g. as described in this question
At this point the device should appear in the drop list of available devices in Android Studio and you should be able to debug it as if it were attached over the USB. However in some cases I have found that the logcat in Android Studio is not updated in this setup. If that happens, run adb logcat and you will see the logcat on the computer. That will have too much information, because includes everything is running on your device, not just your app -- so you will need to filter it, for example with adb shell logcat --pid NNNN where NNNN is the PID of your app. To find such a PID, run adb shell "ps -A | head -1; ps -A | grep -i com.example" (use the java package of your main Activity instead of com.example) which will respond with something like
USER PID PPID VSZ RSS WCHAN ADDR S NAME
u0_a105 NNNN 1511 4970004 182688 0 0 S com.example
When done, run adb usb to restore the phone to regular USB debugging.
Related
I'm using scrcpy to mirror the phone screen to computer, which uses adb.
I've followed the steps and it works:
# connect via USB
adb devices
adb tcpip 5555
# now unplug USB, and the following will work over WiFi
adb connect 192.168.1.14:5555
scrcpy
But when you reboot the phone or computer, doing adb connect 192.168.1.14:5555 and scrcpy doesn't work anymore (NB: the phone IP hasn't changed, it's fixed).
Reading the answers from Run/install/debug Android applications over Wi-Fi?, I see 3 options:
plug the USB cable each time before doing a wireless connection, but this is annoying, and somehow reduces the interest of wireless...
Use "ADB over network" (main answer's screenshot), but as noted by many people in comments: "i do not have the "ADB over network" option in debugging option"; so this option doesn't work for me
other techniques that need root (not possible for me)
Question: how to pair the phone and computer with a USB cable only once, and then be able to use adb between them without having to use a USB connection first, after each reboot?
Note: I don't use Android Studio, but only scrcpy.
It's not possible without root the phone. If your phone has rooted. You can use this app. https://play.google.com/store/apps/details?id=com.ttxapps.wifiadb&hl=en. With this app you can easily debugs android apps with only wifi. Don't want USB cable even only first time.
Based on all the information I have the next conclusion. Just for reference, how a low-level setup with changing add routes works inside, described here. It comes up, there is a pretty straight thing inside changing navigation with adb tcpip <port>.
The first thing we need to do, it's setup system property with tcp port with name setprop service.adb.tcp.port (and values with a port number) By default, there is not such property.
And then just restart adb with few commands. stop adbd and start adbd on a device in order to debug bridge listen new port. What is the trick here, that you don't have access to this service and stop/start it without root. But it could be easily done, via Developer setting with stop/start debugging.
This few simple action hidden inside adb tcpip <port>. And the problem, that you cannot change system properties (point 1). That is why all application requires root access, it's just to change the single option. That is why, we need to connect with USB to debug bridge, which can change properties with his sepolicy.
So answering your question. I don't understand either, what is the problem or enabling this small setup in Developer options. But right now, it's possible to do so on some Roms, or devices with root access.
I am debugging an Android app that usually crashes when I plug into an Arduino it is supposed to be communicating with. Consequently, I need to get a wireless connection to the device's logcat, which is what brought me to ADB over TPC as seen on the Android developers webpage.
The recommended process is:
C:\Users\User> cd AppData\Local\Android\sdk\platform-tools
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb kill-server
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb start-server
* daemon not running; starting now at tcp:5037
* daemon started successfully
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb tcpip 5555
restarting in TCP mode port: 5555
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb connect 10.20.5.160:5555
However, the typical response is:
unable to connect to 10.20.5.160:5555: cannot connect to 10.20.5.160:5555: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)
I could not consistently get a connection between a PC and mobile device. Over the course of three weeks using several networks and devices, I could only connect two times out of an agonizing many attempts.
I have dug deep into the posted questions on stackoverflow and tried literally every recommended alteration to getting an adb connection, from using an Android Studio plugin to forwarding ports to trying several other ports to toggling my device's internet. I still could not get a connection more than a fraction of a percent of the time.
When I look at the network state of my device after adb tpip <port>, I find no evidence of the device trying to look for a connection:
C:\Users\User\AppData\Local\Android\sdk\platform-tools> adb shell
j3ltevzw:/ $ netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ::ffff:10.20.5.160:4214 ::ffff:173.194.196.188: ESTABLISHED
udp 4288 0 adb shell ip -f inet addr show wlan0:68 10.20.5.1:67 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
... ... ... ... ... ...
... ... ... ... ... ...
Also, when I try to ping my device, I usually get:
C:\Users\Benjamin\AppData\Local\Android\sdk\platform-tools> ping 10.20.5.160
Pinging 10.20.5.160 with 32 bytes of data:
Reply from 10.20.5.232: Destination host unreachable.
Reply from 10.20.5.232: Destination host unreachable.
Reply from 10.20.5.232: Destination host unreachable.
Reply from 10.20.5.232: Destination host unreachable.
Ping statistics for 10.20.5.160:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)
What I am looking for is a process or file that can be run and consistently open a tcp adb connection, or at least explain what about my setup is invalid. Ideas?
Update 1:
I have started a bounty on this question. I feel like the problem with the ADB software used to make connection for debugging is two fold:
adb tcpip <port> does not always invoke the mobile device to listen on the specified port
adb connect <ip>:<port>, I am assuming, is not given permission by the network to connect; I am not a networking expert, but the fact that pinging a device returns "destination host unreachable" seems a little off
I will award the first person to find a successful solution. Seeing that having an ADB USB connection gives a user access to both the mobile device's and host's terminal, there really is no excuse for a connection not to be able to be made between the two.
Update 2:
I have recently had more success in connecting to the ADB as well as receiving successful pings at my school network. Note at this network connects are insecure and the host and mobile devices where on separate subnetworks. Any idea why these conditions would work over my secure network at home that uses only one subnet?
Try starting from scratch and setup ADB-over-tcp following steps below:
Turn off USB debugging on your device and turn it on again (just to reset).
Kill the ADB server on your PC using adb kill-server
Connect your device to the PC using the USB cable and type adb devices. This will automatically restart the ABD server again.
Enter adb tcpip 5555 on your PC terminal. This will switch the adbd daemon on your device to tcp-ip mode.
Disconnect your device from USB. Now connect your device to the same wireless network as your PC, either the same wi-fi or use your phone as a hotspot.
Determine your phone's ip. You can do this in the wi-fi settings on your phone. If you are using your phone as hotspot, then generally your ip should be 192.168.43.1 (not required but most of the time it's the same).
Connect to ADB via tcp using adb connect <IP>
This should hopefully connect your device to your pc via tcp-ip.
I found a solution that works for me, but I feel it shouldn't necessary and it may not applicable for everybody else with these issues, so its still not quite what I was aiming for.
Anyways, after seeing riyaz-ali suggest what an IP address I should be for using a mobile hotstop through a phone, I realized any network complications would be solved if I was the network, so I connected my mobile device to the mobile hotspot on my laptop.
Instant ping responses and immediate ADB connection with the popularly suggested commands.
Just found myself in the same situation. Had no problem yesterday but today couldn't connect.
Solution was simple, I realized that today I'm connected to VPN which obviously makes me belonging to a different network.
So, disconnecting and connection to the same Wi-Fi solved the trouble.
I'm using Android tablet communicating with a USB device through USB port, the tablet only has one usb port, so I use adb wifi as my debug tool. However, when I connect adb through WIFI, the connection won't keep long(only a few minutes us usually), how to keep the adb wifi connection online all the time?
Another question, is it possible to use USB HUB to connect the tablet with both USB flash-drive and my computer, and keep adb running?
Many thanks!!!
After lots of unsuccessful attempts,I have noticed that turning Allow ADB debugging in charge only mode specification on , solved the problem . It makes the device online and accessable over wifi to ADB.
You can find it under Developer options section.
Here is what I found out from researching everywhere and developing on my own for more than a week.
Q1. adb service often crashes if it is not doing any executions for a long long time.
A1. calling adb commands time to time keeps adb server online.
Q2. wifi connected device sometimes disconnect.
A2. Before you check your wifi on the connected device, check the wifi that your ADB server is running on. Ping to google and if it is well connected, ping to the device.
Wireless can disconnect on its own and even wired connection is sometimes disconnected. so calling "adb connect ..." when the device is not already connected is necessary. You could do this easily by saving "adb devices > device.txt" and query out whether your expected wifi is on the list. If not, connect again..(do this in another thread).
Q3. wifi connected device is sometimes offline.
A3. This is because by TCP/IP connection your ADB server is now on Time_Wait stance. The last connection was interrupted that the next connection is now offline because the last connection is not completely shut down. You can either manually disconnect/connect your device's wifi or restart TCPIP connection by ex) "adb tcpip 5555"
If you want to do this automatically, you are going to have to disconnect/connect your device's wifi programmatically.
Leave any other questions and I can answer on the comments.
When connection is lost and adb devices shows device is offline, like this:
prompt> adb devices
List of devices attached
192.168.1.1:5555 offline
then adb disconnect IP followed by adb connect IP will often work:
prompt> adb disconnect 192.168.1.1
disconnected 192.168.1.1
prompt> adb devices
List of devices attached
prompt> adb connect 192.168.1.1
connected to 192.168.1.1
prompt> adb devices
List of devices attached
192.168.1.1:5555 device
Use adb over wi-fi without any third-party apps. Follow the steps.
Connect device via USB
Open your terminal and check your device is listed with adb devices command
Type adb tcpip 5555 and enter (or use another port instead of 5555)
Plug out your USB cable. You don't need it anymore.
Take your Android phone and go Settings/About/Status
Check the IP address part. Let's say your IP address is 192.168.x.y
In your terminal, write the command adb connect 192.168.x.y
Now, you have connected your device via wi-fi.
You can see your device in adb devices
Enjoy it! :)
This seems like an issue with your wifi. As a precaution, just keep the terminal open and whenever you need to test just press the 'UP arrow' key to get to previously ran adb connect "<ip>" command.
I tried many ways but this one worked out for me:
force the display on and of in a loop would keep the device online
adb shell input keyevent 224
timeout 2
adb shell input keyevent 26
timeout 58
this turns the display on every minute for 2 seconds
So, I'm using Scrcpy and Adb to wirelessly connect to my Phone(for developing Flutter Apps) and since the screen wasn't staying awake even by Scrcpy --stay-awake command.
I had to change Lock Settings=>Lock Screen => Sleep, and settings to Never Sleep.
I have an android device which I want to connect to using ADB. For reasons that don't matter here I need it to have an ethernet connection, not wifi. The device itself refuses to have both ethernet and wifi connections at the same time.
I have been using a third party app called ADB WiFi to connect to it with ADB when it is in WiFi mode, and I have found that I can connect to it in wifi, then switch the device into ethernet, and then reconnect to the new IP, and it works.
I would dearly like to be able to connect to my device (which is often remote and hard to reach) without first switching it into wifi and back, to which end I have written my own little app which uses SU and does:
setprop service.adb.tcp.port 5555
stop adbd
start adbd
It says that it is working when I run it and produces no error messages, but I can't connect afterwards. It doesn't work in wifi mode either. After setting up a connection once using the 3rd party app, I can then use stop adbd to prevent adb working, and I can use my application above to enable it again, without wifi or 3rd party intervention, which suggests that it is doing what it thinks it is doing, but missing a one off step.
Does anyone know if there is another thing that must be set / enabled / poked in order for adbd to work? Does anyone know what I'm missing?
Thanks
Have you tried this:
adb tcpip 5555
adb connect 192.168.1.1:5555
replacing the 192.168.1.1 with your phones IP Address?
I was able to get both ADB (through USB) and Ethernet, but it was device specific. I used the Asus Transformer Pad TF701T with the keyboard dock. The dock allowed me to connect the ADB cable and provided a full size USB port. I connected a compatible USB to Ethernet adapter to get Ethernet.
Can I dump logs from an android phone over wifi (using tcp/ip) using adb? adb connect can be used, but it needs some server to be running on the phone (at 5555 port). How do I start this server?
Or the only way to get logs from a phone is by connecting it as a USB device?
Install adbWireless on your phone. Run the application and click on the big button (you cannot miss it!). It will activate ADB over Wifi and display the URL to use to connect to it with the adb command.
On your computer, run the adb command with the connect parameter. The usage for adb says:
connect <host>[:<port>] - connect to a device via TCP/IP
Port 5555 is used by default if no port number is specified.
Obviously the computer and the smartphone must be on the same Wifi network.
zero4
All you are trying to do is drop adb logcat command on the device & send output stream to a remote location. I would suggest, read this post about how to run shell commands in your app.
The summary is
Run "adb logcat"
Collect Output Stream of the command in a file on device
& Finally post that file to your local server OR Manually pull that file from device
The post contains link to everything you are looking for.
Android is very paranoid when it comes to network access. Without root access, you can't really run any servers, just clients. In short, without root, look at the answer from 100rabh.
If you do have root, you could either open up your network stack for incoming connections on port 5555, or you could hack adb to do the inverse connection (that is, connect to your client). The latter is way more secure and shouldn't really be to hard to do. (I haven't looked at the code for a while, though.) The communication bits for all parts of adb is handled in one and the same library, for all three parts of adb (server, daemon and client).
By the way, what you refer to as a server on the phone is really the adb daemon.