raspberry pi3でandroid things を始める作業メモです。
以下macで作業しています。android studioは2.3をインストールした状態です。
ラズパイ本体以外に必要なもの
- PCモニタ
- HDMIケーブル
- micro SDカードとそれをRead/Writeするもの
- LANケーブル
システムのイメージを焼くまで
android thingsの立ち上げまでは公式に書いてある通りで進められます。
https://developer.android.com/things/hardware/raspberrypi.html
- 最新のイメージをダウンロード
https://developer.android.com/things/preview/download.html
raspberry pi用のイメージ:androidthings_rpi3_devpreview_4.zip(2017/05現在)
- unarchiverをインストールしてイメージを解凍
ダウンロードしたイメージを解凍するために公式に載ってるアーカイバをインストール
http://unarchiver.c3.cx/unarchiver
Raspberry Pi androidthings_rpi3_devpreview_4.zipを解凍すると
iot_rpi3.imgというファイルができる。
- etcherをインストール
https://etcher.io/
etherを使って先ほど解凍した iot_rpi3.img をmicro SDカードに焼く。
焼き終わるとMacが「読み取れないディスクでした」というので、microSDカードを取り出す。
rpi3を起動
メディアをrpi3にさして電源ON
android things 0.4.0-devpreviewが立ち上がる
結構遅い
Ethernet eth0 connected
の下にipアドレスが表示される。
macからrpi3に接続
adb connect Android.local
または ip指定で
adb connect xxx.xxx.xxx.xxx
このようなログが出る。
- MACでadbコマンドがnot foundの時
android studio -> preferences からsdk locationを調べて
./bash_profileに
export PATH=$PATH:/Users/username/Library/Android/sdk/platform-tools
のように書く
platform-toolsをつける必要あり
で、source ~/.bash_profile する。
wifiの設定
公式にある通り以下のコマンドで。
adb shell am startservice \
-n com.google.wifisetup/.WifiSetupService \
-a WifiSetupService.Connect \
-e ssid 自分の環境のSSID \
-e passphrase パスワード
設定を一度リセットしたい時は
1 |
adb shell am startservice -n com.google.wifisetup/.WifiSetupService -a WifiSetupService.Reset |
- 確認
adb logcat -d | grep Wifi
で確認
1 2 3 |
V WifiWatcher: Network state changed to CONNECTED V WifiWatcher: SSID changed: "自分の環境のSSID" I WifiConfigurator: Successfully connected to 自分の環境のSSID |
ログの最後の方にこんな表示が出てればOK
- ping
adb shell ping xxx.xxx.xxx.xxx
1 2 3 4 |
PING xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx) 56(84) bytes of data. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=1 ttl=64 time=0.286 ms 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2 ttl=64 time=0.247 ms 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=3 ttl=64 time=0.193 ms |
サンプルアプリを作成
普通のアプリと同様に新規プロジェクトを作成する。layoutとかappcompatはチェックをoff。
このあたりも公式そのままで。
1 2 3 4 |
dependencies { provided 'com.google.android.things:androidthings:0.4-devpreview' ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.canalier.andthingstest"> <application android:label="@string/app_name"> <uses-library android:name="com.google.android.things"/> <activity android:name=".MainActivity"> <!-- Launch activity as default from Android Studio --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!-- Launch activity automatically on boot --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.IOT_LAUNCHER"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> </manifest> |
1 2 3 4 5 6 7 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PeripheralManagerService service = new PeripheralManagerService(); Log.d(TAG, "Available GPIO: " + service.getGpioList()); } |
実行
デバイスに繋いでない場合はandroid studio のコンソールで
adb connect Android.local
をやると
runした時にiotデバイスがターゲット一覧に表示されるので、選択して実行。
すると、初めての場合はinstant runがどうのこうの言われるのでとりあえずinstall。
component installerが終わるとアプリが実行される。
HDMIをつないでいるPCモニターを確認するとアプリの名前が表示されている。
今回は画面表示はないので真っ白です。
サンプルアプリはGPIOのピン一覧を取得してログを出力するだけなので、
android studioの下のタブでandroid monitorを選ぶと
1 |
Available GPIO: [BCM12, BCM13, BCM16, BCM17, BCM18, BCM19, BCM20, BCM21, BCM22, BCM23, BCM24, BCM25, BCM26, BCM27, BCM4, BCM5, BCM6] |
という様に、GPIOの一覧が取得できています。
BCMxxというのがGPIOの名前で、この名前を使ってピンの設定をしたりするようです。