BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Google Released Cloud IoT Core Client Library for Android Things

Google Released Cloud IoT Core Client Library for Android Things

Leia em Português

This item in japanese

Bookmarks

Google has released a client library to make it easy for developers to use Google Cloud IoT Core from Android Things devices. Developers can connect to the IoT Core MQTT bridge, authenticate a device, publish device telemetry, subscribe to configuration changes, and handle errors and network outages.

Cloud IoT Core is a fully managed service on Google Cloud Platform that allows developers to securely connect, manage, and ingest data from globally dispersed devices. Cloud IoT Core, in combination with other services from Google Cloud Platform, provides a solution for collecting, processing, analyzing, and visualizing IoT data in real time. Furthermore, Android Things is designed to support data collection for telemetry, powerful computer vision, audio processing, and machine learning applications.

The Cloud IoT Core client library provides abstractions for every Cloud IoT Core function, such as publishing telemetry events, device state, and receiving device configuration from Cloud IoT Core.

The Cloud IoT Core keeps track of approved devices through a device registry, and each device uses a public key to authenticate with the server. For authentication with Cloud IoT Core, the client library supports both RSA and ECC keys, and implements JSON Web Tokens (JWTs).

Devices can publish their telemetry data to one or more buckets by issuing a PUBLISH message through the MQTT connection. Messages must be published to an MQTT topic in the following format:

/devices/{device-id}/events

Using Cloud IoT Core, developers can monitor the state of each connected device. Device state updates are usually triggered by a change to the device, for example a configuration update from Cloud IoT Core, or a change from an external source, such as a firmware update. Device state is published to an MQTT topic using the format:

/devices/<device_id>/state

Developers can control a device by sending it a device configuration from Cloud IoT Core. The data can be in any format, such as binary data, text, JSON, or serialized protocol buffers. It is important to note than a device is not guaranteed to receive every configuration update; if there are many updates released in a short period of time, devices may not receive intermediate versions.

To get started with the Cloud IoT Core client library, add the following to the build.gradle file in your Android Things project:

implementation 'com.google.android.things:cloud-iot-core:1.0.0'

The following Kotlin example demonstrates how to create a new configuration and client based on an existent project:

var configuration = IotCoreConfiguration.Builder().
                         .setProjectId("my-gcp-project")
                         .setRegistry("my-device-registry", "us-central1")
                         .setDeviceId("my-device-id")
                         .setKeyPair(keyPairObject)
                         .build()

var iotCoreClient = IotCoreClient.Builder()
              .setIotCoreConfiguration(configuration)
              .setOnConfigurationListener(onConfigurationListener)
              .setConnectionCallback(connectionCallback)
              .build()

iotCoreClient.connect()

The following Kotlin examples show how to publish telemetry information or device state:

private fun publishTelemetry(temperature: Float, humidity: Float) {
    // payload is an arbitrary, application-specific array of bytes
    val examplePayload = """{
        |"temperature" : $temperature,
        |"humidity": $humidity
        |}""".trimMargin().toByteArray()
    val event = TelemetryEvent(examplePayload, topicSubpath, TelemetryEvent.QOS_AT_LEAST_ONCE)
    iotCoreClient.publishTelemetry(event)
}

private fun publishDeviceState(telemetryFrequency: Int, enabledSensors: Array<string>) {
    // payload is an arbitrary, application-specific array of bytes
    val examplePayload = """{
        |"telemetryFrequency": $telemetryFrequency,
        |"enabledSensors": ${enabledSensors.contentToString()}
        |}""".trimMargin().toByteArray()
    iotCoreClient.publishDeviceState(examplePayload)
}

Google also provided a sample that shows how to implement a sensor hub on Android Things, collecting sensor data from connected sensors and publishing them to a Google Cloud IoT topic.

More information on Cloud IoT Core is available on the Cloud IoT Core page, official documentation, and Google's IoT Developers community.

Rate this Article

Adoption
Style

BT