chengxiang.peng@QW-pengchengxia MINGW64 ~/AndroidThingsSources $ git clone https://github.com/androidthings/sample-simplepio.gitCloning into 'sample-simplepio'...remote: Counting objects: 781, done.remote: Total 781 (delta 0), reused 0 (delta 0), pack-reused 781Receiving objects: 100% (781/781), 1.85 MiB | 18.00 KiB/s, done.Resolving deltas: 100% (265/265), done.Checking connectivity... done.2.导入工程Open an existing Android Studio PRoject->选择sample-simplepio项目->OK->按照默认引导导入项目;3.工程目录sample-simplepio项目运行基本的代码练习PIO API, 项目包含3个模块,每个例子是一个可以独立运行的Android模块。这里我们仅仅使用blink模块跟大家演示和介绍:
4.电路搭建在搭建电路之前,我们列举一下需要的硬件设备: 树莓派3开发板 1块 LED灯 1个 电阻 1个 面包板 1块根据下面的电路设计图,使用相关的元器件搭建电路如下(注意正极连接BCM6引脚,负极连接Ground引脚 ):
5.连接系统连接好树莓派开发板,启动Android Things系统,并在开发电脑中使用adb connect命令连接到开发板(这样你就可以在构建运行的时候选择树莓派开发板进行安装了 );
C:/Users/chengxiang.peng.QUNARSERVERS>adb connect 192.168.1.106:5555connected to 192.168.1.106:5555C:/Users/chengxiang.peng.QUNARSERVERS>adb devicesList of devices attached192.168.1.106:5555 device6.项目概述该项目实现了使用SeekBar UI控件来控制电路中LED等闪烁频率的功能,主要使用了GPIO API进行如下处理: 使用PeripheralManagerService来打开一个连接到GPIO端口的LED连接; 使用DIRECTION_OUT_INITIALLY_LOW配置端口; 给setValue()方法传递getValue()相反的值来改变LED的状态; 使用Handler来执行触发GPIO的事件,在一段时间后再次触发; 当应用程序不在需要GPIO连接的时候,关闭Gpio资源;原有官方的Demo上我进行相关的修改,使用一个SeekBar来控制LED等的闪烁频率;sample-simplepio/blink/build.gradle:apply plugin: 'com.android.application' android { ... ... } dependencies { //添加依赖 provided 'com.google.android.things:androidthings:0.1-devpreview' } sample-simplepio/blink/src/main/AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidthings.simplepio"> <application android:allowBackup="true" android:icon="@android:drawable/sym_def_app_icon" android:label="@string/app_name"> //添加共享库 <uses-library android:name="com.google.android.things"/> //添加启动Activity <activity android:name=".BlinkActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> //在系统启动的时候自动启动app <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>BlinkActivity.java:实现SeekbarUI控制界面,并且向Gpio接口引脚发送指定频率的信号;public class BlinkActivity extends Activity { private static final String TAG = BlinkActivity.class.getSimpleName(); //LED闪烁时间间隔,单位毫秒 private int interval_between_blinks_ms = 1000; //控制LED灯闪烁频率控件 private SeekBar mSeekbar; //当前LED等闪烁频率 private TextView mSeekbarValue; private Handler mHandler = new Handler(); //Gpio接口对象 private Gpio mLedGpio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "Starting BlinkActivity"); setContentView(R.layout.blink_activity); mSeekbarValue = (TextView) findViewById(R.id.seekBar_value); mSeekbar = (SeekBar) findViewById(R.id.seekBar); mSeekbar.setProgress(interval_between_blinks_ms); mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { mSeekbarValue.setText("LED between time:" + i + "ms"); //通过SeekBar控件改变LED等闪烁频率 interval_between_blinks_ms = i; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); // 使用指定引脚名称,PeripheralManagerService来打开一个连接到GPIO端口的LED连接 PeripheralManagerService service = new PeripheralManagerService(); try { String pinName = BoardDefaults.getGPIOForLED(); mLedGpio = service.openGpio(pinName); //设置引脚为输出信号 mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); Log.i(TAG, "Start blinking LED GPIO pin"); //Post一个Runnable对象,在指定的时间间隔持续的改变GPIO接口的状态,使得LED等闪烁 mHandler.post(mBlinkRunnable); } catch (IOException e) { Log.e(TAG, "Error on PeripheralIO API", e); } } @Override protected void onDestroy() { super.onDestroy(); //从Handler中移除blink Runnable对象 mHandler.removeCallbacks(mBlinkRunnable); Log.i(TAG, "Closing LED GPIO pin"); try { //页面销毁,当应用程序不在需要GPIO连接的时候,关闭Gpio资源 mLedGpio.close(); } catch (IOException e) { Log.e(TAG, "Error on PeripheralIO API", e); } finally { mLedGpio = null; } } private Runnable mBlinkRunnable = new Runnable() { @Override public void run() { // 如果GPIO引脚已经关闭,则退出Runnable if (mLedGpio == null) { return; } try { //使用setValue()方法传递getValue()相反的值来改变LED的状态; mLedGpio.setValue(!mLedGpio.getValue()); Log.d(TAG, "State set to " + mLedGpio.getValue()); mHandler.postDelayed(mBlinkRunnable, interval_between_blinks_ms); } catch (IOException e) { Log.e(TAG, "Error on PeripheralIO API", e); } } };}blink_activity.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--控制LED灯闪烁频率SeekBar控件--> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="1000" android:progress="1000"/> <!--LED等当前闪烁频率--> <TextView android:id="@+id/seekBar_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="30dp"/></LinearLayout>7.项目运行选择blink模块,点击运行,选择树莓派(Iot_rpi3)安装程序;注意:如果工程导入构建的过程中,提示升级Build Tools,则按照Android Studio引导升级即可。Android Things要求:
SDK Tools为24或者更高版本; Android SDK为7.0(API 24)或者更高版本;8.安装成功应用安装成功后,显示器上显示如下;
电路运行效果,如搭建电路上图。代码库:sample-simplepio:https://github.com/ThingsDeveloper/sample-simplepio.git 分支:feature:thingsdemo提示1:在安装其它的项目模块的时候,报错如下:Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.androidthings.simplepio.PWMActivity" on path: DexPathList[[zip file "/system/framework/com.google.android.things.jar", zip file "/data/app/com.example.androidthings.simplepio-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.androidthings.simplepio-2/lib/arm, /system/lib, /vendor/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 处理1:Android Things系统只允许安装一个应用,在安装其它应用之前,应该确保当前系统的应用被卸载C:/Users/chengxiang.peng.QUNARSERVERS>adb uninstall com.example.androidthings.simplepioSuccess参考:https://developer.android.com/things/training/first-device/create-studio-project.htmlhttps://developer.android.com/things/sdk/pio/gpio.htmlhttps://github.com/androidthings/sample-simplepio
新闻热点
疑难解答