Android ThingsをIntel Edison Kit for Arduinoにインストール

# Android Thingsとは？
GoogleのIoT platformとして発表されたもので、
発表時点では

Intel Edison
NXP Pico
Raspberry Pi 3
の3つのハードウェアにインストール可能なOSイメージがGoogleから提供されています。

https://developer.android.com/things/index.html
https://android-developers.googleblog.com/2016/12/announcing-googles-new-internet-of-things-platform-with-weave-and-android-things.html

このページではIntel EdisonにAndroid Thingsをインストールする手順を記載しています
Raspberry Pi 3への導入は以下の記事をどうぞ。

> Android ThingsをRaspberry Pi 3にインストール
> http://qiita.com/aoinakanishi/items/fae26dc1ad0f16b58558

# ハードウェアの準備
Intel Edison Kit for Arduino
micro usbケーブル ×2
があれば動くようになります。

# OSイメージのダウンロード
https://developer.android.com/things/preview/download.html
こちらからandroidthings_edison_devpreview_1.zipをダウンロードします。

![System Image Downloads](https://qiita-image-store.s3.amazonaws.com/0/129712/1ffad16d-644a-b475-e4c0-83dd9a69f9d4.png)
![Terms and Conditions](https://qiita-image-store.s3.amazonaws.com/0/129712/5c52838f-f1a4-9774-4364-0edecb537ec0.png)

# zipファイルの解凍

```
% unzip -d devpreview androidthings_edison_devpreview_1.zip
Archive:  androidthings_edison_devpreview_1.zip
  inflating: devpreview/boot.img
  inflating: devpreview/system.img
  inflating: devpreview/userdata.img
  inflating: devpreview/oem.img
  inflating: devpreview/gapps.img
  inflating: devpreview/partition-table.img
  inflating: devpreview/edison_ifwi-dbg-00.bin
  inflating: devpreview/edison_dnx_fwr.bin
  inflating: devpreview/edison_dnx_osr.bin
  inflating: devpreview/u-boot-edison.bin
  inflating: devpreview/u-boot-edison.img
  inflating: devpreview/FlashEdison.json
  inflating: devpreview/README
  inflating: devpreview/brillo-flashall-edison.sh
  inflating: devpreview/fastboot
  inflating: devpreview/adb
```

# Fastbootのインストール
Android SDKをインストールしてください。
特別な設定変更等がなければMacの場合以下の場所にあります。
```
~/Library/Android/sdk/platform-tools/fastboot
```
# Intel® Platform Flash Tool Liteのインストール
https://01.org/android-ia/downloads/intel-platform-flash-tool-lite
からそれぞれの環境にあったフラッシュツールをダウンロードします。

Macの場合、PlatformFlashToolLite_5.5.2.0_mac64.pkg を実行するとインストールが始まります。
![install Platform Flash Tool Lite](https://qiita-image-store.s3.amazonaws.com/0/129712/e3e78381-c76c-75d2-fba3-30ef0b4d32d7.png)

# Android Thingsを焼き込む
- 先程インストールしたPlatform Flash Tool Liteを起動します![Screen Shot](https://qiita-image-store.s3.amazonaws.com/0/129712/49cdfe5a-d6f6-f159-193c-62acdcc49a08.png)
- 「Browse...」を押し、先程ダウンロードしたファイルを選択します![Screen Shot](https://qiita-image-store.s3.amazonaws.com/0/129712/db7c9795-7339-3b33-aa1a-64acc51f82cb.png)

> Flash file: FlashEdison.json 
> Configuration: Non_OS

の表示になっている事を確認

- Intel Edisonをファームウェアリカバリモードで起動

![edison-arduino-connections.png](https://qiita-image-store.s3.amazonaws.com/0/129712/2e83fc54-3376-bad5-b7d1-4d4102305a49.png)

1. (1)のスイッチがmicro USB側になっている事を確認
2. (2)のFWスイッチを押したまま(3)のJ16にmicro USBケーブルを刺して母艦(Mac)と接続
3. 基板上にあるLEDが点灯するのを確認してFWスイッチを放す

- Start to Flashをクリックしてフラッシュ開始![Screen Shot](https://qiita-image-store.s3.amazonaws.com/0/129712/2de59c25-6672-80c1-f9ff-54eabf83aa47.png)
- 完了![Screen Shot](https://qiita-image-store.s3.amazonaws.com/0/129712/6a4bbeee-1121-d592-d918-2939088de09c.png)

- 失敗することもあります・・・![Screen Shot](https://qiita-image-store.s3.amazonaws.com/0/129712/665c88d1-b1f5-da64-24e1-73d5e28e1647.png)

## Terminalからfastbootをインストール
Intel® Platform Flash Tool Liteからのインストールが失敗してもTerminalからインストールすることが出来ます

```
% /Applications/Platform\ Flash\ Tool\ Lite.app/Contents/MacOS/platformflashtoollitecli -f ~/Downloads/androidthings_edison_devpreview_1.zip -c non_os
01/26/17 22:29:18.014  INFO   : Intel(R) Platform Flash Tool Lite V 5.5.2.0 x64 (build on Tuesday April 26th 2016, 14:52:58 UTC)
01/26/17 22:29:18.014  INFO   : Using xFSTK Downloader version: 1.8.0
01/26/17 22:29:18.026  INFO   : Using DFU Downloader version: 0.8.4
01/26/17 22:29:18.146  INFO   : Using Intel Command Line FlashTool v.2.56 (Download Library v.5.23)
01/26/17 22:29:18.161  INFO   : Using Android Debug Bridge version 1.0.35
Revision f001255927f8-android
01/26/17 22:29:18.161  INFO   : No external tool registered
01/26/17 22:29:18.174  INFO   : OS version detected: OS X 10.10.5 (Yosemite) x64
01/26/17 22:29:18.178  INFO   : SSL supported: Yes
01/26/17 22:29:18.213  INFO   : Adb and fastboot binaries OK
01/26/17 22:29:18.221  INFO   : Loading /Users/*****/Downloads/androidthings_edison_devpreview_1/FlashEdison.json
01/26/17 22:29:18.238  INFO   : Available flash configurations:
conf[full] name=Full, description=full reflash
conf[non_os] name=Non_OS, description=ifwi and u-boot reflash
conf[os] name=OS, description=os reflash
conf[update] name=Update, description=update system
01/26/17 22:29:18.238  INFO   : 'non_os' configuration used to flash
01/26/17 22:29:18.288  INFO   : [Port 1/2] New device detected on 1/2 (id=0101000000000001)
01/26/17 22:29:18.334  INFO   : Platform Flash Tool Lite initialized successfully
01/26/17 22:29:18.335  INFO   : [Port 1/2] Starting flash ...
01/26/17 22:29:18.335  INFO   : [Port 1/2] Running `Notify Please reset the edison (with reset button)` command
01/26/17 22:29:18.335  INFO   : [Port 1/2] Command `Notify Please reset the edison (with reset button)` succeed
01/26/17 22:29:18.335  INFO   : [Port 1/2] Running `"xfstkFlashTool" "-1" "/Users/*****/Downloads/androidthings_edison_devpreview_1/edison_dnx_fwr.bin" "/Users/*****/Downloads/androidthings_edison_devpreview_1/edison_ifwi-dbg-00.bin" "/Users/*****/Downloads/androidthings_edison_devpreview_1/edison_dnx_osr.bin" "/Users/*****/Downloads/androidthings_edison_devpreview_1/u-boot-edison.img" "0x80000007" "none" "none" "0"` command
01/26/17 22:29:18.335  INFO   : [Port 1/2] Disabling xFSTK wipe out IFWI option
01/26/17 22:29:18.336  INFO   : [Port 1/2] Disabling xFSTK Misc DnX
01/26/17 22:29:18.336  INFO   : [Port 1/2] xFSTK Soft Fuse not set
01/26/17 22:29:18.336  INFO   : [Port 1/2] Using xFSTK Solo interface (new API)
01/26/17 22:29:18.337  INFO   : [Port 1/2] XFSTK Downloader API 1.8.0
Copyright (c) 2015 Intel Corporation
01/26/17 22:29:18.337  INFO   : [Port 1/2] fwdnx -- /Users/*****/Downloads/androidthings_edison_devpreview_1/edison_dnx_fwr.bin
01/26/17 22:29:18.337  INFO   : [Port 1/2] fwimage -- /Users/*****/Downloads/androidthings_edison_devpreview_1/edison_ifwi-dbg-00.bin
01/26/17 22:29:18.337  INFO   : [Port 1/2] osdnx -- /Users/*****/Downloads/androidthings_edison_devpreview_1/edison_dnx_osr.bin
01/26/17 22:29:18.337  INFO   : [Port 1/2] osimage -- /Users/*****/Downloads/androidthings_edison_devpreview_1/u-boot-edison.img
01/26/17 22:29:18.337  INFO   : [Port 1/2] gpflags -- 0x80000007
01/26/17 22:29:18.337  INFO   : [Port 1/2] softfuse --
....01/26/17 22:29:18.439  INFO   : [Port 1/2] Initiating download...
01/26/17 22:29:18.498  INFO   : [Port 1/2] XFSTK-STATUS--FW download is in progress ...
01/26/17 22:29:18.499  INFO   : [Port 1/2] XFSTK-STATUS--FW: Sending DnX DCFI00 ...
01/26/17 22:29:21.526  INFO   : [Port 1/2] XFSTK-STATUS--Error Code: 0 - Success
01/26/17 22:29:21.527  INFO   : [Port 1/2] XFSTK-STATUS--Firmware download completed. Continuing to OS...
01/26/17 22:29:21.527  INFO   : [Port 1/2] XFSTK-STATUS--Reconnecting to device - Attempt #1
01/26/17 22:29:22.531  INFO   : [Port 1/2] XFSTK-STATUS--Reconnecting to device - Attempt #2
01/26/17 22:29:23.535  INFO   : [Port 1/2] XFSTK-STATUS--Reconnecting to device - Attempt #3
01/26/17 22:29:24.539  INFO   : [Port 1/2] XFSTK-STATUS--Reconnecting to device - Attempt #4
01/26/17 22:29:25.556  INFO   : [Port 1/2] XFSTK-STATUS--POS download is in progress ...
01/26/17 22:29:28.795  INFO   : [Port 1/2] XFSTK-STATUS--Error Code: 0 - Success
01/26/17 22:29:28.796  INFO   : [Port 1/2] XFSTK-STATUS--Firmware and OS download completed.

XFSTK: Transfer Completed Successfully.
01/26/17 22:29:28.797  INFO   : [Port 1/2] Success: Download of FW Completed.
01/26/17 22:29:28.800  INFO   : [Port 1/2] Command `"xfstkFlashTool" "-1" "/Users/*****/Downloads/androidthings_edison_devpreview_1/edison_dnx_fwr.bin" "/Users/*****/Downloads/androidthings_edison_devpreview_1/edison_ifwi-dbg-00.bin" "/Users/*****/Downloads/androidthings_edison_devpreview_1/edison_dnx_osr.bin" "/Users/*****/Downloads/androidthings_edison_devpreview_1/u-boot-edison.img" "0x80000007" "none" "none" "0"` succeed
01/26/17 22:29:28.800  INFO   : [Port 1/2] Running `Wait for device with status pos on port 1/2` command
01/26/17 22:30:22.673  INFO   : [Port 1/2] Command `Wait for device with status pos on port 1/2` succeed
01/26/17 22:30:22.673  INFO   : [Port 1/2] Running `Notify Run 'provision' or 'brillo-flashall-edison.sh', then 'fastboot reboot' to boot the device.` command
01/26/17 22:30:22.673  INFO   : [Port 1/2] Command `Notify Run 'provision' or 'brillo-flashall-edison.sh', then 'fastboot reboot' to boot the device.` succeed
01/26/17 22:30:22.673  INFO   : [Port 1/2] Flash success (duration=00:01:04.338)
01/26/17 22:30:22.686  INFO   : Platform Flash Tool Lite exited with code 0
```

# fastbootでデバイスを認識している事を確認

```
% fastboot devices
edison********	fastboot
```

# Android Things OSの焼き込み
先程unzipしたディレクトリに移動しbrillo-flashall-edison.shスクリプトを実行します

```
% sh brillo-flashall-edison.sh
brillo-flashall-edison.sh: line 62: ./fastboot: cannot execute binary file
```

fastbootが実行できないというエラーが出るので、

> mv fastboot fastboot.bk

などとして、実行ファイルをリネームして無効にしてしまいます（先にfastbootはインストールしてあるので不要）


```
% sh brillo-flashall-edison.sh
target reported max download size of 117440512 bytes
sending 'gpt' (33 KB)...
OKAY [  0.009s]
writing 'gpt'...
OKAY [  0.149s]
sending 'u-boot' (375 KB)...
OKAY [  0.023s]
writing 'u-boot'...
OKAY [  0.103s]
erasing 'misc'...
OKAY [  0.133s]
rebooting into bootloader...
OKAY [  0.000s]
finished. total time: 0.417s
< waiting for any device >
ERROR: Unable to create a plug-in (e00002be)
ERROR: Unable to create a plug-in (e00002be)
target reported max download size of 117440512 bytes
sending 'gpt' (33 KB)...
OKAY [  0.008s]
writing 'gpt'...
OKAY [  0.079s]
erasing 'u-boot'...
OKAY [  0.489s]
sending 'u-boot' (375 KB)...
OKAY [  0.021s]
writing 'u-boot'...
OKAY [  0.105s]
sending 'boot_a' (10362 KB)...
OKAY [  0.435s]
writing 'boot_a'...
OKAY [  1.165s]
sending 'boot_b' (10362 KB)...
OKAY [  0.459s]
writing 'boot_b'...
OKAY [  1.161s]
sending sparse 'system_a' 1/5 (114685 KB)...
OKAY [  5.800s]
writing 'system_a' 1/5...
OKAY [ 22.825s]
sending sparse 'system_a' 2/5 (114573 KB)...
OKAY [  6.017s]
writing 'system_a' 2/5...
OKAY [ 13.719s]
sending sparse 'system_a' 3/5 (114338 KB)...
OKAY [  5.481s]
writing 'system_a' 3/5...
OKAY [ 14.203s]
sending sparse 'system_a' 4/5 (114646 KB)...
OKAY [  5.965s]
writing 'system_a' 4/5...
OKAY [ 17.450s]
sending sparse 'system_a' 5/5 (40618 KB)...
OKAY [  2.202s]
writing 'system_a' 5/5...
OKAY [  6.713s]
sending sparse 'system_b' 1/5 (114685 KB)...
OKAY [  5.835s]
writing 'system_b' 1/5...
OKAY [ 22.848s]
sending sparse 'system_b' 2/5 (114573 KB)...
OKAY [  5.976s]
writing 'system_b' 2/5...
OKAY [ 13.714s]
sending sparse 'system_b' 3/5 (114338 KB)...
OKAY [  6.167s]
writing 'system_b' 3/5...
OKAY [ 14.156s]
sending sparse 'system_b' 4/5 (114646 KB)...
OKAY [  6.075s]
writing 'system_b' 4/5...
OKAY [ 17.452s]
sending sparse 'system_b' 5/5 (40618 KB)...
OKAY [  2.326s]
writing 'system_b' 5/5...
OKAY [  6.690s]
sending 'userdata' (35529 KB)...
OKAY [  1.433s]
writing 'userdata'...
OKAY [  4.813s]
erasing 'misc'...
OKAY [  0.133s]
Setting current slot to '_a'...
OKAY [  0.104s]
finished. total time: 212.023s
target reported max download size of 117440512 bytes
sending sparse 'gapps_a' 1/2 (114684 KB)...
OKAY [  5.510s]
writing 'gapps_a' 1/2...
OKAY [ 12.781s]
sending sparse 'gapps_a' 2/2 (65384 KB)...
OKAY [  3.198s]
writing 'gapps_a' 2/2...
OKAY [  7.112s]
sending sparse 'gapps_b' 1/2 (114684 KB)...
OKAY [  4.614s]
writing 'gapps_b' 1/2...
OKAY [ 12.783s]
sending sparse 'gapps_b' 2/2 (65384 KB)...
OKAY [  2.669s]
writing 'gapps_b' 2/2...
OKAY [  7.109s]
finished. total time: 55.777s
target reported max download size of 117440512 bytes
sending 'oem_a' (4676 KB)...
OKAY [  0.186s]
writing 'oem_a'...
OKAY [  0.878s]
sending 'oem_b' (4676 KB)...
OKAY [  0.197s]
writing 'oem_b'...
OKAY [  0.868s]
finished. total time: 2.128s
Please run the following command to restart your Edison
    fastboot  reboot
```

ここまででインストールの完成です。

>  fastboot  reboot

を実行して再起動しましょう

# adbで確認
再起動後、しばらくすると

```
% adb devices
List of devices attached
edison********	device

```

のように端末を見ることが出来ます。


# adb shellで接続

```
% adb shell
edison:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc) context=u:r:shell:s0
```

# adb rootでルートアクセス

```
% adb root
restarting adbd as root

% adb shell
edison:/ # id
uid=0(root) gid=0(root) groups=0(root),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc) context=u:r:su:s0
```

# 起動直後のプロセス一覧
Android Device Monitorを起動すると通常のAndroidデバイスと同様にデバイスの中身が確認できます。
![Android Device Monitor](https://qiita-image-store.s3.amazonaws.com/0/129712/6e8e85d7-ed14-a35a-6168-05a355f06a5b.png)

# 所感
AndroidのスマホやタブレットにROMを焼き直すのと同様の手順で、感覚としてはRaspberry Pi 3よりもIntel EdisonにAndroid ThingsをセットアップするほうがAndroidっぽい印象です。USBでそのまま接続できるので、セットアップ時にEthernet環境も不要で、アプリの開発にあたってもWifi経由でadb 接続する必要もありません