10

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.

Dan Hulme
  • 35,000
  • 17
  • 90
  • 155
Mika Fischer
  • 336
  • 3
  • 9
  • 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 08: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 08: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 08:14
  • @MikaFischer So, you can deploy a light-weight Android app in your TV acting like a server which receives sleep signals from the Android phones. Therefore, you'd need install a client in Android phones that send such a signal to the server. – frogatto Dec 14 '15 at 08:15
  • @MikaFischer That's a Pushing technique, however your solution is a pulling technique. – frogatto Dec 14 '15 at 08:17
  • A possible duplicate of http://stackoverflow.com/questions/23084389/how-to-detect-device-status-offline-online-in-android – naXa stands with Ukraine Dec 14 '15 at 08:18
  • @HiI'mFrogatto There are no phones involved, just the Android TV box. And the question is how to do this without writing my own app, but with an existing one, like Tasker. I'm also open for a push-solution. – Mika Fischer Dec 14 '15 at 08:21
  • 3
    @MikaFischer "... without writing my own app, ..." Stack Overflow is for those who code :) – frogatto Dec 14 '15 at 08:23
  • @naXa That question is similar, but concerned with network connectivity. This question is concerned with sleep mode vs. device is active. However I think a similar technique can be applied to my problem, so if this can really not be done without writing my own app, the info will be useful. – Mika Fischer Dec 14 '15 at 08:24
  • @MikaFischer It's not obvious. I've asked a question on Meta. Let's see what we can do. – naXa stands with Ukraine Dec 14 '15 at 08:47

3 Answers3

1

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.


Firelord
  • 25,084
  • 20
  • 124
  • 286
  • 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 06 '16 at 04: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 06 '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 06 '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 06 '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.

  • 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. –  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

  • 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