androidconnectionhdmi

How to check the HDMI device connection status in Android?


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 BroadcastReceiver I'm unable to handle the scenario when the HDMI device is connected even before my application was launced. In this case the BroadcastReceiver is unable to find if any HDMI device is connected or not. Is there any way I can get to know if any HDMI device is connected or not at any point?


Solution

  • 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();
    }