Getting Started With Google's Android Things - Preview 2
Written by Harry Fairhead   
Monday, 13 February 2017
Article Index
Getting Started With Google's Android Things - Preview 2
GIPO Performance
How Fast Is Native?

How Fast Is Native?

To find our how fast we can toggle the GPIO lines, load the blink.cpp file and change its contents to read: 

#include <cmath>
#include <android/log.h>
#include <android_native_app_glue.h>
#include <pio/gpio.h>
#include <pio/peripheral_manager_client.h>
#include "AndroidSystemProperties.h"

void android_main(android_app* app) {
app_dummy(); // prevent native-app-glue to be stripped.
AndroidSystemProperties systemProperties(app->activity);

APeripheralManagerClient* client =
AGpio* gpio;
APeripheralManagerClient_openGpio(client, "BCM4", &gpio);
 int result = AGpio_setDirection(gpio,
 while (true) {
  AGpio_setValue(gpio, 0);
  AGpio_setValue(gpio, 1);

This will toggle line BCM4 exactly as the Java program given earlier did. 

So what is the speed improvement?


The bad news is that the 0.23ms is reduced to around 0.15ms which is an improvement, but not enough. A pulse time of 150us is not fast enough to write drivers that interface to custom protocols. 

So why is it so slow?

As the source code for the Android Things native library hasn't been released it is impossible to be certain. but it seems likely that it isn't using memory mapping to access the GPIO lines. For example, using SYSFS inefficiently yields a pulse time of around 200us. Doing the job correctly without opening and closing the file for each pulse improves this to 5us and using memory mapping you can generate pulses shorter than 1us. 

It seems that the problem isn't that Java slows things down, it is probably the basic GPIO access method.  

The UI 

To finish on a happier note - the real strength of Android Things is the ease with which you can create a UI. There is almost no need to explain if you already know how to create an Android UI - if you don't then obtain a copy of Android Programming: Starting With An App by Mike James which is a comprehensive introduction to creating an Android UI.

To demonstrate how easy it is let's just add a button to our current example.

First select the res directory, right click on it and select New, Android Resource file. Fill in the details in the dialog box that appears:


Call the file mainlayout and set its resource type to Layout. You can leave the root element as LinearLayout, but in most cases you would want to choose something more sophisticated.

When you close the dialog box the resource is created. The chances are you will see an error message when the Designer tries to open. Don't panic - just refresh the editor and it should work. Use the Designer to add a Button to the layout . You could add an event handler if you want to but more of this another time. 

Now all you have to do is add the line:


in onCreate immediately after 


This loads, inflates and displays the resource file to display the UI.

Now when you run the program you will see a single button, assuming you have a monitor. If you also have a mouse, or if the display is a supported touch-sensitive screen, then you can also click the button.

For the IoT having a complete Android UI toolkit is a luxury indeed. 

Watch this space for more developments.


The complete blinky Java program including a suitable destructor is:

package com.example.androidthings.myproject;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import java.util.List;

public class MainActivity extends Activity {
 private static final String TAG =
 private Gpio mGpio;
 protected void onCreate(Bundle savedInstanceState) {
  PeripheralManagerService manager =
                        new PeripheralManagerService();
  List<String> portList = manager.getGpioList();
  if (portList.isEmpty()) {
   Log.i(TAG, "No GPIO port available
                             on this device.");
  } else {
   Log.i(TAG, "List of available ports: " + portList);
  Log.d(TAG, "onCreate");
  try {
   mGpio = manager.openGpio("BCM4");     
   for(int i=0;i<10000;i++){
  } catch (IOException e) {

@Override protected void onDestroy() {   super.onDestroy();
 if (mGpio != null) {
   try {
    mGpio = null;
   } catch (IOException e) {
    Log.w(TAG, "Unable to close GPIO", e);
 Log.d(TAG, "onDestroy");

The XML file for the simple layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
 android:orientation="vertical"  android:layout_width="match_parent"
 android:id="@+id/button" />



More Information

Android Things


Related Articles

Android Things Dev Preview 2 Could Be What We Need.

Android Things - Google's IoT


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.



C book



or email your comment to:






Last Updated ( Sunday, 26 March 2017 )