HDMI-tutorial

UPDATED: How to use the hidden HDMI API [tutorial]

***Update: This Sony specific API will be deprecated. Please use the hidden Android intent “android.intent.action.HDMI_PLUGGED” instead. This is supported on smartphones running Ice Cream Sandwich and Jellybean. Stay tuned to Developer World for more information going forward.*** 

Are you working on an Android™ app that you’d like users to experience on an external device like a TV or monitor? If your Xperia™ smartphone (or other Android™ device) has an HDMI port, then your app should detect when it’s plugged into a powered high-definition screen to ensure the display is rendered properly on the larger screen. This quick tutorial includes some code that shows you how. Read more after the jump.

HDMI tutorial image 2

Phone content shown on a TV using the HDMI connection.

A number of Sony and Sony Ericsson phones (Xperia™ arc, Xperia™ arc S, Xperia™ neo, Xperia™ pro, Xperia ion™ and Xperia™ S) feature a High Definition Multimedia Interface (HDMI) port. When you connect an HDMI cable, the screen content shown on the device is automatically mirrored to the larger screen. In some cases, maybe the app developer intentionally wants to prevent the HDMI port becoming active, if the content is only meant for the handset screen. But if you want your app to receive and render content via the HDMI port, your app must be registered to receive HDMI port status changes.

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"

This intent carries an extra parameter that contains information regarding if HDMI is in use. This extra parameter is named:

"com.sonyericsson.intent.extra.HDMI_STATE"

The supported HDMI states are: “HDMI_OFF” and “HDMI_IN_USE.”

In this case, you will need to prepare a BroadcastReceiver listening to that event, as shown by the following sample code:

In AndroidManifest.xml:

<receiver android:name=".HdmiListener">
<intent-filter>
<action android:name="com.sonyericsson.intent.action.HDMI_EVENT" />
</intent-filter>
</receiver>

In the source code of the BroadcastReceiver:

public class HdmiListener extends BroadcastReceiver {
 static String HDMIAPPEXTRA = “abrisHdmiExtraParameter”;

 // Just a name for my launched activity
 private static String HDMIINTENT = “com.sonyericsson.intent.action.HDMI_EVENT”;
 private  static final String EXTRA_HDMI_STATE =
 ”com.sonyericsson.intent.extra.HDMI_STATE”;

@Override
 public void onReceive(Context ctxt, Intent receivedIt) {
 String action = receivedIt.getAction();
 if (action.equals(HDMIINTENT)){
 String extras = receivedIt.getStringExtra(EXTRA_HDMI_STATE);
 Intent it = new Intent(ctxt,HdmiFromIntent.class);
 it.putExtra(HDMIAPPEXTRA, extras);
 it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 ctxt.startActivity(it);

 //Do whatever you want to do – preferably
 //hand an intent to a service instead of
 //launching an activity – in this case a UI was
 //desired for demo purpose.
 }
 }

}

It’s as easy as that. So if you haven’t yet bothered ensuring that your Android™ app detects HDMI, just remember that several Xperia™ smartphones, and other Android phone manufacturers, have an HDMI port. Including HDMI connection is a small fix for your app that could go a long way, and might help your app stand out from similar apps missing the feature. Interested in trying out the HDMI API? Let us know how it goes and if you have any questions.

More information

Comments 8

Sort by: Newest | Oldest | Most popular

  1. By sam scapin

    #1

    I need help badly. My Sony Xperia s displays that hdmi is connected when there is nothing plugged in at all. Then consequently it displaying the error message all the time. Thus stopping me from using the phone. Very frustrating. I reset default factory settings and still it continues to happen. Please can someone resolve this issue before I pull all of my hair out!!!!

  2. By Enzo Sh

    #2

    Are the MHL adapters useful for the XPERIA smartphones, if they don’t have HDMI port?

  3. By ANKIT SHARMA

    #3

    REGARDING MY ABOVE WRITTEN COMMENT I WANT TO CLEAR THAT HDMI CABLE IS CONNECTED TO XPERIA S & LCD-LED TV PROPERLY AND I ALSO GET A NOTIFICATION REGARDING HDMI CONNECTION IN MY S i.e. HDMI IS CONNECTED, BUT THE PROBLEM IS NO OUTPUT ON TV

  4. By ANKIT SHARMA

    #4

    hi sir,
    I AM ANKIT KUMAR SHARMA FORM MORADABAD IN INDIA (ZIP/POSTAL CODE 244001) & MY CONTACT NO. IS +91 9045929964. I PURCHASED AN XPERIA S, IMEI NO. IS 351710058973958 IN MAY 2012. THE PROBLEM REGARDING HDMI MIRRORING WHEN I CONNECT MY S TO SONY KDL EX 520 SERIES HD LED TV TROUGH THE HDMI CABLE THAT I GOT IN THE BOX. & SELECT THE CONCERNING HDMI ON THE TV, BUT ALL IN VAIN BECAUSE THERE IS NO OUTPUT ON THE TV & NO CONTENT IS SHOWN ON THE TV WHILE THE OTHER ONE XPERIA NEO V WITH THE SAME HDMI CABLE IS WORKING AUTOMATIC & PROPERLY. I WROTE AN EMAIL TO SONY REGARDING THIS & I GOT AN AUTO REPLY REGARDING MY TICKET ID [X-Ticket:93062174,2], BUT NONE HAS CONTACTED ME SO FAR. PLEASE HELP ME & SHOW THE WAY TO RESOLVE THE PROBLEM. THANKING YOU

  5. Pingback #5

    […] has published tutorial on using HDMI API, the tutorial is targeted for devices that has HDMI built into it like […]

  6. By Phil Gunner

    #6

    Is there any way to simulate the HDMI insertion. Specifically I am trying to develop a TV Launcher plugin and I want to be able to launch TV launcher without having to physically attach a HDMI cable. Doing this is really slowing down my development cycle.

    When I broadcast that intent directly (com.sonyericsson.intent.action.HDMI_EVENT) I do get a popup saying that TV launcher has started (from LiveWare I think) but nothing else happens.

    Thanks

    — Phil

  7. By Peter Pasztor

    #7

    Is there a way to display different image on the device screen and on the HDMI output?

    For example the HDMI would display the slides for a presentation or a lecture, and the device would display the notes for the actual slide, and the controls for the slideshow. Is it possible to use true full HD resolution output, not only upscaled device resolution?

    • By Johan Abramsson

      #8

      Hi,
      The phone only dispays the image of the device – that is the capabilites of this feature.
      Kind regards
      /Johan

1-8 of 8 comments.