Android Things を Raspberry Pi 3Model B で使う

# やりたいこと
ラズパイにAndroid ThingsのOSを入れて、WiFi経由でビルド＆実行する。

# Setup
### 準備
* Raspberry Pi 3 Model B, SDカードなど
* 注意：2018-09-24時点で、 `Model B+` はAndroid Thingsのイメージがサポートしてないようです。ハマりました。
    * https://stackoverflow.com/questions/49434627/does-android-things-support-the-raspberry-pi-3-model-b


### ここでProductを登録
適当でOK
https://partner.android.com/things/console/#/

### Setup Utility 
https://partner.android.com/things/console/#/tools
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/922e09c9-9bf3-2733-9a30-1177397165a7.png)
ダウンロードしたら、右クリックでOpen（mac）
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/cbc98a67-b59e-1ac3-c021-5165918b447d.png)
起動するといろいろ質問されるので、答えていく。

```bash 
Android Things Setup Utility (version 1.0.21)
============================
This tool will help you install Android Things on your board and set up Wi-Fi.

What do you want to do?
1 - Install Android Things and optionally set up Wi-Fi
2 - Set up Wi-Fi on an existing Android Things device
1
What hardware are you using?
1 - Raspberry Pi 3
2 - NXP Pico i.MX7D
1 
You chose Raspberry Pi 3.

Setting up required tools...
Fetching additional configuration...
Downloading platform tools...
5.45 MB/5.45 MB
Unzipping platform tools...
Finished setting up required tools.

Raspberry Pi 3
Do you want to use the default image or a custom image?
1 - Default image: Used for development purposes. No access to the Android
Things Console features such as metrics, crash reports, and OTA updates.
2 - Custom image: Upload your custom image for full device development and
management with all Android Things Console features.
1
Downloading Android Things image...
342 MB/342 MB 
Unzipping image...

Downloading Etcher-cli, a tool to flash your SD card...
20.5 MB/20.5 MB
Unzipping Etcher-cli...

Plug the SD card into your computer. Press [Enter] when ready

Running Etcher-cli...
This should should be run with root/administrator permissions.

This tool requires special permissions to write to external drives
Error: error running etcher-cli: did not flash successfully
Stopping adb server...
Stopped adb server...

If you are having trouble using the Setup Utility, please file a ticket at
https://issuetracker.google.com/issues/new?component=192720.
```
いきなりエラー。sudo でやってやる必要がありそう。

```bash
sudo ~/Downloads/android-things-setup-utility/android-things-setup-utility-macos 
```
で無事成功。

```bash

iot_rpi3.img was successfully written to Mass-Storage (/dev/disk3)
Checksum: 67737a7a

If you have successfully installed Android Things on your SD card, you can now
put the SD card into the Raspberry Pi and power it up. Otherwise you can abort
and run the tool again.

Would you like to set up Wi-Fi on this device? (y/n) y

Please plug your Raspberry Pi to your router with an Ethernet cable, then press [Enter].

Attempting to connect to your Raspberry Pi at Android.local...
Connected to device through Ethernet.
Enter the Wi-Fi network name: EthWirelessNetworkC
Enter the Wi-Fi network password (leave empty if no password): 
Connecting to Wi-Fi network EthWirelessNetworkC...
Looking for device... This can take up to 3 minutes.
Device found.
Waiting...
Successfully connected to Wifi
Stopping adb server...
Stopped adb server...

Now that you’re set up, try sample projects in Android Studio or in the sample
repository here: https://developer.android.com/things/sdk/samples.html

To learn more about features like over-the-air updates, visit the Android Things
Console: https://partner.android.com/things/console

Press [Enter] to quit.
```
無事セットアップできたようだ。

### Android Studio のインストール
https://developer.android.com/studio/

### プロジェクト作成
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/b5645ee3-dd8e-982a-807f-075fa70fc4ba.png)

ここではAndroid Thingsを選択
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/12819595-f2de-d31d-dc22-c58a778a2b0f.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/9390a76f-c2f2-d8e0-1f63-72f2508e2e5a.png)

### コード修正
ビルドができたことを確認するために、一行だけいれる

```kotlin

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("test", "Hello Things") // この行を追加
    }
}
```

### ビルド
Wi−Fi経由でのビルドを試します。
とりあえずRunする。
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/5c1a6129-5355-d5a5-a53d-66e2aec04c71.png)
ビルドには成功しますが、Targetのデバイスが、デバイスリストに見つからない。
どうやら、adb のConnectをしてやる必要がありそう。
https://medium.com/@shivrajp130/connect-android-things-powered-raspberry-pi-3-to-wifi-36aaf2ced174

なのでまず、以下を参考にadbのパスを通して、
https://qiita.com/furusin_oriver/items/f956848788c7a63922bd

で、Android StudioのTerminalを再起動して、adb接続する。

```bash
$ adb connect Android.local
connected to Android.local:5555
```

無事つながったので、もう一度、Runすると、以下のように表示されました。
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/e30e0e50-038e-49db-0f43-1dff0a6d257d.png)

LogCatを見ると、追加したログが表示されていました。
![image.png](https://qiita-image-store.s3.amazonaws.com/0/102681/bcfa81c6-760b-05e4-9087-53ffd1a398e8.png)