Android Things 架构
先看 Brillo 和 Android Things 的架构图进行对比:
这是 Brillo:
这是 Android Things:
可以很清楚的看出来:
Brillo 使用 C/C++ 基于 NDK 进行开发,Android Things 通过 Java API 面向广大的 Android 和 Java 开发者,就算是新手,Android 的也是极易上手的。各位苦于嵌入式开发各种工具坑的福音到了,对于性能和底层要求高的部分仍然可以用 NDK 编写,在 Android Studio 里调试 NDK 代码也和 Java 代码一样的简单。
Android Studio,Android SDK,Play service 和 Firebase,这些工具和 Service 形成了完整易用的工具链。
Android Things 出生最晚,更新条件也是最好的,直接使用 Android Nougat 的自动后台更新机制,最大限度的提高系统的安全性。
广泛的硬件平台支持
现在支持以下3款硬件:
1)Intel Edison
2)NXP Pico
3)Raspberry Pi 3
Hello Android Things
买到的开发版都是没有装操作系统系统的,第一步先把 Android Things 刷到板子里。
Flash image (刷机):
以Intel Edison为例:
0.Android SDK Platform Tools 25.0.3以上,fastboot 工具添加到 PATH 环境变量中,以便从任意目录运行。
1. 下载后打Intel Flash Tool,加打开下载好的对应刷机包。
2. 使用 USB 线链接 Edison,如果 Edison 没有显示,换 USB 口和线试试。
3. Start to Flash(开始刷机)
4. 使用 Fastboot 刷入系统镜像,此时需要几十秒,光 System.img 就有 500 多M。
5. 刷入 Google Service 镜像。
6. 刷入 OEM 镜像。
7. 重启
8. 验证系统状态。
如果出现以下 Error,把 Intel Flash Tool 关掉,重新连接下 USB。
Connecting WIFI (联网)
依然是熟悉的 adb 命令和服务启动参数
用 logcat 查看网络状态
Ping 检测
Hello Android Things 项目
1. Android Studio 中新建项目。
2.在 build.gralde 中添加依赖 com.google.android.things:androidthings
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.geekdev.alpha.androidthings"
minSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })
provided 'com.google.android.things:androidthings:0.1-devpreview'
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}
此处依赖方式是 provided,让 Android Things 使用系统中的库。
添加 activity
添加一个主 activity 并配置 AndroidManifest.xml
在 Activity 中输入 Hello World
运行输出
直接 Command+R,可以在 logcat 窗口中看到结果了。
Peripheral I/O
不满足于 Hello Android Things,继续来使用 Android Things 对外设进行操作。
package com.geekdev.alpha.androidthings;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import com.google.android.things.contrib.driver.button.ButtonInputDriver;
import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManagerService;
import java.io.IOException;
/**
* Created by Alpha.
*
* Example of using Button driver for toggling a LED.
*
* This activity initialize an InputDriver to emit key events when the button GPIO pin state change
* and flip the state of the LED GPIO pin.
*
* You need to connect an LED and a push button switch to pins specified in {@link BoardDefaults}
* according to the schematic provided above.
*/
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private Gpio mLedGpio;
private ButtonInputDriver mButtonInputDriver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Hello Android Things!");
Log.i(TAG, "Starting ButtonActivity");
PeripheralManagerService pioService = new PeripheralManagerService();
try {
Log.i(TAG, "Configuring GPIO pins");
mLedGpio = pioService.openGpio(BoardDefaults.getGPIOForLED());
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
Log.i(TAG, "Registering button driver");
// Initialize and register the InputDriver that will emit SPACE key events
// on GPIO state changes.
mButtonInputDriver = new ButtonInputDriver(
BoardDefaults.getGPIOForButton(),
Button.LogicState.PRESSED_WHEN_LOW,
KeyEvent.KEYCODE_SPACE);
mButtonInputDriver.register();
} catch (IOException e) {
Log.e(TAG, "Error configuring GPIO pins", e);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// Turn on the LED
setLedValue(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// Turn off the LED
setLedValue(false);
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* Update the value of the LED output.
*/
private void setLedValue(boolean value) {
try {
mLedGpio.setValue(value);
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mButtonInputDriver != null) {
mButtonInputDriver.unregister();
try {
mButtonInputDriver.close();
} catch (IOException e) {
Log.e(TAG, "Error closing Button driver", e);
} finally {
mButtonInputDriver = null;
}
}
if (mLedGpio != null) {
try {
mLedGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error closing LED GPIO", e);
} finally {
mLedGpio = null;
}
mLedGpio = null;
}
}
}
使用 Button driver 对 LED 灯进行开关操作。
package com.geekdev.alpha.androidthings;
import android.os.Build;
import com.google.android.things.pio.PeripheralManagerService;
import java.util.List;
/**
* Created by Alpha.
*/
public class BoardDefaults {
private static final String DEVICE_EDISON_ARDUINO = "edison_arduino";
private static final String DEVICE_EDISON = "edison";
private static final String DEVICE_RPI3 = "rpi3";
private static final String DEVICE_NXP = "imx6ul";
private static String sBoardVariant = "";
/**
* Return the GPIO pin that the LED is connected on.
* For example, on Intel Edison Arduino breakout, pin "IO13" is connected to an onboard LED
* that turns on when the GPIO pin is HIGH, and off when low.
*/
public static String getGPIOForLED() {
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO13";
case DEVICE_EDISON:
return "GP45";
case DEVICE_RPI3:
return "BCM6";
case DEVICE_NXP:
return "GPIO4_IO21";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}
/**
* Return the GPIO pin that the Button is connected on.
*/
public static String getGPIOForButton() {
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO12";
case DEVICE_EDISON:
return "GP44";
case DEVICE_RPI3:
return "BCM21";
case DEVICE_NXP:
return "GPIO4_IO20";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}
private static String getBoardVariant() {
if (!sBoardVariant.isEmpty()) {
return sBoardVariant;
}
sBoardVariant = Build.DEVICE;
// For the edison check the pin prefix
// to always return Edison Breakout pin name when applicable.
if (sBoardVariant.equals(DEVICE_EDISON)) {
PeripheralManagerService pioService = new PeripheralManagerService();
List gpioList = pioService.getGpioList();
if (gpioList.size() != 0) {
String pin = gpioList.get(0);
if (pin.startsWith("IO")) {
sBoardVariant = DEVICE_EDISON_ARDUINO;
}
}
}
return sBoardVariant;
}
}
运行到如下的 Raspberry Pi 3 中,使用按钮来控制 LED 灯。
Code
所有示例项目代码都可以在 Github 中找到:
github.com/alphayang/android-things-start
点击文末“阅读原文”,您可以访问文中所有链接。