How to test wifi connection via adb? - android

Im automating wifi off/on via adb.
I would either like to disable/enable wifi based on the test case
so far I have found good information here
However I want to test the connection before executing following command
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 23
Above command disables wifi if enabled, enables wifi if disabled. I want to first test the connection and take action if required. I am wondering if there is a way to do using adb command. This can be achieved programatically but I want it to be done through adb for making it more reliable.
Also following command works only if device is rooted
adb shell "svc wifi enable"
Also following command launches test on screen but provides no info via adb
adb shell am start -n com.android.settings/.wifi.WifiStatusTest

To know everything about Wifi status of the device:
adb shell dumpsys wifi
To just know whether it's enabled or not:
adb shell dumpsys wifi | grep "Wi-Fi is"
To know more detailed status:
adb shell dumpsys wifi | grep "mNetworkInfo"
If connected to a valid network, it shows
"CONNECTED/CONNECTED". It also shows the name of the connected network.
If wifi has been enabled but it is yet to connect to a network, it shows "DISCONNECTED/SCANNING".
If wifi is disabled, it shows "DISCONNECTED/DISCONNECTED".

Below command will give you the internet status and connectivity mode(cellular or Wi-Fi)
adb shell dumpsys connectivity

If you're trying to determine whether or not WiFi is turned on you can run adb shell settings get global wifi_on which returns 1 if it's on and 0 if it's off.
If you have multiple devices connected you can run adb -s [UDID_HERE] shell settings get global wifi_on to get the WiFi status of an individual phone. To find the UDID you can run adb devices.

This would only tell you if there is internet access but you can do:
adb shell ping www.google.com

adb shell dumpsys wifi | sed -n '1p'
is faster and safer than
adb shell dumpsys wifi | grep "Wi-Fi is"
because the output of dumpsys wifi contains the system logs that may have been contaminated by other applications

Atish's answer was working fine until Android 11/R. For this OS onwards, I recommend using Abhishek's suggestion:
adb shell dumpsys wifi | grep "curState=ConnectedState"

Not perfect, but you could dump UI layout and then search for ON/OFF text like this (bash):
adb pull $(adb shell uiautomator dump | grep -o '[^ ]*.xml') ui.xml
grep 'text="OFF"' ui.xml
if [ $? == 0 ]; then
# Turn WIFI on
fi

Related

I want to enable or say start WiFi tethering in android device by executing adb shell command

I want to enable hotspot or say start WiFi tethering in android device by executing adb shell command.
What is the adb command used for it?
To turn on bluetooth - adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE.
To open wifi- adb shell am start -n com.android.settings/.wifi.WifiSettings.
But I can not find for turning on hotspot.

How to verify WIFI wake lock using adb command

Can someone tell me is there any adb command to verify wifi wake lock in android phone. I am trying to check that wake lock is active or not after acquire from my application but not found this information in following file:
adb shell "cat /sys/kernel/debug/wakeup_sources"
Is it written somewhere else ?
To list the wifilocks active in android device, use the following command.
adb shell dumpsys wifi | grep "your wake_lock tag name"
Example:
adb shell dumpsys wifi | grep wake_lock_next
output:
WifiLock{wake_lock_next type=2 binder=android.os.BinderProxy#420bcfd0}

How to disconnect from a wifi network through adb?

I understand we could turn off Wifi through the "adb shell svc wifi disable" command but I don't want to completely turn off Wifi. I want to disconnect from a particular ssid through adb. Is it possible?
Edit: I got a notification saying this question is identified as a dupe of this question: How to turn off Wifi via ADB?. It actually is not. As mentioned clearly in the first paragraph, I don't want to turn off wifi but want to simply disconnect from a particular network. This is like long pressing a network and tapping "Forget network". Essentially I want to simulate a condition of the user moving out of a wifi network without having to turn off wifi.
If you have root access, you can make it by using wpa_cli.
First, you can use the wpa_cli`s list_networks to get the network id of the network that you want to disconnect
$ adb shell
# wpa_cli
> list_networks
**network id** / ssid / bssid / flags
0 001aLinksys14081-2G any [CURRENT]
and then you just need to run the wpa_cli remove_network {network id}, where the {network id} parameter is the one that you got on the list_networks method.
One option is to use cmd wifi. For disconnecting from a Wi-Fi network, you can "forget" it like so:
adb shell cmd wifi list-networks
adb shell cmd wifi forget-network <networkId from list-networks>
You can view the help documentation for cmd wifi with:
adb shell cmd wifi -h
This command did it for me
docker exec -it $container_id /bin/bash -c "cd /root//shared-tools/android-sdk/platform-tools; ./adb shell 'svc wifi disable'"
You cannot. But, this will not be enough to satisfying your question, there is tricky way to connect to "other wifi". (Yes, it is not disconnect)
Install an app, and send command to the app via adb.
See this: https://stackoverflow.com/a/37303412/850347
I already build an app which does so and it's available here: https://github.com/steinwurf/adb-join-wifi
Once the app is installed, a wifi access point can be joined using the following ADB command:
adb shell am start -n com.steinwurf.adbjoinwifi/com.steinwurf.adbjoinwifi.MainActivity -e ssid [SSID] -e password_type [PASSWORD_TYPE] -e password [WIFI PASSWORD]
See this: https://stackoverflow.com/a/37303412/850347

How to get the name (SSID) of the connected WiFi network using ADB?

I am trying to get the connected Wifi network name through the ADB commands. The only method I could figure out was search the dumpsys for the WiFi network info and get the SSID. But this is cumbersome. Is there a more direct way?
What about using dumping netstats info:
adb shell dumpsys netstats | grep -E 'iface=wlan.*networkId'

Is there an ADB command to enable/disable mobile data?(Rooted Device)

I'm looking for an adb shell command to enable/disable mobile data. The app is only being tested on rooted devices so I have adb root privileges. I looked around and found that wifi can be enabled/disabled with:
$ adb shell svc wifi enable
$ adb shell svc wifi disable
These work for me but I wasn't able to find anything for mobile data.
Any reference to a list of adb shell svc commands that can be performed would be appreciated as well. I know there's a list of key input commands from this question and obviously I'm aware of the developer page but there's no mention of svc.
Lastly, what is svc?
I found the answer to my question:
To enable mobile data:
$ adb shell svc data enable
To disable mobile data:
$ adb shell svc data disable

Categories

Resources