与外设交互
当开发人员直接将硬件外设连接到应用程序时,Android Things的强大功能就实现了。
在本课中,您将学习如何使用基本的外设 I / O API来发现和与通用输入输出(GPIO)端口进行通信。
列出可用的外围设备
负责管理外设连接的系统服务是 PeripheralManagerService
。您可以使用此服务列出所有已知外设类型的可用端口。
以下代码将可用的GPIO端口列表写入 logcat
:
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());
}
}
处理按钮事件
当按下连接到GPIO的按钮时接收事件:
PeripheralManagerService
打开与连接到该按钮的GPIO端口的连接。DIRECTION_IN
配置端口。setEdgeTriggerType()
生成回调事件。GpioCallback
以接收边缘触发事件。onGpioEdge()
中返回 true
以继续接收未来的边缘触发事件。Gpio
资源。public class ButtonActivity extends Activity {
private static final String TAG = "ButtonActivity";
private static final String BUTTON_PIN_NAME = ...; // GPIO port wired to the button
private Gpio mButtonGpio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManagerService service = new PeripheralManagerService();
try {
// Step 1. Create GPIO connection.
mButtonGpio = service.openGpio(BUTTON_PIN_NAME);
// Step 2. Configure as an input.
mButtonGpio.setDirection(Gpio.DIRECTION_IN);
// Step 3. Enable edge trigger events.
mButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
// Step 4. Register an event callback.
mButtonGpio.registerGpioCallback(mCallback);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
// Step 4. Register an event callback.
private GpioCallback mCallback = new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
Log.i(TAG, "GPIO changed, button pressed");
// Step 5. Return true to keep callback active.
return true;
}
};
@Override
protected void onDestroy() {
super.onDestroy();
// Step 6. Close the resource
if (mButtonGpio != null) {
mButtonGpio.unregisterGpioCallback(mCallback);
try {
mButtonGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
}
}
闪烁LED
要在连接到GPIO的LED上执行闪烁模式:
PeripheralManagerService
打开与连接到LED的GPIO端口的连接。DIRECTION_OUT_INITIALLY_LOW
配置端口。getValue()
的反向传递给 setValue()
方法来切换LED的状态。Handler
调度事件以重新切换GPIO。Gpio
资源。public class BlinkActivity extends Activity {
private static final String TAG = "BlinkActivity";
private static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;
private static final String LED_PIN_NAME = ...; // GPIO port wired to the LED
private Handler mHandler = new Handler();
private Gpio mLedGpio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Step 1. Create GPIO connection.
PeripheralManagerService service = new PeripheralManagerService();
try {
mLedGpio = service.openGpio(LED_PIN_NAME);
// Step 2. Configure as an output.
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Step 4. Repeat using a handler.
mHandler.post(mBlinkRunnable);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Step 4. Remove handler events on close.
mHandler.removeCallbacks(mBlinkRunnable);
// Step 5. Close the resource.
if (mLedGpio != null) {
try {
mLedGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
}
private Runnable mBlinkRunnable = new Runnable() {
@Override
public void run() {
// Exit if the GPIO is already closed
if (mLedGpio == null) {
return;
}
try {
// Step 3. Toggle the LED state
mLedGpio.setValue(!mLedGpio.getValue());
// Step 4. Schedule another event after delay.
mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
};
}