android tvでテレビを操作
TRANSCRIPT
Android TVでテレビを操作@oxsoft
自己紹介Naoto Nakazato
Yahoo Japan Corporation
Yahoo!知恵袋アカウント
Twitter: @oxsoft
Facebook: naoto.nakazato
GitHub: oxsoft
Qiita: oxsoft
今回の Qiita記事 : http://qiita.com/oxsoft/items/6540edf3823bada4c2e7
2
Android TV持ってますか?Nexus Player
Amazon Fire TV
SONY BRAVIA
Mi Box
etc...
3
テレビの操作はできないチャンネルや音量を変更できないでも電源が常に供給され常時起動している
4
テレビの操作はできないチャンネルや音量を変更できないでも電源が常に供給され常時起動している
_人人人人人人人人人人人人人人人人人_ > Android TV は自宅サーバ < ̄ Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y ̄
5
スマホで操作可能にしましたhttps://www.youtube.com/watch?v=2t9gTPkf2Tw
6
システム構成1.Arduinoを使い USBで受けたデータを赤外線として発信する回路を作成2.Arduinoを Nexus Playerの USB端子に接続し、シリアル通信するアプリを作成3.アプリ内で HTTPサーバを立て、ホームネットワークないから指令を受ける
HTTP USB(Serial通信 ) 配線 赤外線
Nexus Player Arduino 8
Arduino
Arduino-IRremoteというライブラリを使用https://github.com/z3t0/Arduino-IRremote
シリアル通信を受け付け、SHARPの AQUOSに応じたコードを LEDに送る
#include <IRremote.h>
IRsend irsend;
void setup() { Serial.begin(9600);}
void loop() { int input; if (Serial.available() >= 2) { input = Serial.read() << 8; input |= Serial.read(); irsend.sendSharpRaw(input, 15); }}
9
Androidから USBでシリアル通信UsbManagerから UsbDeviceを取得
UsbDevice usbDevice;UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);HashMap<String, UsbDevice> map = manager.getDeviceList();for (HashMap.Entry<String, UsbDevice> entry : map.entrySet()) { int vendorId = entry.getValue().getVendorId(); int productId = entry.getValue().getProductId(); if (vendorId == 0x2A03) { usbDevice = entry.getValue(); break; }} 10
Androidから USBでシリアル通信UsbInterface, UsbEndpointを取得して byte[]を送信する
byte[] buffer;UsbInterface usbInterface = usbDevice.getInterface(0);UsbEndpoint usbEndpoint = usbInterface.getEndpoint(1);UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);usbDeviceConnection.bulkTransfer(usbEndpoint, buffer, buffer.length, 0);
11
Jettyで HTTPサーバを立てる/commandが来たら Arduinoに信号を送るそれ以外は assetsにあるファイルを返すAndroid TVでは良いインタフェースかもpublic class JettyHandler extends AbstractHandler {
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) { if (target.equals("/")) target = "/index.html"; if (target.equals("/command")) { // 送信処理 } else { // assetsのファイルを返す } }} 12
まとめAndroidから USBを操作するのは意外と簡単Android TVは HTTPをインタフェースにすると便利HTTP以外にもホームサーバとして使えそう
Android TV向けの FTPサーバアプリを作ってみたhttp://qiita.com/oxsoft/items/bb0edc4507b33cca525a
FTP Server for Android TVhttps://play.google.com/store/apps/details?id=com.oxsoft.ftpserver
13