How to check Wi-Fi connection via adb?

Im automating wifi on / off via adb.

I would like to either disable / enable wifi based on test case

so far i have found good information here

However, I want to test the connection before running the 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

The above command disables wifi if enabled, enables wifi if disabled. First I want to check the connection and take action if necessary. I am wondering if there is a way to do this with the adb command. This can be achieved programmatically, but I want it to be done via adb to make it more reliable.

Also, the following command only works if the device is rooted

adb shell "svc wifi enable" 

Also, the following command starts the test on the screen, but does not provide information through adb

adb shell am start -n com.android.settings/.wifi.WifiStatusTest
+5
5

( Wi-Fi).

adb shell dumpsys connectivity
+11

Wi-Fi :

adb shell dumpsys wifi

, :

adb shell dumpsys wifi | grep "Wi-Fi"

:

adb shell dumpsys wifi | grep "mNetworkInfo"

  • , "CONNECTED/CONNECTED" . .
  • Wi-Fi , , "/" .
  • wifi , "/" .
+8

If you are trying to determine if WiFi is turned on, you can run adb shell settings get global wifi_onthat returns 1 if it is turned on and 0 if it is turned off.

If you have multiple devices connected, you can run adb -s [UDID_HERE] shell settings get global wifi_onto get the WiFi status of a separate phone. To find the UDID, you can run adb devices.

+2
source

This will only tell you if there is internet access, but you can do:

adb shell ping www.google.com
0
source

Not ideal, but you can reset the user interface layout and then search for text ON / OFF (bash):

adb pull $(adb shell uiautomator dump | grep -o '[^ ]*.xml') ui.xml
grep 'text="OFF"' ui.xml
if [ $? == 0 ]; then
  # Turn WIFI on
fi
-1
source

Source: https://habr.com/ru/post/1599457/


All Articles