Get started with Java (Android)¶
Follow this guide to get started with the Astarte device SDK for the Java programming language, compatibile with Android.
Generating a device ID¶
A device ID will be required to uniquely identify a device in an Astarte instance. Some of the Astarte device SDKs provide utilities to generate a deterministic or random device identifier, in some cases based on hardware information.
This step is only useful when registering a device using a JWT token and the provided Astarte device SDKs registration APIs. Registration of a device can also be performed outside the device in the Astarte instance. In such cases the device ID should be obtained via astartectl, or the Astarte dashboard. The device ID should then be loaded manually on the device.
A device ID can be generate randomly:
String randomID = AstarteDeviceIdUtils.generateId();
Or in a deterministic way:
String deviceID = AstarteDeviceIdUtils.generateId(namespaceUuid, payload);
Registering a device¶
In order for a device to connect to Astarte a registration procedure is required. This registration will produce a device specific credential secret that will be used when connecting to Astarte. Some of the Astarte device SDKs provide utilities to perform a device registration directly on the device. Those APIs will require a registration JWT to be uploaded to the device. Such JWT should be discarded following the registration procedure.
This step is only useful when registering the device through the APIs using the JWT token. Registration of a device can also be performed outside the device in the Astarte instance using tools such as astartectl, the Astarte dashboard, or the dedicated Astarte API for device registration. The generated credential secret should then be loaded manually on the device.
AstartePairingService astartePairingService = new AstartePairingService(pairing_url, realm);
String credentialsSecret = astartePairingService.registerDevice(jwt_token, device_id);
Instantiating and connecting a new device¶
Now that we obtained both a device ID and a credential secret we can create a new device instance.
Some of the SDKs will connect instantly as soon as the device is instantiated while others will
require a call to a connect
function.
Furthermore, depending on the SDK the introspection of the device might be defined while
instantiating the device or between instantiation and connection.
// Device creation
// connectionSource allows to connect to a db for persistency
// The interfaces supported by the device are populated by ExampleInterfaceProvider
AstarteDevice device =
new AstarteGenericDevice(
deviceId,
realm,
credentialsSecret,
new ExampleInterfaceProvider(),
pairingUrl,
connectionSource);
// ExampleMessageListener listens for device connection, disconnection and failure.
device.setAstarteMessageListener(new ExampleMessageListener());
// Connect the device
device.connect();
Streaming data¶
All Astarte Device SDKs include primitives for sending data to a remote Astarte instance.
Streaming of data could be performed for device owned interfaces of individual
or object
aggregation type.
Streaming individual data¶
In Astarte interfaces with individual
aggregation, each mapping is treated as an independent value
and is managed individually.
The snippet bellow shows how to send a value that will be inserted into the "/test0/value"
datastream which is defined by "/%{sensor_id}/value"
parametric endpoint, that is part of
"org.astarte-platform.genericsensors.Values"
datastream interface.
genericSensorsValuesInterface.streamData("/test0/value", 0.3, DateTime.now());
Streaming aggregated data¶
In Astarte interfaces with object
aggregation, Astarte expects the owner to send all of the
interface’s mappings at the same time, packed in a single message.
The following snippet shows how to send a value for an object aggregated interface. In this
examples, lat
and long
will be sent together and will be inserted into the "/coords"
datastream which is defined by the "/coords"
endpoint, that is part of "com.example.GPS"
datastream interface.
Map<String, Double> coords = new HashMap<String, Double>()
{
{
put("lat", 45.409627);
put("long", 11.8765254);
}
};
exampleGPSInterface.streamData("/coords", coords, DateTime.now());
Setting and unsetting properties¶
Interfaces of property
type represent a persistent, stateful, synchronized state with no concept
of history or timestamping. From a programming point of view, setting and unsetting properties of
device-owned interface is rather similar to sending messages on datastream interfaces.
The following snippet shows how to set a value that will be inserted into the "/sensor0/name"
property which is defined by "/%{sensor_id}/name"
parametric endpoint, that is part of "org.astarte-platform.genericsensors.AvailableSensors"
device-owned properties interface.
It should be noted how a property should be marked as unsettable in its interface definition to be able to use the unsetting method on it.
Set property:
availableSensorsInterface.setProperty("/sensor0/name", "foobar");
Unset property:
propertyInterface.unsetProperty("/sensor0/name");