Android Things Day1

pre-requisite:

  1. Android 开发相关知识以及环境
  2. Raspberry Pi 3 Model B 开发板, 红外传感器

Hello, world!

环境搭建

环境搭建分成几个部分, 一是开发Android应用相关的环境, 比如电脑, Android Studio 等。

Android 开发环境

  • Mac Pro
  • Android Studio 2.2.3
  • Android SDK 最新版本
    关于Android应用开发环境这里不再赘述。

Raspberry Pi 3 Model B with Android Things Developer Preview

需要将买回的树莓派刷上新的 Android Things 操作系统。
刷系统也很简单, 从官网上下载系统镜像, 写入到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

所以刷系统时,一定注意 /dev/rdisk2 中的 r,加上之后,几分钟, 不加的话,可能得好几个小时。

启动

将 Pi 通上电, HDMI 连接显示器, 接上网线和键盘,启动之后, 屏幕上就会显示IP地址了。
比较不方便的是需要接网线和显示器, 可能有很多人家里都没有这两样东西, 或者不在一块。

看到IP 之后,就可以在PC 端通过 adb 网络连接到Pi 上了。 然后再设置 WiFi 连接, 就可以不需要再插着网线和显示器了。

# connect to Pi with eth0
➜ adb connect ip

# connect WiFi
adb shell am startservice \
    -n com.google.wifisetup/.WifiSetupService \
    -a WifiSetupService.Connect \
    -e ssid  \
    -e passphrase 

# 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: 在开发过程中,可能经常插拔线,需要关闭电源。 Pi 重启后IP 可能会变,这个时候重新连接显示器查看会很麻烦。 我是通过 nmap 这个工具扫描局域网的活跃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 可以查看到 系统的版本等信息

➜  ~ 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 了。 不知道以后的正式版本发布后还会不会带这个。

App 开发

同Android 应用开发基本一样。 几处不同在https://developer.android.com/things/training/first-device/create-studio-project.html#declare_a_home_activity 都提出来了

  1. 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.

  1. Add the things shared library entry to your app's manifest file:

    
    ...


如果忘记添加,会出现这样的错误:

--------- 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
  1. 操作外设
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]

你可能感兴趣的:(Android Things Day1)