Find out over the network if Android device is active


Question

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.


Answer

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 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 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 → Display → Display Off

  • Task: (Actions) : File → Write File → select a text file to write into, type Sleeping and uncheck Add Newline

  • Profile: Event → Display → Display On

  • Task: (Actions) : File → Write File → 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 → Screen Off

  • Action: Secure Settings → Actions → Run Command:




    • Command: echo Sleeping > FILE_PATH

    • Command Name: Screen off


  • Trigger: Screen On/Off → Screen On

  • Action: Secure Settings → Actions → Run Command:




    • Command: echo Awake > 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)



IMG:



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 & 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.





Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes