Search Unity

Android TV detection

Discussion in 'Android' started by Radiangames2, Feb 5, 2015.

  1. Radiangames2

    Radiangames2

    Joined:
    Aug 22, 2012
    Posts:
    45
    Is there any way to detect when the game is being played on an Android TV? Google has requested changes to my game (not linking to games not compatible with Android TV, for example), and I can't figure out any way to tell if the game is running on Android TV or not.

    Any help/plug-in suggestion would be greatly appreciated.

    Thanks!
    Luke
     
  2. njwm

    njwm

    Joined:
    Apr 30, 2013
    Posts:
    26
    I have the same problem, anyone have a solution?
     
  3. BlastoIO

    BlastoIO

    Joined:
    Mar 3, 2015
    Posts:
    5
    List out the points that they outlined you on.

    When we originally put up our APK (Unity 4.6), we got hit with:

    Please refer to our Home Screen Banner and UI Patterns documentation:
    http://developer.android.com/training/tv/start/start.html#banner
    http://developer.android.com/design/tv/patterns.html#banner

    Your game must have the "isGame" flag in the AndroidManifest.xml set to
    true. Android TV uses the android:isGame flag to differentiate games with
    apps.

    Please refer to our Manifest documentation for details:
    http://developer.android.com/training/tv/games/index.html#manifest

    Your app or game that does require a game controller, is inaccessible with
    a standard Android game controller.

    Please refer to our Game Controller Support documentation for details:
    http://developer.android.com/training/tv/games/index.html#gamepad



    Banner issue was fixed by adding and resizing our banner to a higher DPI since Android TV is not DPI specific. We took the (Unity 5) suggested size and increased by 33%.

    Our fixes were to the rest were to the following in AndroidManifest:

    - Add after screen list:
    <uses-feature android:name="android.hardware.gamepad" android:required="false"/>

    - Add into <application> tag:
    android:isGame="true"

    - (Unity 4.6) Add into <application tag> to add a banner, make sure you throw a banner.png into res/drawables as well:
    android:banner="@drawable/banner"

    - Add after category for android.intent.category.LAUNCHER:
    <category android:name="android.intent.category.LEANBACK_LAUNCHER" />


    We're about to push our first Unity 5 build within the next few hours, so we'll see how this goes with the new changes that Unity has done.
     
  4. florianpenzkofer

    florianpenzkofer

    Unity Technologies

    Joined:
    Sep 2, 2014
    Posts:
    479
    I have not tried it, but check the sample code in the "Check for a TV Device" section on https://developer.android.com/training/tv/start/hardware.html.
     
  5. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    How can I use native code to Unity? Is there some plugins. It would be great if I can test if it is Android TV. Now I have to publish different APK.
     
  6. trawitz

    trawitz

    Joined:
    Oct 6, 2014
    Posts:
    1
    I was able to detect it by doing the following. This basically calls all that java code from that link in C#. It's a bit cryptic, but I needed a quick solution.

    Code (CSharp):
    1. // Essentially this code is doing some java stuff to detect if the UI is in TV mode or not
    2.         // What it does is it gets the Android Activity that is running Unity,
    3.         // gets the value of android.content.Context.UI_MODE_SERVICE so we can call getSystemService on
    4.         // the activity, passing in the UI_MODE_SERVICE, which gets us the UiModeManager.  Next we
    5.         // call getCurrentModeType on our UiModeManager instance which gives us some integer that represents the UI mode.
    6.         // We then have to get the value of android.content.res.Configuration.UI_MODE_TYPE_TELEVISION as an integer and then
    7.         // finally we can compare that with our mode type and if they match, it is android tv.
    8.         AndroidJavaClass unityPlayerJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    9.         AndroidJavaObject androidActivity = unityPlayerJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
    10.         AndroidJavaClass contextJavaClass = new AndroidJavaClass("android.content.Context");
    11.         AndroidJavaObject modeServiceConst = contextJavaClass.GetStatic<AndroidJavaObject>("UI_MODE_SERVICE");
    12.         AndroidJavaObject uiModeManager = androidActivity.Call<AndroidJavaObject>("getSystemService", modeServiceConst);
    13.         int currentModeType = uiModeManager.Call<int>("getCurrentModeType");
    14.         AndroidJavaClass configurationAndroidClass = new AndroidJavaClass("android.content.res.Configuration");
    15.         int modeTypeTelevisionConst = configurationAndroidClass.GetStatic<int>("UI_MODE_TYPE_TELEVISION");
    16.  
    17.         if(modeTypeTelevisionConst == currentModeType)
    18.         {
    19.             Debug.Log ("This is an AndroidTV device");
    20.         }
    21.         else
    22.         {
    23.             Debug.Log ("This is NOT an AndroidTV device");
    24.         }
     
    JJC1138 likes this.
  7. wartron

    wartron

    Joined:
    Jan 23, 2015
    Posts:
    8
    @trawitz thanks!

    Just a note, your gonna wanna wrap this with build defs like so



    Code (CSharp):
    1.     bool isAndroidTv()
    2.     {
    3.         #if ! UNITY_ANDROID || UNITY_EDITOR
    4.             return false;
    5.         #else
    6.  
    7.         // Essentially this code is doing some java stuff to detect if the UI is in TV mode or not
    8.         // What it does is it gets the Android Activity that is running Unity,
    9.         // gets the value of android.content.Context.UI_MODE_SERVICE so we can call getSystemService on
    10.         // the activity, passing in the UI_MODE_SERVICE, which gets us the UiModeManager.  Next we
    11.         // call getCurrentModeType on our UiModeManager instance which gives us some integer that represents the UI mode.
    12.         // We then have to get the value of android.content.res.Configuration.UI_MODE_TYPE_TELEVISION as an integer and then
    13.         // finally we can compare that with our mode type and if they match, it is android tv.
    14.         AndroidJavaClass unityPlayerJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    15.         AndroidJavaObject androidActivity = unityPlayerJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
    16.         AndroidJavaClass contextJavaClass = new AndroidJavaClass("android.content.Context");
    17.         AndroidJavaObject modeServiceConst = contextJavaClass.GetStatic<AndroidJavaObject>("UI_MODE_SERVICE");
    18.         AndroidJavaObject uiModeManager = androidActivity.Call<AndroidJavaObject>("getSystemService", modeServiceConst);
    19.         int currentModeType = uiModeManager.Call<int>("getCurrentModeType");
    20.         AndroidJavaClass configurationAndroidClass = new AndroidJavaClass("android.content.res.Configuration");
    21.         int modeTypeTelevisionConst = configurationAndroidClass.GetStatic<int>("UI_MODE_TYPE_TELEVISION");
    22.  
    23.         return (modeTypeTelevisionConst == currentModeType);
    24. #endif
    25.     }
    26.  
     
    Last edited: Jan 24, 2016
    IndieFist likes this.
  8. IndieFist

    IndieFist

    Joined:
    Jul 18, 2013
    Posts:
    520
    On my android TV isn´t working, i get false and its a android tv.
     
  9. KEric

    KEric

    Joined:
    Jan 11, 2013
    Posts:
    62
    The same as you I've been trying to find appropriate solution for differentiating between smartphone and Android-Tv with no luck. So I've wrote a Unity's plugin that can be incorporated into Unity's project available under https://github.com/rixment/awu-plugin Hope it helps
     
    CrandellWS likes this.
  10. -chris

    -chris

    Joined:
    Mar 1, 2012
    Posts:
    99
    Made a single-file Android TV-checker without statics. Seems to work okay.

    GitHub gist: Platform_AndroidTV.cs
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    How does this work exactly? AFAIK the TV binary still requires the LEANBACK property but the phone/tablets won't work with LEANBACK... so you still need separate binaries, correct??
     
  12. -chris

    -chris

    Joined:
    Mar 1, 2012
    Posts:
    99
    You'd make a GameObject, add the component, reference it, and call:

    Code (CSharp):
    1. Platform_AndroidTV platform;
    2.  
    3. ...
    4.  
    5. if (platform.IsAndroidTV) { /* do something */ }
    Builds, runs okay, and detects okay far as I can tell.

    Tested on a Samsung Galaxy S9+, and the Android Studio device emulator for an x86 Android TV.

    If you've got a proper TV device and want to test other native calls, please post here or on the Gist with code examples, and I'll update the gist as necessary.

    (I am not super familiar with leanback – trying to avoid submitting multiple binaries to Google Play if I can help it)
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    My understanding was that LEANBACK was needed to make it appear in the TV catalog.

    Simultaneously I believe LEANBACK makes it not appear in the touch catalog.

    That's great. Can you make a single binary that appears in both catalogs? If so, that's news to me.
     
  14. -chris

    -chris

    Joined:
    Mar 1, 2012
    Posts:
    99
    I'm implementing this Android TV detection because at the moment, Google has approved my binary for touchscreen/phone/tablet versions... But has rejected the binary for the Android TV catalog; I need to improve my in-game menu navigations with the basic Android TV remote to be eligible, so am working on a fix.

    Google has not reported any other rejection issues with the single binary I upload (using the AAB delivery method), so I'm not anticipating further issues at this point.

    This is all fairly new to me. I get an email from the Google Play Console saying "your update is live", but then a while later a different email stating the specific Android TV rejection issues (D-Pad menu navigation for me), but no other issues reported for now.

    ---

    For what it's worth, I tick these boxes in the Project Settings > Player > Android tab:



    but I don't specifically meddle with any LEANBACK strings in any config files anywhere.
     
    Last edited: Jul 17, 2022