Android   电子/物联      开始 Google things   
文章目录  [隐藏]

请参阅谷歌官方文档,此文如有误请指正,谢谢!
https://developer.android.com/things/sdk/index.html

准备

Android SDK tools 升级到24以上
Android SDK api 升级到24以上

依赖

为新建的项目添加必须的things依赖
1.主app的build.gradle文件:

2.在manifest文件:


入口

创建一个入口Activity,并做必要的配置:


连接

首先要有一个通用输入输出(GPIO)设备,基于此连接到目标外围设备(外设)。
如此将GPIO和面包板连接:
1.Connect one side of the button to the chosen GPIO input pin, and the other side to ground.
2.Connect the same GPIO input pin to +3.3V through a pull-up resistor.
3.Connect the chosen GPIO output pin to one side of a series resistor.
4.Connect the other side of the resistor to the anode side (longer lead) of the LED.
5.Connect the cathode side (shorter lead) of the LED to ground.



英语好的如上操作,看不懂的如下操作:
1.选择一个GPIO的输入针脚,连到面包板的按钮的一端,按钮另一端连到GPIO的接地针脚。
2.再把这个输入针脚先连到面包板的一个上拉电阻,再连回GPIO的3.3V针脚。
3.选择一个GPIO的输出针脚,串联一个电阻。
4.将电阻的另一头连到面包板上LED的阳极。
5.将LED的阴极连到GPIO的接地针脚。



更多外设信息:
https://developer.android.com/things/hardware/hardware-101.html


可选的GPIO设备:

https://developer.android.com/things/hardware/edison-arduino-io.html
https://developer.android.com/things/hardware/edison-sparkfun-io.html
https://developer.android.com/things/hardware/pico-io.html
https://developer.android.com/things/hardware/raspberrypi-io.html

交互

1.扫描外围设备

核心api就是PeripheralManagerService。


2.响应按钮事件

和面包板上的按钮来个交互。按下按钮时对事件进行处理。
1.Use PeripheralManagerService to open a connection with the GPIO port wired to the button.
2.Configure the port with DIRECTION_IN.
3.Configure which state transitions will generate callback events with setEdgeTriggerType().
4.Register a GpioCallback to receive edge trigger events.
5.Return true within onGpioEdge() to continue receiving future edge trigger events.
6.When the application no longer needs the GPIO connection, close the Gpio resource.


英语好的看上面,不好的看我说:
1.使用PMS通过GPIO连接到按钮。
2.使用DIRECTION_IN标记配置端口。
3.使用setEdgeTriggerType()方法配置在什么状态下触发事件回调。
4.注册一个GpioCallback接收事件。
5.事件处理结束后返回true。
6.应用退出时关闭连接。


看代码吧:


3.点亮LED

1.Use PeripheralManagerService to open a connection with the GPIO port wired to the LED.
2.Configure the port with DIRECTION_OUT_INITIALLY_LOW.
3.Toggle the state of the LED by passing the inverse of getValue() to the setValue() method.
4.Use a Handler to schedule an event to toggle the GPIO again after a short delay.
5.When the application no longer needs the GPIO connection, close the Gpio resource.


1.使用PMS通过GPIO连接上LED。
2.使用DIRECTION_OUT_INITIALLY_LOW标记配置端口。
3.调用 getValue() 和 setValue() 方法切换LED的状态。
4.使用Handler进行适当延迟,再次切换LED状态,产生闪烁效果。
5.应用退出时断开连接回收资源。


集成外设驱动

外设驱动库:
https://developer.android.com/things/sdk/driver-library.html


一个示例:
https://github.com/androidthings/sample-button


关键api,UserDriverManager。通过此进行集成和绑定。

1.初始化库

1)关于支持(电子元件)的外设清单:
https://github.com/androidthings/contrib-drivers
https://github.com/androidthings/drivers-samples


2)build.gradle



3)使用选择的元器件资源文件初始化驱动类。


4)及时关闭连接回收资源。


2.使用

代码中,注册了一个ButtonInputDriver,当外设按下或放开时会做出响应。


承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设