Android Things

Have you ever tried to connect a peripheral device to the GPIO pins on your ODROID SBC with the Android OS? For example, you wished to connect a switch to launch an application or you wanted to connect a dimming sensor. The first problem you will face would be the difficulty to handle the GPIO pins from your Android application or service and maybe you would be faced with permission problems to access a GPIO, PWM or I2C, since a general Android application is denied access to a hardware resource. The alternative solution is to port a low-level library such as wiringPi based on C/C++, but it will be required to interface to your application through JNI (Java Native Interface) using NDK. Still, you have to figure out the permission problem.

Google has introduced yet another Operating System (OS) known as “Android Things”, that is designed to run on light embedded devices and offers the framework with Java to handle peripherals. My idea was to incorporate the Android Things framework into ODROID software and let users use the expansion pins easily. However, the problem is that this OS is not open source, therefore, I had to implement the code in the Android for ODROID. Fortunately, Google opens the framework APIs with its document and Android Things SDK. This fact encouraged me to implement the full stack of the framework that works like Android Things, from bottom to top.

I used some APIs from the Android Things’ Peripheral managing parts. It has many other features, but these are not needed for our task. I made interfaces for using the Android Things API. For processing and managing the request from user-layer via API, I built the server and client architecture and connected it to the hardware layer via wiringPi to control real hardware. Initially, GPIO, I2C and PWM features were implemented, because people use them more often than other features like SPI and UART. Explaining all of the implementation is best, but I will just show you how to use it. This tact will be more useful.

Since I utilized the process of reverse engineering. My solution can become incompatible with the real Android Things OS and/or degrade its performance. However, I expect that users who previously wanted to use GPIO pins on Android will be relieved from some of the difficulty of working in C through my work. Let me show you an example of Android Things about GPIO, I2C and PWM to learn how to use it.

Fig. 01 - Architecture

There is nothing as simple as using the Android Studio to create, compile, and test an application or service that contains Android Things. You just need to install the Android Studio, and add official option and official code to use Android Things, and install a package to ODROID via otg port. and execute a package. It just works! That is all. you do not need to do anything else.

I uploaded all of the example code to my github repository (https://github.com/xiane/thingsGpioExample). And each of the examples is separated by branches. On the master branch, you can control the GPIO pin. On the i2c_16x4 branch, you can use 16x4 lcd through I2C. On the PWM branch, you can control the PWM. and on the i2c_weather_board branch, you can use a weatherboard. Please use and test it for your own projects.

All of the behind code is based on the Android Things official site. Please check the official site at: https://developer.android.com/things.

Manifest

Before you try my examples, you should add the following lines to your manifest:


For example, in here, https://bit.ly/2spndDW.

You should add dependencies to a build.gradle file:

compileOnly 'com.google.android.things:androidthings:1.0'

GPIO

Following is the GPIO Pin # and Pin Map.

Fig. 02 - GPIO Pin map

The above map table is based on the wiki at: https://bit.ly/37dXwFi.

First, you should get PeripheralManager. You can get a GPIO instance and available list of GPIO from the manager instance.

import com.google.android.things.pio.PeripheralManager;
import com.google.android.things.pio.Gpio;
…
PeripheralManager manager = PeripheralManager.getInstance();
You can get an available GPIO list via the getGpioList method. This method provides an available GPIO name list. So you can select from the list to use. Each pin has a name that comes from a physical pin number. Yes, the GPIO pin name is pin number. You can get GPIO instance through openGpio method with pin name by parameter.
List gpioList = manager.getGpioList();

Gpio gpio = manager.openGpio(gpioList.get(0));
// or Gpio gpio = manager.openGpio(“7”);
In this example, I will introduce to you how to use a GPIO pin as an output. In the example, I want to use pin #7 as output and if I push the button in my application, an LED that is connected to GPIO pin #7 will be lit. Like above, after getting a GPIO instance, you can set the direction IO of the GPIO pin. You can set direction by setDirection method and direction values are DIRECTION_IN, DIRECTION_OUT_INITIALLY_HIGH and DIRECTION_OUT_INITIALLY_LOW. I chose DIRECTION_OUT_INITIALLY_LOW to make the GPIO value low.

Then you can set value via the setValue method. If you want to make output value high or 1, you should pass the True parameter or you can pass the false parameter as low or 0. In this example, I get input from the application's button. So when you click the button an LED lights up.

gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
Switch gpioSwitch = find ViewById(R.id.gpio_switch);

gpioSwitch.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    try {
      Switch gpioSwitch = (Switch) v;
      if (gpioSwitch.isChecked()) {
        gpio.setValue(true);

      } else {
        gpio.setValue(false);
      }
    } catch (IOException io) {
      io.printStackTrace();
    }
  }
});
Android Things also provides other methods like getValue, setActiveType, setEdgeTriggerType and registerGpioCallback. You can learn about it from the official web page. However, the ODROID still does not provide registerGpioCallback properly. In particular, Callback configuration using Handler has not been implemented yet. I hope it will be implemented.

GPIO method reference - https://bit.ly/2tXHUHI.

Fig. 03

Fig. 04

Fig. 05

Fig. 06

Fig. 07

You can control I2C and PWM by checking an example from my github. Also, you can learn about each peripheral API from the Web site.

I2C - https://developer.android.com/things/sdk/pio/i2c PWM - https://developer.android.com/things/sdk/pio/pwm

I hope it will help you better utilize your Android peripherals!

References

https://developer.android.com/things https://forum.odroid.com/viewtopic.php?f=178&t=37101

Be the first to comment

Leave a Reply