2

Background:

I'm using automated tests on many different variants of devices (Samsung tablets and smartphones such as Galaxy A5, S8, S7 etc.) where the devices have different versions of Android (6.0, 6.0.1, 7.0 and 7.1.1) - all in varying combinations.

Some of my tests require locking the screen (turning it off) and then turning it on and PIN-ing in. I have not been able to find a (universal) adb-command to get the phones' state. Ideally, I would like one command to tell me in which of the following states the phone is in:

  1. Screen turned off (Assumed locked as well)
  2. Screen turned on, locked
  3. Screen turned on, unlocked

My best solution so far is to use the following 2 (bash terminal) commands:

#1:
adb shell dumpsys display | grep "mScreenState"

#2:
adb shell service call trust 7

Command #1 tells me whether the screen is on, off or "dozed".

Command #2 tells me whether the phone is locked or unlocked.

Questions:

Is there a single adb command which can give me the information I need? Is this command compatible with the Android versions listed above?

On Android version 6.* command #2 always indicates that the phone is locked. What is the "Android 6.* compatible" alternative of command #2? ----> By pure chance I found that "service call trust 6" works as expected on Android 6. devices!*

Note:

The resource I've found that most closely resembles my issue is the following SO question/response. However, not all of the devices have/use NFC so the following command (although compatible with most devices/versions) is not acceptable in my tests:

adb shell dumpsys nfc | grep 'mScreenState='

Since timing is an issue, the proposed solution (best answer in the link) is not acceptable either.

1

I am a Windows fanboy, so the solution contains find instead of using grep, just replace find with grep.

adb shell dumpsys power | find "mWakefulness="
adb shell dumpsys power | find "mHoldingWakeLockSuspendBlocker=" 

Screen OFF and Locked = mWakefulness=Asleep & mHoldingWakeLockSuspendBlocker=false

Screen ON and Locked = mWakefulness=Awake & mHoldingWakeLockSuspendBlocker=false

Screen ON and Unlocked = mWakefulness=Awake & mHoldingWakeLockSuspendBlocker=true

P.S: I've tested it on Android 7.0, don't know if it'll work with other versions.

0

You could use awk to spit out the answer you want. Here is a quick and dirty awk script I whipped up just now:

adb shell dumpsys power | awk -F"[ =]" '/mWakefulness=/ { mw=$NF; }
/mHoldingWakeLockSuspendBlocker=/ {mh=$NF;}
END {
  print mw
  print mh
}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.