How to check the HDMI device connection status in Android?

前端 未结 5 599
不知归路
不知归路 2020-12-03 05:55

I need to detect whether an HDMI device is connected or not to my Android device. For this I\'m using a BroadcastReceiver and it is able to detect also. But with BroadcastRe

5条回答
  •  生来不讨喜
    2020-12-03 06:17

    You can get the data from /sys/class/display/display0.hdmi/connect. If the content of the file is 0, HDMI is not connected, otherwise if it's 1, HDMI is connected.

    try {
        File file = new File("/sys/class/display/display0.hdmi/connect");
        InputStream in = new FileInputStream(file);
        byte[] re = new byte[32768];
        int read = 0;
        while ((read = in.read(re, 0, 32768)) != -1) {
            String string = new String(re, 0, read);
            Log.v("String_whilecondition", "HDMI state = " + string);
            result = string;
        }
        in.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    

提交回复
热议问题