ToddBFisher
ToddBFisher

Reputation: 11590

How to Add Support For the Amazon Fire TV Remote Control Hardware in a Xamarin Android App?

I am writing a Xamarin app and have successfully deployed it to my Fire TV hardware; however, when I try to use the amazon remote my app is not responding, since the hardware does not support Tap Gestures, which makes sense.

What I have tried

After following the link on this page, it appears that there was a Fire TV Component, but when I click on the link, it turns out the component has been removed.

I found this link, but it suggests a round about work around to use the deprecated component, by ripping it out from some other random project, which is not ideal.

I saw mention that the preferred way is to use a Nuget package; however, after a bit of searching, I have yet to find any packages that seem to match what I am looking for.

I also saw mention that the latest Fire OS prefers use of the Google apis instead of custom Amazon ones, which makes me think there is a "Xamarin Google Game Input" type of nuget package out there somewhere that might work, but have had no luck in finding.

As a potential alternative to using a Nuget package that may or may not exist, I thought about manually handling keyboard input and mapping using this table that maps a button on the Amazon remote to a keyboard code, but I am unsure how to implement this for Android (Xamarin) as all the examples I have found are for Windows.

Further details

The current UI is pretty simple, tap the screen to pull up a menu, then select one of three buttons. I image translating that for the Amazon remote would be: Press the Select button (Middle circle), then us the directional pad to select one of the three buttons, then press the Select button.

I anticipate also needing to add support for the back button, which fingers crossed should come naturally once the other input needs are met.

So in summary, my question is:

For my Xamarin Android App, how can I add support for the stock Amazon remote that comes with the Fire TV hardware?

Upvotes: 0

Views: 974

Answers (1)

ToddBFisher
ToddBFisher

Reputation: 11590

To manually add support, here is a summary of the main points to get it working.

Disclaimer, this is untested code.

  1. For Android (Fire OS), in the MainActivity:
using Xamarin.Forms;

public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
    //Use Xamarin's event system to send the event.
    MessagingCenter.Send<MyCustomPage, int>(this, "KeyUp", (int)keyCode);
    return base.OnKeyDown(keyCode, e);
}
  1. Then in any of the pages *.xaml.cs
using Xamarin.Forms;

public void OnAppearing()
{
    MessagingCenter.Subscribe<MyCustomPage, int>(this, "KeyUp", async (sender, KeyCodeArg) =>
    {
        await TheUserGaveInput(KeyCodeArg); // do whatever you need 
    });
}

public void OnDisappearing()
{
    MessagingCenter.Unsubscribe<MyCustomPage, int>(this, "KeyUp");
}
  1. If there are many pages, you could consider adding this within an abstract/base class that each page extends, then each page's custom logic could be handled within an overriden ProcessUserInput() method.

Example handing for a specific page:

public override async Task TheUserGaveInput(int keyCode)
{
    if(keyCode == (int)MyKnownKeyCodes.PlayPause_FireOS ) {
        // Do something, like toggle play or pause for a video
    } 
}
  1. Identify desired KeyCodes triggered when a button is pressed on the physical remote control, that you could store as an enum, such as the one named MyKnownKeyCodes in the example above.

For specific Fire OS input:

// Button name = keycode
DpadUp_FireOS = 38,
DpadDown_FireOS = 40,
DpadLeft_FireOS = 37,
DpadRight_FireOS = 39,
PlayPause_FireOS = 179,
FastForward_FireOS = 228,
Rewind_FireOS = 227,
SelectDPadCenter_FireOS = 13,

To support all the other devices:

DpadUp = 19,
DpadDown = 20,
DpadLeft = 21,
DpadRight = 22,
Enter = 66,
Space = 62,
PlayPause = 85,
Rewind = 89,
SelectDpadCenter = 23,

You can also read more about Xamarin's MessageCenter for clarity.

Upvotes: 2

Related Questions