Android TV - Switch to HDMI input programatically

152
September 11, 2021, at 11:50 AM

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, but nothing happens when I run 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,
    )
)

Should this work, but I am doing something wrong? Is there another way to do this?

Running it via adb from my computer works:

adb connect MY_TV
adb shell input keyevent 246

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") and instrumentation.sendKeyDownUpSync(...). It did not work, but as I understand it, those need special privileges anyway.
Rent Charter Buses Company
READ ALSO
Best practice to get summary/filters from a large SQL table

Best practice to get summary/filters from a large SQL table

In our system, a user can be of 2 types

87
Right to Left Icicle Tree

Right to Left Icicle Tree

How can I mirror the d3 tree? I have use these code https://observablehqcom/@d3/zoomable-icicle but I can't put the father at right

63
How to reset row indexes on filtered values in React-Table?

How to reset row indexes on filtered values in React-Table?

I'm trying to implement a feature in my table where I need the current row index after a filter is applied in React-Table

126