pre-requisite:
- Android 開發(fā)相關知識以及環(huán)境
- Raspberry Pi 3 Model B 開發(fā)板红氯, 紅外傳感器
Hello, world!
環(huán)境搭建
環(huán)境搭建分成幾個部分蛉顽, 一是開發(fā)Android應用相關的環(huán)境敛瓷, 比如電腦叁巨, Android Studio 等。
Android 開發(fā)環(huán)境
- Mac Pro
- Android Studio 2.2.3
- Android SDK 最新版本
關于Android應用開發(fā)環(huán)境這里不再贅述呐籽。
Raspberry Pi 3 Model B with Android Things Developer Preview
需要將買回的樹莓派刷上新的 Android Things 操作系統(tǒng)锋勺。
刷系統(tǒng)也很簡單, 從官網(wǎng)上下載系統(tǒng)鏡像狡蝶, 寫入到micro sdcard 就好了庶橱。詳細流程說明參考: https://developer.android.com/things/preview/index.html
關于刷機
flashing image to sdcard is dead slow. Instead, by flashing to rdisk to improve the speed significantly.
checkout http://daoyuan.li/solution-dd-too-slow-on-mac-os-x/
/dev/rdisk nodes are character-special devices, but are “raw” in the BSD sense and force block-aligned I/O. They are closer to the physical disk than the buffer cache. /dev/disk nodes, on the other hand, are buffered block-special devices and are used primarily by the kernel’s filesystem code.
diskutil unmountDisk /dev/disk2
sudo dd bs=1m if=~/Downloads/2013-10-09.alice.img of=/dev/rdisk2
所以刷系統(tǒng)時,一定注意 /dev/rdisk2 中的 r
贪惹,加上之后苏章,幾分鐘, 不加的話,可能得好幾個小時枫绅。
啟動
將 Pi 通上電泉孩, HDMI 連接顯示器, 接上網(wǎng)線和鍵盤并淋,啟動之后寓搬, 屏幕上就會顯示IP地址了。
比較不方便的是需要接網(wǎng)線和顯示器县耽, 可能有很多人家里都沒有這兩樣東西句喷, 或者不在一塊。
看到IP 之后兔毙,就可以在PC 端通過 adb 網(wǎng)絡連接到Pi 上了唾琼。 然后再設置 WiFi 連接, 就可以不需要再插著網(wǎng)線和顯示器了澎剥。
# connect to Pi with eth0
? adb connect ip
# connect WiFi
adb shell am startservice \
-n com.google.wifisetup/.WifiSetupService \
-a WifiSetupService.Connect \
-e ssid <Network_SSID> \
-e passphrase <Network_Passcode>
# check ip address
? ifconfig
? Downloads adb connect 192.168.1.109
connected to 192.168.1.109:5555
Pi 連接上 WiFi 之后父叙, PC 就可以通過adb 無線控制 Pi了,就再也不需要讓Pi 連接各種線了肴裙。當然電源線還是需要的趾唱,也可以直接將Pi 通過USB 連接到 Mac Pro 上。(某些電腦的USB 電壓和Pi 電壓電流要求不同蜻懦, 請謹慎甜癞。 我的 MacBook Pro (Retina, 15-inch, Mid 2015) 沒有問題。 )
Note: 在開發(fā)過程中宛乃,可能經(jīng)常插拔線悠咱,需要關閉電源。 Pi 重啟后IP 可能會變征炼,這個時候重新連接顯示器查看會很麻煩析既。 我是通過 nmap 這個工具掃描局域網(wǎng)的活躍IP 查找 Pi 的。 當然你也可以看看無線路由器的管理頁面谆奥, 得知 Pi 的IP 地址眼坏。
? ~ nmap -sP 192.168.1.1/24
Starting Nmap 7.12 ( https://nmap.org ) at 2017-01-07 17:39 CST
Nmap scan report for 192.168.1.1
Host is up (0.0076s latency).
Nmap scan report for 192.168.1.101
Host is up (0.038s latency).
Nmap scan report for 192.168.1.102
Host is up (0.079s latency).
Nmap scan report for 192.168.1.105
Host is up (0.00054s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.94 seconds
最后,通過adb 可以查看到 系統(tǒng)的版本等信息
? ~ adb shell
# execute in Raspberry Pi
rpi3:/ $ cat /system/build.prop
# begin build properties
# autogenerated by buildinfo.sh
ro.build.id=NIF73
ro.build.display.id=iot_rpi3-userdebug 7.0 NIF73 3565696 test-keys
ro.build.version.incremental=3565696
ro.build.version.sdk=24
ro.build.version.preview_sdk=0
ro.build.version.codename=REL
ro.build.version.all_codenames=REL
ro.build.version.release=7.0
ro.build.version.security_patch=2016-12-05
ro.build.version.base_os=
ro.build.date=Mon Dec 12 20:53:57 UTC 2016
ro.build.date.utc=1481576037
ro.build.type=userdebug
ro.build.user=android-build
ro.build.host=vpeb11.mtv.corp.google.com
ro.build.tags=test-keys
ro.build.flavor=iot_rpi3-userdebug
ro.build.system_root_image=true
ro.build.ab_update=true
ro.product.model=iot_rpi3
ro.product.brand=generic
ro.product.name=iot_rpi3
ro.product.device=rpi3
ro.product.board=rpi3
# ro.product.cpu.abi and ro.product.cpu.abi2 are obsolete,
# use ro.product.cpu.abilist instead.
ro.product.cpu.abi=armeabi-v7a
ro.product.cpu.abi2=armeabi
ro.product.cpu.abilist=armeabi-v7a,armeabi
ro.product.cpu.abilist32=armeabi-v7a,armeabi
ro.product.cpu.abilist64=
ro.product.manufacturer=unknown
ro.wifi.channels=
ro.board.platform=
# ro.build.product is obsolete; use ro.product.device
ro.build.product=rpi3
# Do not try to parse description, fingerprint, or thumbprint
ro.build.description=iot_rpi3-userdebug 7.0 NIF73 3565696 test-keys
ro.build.fingerprint=generic/iot_rpi3/rpi3:7.0/NIF73/3565696:userdebug/test-keys
ro.build.characteristics=default
# end build properties
#
# from device/pifoundation/rpi3/system.prop
#
ro.kernel.qemu=1
ro.kernel.qemu.force_gles=-1
wifi.interface=wlan0
wifi.supplicant_scan_interval=15
#
# ADDITIONAL_BUILD_PROPERTIES
#
ro.carrier=unknown
ro.hardware.gps=iot
ro.hardware.sensors=iot
log.tag.Hyphenator=SUPPRESS
ro.config.notification_sound=OnTheHunt.ogg
ro.config.alarm_alert=Alarm_Classic.ogg
camera.disable_zsl_mode=1
ro.hardware.camera=v4l2
dalvik.vm.heapsize=256m
persist.sys.dalvik.vm.lib.2=libart.so
dalvik.vm.isa.arm.variant=generic
dalvik.vm.isa.arm.features=default
dalvik.vm.lockprof.threshold=500
net.bt.name=Android
dalvik.vm.stack-trace-file=/data/anr/traces.txt
rpi3:/ $
另外酸些, Android Things 自帶了 toyBox宰译, 就不用安裝 busybox 了。 不知道以后的正式版本發(fā)布后還會不會帶這個魄懂。
App 開發(fā)
同Android 應用開發(fā)基本一樣沿侈。 幾處不同在https://developer.android.com/things/training/first-device/create-studio-project.html#declare_a_home_activity 都提出來了
- Add the dependency artifact to your app-level build.gradle file:
dependencies {
...
provided 'com.google.android.things:androidthings:0.1-devpreview'
}
Note: Use the provided dependency configuration to indicate that your app should use the existing shared library on the device at run-time.
- Add the things shared library entry to your app's manifest file:
<application ...>
<uses-library android:name="com.google.android.things"/>
...
</application>
如果忘記添加,會出現(xiàn)這樣的錯誤:
--------- beginning of crash
01-05 11:00:43.663 6861 6861 E AndroidRuntime: FATAL EXCEPTION: main
01-05 11:00:43.663 6861 6861 E AndroidRuntime: Process: asylum.digithome, PID: 6861
01-05 11:00:43.663 6861 6861 E AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/things/pio/PeripheralManagerService;
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at asylum.digithome.MainActivity.testGPIO(MainActivity.java:64)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at asylum.digithome.MainActivity.onCreate(MainActivity.java:46)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6662)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.ActivityThread.-wrap12(ActivityThread.java)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6077)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.things.pio.PeripheralManagerService" on path: DexPathList[[zip file "/data/app/asylum.digithome-2/base.apk"],nativeLibraryDirectories=[/data/app/asylum.digithome-2/lib/arm, /data/app/asylum.digithome-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
01-05 11:00:43.663 6861 6861 E AndroidRuntime: ... 14 more
01-05 11:00:43.664 6861 6970 D MscSpeechLog: MscHttpRequest start Post
- 操作外設
public class HomeActivity extends Activity {
private static final String TAG = "HomeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManagerService service = new PeripheralManagerService();
Log.d(TAG, "Available GPIO: " + service.getGpioList());
}
}
點擊運行市栗, 就能在Android Studio logcat 中看到 log 了缀拭。
01-05 12:46:34.690 2197-2197/asylum.digithome E/asylum.digithome.MainActivity: List of available ports: [BCM12, BCM13, BCM16, BCM17, BCM18, BCM19, BCM20, BCM21, BCM22, BCM23, BCM24, BCM25, BCM26, BCM27, BCM4, BCM5, BCM6]