14

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?

rohitb
  • 225
  • 1
  • 3
  • 9
  • Share your broadcast receiver code. Is is intended to work for Motorola devices only...?? – Arpit Garg Apr 03 '12 at 09:32
  • 2
    [This](https://stackoverflow.com/a/21383495/1921481) answer worked for me, just had to replace the intent. Instead of "android.intent.action.HDMI_PLUGGED" it has to be "android.intent.action.HDMI_HW_PLUGGED". (This was supposed to be more of a comment to that answer but my reputation doesn't allow me to comment). – Shah Dec 21 '17 at 00:24

5 Answers5

9

I came up with this using the other answers and some from elsewhere:

/**
 * Checks device switch files to see if an HDMI device/MHL device is plugged in, returning true if so.
 */
private boolean isHdmiSwitchSet() {

    // The file '/sys/devices/virtual/switch/hdmi/state' holds an int -- if it's 1 then an HDMI device is connected.
    // An alternative file to check is '/sys/class/switch/hdmi/state' which exists instead on certain devices.
    File switchFile = new File("/sys/devices/virtual/switch/hdmi/state");
    if (!switchFile.exists()) {
        switchFile = new File("/sys/class/switch/hdmi/state");
    }
    try {
        Scanner switchFileScanner = new Scanner(switchFile);
        int switchValue = switchFileScanner.nextInt();
        switchFileScanner.close();
        return switchValue > 0;
    } catch (Exception e) {
        return false;
    }
}

If you're checking often, you'd want to store the result and update it with @hamen's listener.

Tom
  • 6,946
  • 2
  • 47
  • 63
  • I'm trying to implement a similar functionality in my android app where I'd like a video to pause when the display connected via HDMI is switched off. Would you know if the file holds `int` `1` or `0` incase the TV is connected via HDMI but is in power off state? Thank you, any help is appreciated. – Raunaqss Mar 22 '18 at 07:56
  • No sorry. This answer was assembled from other sources, none of which had any of the hard answers. – Tom Mar 22 '18 at 20:55
6

I came out with this eventually. It's working on S3 and S4. It should work with any 4+ Android version.

public class HdmiListener extends BroadcastReceiver {

    private static String HDMIINTENT = "android.intent.action.HDMI_PLUGGED";

    @Override
    public void onReceive(Context ctxt, Intent receivedIt) {
        String action = receivedIt.getAction();

        if (action.equals(HDMIINTENT)) {
            boolean state = receivedIt.getBooleanExtra("state", false);

            if (state) {
                Log.d("HDMIListner", "BroadcastReceiver.onReceive() : Connected HDMI-TV");
                Toast.makeText(ctxt, "HDMI >>", Toast.LENGTH_LONG).show();    
            } else {
                Log.d("HDMIListner", "HDMI >>: Disconnected HDMI-TV");
                Toast.makeText(ctxt, "HDMI DisConnected>>", Toast.LENGTH_LONG).show();
            }
        }
    }
}

AndroidManifest.xml needs this into application tag:

    <receiver android:name="__com.example.android__.HdmiListener" >
        <intent-filter>
            <action android:name="android.intent.action.HDMI_PLUGGED" />
        </intent-filter>
    </receiver>
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
Ivan Morgillo
  • 2,837
  • 4
  • 30
  • 46
  • 3
    This helped me detecting if the hdmi is connected or disconnected, but not knowing is the hdmi is connected before running the app. – jch Jul 25 '14 at 21:40
  • @IvanMorgillo @DmitryGinzburg I'm trying to implement a similar functionality in my android app where I'd like a video to pause when the display connected via HDMI is switched off. Would you know if this function returns `true` or `false` incase the TV is connected via HDMI but is in power off state? Thank you, any help is appreciated. – Raunaqss Mar 22 '18 at 07:59
  • @Raunaqss have you found any decision? – bene25 Sep 13 '18 at 10:06
  • @AleksandrA there's multiple possible ways and what way you pick depends on the hardware, what version of android you're using, etc. Nonetheless, google makes it difficult to access its private HDMI related apis. I can elaborate in detail on each of the approaches. What hardware and android version are you using? – Raunaqss Sep 27 '18 at 10:13
  • @Raunaqss thanks for response. API 17 and 21, Minix X7 and Minix U1 accordingly – bene25 Sep 27 '18 at 10:16
5

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();
}
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
Aj_31
  • 187
  • 2
  • 6
1

Same problem here. Some google-ing told me there's not much hope with other manufacturers besides Motorola, however from http://developer.sonymobile.com/wp/2012/05/29/how-to-use-the-hidden-hdmi-api-tutorial/:

An application can detect if the device is connect via an HDMI connector by listening to the broadcast intent: "com.sonyericsson.intent.action.HDMI_EVENT"

anroots
  • 1,959
  • 1
  • 14
  • 21
1

check the file /sys/class/switch/hdmi/state, if it's 1 then it's connected to an HDMI.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Yi Wang
  • 552
  • 2
  • 7
  • 20