Connecting to Device Cloud

To connect your device to Device Cloud there are several required steps:

  1. Configuring your Cloud Connector.
  2. Cloud Connector instantiation.
  3. Starting/stopping Device Cloud connection.
  4. Listening to connection events.
  5. Adding required API permissions.

Tip Before starting the creation of a Device Cloud connected application, the specific Cloud Connector for your platform should be provided. In this guide we will use the Emulator Java ME implementations included in the SDK as examples.

Configuring Cloud Connector

To properly establish a connection with Device Cloud, you must configure Cloud Connector. General and Connection Settings of the connector_config.txt file manage the device identification and the TCP connection with Device Cloud. This is very important because it defines the way the application communicates with Device Cloud.

Tip The configuration of Cloud Connector includes more settings that will not be covered in this section. Next topics will cover some of the configurations needed. For more information about all the settings, read the Cloud Connector Configuration section.

General Settings

This category includes some parameters that make a device unique for Device Cloud:

Connection Settings

Some parameters manage the TCP connection with Device Cloud. Basically there are three decisions to be taken: Connection cluster, Keepalive and Reconnect configuration and Connection type.

Connection cluster

This will vary depending on which cloud you selected when you initially created your account: Device Cloud US (login.etherios.com) or Device Cloud Europe (login.etherios.co.uk).

Keepalive and reconnect configuration

One characteristic of the Device Cloud’s TCP connection is that it needs to periodically send keepalive messages so the connection keeps opened in both directions:

You must decide how often they are sent in both directions and how many of them can be missed before dropping the connection. As a rule of thumb, both intervals are set to 75 seconds and the wait count is set to 5.

So the device is properly connected to Device Cloud… but what would happen if your precious Ethernet cable is cut or your roaming device goes beyond its carrier coverage? Cloud Connector can attempt to reconnect after a connection is lost:

Connection type

The TCP connection can be configured to use the secure protocol and or use compression. Also, a password can be provided to verify the identity of a device. To learn more about how to configure the password in Device Cloud, please refer to the Device Cloud Programming Guide.

Cloud Connector Instantiation

Once your device is properly identified and the connection configured, you can start writing code.

Connecting to Device Cloud requires an instance of the AbstractPlatformConnector class. This is an abstract class in the Cloud Connector core library and your platform specific implementation should include a subclass of it.

The example of Cloud Connector platform implementation given in this SDK, Java ME Emulator, includes this subclass called CloudConnector. It has several constructors to provide extra functionality to the application that will be explained in the following sections, but now we are using the no-argument constructor.

Cloud Connector object instantiation
import com.etherios.connector.emulator.CloudConnector;
 
[...]
 
// New Cloud Connector object
CloudConnector connector = new CloudConnector();

Starting/Stopping Device Cloud Connection

Once everything is properly configured it is time to connect to Device Cloud. This is as easy as calling the start method of the Cloud Connector object. This method will open a secure or standard connection with the specified Device Cloud cluster.

Connecting to Device Cloud
[...]
 
// New Cloud Connector object
CloudConnector connector = ...;
 
// Connect to Device Cloud
connector.start();
 
[...]

To disconnect your application from Device Cloud use the stop method of the Cloud Connector object. This method will immediately close the connection with Device Cloud freeing the allocated resources locally and remotely. This might be useful in some scenarios, such as a device that is going to sleep for a long time.

Disconnecting from Device Cloud
[...]
 
// New Cloud Connector object
CloudConnector connector = ...;
 
[...]
 
// Disconnect from Device Cloud
connector.stop();
 
[...]

Listening to Connection Events

Your application might want to be aware of the connection events that are taking place so it can process them and maybe execute a specific action.

Registering a listener allows the application to be notified when the connection process has started, when it is finally connected, or if at any time it is disconnected, or if a connection error occurs.

Events listener registration
[...]
 
// New Cloud Connector object
CloudConnector connector = new CloudConnector();
 
// Register the event listeners
connector.registerEventsListener(new MyEventsListener());
 
// Connect to Device Cloud
connector.start();

[...]

This listener, MyEventsListener, implements the EventsListener interface which includes several methods to inform about different events, including connection events.

EventListener implementation example, MyEventsListener
import com.etherios.connector.core.events.ConnectionEvent;
import com.etherios.connector.core.events.EventsListener;
 
public class MyEventsListener implements EventsListener {

	public void notifyConnectionEvent(ConnectionEvent event) {
	}

	[...]

}

The method notifyConnectionEvent is called to notify that a connection event occurred. It receives a ConnectionEvent and its type can be obtained using the getConnectionEventType method. Then it can be casted to the corresponding class depending on its type:


notifyConnectionEvent implementation example
import com.etherios.connector.core.events.ConnectionEvent;
import com.etherios.connector.core.events.ConnectionEvent.ConnectionErrorEvent;
import com.etherios.connector.core.events.EventsListener;
[...]
 
public class MyEventsListener implements EventsListener {

	[...]

	public void notifyConnectionEvent(ConnectionEvent event) {
		switch (event.getConnectionEventType()) {
		case ConnectionEvent.CONNECTION_STARTING:
			System.out.println(">>> Connecting to Device Cloud...");
			// Handle starting connection event properly
			[...]
			break;
		case ConnectionEvent.CONNECTION_CONNECTED:
			System.out.println(">>> Connected to Device Cloud");
			// Handle connection event properly
			[...]
			break;
		case ConnectionEvent.CONNECTION_DISCONNECTED:
			System.out.println(">>> Disconnected from Device Cloud");
			// Handle disconnection event properly
			[...]
			break;
		case ConnectionEvent.CONNECTION_ERROR:
			System.out.println(">>> Error on connection with Device Cloud > "
					+ event.getEventLogString());
			handleConnectionError((ConnectionErrorEvent) event);
			break;
		default:
			System.out.println("Unknown connection event type: "
					+ event.getConnectionEventType());
			// Handle unknown event properly
			[...]
			break;
		}
	}
 
	private void handleConnectionError(ConnectionErrorEvent errorEvent) {
		switch (errorEvent.getConnectionErrorType()) {
		case ConnectionErrorEvent.ERROR_UNABLE_TO_CONNECT:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_CONNECTION_LOST:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_SERVER_UNAVAILABLE:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_BAD_VERSION:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_SERVER_OVERLOAD:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_WRONG_ID:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_NO_CONNECTION:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_SECURITY_FAILURE:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_CANNOT_RESOLVE_HOST:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_DESCRIPTOR_ERROR:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_DEVICE_ID_NOT_RECEIVED:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_INVALID_VENDOR_ID:
			[...]
			break;
		case ConnectionErrorEvent.ERROR_INTERNET_NOT_AVAILABLE:
			[...]
			break;
		default:
			// Handle unknown error type properly
			break;
		}
	}
	[...]
}

Required API Permissions

To establish the connection with Device Cloud the following API permissions are required:

Depending on the connection type you are going to use, standard or secure (defined by the enable_secure_connection value in the configuration file) you will need also:

Checking the Application Connection to Device Cloud

Once the Device Cloud application is running you can verify it is properly connected by entering in your Device Cloud account and selecting the Device Management tab. Your device should be listed in the table as connected with the following values:

Sample Application Code

You can refer to any of the sample applications included in this SDK to review the implementation of the Device Cloud connection using Cloud Connector: