Check if device has been connected by wireless ADB? - android

If my device has opened itself up to Wireless ADB, how can I determine if another device has connected to my device? Currently there's no warning (at least on my device) alerting me when somebody successfully connects to my phone.
I know I have to go out of my way to allow wireless connections and it's pretty stupid to leave that on when you're not planning to use it, I'm just curious if it's programmatically possible to know these things.
Is there a dump I can run or a path I can cat to see if something's connected? Can my code be notified when something tries to connect? If I can tell if something is connected, how much information can I get on what connected, if any? Is root required for any of this?

I was in the same boat, here how I got what I need:
netstat -lptu | grep -E '5555.*ESTABLISHED' | cut -s -d ':' -f2 | awk '{print $2}'
This shows the IP of the one connected to the android device! You can change the port with no problem
Then you can use the information in a "IF" statement for example to grep the IP on a white or black list!
You can use Scripting Layering For Android and make a Toast Notification whenever someone tries to connect or make a Push Notification with the ability to display a dialog asking for some action like disabling the adb wireless.
Here is a way to Enable/Disable ADB Wireless:
Enable:
setprop service.adb.tcp.port 5555 ; stop adbd ; start adbd
Disable:
setprop service.adb.tcp.port -1 ; stop adbd ; start adbd

Related

Logging when Android device is disconnected

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.

how can I understand if the phones are really rebooted adb

Im trying to reboot the devices and really check that they are really rebooted. I tried to use uptime like this: adb s device id uptime
but didnt work .
For the purpose of checking device's uptime the following command provides data in a format which is much easier to work with:
adb -s <device_id> shell cut -d' ' -f1 /proc/uptime
But checking the device's uptime immediately after reboot is pointless - the device would not be available. So it would make more sense to check the local USB device enumeration instead.

How to check whether a service in running on particular port using adb

I have a third party application in my android phone.
The application offers a service at 127.0.0.1:30002.
I don't know the name of service.
Is there a way to find out whether that tcp service is up or not using adb or some other means.
Also what is the use of adb reverse.
Can i run a server in host pc and forward that server on android using adb reverse.
I am completely new to android, so please be brief.
Thanks in advance.
To find which app using specific port:
1. $ cat /proc/net/tcp - This will give you a list of open ports and the UId (unique application ID) of the port's owner.
2. & cat /data/system/packages.list | grep '<The UID you just found>' - This will give you the app name connected to this UID.
3. ps |grep '<the app name>' Will tell you if the app service is running.
What is adb reverse?
adb reverse tells your phone use a port of your local machine (e.g laptop).
Assume you use the command:
adb reverse tcp:80 tcp:3000
Now when your phone tries to access http://localhost:3000/ your request will be routed to localhost:80 of your laptop.

How to toggle turn on/off mobile data with adb command

I looking for toggle my android phone mobile data with adb command. that is usually i do it with touch my mobile data icon in settings.
i found many reference to do it, for example this question , but nothing work for me, i try use:
$ adb shell svc data enable
$ adb shell svc data disable
when i look to my android phone nothing happen.
my android is KitKat 4 and i sure have rooted
Thanks.

Detect if ADB server is running on Android?

I am trying to detect if the adb server is running on the Android device for part of an anti-cheating implementation for my free game.
Specifically, I want to stop use of adb shell input tap x,y, since the game is a competitive multiplayer puzzle game.
Things I have tried:
Using battery info I can detect if USB is plugged in. Well, that is also a legit use.
Using Settings.Secure or Settings.Global, I can query ADB_ENABLED, but that always returns 1 if adb is enabled. It DOES NOT take into account adb connected or not!
Querying all system services, but I cannot see anything that looks like an adb service.
At this point, I am out of ideas. Hopefully someone else knows how to do this?
You can check for running adbd process or query init.svc.adbd system property:
$ ps adbd
USER PID PPID VSIZE RSS WCHAN PC NAME
root 14947 1 4596 208 ffffffff 00019358 S /sbin/adbd
$ getprop init.svc.adbd
running
In Android the adb driver is implemented as a function of universal usb driver. You can check the (comma separated) list of currently enabled usb functions to see if it includes "adb":
$ cat /sys/devices/virtual/android_usb/android0/functions
mtp,adb
But you would not be able stop cheating while your app is running completely on the user controlled device.

Categories

Resources