Tuesday, December 15, 2015

Apps on the big screen part III: Debugging on an Android TV

How cool would it be if you can debug your TV app on a real device! For tvOS all it takes is a provision profile and an USB-C to USB-A cable to connect the Apple TV 4 device with your MacBook. How to enable the debugging option on a real Android TV is less obvious. It starts with selecting the right Android TV device.

Android 5.0 set-top boxes are hardly available in my part of the world and getting one from Ali Express is not an option, because the delivery times are too long and I do not have that much patience. So I decided to get myself a TV running on Android.


1. Pick the right Android TV

First of all, if you have plans to develop apps for Android TV and want to be able to debug them then it is important to decide which brand and model you pick. Previously I have made the mistake to chose a Philips TV, the 32PFK6500 to be exactly. It was the only 32 inch model that was available, which made it the perfect development TV, or so I thought.

It turned out to be not such a good idea. There is no way to debug and test your apps on this or other recent Philips Android TVs, basically because of Philips security policy. Yes, you can unlock the developers menu, but you will never be allowed to connect the ADB. This makes debugging impossible.

Too bad! It is a nice TV and I really enjoyed the Ambilight experience. This TV is probably great if you just want to watch TV and use some apps, but it is not really suitable for app development, although there seems to be a workaround to get your app at least tested through the Google Play alpha- and beta distribution mechanism.

Tip of the day: If you want to return your smart TV to the shop then do not forget to restore it to the factory settings. You may have entered your google account and others details that you do want to erase first. Unlike me, do this before you put everything back into the box ;)

So I went back to the shop and exchanged the Philips TV for a Sony Bravia TV, the 43W80xC one. With this television I have made a new attempt; this time with success!



2. Unlock the developers menu

Just as is the case with Android running on a smart phone, you have to unlock the developers menu first. To do so click the home button on the remote control, go to Settings, choose About and scroll down until you see the Build option. Click seven times on it to unlock the developers menu.


Before you continue: Yes. Here comes the disclaimer. I guess there is a good reason for Philips to prevent app debugging and to disallow apps from unknown sources. Use this tutorial at your own risk. My TV did not explode or anything like that, but I am not too sure about yours ;)



3. Enable ADB debugging

The Developers menu will appear under System Preferences. Choose this option and next choose Debugging. Here you can change the setting for ADB debugging to On.



4. Debugging over LAN

I have not found a way for USB debugging yet. It just does not seem to work, although there are multiple USB (2.0) ports available on the device. So lets do this slightly different. It seems that out of the box the Sony Android TV allows debugging by LAN connection directly. Get its IP address and get connected!

Note : Your MacBook (or PC) and the TV have to be on the same network to make the magic happen.

Click on the Home button on the remote control and under the Settings section choose Network settings. Next click on Wi-Fi or Wired LAN, depending on how your TV is connected. Under IP address you will see the TV's IP address.

Open a new terminal window and connect ADB with the IP address (using the default port 5555) you have just found.

adb connect 192.168.2.18:5555

If everything went well the result will be something like: connected to 192.168.2.18:5555. At this point the Philips TV earlier returned the message Connection refused.


5. Create an Android TV project in Android Studio

In Android Studio create a new project and choose TV as a platform. This will create a ready made media centre app for you, which you can modify if you want to.

6. Launch your app

If the ADB connect command did succeed in the previous step and you run your app in Android Studio the TV will be shown under Connected devices. Select it and click on the OK button.



The first time the Allow USB debugging dialog will popup. Choose Always allow and click on the OK button to continue.

Another dialog that may appear is the one that says Allow Google to regularly check device activity for security problems.... So far I have chosen to decline this but I guess it will not do too much harm if you choose Accept




Conclusion

As you can see it is not that difficult to debug your Android TV app once you have the right equipment and know how to configure things. Having everything up and running the next challenge is to create a real cool TV app.


Further reading

3 comments:

  1. Same here, at first I brought a Philips TV because it was with Android and it was cheap investment in hardware. After 2 - 3 hours trying to enable ADB, I found your post and returned the TV to the store exchanging it for a Sony. Thank you.

    ReplyDelete
  2. Thank-you, my (touch-screen) device allowed me to connect using your debugging over LAN method. You saved my life!

    ReplyDelete
  3. Yesterday, I installed the new software update of my TV Philips 6401, and tadaaaammm!! The "Installation from unknown sources" option appeared!
    It seems that Philips changed his mind about the security policy.
    I tested it with ADB, and I managed to install and debug my app through the wifi. :)
    However, someone succeeded to debug through an USB cable?

    ReplyDelete