9

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.

  • Is your Android device always connected to the Wi-Fi? – frogatto Dec 13 '15 at 12:03
  • I'm getting the feeling that you are mixing "device is active or sleeping" with "being on". Please make you question more precise. – Marian Paździoch Dec 14 '15 at 8:06
  • @HiI'mFrogatto The device is not using WiFi, it's connected via Ethernet, so it's always connected to the network. – Mika Fischer Dec 14 '15 at 8:11
  • @MarianPaździoch Android TV has a mode called "sleep mode", which is a low-power mode, similar to when the screen of a phone or tablet is off. I want to distinguish between this sleep mode, and the active mode, where the device is displaying stuff on the TV. Does that clarify the problem? – Mika Fischer Dec 14 '15 at 8:14
  • 3
    @MikaFischer "... without writing my own app, ..." Stack Overflow is for those who code :) – frogatto Dec 14 '15 at 8:23
1
+50

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.


| improve this answer | |
  • Something tells me this won't be a smooth ride on Android TV. Let me know if my answer fails to work at all. I'll take it down. I do not intend to keep an unhelpful answer lurking around. – Firelord Jan 6 '16 at 4:29
  • 1
    Wow, thanks for the detailed response! Using Tasker like this worked like a charm! The only caveat is that you need to connect a mouse to set it up. I didn't write to a file. Instead I wrote a simple HTTP server in Python and send GET requests to it from the shield. – Mika Fischer Jan 6 '16 at 11:10
  • Yeah, yours is a nice technique as well. It's like telling the server that you're alive. And thanks for the bounty! Would you mind telling me whether my method 1 (that system service method) failed or did you not try it yet? – Firelord Jan 6 '16 at 18:22
  • 1
    I didn't try it because I'm not rooted and as far as I can see, it's necessary to connect a computer to the device enable the adb network mode (after reboots for instance), which is too inconvenient for me... – Mika Fischer Jan 6 '16 at 18:26
0

This is not actually a solution but in device settings there's probably an option which disables Wi-Fi when device goes to idle mode. Even if there's no such option I'm sure there must be an application for this. So if Wi-Fi is disabled in IDLE state then "ping" approach will work but you have to deal with unworking background updates and so on when device goes to IDLE state.

I don't think that there's a default application for this solution but maybe further point will help you. You can create your own Android application which listens to PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED Intent and upon receiving receiving some kind of request from your server it can send back if device is awake or not. However notice that when your device receives server request it will probably come awake.

| improve this answer | |
  • Good idea, however this is a set-top-box and connected via Ethernet. And as I wrote, I know that I can write an app. The questions is if there's a way around doing that :) – Mika Fischer Dec 11 '15 at 13:43
  • AFAIK there's no big difference in types of connection on Android from dev's POV. So there's probably a 3d party app which can disable any type of connection including wired when device enters idle state. – Lingviston Dec 11 '15 at 14:18
0

You can setup a shell script to know whether the device is in idle mode..if device is idle..then you can
Send signal to the device .. For example u can use adb shell command like below: adb shell dumpsys deviceidle

| improve this answer | |
  • Interesting, I'll have to see whether I cen get adb working over the network or whether an SSH server can be used instead. – Mika Fischer Dec 14 '15 at 10:47
  • However, dumpsys deviceidle doesn not really work on my phone at least (gives Can't find service: deviceidle). – Mika Fischer Dec 14 '15 at 10:49

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.