I am using a PlugPC (compute stick) with Android version 4.4.4, connecting it to HDMI port of TV to control what is being displayed on the TV. My app starts on boot, taking control of the screen. Now through my app I want to control TV volume and source switch (maybe). I have following questions in this regard:
Is there a way to send HDMI-CEC commands via android using Java to control TV volume, switch on/off, etc on those TVs compatible ?
Any Java library provided by android or otherwise available for the same ?
A simple Java example using CEC.
P.S – I understand the theory presented in the (https://source.android.com/devices/tv/HDMI-CEC.html), but clueless on how to implement with code. Please be specific with your answers to above questions.
Yes it is entirely possible to do, however it is incredibly fickle. HDMI CEC works like a bus. Usually, in both directions.
- Yes. Though I use JNI to access native code that does the actual work. You might be able to do it purely in Java.
- Probably not. The issue is that each vendor implements the code to send CEC signals. There is no common functionality at all, except for the CEC commands themselves. The device file accessed is likely unique in most devices, especially from vendor to vendor. Therefore, any libraries that are created most support a wide range of different vendors and their quirks, and it would not be future proofed against the next vendor changes.
- Unfortunately I cannot provide my working code. I provide a link below to an Amlogic source file on github that does a lot of the work. My code looks similar.
Some of my own points:
- Not all devices support all commands. Many less used commands will not be implemented, because it costs more.
- Interacting with CEC is often vendor dependant. They will likely have their own library or device file to work with.
- CEC will not always work in both directions, it depends whether the vendor implemented that functionality. It is not always implemented, because it costs more to do.
To do it properly you will likely need to implement your code using JNI to form the proper packets. You might find this useful, at least for Amlogic.
The device access point that I use in my devices is
/dev/amhdmitx0. You can send your well formed packets to that location.
Here is a useful website that can tell you how how to form a CEC command: http://www.cec-o-matic.com/.
Unfortunately, I cannot share much more than this of what I have written. For various legal reasons.
The best thing that you can do is to seek help from the vendor you are programming for. If you are coding for a specific platform. They might be willing to help you.