gpt4 book ai didi

android - Android 上的 HDMI CEC

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:18 42 4
gpt4 key购买 nike

我在访问 this 上的 HDMI CEC 时遇到问题安卓加密狗。

我想打开电视并更改电视的输入源,但我做不到。

Android API 方法

我正在运行一个系统应用,我已经安顿好了

<uses-permission android:name="android.permission.HDMI_CEC" /> 

在 AndroidManifest.xml 上。

我正在通过反射访问 HDMI 服务,因为我无法直接访问它,即使是系统应用程序也是如此。

public class HdmiHelper {

public HdmiHelper(Context context) {
init(context);
}

public void init(Context context) {

try {

//Interface Callback Proxy
Class<?> hotplugEventListenerClass = Class.forName("android.hardware.hdmi.HdmiControlManager$HotplugEventListener");
Class<?> vendorCommandListenerClass = Class.forName("android.hardware.hdmi.HdmiControlManager$VendorCommandListener");
Class<?> oneTouchPlayCallbackClass = Class.forName("android.hardware.hdmi.HdmiPlaybackClient$OneTouchPlayCallback");
Class<?> displayStatusCallbackClass = Class.forName("android.hardware.hdmi.HdmiPlaybackClient$DisplayStatusCallback");

Object interfaceOneTouchPlaybackCallback = Proxy.newProxyInstance(oneTouchPlayCallbackClass.getClassLoader(),
new Class<?>[]{ oneTouchPlayCallbackClass } , new callbackProxyListener() );

Object interfaceHotplugEventCallback = Proxy.newProxyInstance(hotplugEventListenerClass.getClassLoader(),
new Class<?>[]{ hotplugEventListenerClass } , new callbackProxyListener() );

Object interfaceDisplayStatusCallbackClass = Proxy.newProxyInstance(displayStatusCallbackClass.getClassLoader(),
new Class<?>[]{ displayStatusCallbackClass } , new callbackProxyListener() );


Method m = context.getClass().getMethod("getSystemService", String.class);
Object obj_HdmiControlManager = m.invoke(context, (Object) "hdmi_control");

Log.d("HdmiHelper", "obj: " + obj_HdmiControlManager + " | " + obj_HdmiControlManager.getClass());

for( Method method : obj_HdmiControlManager.getClass().getMethods()) {
Log.d("HdmiHelper", " method: " + method.getName() );
}


Method method_addHotplugEventListener = obj_HdmiControlManager.getClass().getMethod("addHotplugEventListener", hotplugEventListenerClass);
method_addHotplugEventListener.invoke(obj_HdmiControlManager, interfaceHotplugEventCallback);


Method m2 = obj_HdmiControlManager.getClass().getMethod("getPlaybackClient");
Object obj_HdmiPlaybackClient = m2.invoke( obj_HdmiControlManager );
Log.d("HdmiHelper", "obj_HdmiPlaybackClient: " + obj_HdmiPlaybackClient + " | " + obj_HdmiPlaybackClient.getClass());

Method method_oneTouchPlay = obj_HdmiPlaybackClient.getClass().getMethod("oneTouchPlay", oneTouchPlayCallbackClass);
method_oneTouchPlay.invoke( obj_HdmiPlaybackClient, interfaceOneTouchPlaybackCallback);


Method method_queryDisplayStatus = obj_HdmiPlaybackClient.getClass().getMethod("queryDisplayStatus", displayStatusCallbackClass);

method_queryDisplayStatus.invoke( obj_HdmiPlaybackClient, interfaceDisplayStatusCallbackClass);

Method method_getActiveSource = obj_HdmiPlaybackClient.getClass().getMethod("getActiveSource");
Log.d("HdmiHelper", "getActiveSource: " + method_getActiveSource.invoke(obj_HdmiPlaybackClient));


}catch (Exception e) {
e.printStackTrace();
}

}

public class callbackProxyListener implements java.lang.reflect.InvocationHandler {

public callbackProxyListener() {

}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

try {
Log.d("HdmiHelper", "Start method " + method.getName() + " | " + proxy.getClass() + " | " + method.getDeclaringClass() );

if ( args != null ) {

// Prints the method being invoked
for (int i = 0; i != args.length; i++) {
Log.d("HdmiHelper", " - Arg(" + i + "): " + args[i].toString());
}

}

if (method.getName().equals("onReceived")) {

if (args.length == 1) {
onReceived(args[0]);
}else
if (args.length == 3) {
onReceived( (int) args[0], BytesUtil.toByteArray( args[1] ), (boolean) args[2] );
}


}else
if (method.getName().equals("onComplete")) {
onComplete( (int) args[0] );
}else
if (method.getName().equals("toString")) {
return this.toString();
}else {
return method.invoke(this, args);
}

}catch (Exception e) {
e.printStackTrace();
}

return null;
}

void onComplete(int result) {
Log.d("HdmiHelper", "onComplete: " + result);
}

void onReceived(Object event) {

Class eventClass = event.getClass();

Log.d("HdmiHelper", "onReceived(1): " + event.toString() + " | " + eventClass);

try {
Method method_getPort = eventClass.getMethod("getPort");
Method method_isConnected = eventClass.getMethod("isConnected");
Method method_describeContents = eventClass.getMethod("describeContents");

Log.d("HdmiHelper", " - " + method_getPort.invoke(event) + " | " + method_isConnected.invoke(event) + " | " + method_describeContents.invoke(event) );

}catch (Exception e) {

e.printStackTrace();

}


}


void onReceived(int srcAddress, byte[] params, boolean hasVendorId) {
Log.d("HdmiHelper", "onReceived(3): " + srcAddress + " | " + params + " | " + hasVendorId);
}


}

记录答案:

D/HdmiHelper: obj: android.hardware.hdmi.HdmiControlManager@7bca63c | class android.hardware.hdmi.HdmiControlManager

D/HdmiHelper: obj_HdmiPlaybackClient: android.hardware.hdmi.HdmiPlaybackClient@6345d1a | class android.hardware.hdmi.HdmiPlaybackClient

D/HdmiHelper: Start method onReceived | class $Proxy2 | interface android.hardware.hdmi.HdmiControlManager$HotplugEventListener
D/HdmiHelper: onReceived(1): android.hardware.hdmi.HdmiHotplugEvent@4c5c04b | class android.hardware.hdmi.HdmiHotplugEvent
D/HdmiHelper: - 1 | true | 0
  • 问题 1:

我收到了 true: 这意味着电视上的内容是真实的。如果电视关闭,我会收到假消息。这似乎有效。

不过,我希望每次更改电视状态时都能收到回调,但这并没有发生。有什么想法吗?

  • 问题 2:

继续 OneTouchPlayCallback 的日志:

D/HdmiHelper: Start method onComplete | class $Proxy1 | interface android.hardware.hdmi.HdmiPlaybackClient$OneTouchPlayCallback
D/HdmiHelper: onComplete: 2

看类HdmiPlaybackClient.java如果一切顺利,答案将是 0(@param result 操作的结果。{@link HdmiControlManager#RESULT_SUCCESS。您可以在 HdmiControlManager.java 类中找到此变量}。相反,我收到 2,我认为它是 RESULT_SOURCE_NOT_AVAILABLE。

知道为什么吗?

  • 问题 3

现在继续 DisplayStatusCallback 的日志:

D/HdmiHelper: Start method onComplete | class $Proxy3 | interface android.hardware.hdmi.HdmiPlaybackClient$DisplayStatusCallback
D/HdmiHelper: onComplete: 2

根据这个回调的定义:

/**
* Listener used by the client to get display device status.
*/
public interface DisplayStatusCallback {
/**
* Called when display device status is reported.
*
* @param status display device status. It should be one of the following values.
* <ul>
* <li>{@link HdmiControlManager#POWER_STATUS_ON}
* <li>{@link HdmiControlManager#POWER_STATUS_STANDBY}
* <li>{@link HdmiControlManager#POWER_STATUS_TRANSIENT_TO_ON}
* <li>{@link HdmiControlManager#POWER_STATUS_TRANSIENT_TO_STANDBY}
* <li>{@link HdmiControlManager#POWER_STATUS_UNKNOWN}
* </ul>
*/
public void onComplete(int status);
}

查看 HdmiControlManager,我收到 2,这意味着:

public static final int POWER_STATUS_TRANSIENT_TO_ON = 2;

这是一个奇怪的结果,因为事实并非如此。

  • 继续记录您的信息:

回答:

getActiveSource 为空

我还测试了这段调用 getTvClient() 方法的代码:

Method method_getTvClient = obj_HdmiControlManager.getClass().getMethod("getTvClient");
Object obj_HdmiTvClient = method_getTvClient.invoke( obj_HdmiControlManager );
Log.d("HdmiHelper", "obj_HdmiTvClient: " + obj_HdmiTvClient);

结果为空。

我还尝试了在 CEC-O-MATIC website 之后发送供应商命令的方法但我无法取得成功。如果您对此有任何指示,请给我一些指示,我会对其进行测试。

LibCEC 方法:

感谢这个 post,我能够将 libcec 交叉编译到 android .但是 libcec 总是回答我“命令‘PING’没有被 Controller 确认”。

我已将标志 -DHAVE_EXYNOS_API=1 和 -DHAVE_AOCEC_API=1 添加到 libcec。

系统信息

设备/dev/cec 已解决:

q8723bs:/ # ls -l /dev/cec                                                                                                                                              
crw-rw-rw- 1 root root 218, 0 2017-12-19 16:33 /dev/cec

我也可以在/sys/class/cec 上找到它:

q8723bs:/ # ls -laht /sys/class/cec/                                                                                                                                    
total 0
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 arc_port
lrwxrwxrwx 1 root root 0 2017-12-19 16:45 cec -> ../../devices/aocec/cec
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 cec_version
--w------- 1 root root 4.0K 2017-12-19 16:45 cmd
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 dbg_en
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 device_type
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 dump_reg
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 fun_cfg
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 menu_language
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 osd_name
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 physical_addr
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 pin_status
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 port_num
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 port_seq
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 port_status
-rw-rw-r-- 1 root root 4.0K 2017-12-19 16:45 vendor_id
-r--r--r-- 1 root root 4.0K 2017-12-19 16:45 wake_up

但是当我运行 cec-client 时,我收到了这个答案:

q8723bs:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:toolbox:s0
q8723bs:/ # cec-client -s /dev/cec
opening a connection to the CEC adapter...
DEBUG: [ 1] Broadcast (F): osd name set to 'Broadcast'
DEBUG: [ 2] connection opened, clearing any previous input and waiting for active transmissions to end before starting
DEBUG: [ 396] communication thread started
DEBUG: [ 1396] command 'PING' was not acked by the controller

请注意,我还有设备/dev/input/event2,它是只读的 cec_input:

q8723bs:/ # ls -l /dev/input/event2                                                                                                                                     
crw-rw---- 1 root input 13, 66 2017-12-19 16:33 /dev/input/event2
q8723bs:/ # ls /sys/devices/virtual/input/input2/
capabilities/ event2/ id/ modalias name phys power/ properties subsystem/ uevent uniq
q8723bs:/ # cat /sys/devices/virtual/input/input2/name
cec_input

我试图在/dev/input/event2 上运行它,但显然它没有工作,因为它无法打开连接:

q8723bs:/ # cec-client /dev/input/event2                                                                                                                                
No device type given. Using 'recording device'
CEC Parser created - libCEC version 4.0.2
opening a connection to the CEC adapter...
DEBUG: [ 1] Broadcast (F): osd name set to 'Broadcast'
ERROR: [ 3335] error opening serial port '/dev/input/event2': Couldn't lock the serial port
ERROR: [ 3335] could not open a connection (try 1)

总结:

在这两种情况下,我都无法获得打开或更改电视输入源的命令。任何方向都会非常有帮助。提前致谢。

注意:我能够在同一台电视上使用 libcec 和 raspberry pi 来完成它

最佳答案

所以在围绕这个问题做了很多工作之后我发现,为了在 android 中启用 CEC 控制,你需要在 shell 上运行这个命令:

settings put global hdmi_control_enabled 1 

#if you want, you can also enable this self-explanatory command
settings put global hdmi_control_auto_wakeup_enabled 1

#and this
settings put global hdmi_control_auto_device_off_enabled 1

在此之后,android 自动开始使用 cec,例如,在启动后,它会更改电视的输入源和/或打开电视。

现在,关于开发者控制:

我明白,当我调用方法时:

Method m2 = obj_HdmiControlManager.getClass().getMethod("getPlaybackClient");

我基本上可以访问加密狗本身(而不是电视)的 CEC。

尽管如此,当我运行该方法时,我仍然继续收到空值:

Method method_getTvClient = obj_HdmiControlManager.getClass().getMethod("getTvClient");

我的猜测是,这是一种正常行为,因为加密狗本身是一种播放类型,而不是电视类型。

所以我尝试使用函数 sendVendorCommand 但我无法弄清楚如何使用它。我找不到关于这个主题的任何文档/示例可以帮助我。

所以我决定直接进入操作系统级别并且它起作用了。特别是在这个加密狗中,您在/sys/class/cec 中有两个重要文件:

. cmd(发送cec命令)

例如(作为 root @android shell)

#turn on tv
echo 0x40 0x04 > /sys/class/cec/cmd

#change input source to HDMI 1
echo 0x4F 0x82 0x10 0x00 > /sys/class/cec/cmd

. dump_reg(读取cec的输出)

使用this检查其他命令代码的站点

就是这样!我更愿意通过 android 框架执行这些命令,但至少,这是可行的。

关于android - Android 上的 HDMI CEC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892639/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com