tomix89
tomix89

Reputation: 19

How to (auto) turn off a (SONY) android TV on no signal

I have a really annoying SONY android TV. The device just does not want to turn off when there is no signal via HDMI. I have also a mini PC connected to it and in majority of time the TV just works as a monitor. Now when the PC goes to sleep, the TV keeps the screen on and has a message there - no signal...
(Yes I know I should buy another TV, but this is not an option).

I decided to take some actions, and at least turn the TV off when the PC goes to sleep.
My first idea was to go with the DDC/CI way, but the TV does not have a setting to enable it, and does not react to the actual commands.
So since it is an android TV i got an idea to connect via adb via WiFi and send a shutdown command: adb shell input keyevent KEYCODE_POWER
Tried this manually from command line - so far so good, the TV accepts the command and turns off.

But here comes the harder part, on Windows 10 I'm (only) able to detect user presence via: User32.dll and GetLastInputInfo()
But this is not enough as I may watch a video (vlc, chrome, etc) In this case there is no user input, but the system is kept awake by the video player. So I need to know when something is blocking the Windows from sleep.

I searched and I found:

  1. some are wanting to know if the TV as hardware is turned on or off - I don't need this!
  2. see if the monitor is on / off in a manner that windows is sending a signal or not via Win32_DesktopMonitor and ManagementObject but accessing properties field throws an exception. I would need to access result[n].Properties["Availability"].
  3. some suggest to hook onto system power change notifications.
    This is not a good idea in my case as when my app receives the 'going to sleep' event from the OS there is only a little time to act, and I'm afraid the adb won't be so quick to actually send the commands (connect, doublecheck, turn off).
  4. some suggest to hook onto monitor power change notifications.
    Which might be a good idea actually, but also some other people warn about these notification being not 100% reliable. (I haven't tried this for now, but might fall back to this if other trials fail.)

But later I came across this:
https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/display--sleep--and-hibernate-idle-timers
which suggest that 'Turn off the display timer' does exist. Although I'm unable to find any example how to access it. I also got across that if you actually want windows to prevent from sleep you need to use SetThreadExecutionState. OK but how to read this ? I actually tried to list all the processes and search for such flags, but either I'm unable to find them or they are not there at all.
Then I came across a command line command: powercfg /requests which provides me with the info what i need, but this would mean string parsing and more importantly to run the app under admin rights which I would like to avoid.

So my questions:

  1. does anybody know if and how to read the 'Turn off the display timer' (preferably in c#) ? (which I assume is being reset by the SetThreadExecutionState)
  2. does anybody know how to get the same info that powercfg /requests provides in more code friendly way preferably without the need of admin privileges ?
  3. just by chance does anybody know if this could be solved purely on the TV - by an android app maybe ?

Thank You very much if you managed to read through, and even more if you are able to provide some clues.

Update:
I found out that the 2) is probably impossible: Possible to find all Windows processes preventing automatic sleep, w/o admin rights?

Upvotes: 0

Views: 1306

Answers (1)

tomix89
tomix89

Reputation: 19

I realized that the mentioned TV (kd-49xf7596) has also a possibility to be controlled via network (so called IP control).
https://pro-bravia.sony.net/develop/integrate/rest-api/spec/index.html
Reading the doc. I would say this API provides me with all the info which I need:

  1. read/write the power settings getPowerStatus (v1.0) and setPowerStatus (v1.0)
  2. read if the TV is set to external HDMI - getPlayingContentInfo (v1.0)
  3. read if the input has an active signal - getCurrentExternalInputsStatus (v1.1)

UPDATE: if anybody is interested it is now a working prototype:
https://github.com/tomix89/sony-stopper

Upvotes: 1

Related Questions