Switch on a lamp from Android TV

Switch on a lamp from Android TV

The Nexus Player is a small round box that you can connect to a TV to play movies, display pictures and play video games. In short, it's a multimedia box like many others (Apple TV, WD TV, ...). But, unlike its competitors, the Nexus Player runs Android. So it is possible to plug and use our devices on this Player.




The Nexus Player does not use exactly the same Android as a phone or a tablet, instead it uses Android TV which is an Android version designed to use a TV remote instead of a touchscreen for navigation. Both versions share the same kernel and the same SDK, so it is possible to use any library that is available for Android.


The Nexus Player, the remote, and the Yocto-MaxiPowerRelay
The Nexus Player, the remote, and the Yocto-MaxiPowerRelay



To ensure that our Android library works with Android TV, we wrote a small application to switch ON and OFF an audio amplifier and a lamp. Specifically, the application simply switches the outputs of a Yocto-MaxiPowerRelay plugged on the micro USB port of the Nexus Player. The relays of the Yocto-MaxiPowerRelay simply disconnect the power cables of the lamp and of the amplifier.

The Nexus Player has an HDMI port, a power socket, and an USB OTG port
The Nexus Player has an HDMI port, a power socket, and an USB OTG port



For this application, we started from the example of the Android SDK. This example is very comprehensive and well documented. We simplified the interface and added few lines of code to call a service instead of playing the Google video.



class ItemViewClickedListener implements OnItemViewClickedListener
{
  @Override
  public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                            RowPresenter.ViewHolder rowViewHolder, Row row)
  {
    ...
    YoctoIntentService.setRelayState(getActivity(), relay.getHwid(), relay.ison());
    ....
  }
}
 



The method setRelayState calls an IntentService which sets up our library and inverts the output of the Yocto-MaxiPowerRelay. As we use an IntentService, all this is executed in a background thread.


public class YoctoIntentService extends IntentService
{
  private static final String ACTION_SET_RELAY_STATE =
        "com.yoctopuce.testnexusplayer.action.SET_RELAY_STATE";
  private static final String EXTRA_HWID = "HWID";
  private static final String EXTRA_ISON = "ISON";

  ...

  public static void setRelayState(Context context, String hwid, boolean isOn)
  {
      Intent intent = new Intent(context, YoctoIntentService.class);
      intent.setAction(ACTION_SET_RELAY_STATE);
      intent.putExtra(EXTRA_HWID, hwid);
      intent.putExtra(EXTRA_ISON, isOn);
      context.startService(intent);
  }


  ....

  @Override
  protected void onHandleIntent(Intent intent)
  {
    if (intent != null) {
      final String action = intent.getAction();
      try {
        YAPI.EnableUSBHost(getApplicationContext());
        YAPI.RegisterHub("usb");
        if (ACTION_SET_RELAY_STATE.equals(action)) {
          String hwid = intent.getStringExtra(EXTRA_HWID);
          boolean on = intent.getBooleanExtra(EXTRA_ISON, false);
          YRelay relay = YRelay.FindRelay(hwid);
          if (relay.isOnline()) {
            relay.set_output(on ? YRelay.OUTPUT_ON : YRelay.OUTPUT_OFF);
          }
        }
      } catch (YAPI_Exception e) {
        e.printStackTrace();
      }
      YAPI.FreeAPI();
    }
  }
}
 



To compile the project, we must add a dependency to our Android library in the build.gradle file.


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:leanback-v17:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.yoctopuce.android:YoctoLib:1.10.20255'
}
 



You can find the full project on GitHub. We also published this application on Google Play if you only want to test it.


  
A small demo



Our devices work perfectly on Android TV. The Nexus Player is an interesting solution if you need to show some physical measurements on a big screen. For instance, showing to a classroom the temperature of a liquid with a Yocto-PT100 during an experiment.

Add a comment No comment yet
Back to blog












Yoctopuce, get your stuff connected.