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.

| improve this answer | |
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
}'
| improve this answer | |
-1

has answered in Is there a way to check if Android device screen is locked via adb? - Stack Overflow and copy to here:

  • My Phone: XiaoMi 9
    • Android: 10

use adb to check status of screen locked

method 1: (universal) use mDreamingLockscreen

  • Command: adb shell dumpsys window | grep mDreamingLockscreen
  • Output:
    • mShowingDream=false mDreamingLockscreen=true mDreamingSleepToken=null -> Screen Locked
      • no matter Screen is ON or OFF
    • mShowingDream=false mDreamingLockscreen=false mDreamingSleepToken=null -> Scrren Unlocked

method 2: use nfc (if android has NFC module)

  • Command: adb shell dumpsys nfc | grep 'mScreenState='
  • Output:
    • mScreenState=OFF_LOCKED -> Screen OFF and Locked
    • mScreenState=ON_LOCKED -> Screen ON and Locked
    • mScreenState=ON_UNLOCKED -> Screen ON and Unlocked
| improve this answer | |
  • I would say that it does matter whether the screen is on or off. If the screen is off, then I would first need to turn it on before I can input pin (by simulating menu press/swipe) and then PIN in. How would i input PIN if the screen is off? – ledwinder96 Feb 3 at 13:23

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.