Integrate Peripheral Drivers

整合外设驱动

This lesson teaches you to

这一部分内容将为您展示

  1. Integrate Driver Libraries

  2. 整合驱动库

  3. Register Framework Drivers

  4. 注册框架驱动

Try it out

试试看

Get up and running quickly with pre-built drivers from the Peripheral Driver Library. These drivers abstract the low-level communication details associated with many common hardware peripherals.

通过从 Peripheral Driver Library 获取的已经构建好的驱动,您可以快速的启动并允许 Android Things 应用。这些驱动可以将那些与很多常见硬件外设相关联的底层通信细节抽象化。

In this lesson, you will learn to integrate library drivers into your app and bind peripherals to the Android framework through the UserDriverManager.

在这节课里,您将学会如何将驱动库整合进您的应用中,并通过利用 UserDriverManager 将外设绑定到 Android Framework 之中。

Initialize driver

初始化驱动库


To import a library driver into your app:

为了将驱动库导入到您的应用中,您需要:

  1. Search the driver index for a driver that matches your peripheral.

  2. driver index 中搜索与您的外设设备相匹配的驱动。

  3. Add the driver artifact dependency to your app-level build.gradle file:

  4. 在应用层的 build.gradle 文件中添加驱动依赖:

    1. dependencies { ... compile 'com.google.android.things.contrib:driver-button:0.3'}
  5. Add the required permissions for the driver to your app’s manifest file:

  6. 在应用的 manifest 文件中为驱动添加其所需要的权限:

    1. <uses-permission android:name="com.google.android.things.permission.MANAGE_INPUT_DRIVERS" />

Different drivers may require different permissions. If you aren’t sure which permissions are required for a specific driver, locate the driver’s README file. See User-Space Drivers for more information.

不同的驱动可能需要不同的权限。如果您不知道某些驱动具体需要什么权限,您可以在驱动的 README 文件查看相关信息。详情请见 User-Space Drivers

  1. Initialize the driver class with the appropriate Peripheral I/O resources.

  2. 使用正确的外设 I/O 资源来初始化驱动类。

  3. When your app no longer needs the resource, close the connection.

  4. 当您的应用不在需要这个资源时,关闭相关链接。

  1. import com.google.android.things.contrib.driver.button.Button;
  2. import com.google.android.things.contrib.driver.button.ButtonInputDriver;
  3. public class ButtonActivity extends Activity {
  4. private static final String TAG = "ButtonActivity";
  5. private static final String BUTTON_PIN_NAME = ; // GPIO port wired to the button
  6. private ButtonInputDriver mButtonInputDriver;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. try {
  11. // Step 3\. Initialize button driver with selected GPIO pin
  12. mButtonInputDriver = new ButtonInputDriver(BUTTON_PIN_NAME,Button.LogicState.PRESSED_WHEN_LOW, KeyEvent.KEYCODE_SPACE);
  13. } catch (IOException e) {
  14. Log.e(TAG, "Error configuring GPIO pin", e);
  15. }
  16. }
  17. @Override
  18. protected void onDestroy(){
  19. super.onDestroy();
  20. // Step 5\. Close the driver and unregister
  21. if (mButtonInputDriver != null) {
  22. try {
  23. mButtonInputDriver.close();
  24. }
  25. catch (IOException e) {
  26. Log.e(TAG, "Error closing Button driver", e);
  27. }
  28. }
  29. }
  30. }

Binding with the Framework

与 Framework 进行绑定


The driver library includes user drivers for supported peripheral types. When these drivers are available, they allow your app to interact with the standard Android framework APIs instead of the Peripheral I/O APIs.

驱动库中包含与具体外设类型相匹配的 用户驱动,当这些驱动可用时,它们允许您的应用使用标准的 Android Framework API 代替外设 I/O API 来与外设交互。

The following code registers a ButtonInputDriver peripheral as a key input with the framework. The driver will generate key events using the provided key code each time the button is pressed or released.

接下来的代码将一个 ButtonInputDriver 外设在 Framework 中注册为一个按键输入驱动。这个驱动通过在按键按下或者释放时产生一个 key_code 来生成按键事件。

  1. import com.google.android.things.userdriver.InputDriver;
  2. import com.google.android.things.userdriver.UserDriverManager;
  3. public class ButtonActivity extends Activity {
  4. private static final String TAG = "ButtonActivity";
  5. private ButtonInputDriver mButtonInputDriver;
  6. @Override
  7. protected void onStart() {
  8. super.onStart();
  9. mButtonInputDriver.register();
  10. }
  11. @Override
  12. protected void onStop() {
  13. super.onStop();
  14. mButtonInputDriver.unregister();
  15. }
  16. @Override
  17. public boolean onKeyDown(int keyCode, KeyEvent event){
  18. if (keyCode == KeyEvent.KEYCODE_SPACE) {
  19. // Handle button pressed event
  20. return true;
  21. }
  22. return super.onKeyDown(keyCode, event);
  23. }
  24. @Override
  25. public boolean onKeyUp(int keyCode, KeyEvent event) {
  26. if (keyCode == KeyEvent.KEYCODE_SPACE) {
  27. // Handle button released event
  28. return true;
  29. }
  30. return super.onKeyUp(keyCode, event);
  31. }
  32. }