Find out over the network if Android device is active I have an nVidia Shield TV device, which is running Android TV.

In order to integrate this into my home theater setup, I need to be able to determine if the device is active or sleeping.

For my other devices I just use ping to determine this. However, since Android devices are basically always on, this doesn't work in this case.

One way, of course, would be to write a simple app, which opens a TCP port when the device is active and closes it when it's sleeping.

My question is if there's a simpler way, maybe an existing app or some other way to do this without developing my own app to do this.
It appears that nVidia Shield Android TV comes with Android 5.1.1 and a Marshmallow update is available since December only. I'm assuming that Android 5.1.1 is the version installed in your device. 

Query system services

Enable adb in wireless mode on device. Follow the official guide or this answer of Izzy for instructions, if needed.

You can find the status of Android - sleep or awake - from the dump of various system services. 


Service: Power

adb shell dumpsys power


Search the strings mWakefulness and/or Display Power: state. Both of them would give you the status you need.

Example:

adb shell 'dumpsys power | grep -e "mWakefulness=" -e "Display Power"'


gives me

mWakefulness=Asleep
Display Power: state=OFF

Service: Window

adb shell dumpsys window


Search the strings mAwake=true and/or mScreenOnEarly=true and/or mScreenOnFully=true

Example:

adb shell 'dumpsys window | grep -e "mAwake=" -e "mScreenOnEarly" -e "mScreenOnFully"'


gives me

mAwake=true
mScreenOnEarly=true mScreenOnFully=true

Service: Display

adb shell dumpsys display


Search the strings mState=OFF and/or mScreenState=OFF.

Example:

adb shell 'dumpsys display | grep -e "mState=" -e "mScreenState"'


gives me

mState=OFF
mScreenState=OFF





Use Automation

If none of the system services mentioned in previous method worked for you, setup an automation app, such as Tasker or MacroDroid or Automate.

We would now use automation to write the status of Android into a file, such as Sleeping for sleep mode and Awake when it is awake.

Tasker,


Profile: Event &rarr; Display &rarr; Display Off
Task: (Actions) : File &rarr; Write File &rarr; select a text file to write into, type Sleeping and uncheck Add Newline
Profile: Event &rarr; Display &rarr; Display On
Task: (Actions) : File &rarr; Write File &rarr; select a text file to write into, type Awake and uncheck Add Newline


MacroDroid

Android sadly doesn't come with echo or printf utility, If the device is rooted then you can install busybox and be at peace. If not, download busybox binary, set executable permissions and push it into /data/local/tmp using adb push. Another way for a non-rooted device is to keep three files, one for state, one with the text Sleeping and and the last one  with the text Awake. Whenever the screen turns off/on, copy the second/third file into first one. 

MacriDroid also requires a plugin, such as Secure Settings to execute commands.


Trigger: Screen On/Off &rarr; Screen Off
Action: Secure Settings &rarr; Actions &rarr; Run Command:


Command: echo Sleeping &gt; FILE_PATH
Command Name: Screen off

Trigger: Screen On/Off &rarr; Screen On
Action: Secure Settings &rarr; Actions &rarr; Run Command:


Command: echo Awake &gt; FILE_PATH
Command Name: Screen off



For a non-rooted Android, echo should be replaced with ./data/local/tmp/busybox echo. Otherwise, do a copy operation using the command cp SOURCE DESTINATION.

Automate

Your flow should more or less look like this image 

(Click image to enlarge)



Your need to create two flows, one for sleep and other for awake. The second block in the flow is named Broadcast receive and can be found under Apps. The third block is named File write text and can be found under File &amp; Storage.

You can now check the content of that file using adb in wireless mode or using SSH (requires an SSH server on Android; run the server on all interfaces and do not bind it to a single interface.)



There is another possibility, such as making your automation app upload the file to a local or remote server so that you can query that server instead of Android to know status.