Is there a way to switch to an HDMI input programmatically on Android TV? I.e. a Smart TV, not a separate box.
What I was trying is to send a key event like this:
# `someButton` is a button on the `activity_main.xml`
val inputConnection = BaseInputConnection(someButton.rootView, true);
inputConnection.sendKeyEvent(
KeyEvent(
KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_TV_INPUT_HDMI_4,
)
)
inputConnection.sendKeyEvent(
KeyEvent(
KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_TV_INPUT_HDMI_4,
)
)
Running this has no effect. Other keys like KEYCODE_BACK
work as expected, so I assume this functionality can only send key events to the app, not the host device itself.
For reference, running the following via adb from my computer works:
adb connect MY_TV
adb shell input keyevent 246
But of course I don't have permissions to run Runtime.getRuntime().exec(arrayOf("/bin/sh", "-c", "input keyevent 246"))
from within the app.
For reference, what I tried so far:
- Found this old post asking the same question, but no answer: Android TV switch HDMI input port programmatically
- Got the idea with
sendKeyEvent
from here, but as mentioned did not work: How can I send key events in android? - From the above post, I also unsuccessfully tried the
exec("input keycode 246")
andinstrumentation.sendKeyDownUpSync(...)
. It did not work, but as I understand it, those need special privileges anyway.