4

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.

4 Answers 4

3

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
1

As I've run into this recently and since using the Daydream lockscreen is not an requirement on Android, here's some newer variables that are (probably) universal.

They should be available starting at Android M (6.0 / API 23) since they come with Doze mode; I've confirmed them with Android 10 on a Samsung Galaxy Tab S4 and Android 12 on a Pixel 4 XL.

$ adb shell dumpsys deviceidle | grep '\bm[A-Z]'
mLightEnabled=true  mDeepEnabled=true
mForceIdle=false
mUseMotionSensor=true mMotionSensor={Sensor name="Significant Motion", vendor="Google", version=1, type=17, maxRange=1.0, resolution=1.0, power=0.25, minDelay=-1}
mScreenOn=false
mScreenLocked=true
mNetworkConnected=true
mCharging=false
mMotionActive=true
mNotMoving=false
mMotionListener.activatedTimeElapsed=36192
mLastMotionEventElapsed=0
mLocating=false mHasGps=false mHasNetwork=false mLocated=false
mState=INACTIVE mLightState=IDLE
mInactiveTimeout=+30m0s0ms
mNextAlarmTime=+22m36s283ms
mNextIdleDelay=+10m0s0ms (flex=+2m0s0ms)
mNextLightAlarmTime=-23s699ms
mCurLightIdleBudget=+1m0s0ms

Of course, the prize jewels here are mScreenOn and mScreenLocked, which are booleans doing exactly what you went them to.

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
}'
0

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
2
  • 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? Feb 3, 2020 at 13:23
  • Not quite, only universal if they're implementing the Daydream lock… Samsung, for example, isn't.
    – towo
    Mar 2, 2022 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .