1. Home
  2. Knowledge Base
  3. Tutorials
  4. Handling Connection with IoT-Ignite Agent

Handling Connection with IoT-Ignite Agent

Connection Handler

In this tutorial, you will learn how to handle connection with IoT-Ignite Agent.

To connect your application to IoT-Ignite Agent, check this tutorial first.

So you have connected to IoT-Ignite Agent successfully. Why do you have to handle connections? The connection between your application and IoT-Ignite Agent could be broken. For example, if IoT-Ignite Agent application gets an update while connected to your application, ConnectionCallback’s onDisconnected() function is going to be triggered.

You must keep your connection alive. To achieve this, we will write a class that checks the connection between IoT-Ignite Agent and your application.

Our class is going to handle:

  • Connecting to IoT-Ignite
  • Reconnecting periodically while your application is disconnected.

Check the following code. When the instance is created and start() function is called, the application tries to connect to IoT-Ignite. It also creates a watchdog to handle the connection.

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.ardic.android.iotignite.callbacks.ConnectionCallback;
import com.ardic.android.iotignite.exceptions.UnsupportedVersionException;
import com.ardic.android.iotignite.nodes.IotIgniteManager;

 * Created by yavuz.erzurumlu on 2/17/17.

public class IotIgniteHandler implements ConnectionCallback {

    private static final String TAG = IotIgniteHandler.class.getSimpleName();

    // Static singleton instance
    private static IotIgniteHandler INSTANCE = null;
    private static final long IGNITE_RECONNECT_INTERVAL = 10000L;

    private IotIgniteManager mIotIgniteManager;
    private boolean igniteConnected = false;
    private Context appContext;
    private Handler igniteWatchdog = new Handler();

    private Runnable igniteWatchdogRunnable = new Runnable() {
        public void run() {

                Log.e(TAG,"Ignite is not connected. Trying to reconnect...");
            }else {
                Log.e(TAG,"Ignite is already connected.");

    private IotIgniteHandler(Context context){
        this.appContext = context;

    public static synchronized IotIgniteHandler getInstance(Context appContext){

        if(INSTANCE == null){
            INSTANCE = new IotIgniteHandler(appContext);
        return INSTANCE;


    public void start(){

    public void onConnected() {
        Log.i(TAG,"Ignite is connected.");
        igniteConnected = true;
        // cancel watchdog //


    public void onDisconnected() {
        Log.i(TAG,"Ignite is disconnected.");
        igniteConnected = false;
        // start watchdog again here.

     * Connect to iot ignite

    private void rebuildIgnite(){
        try {
            mIotIgniteManager = new IotIgniteManager.Builder()
        } catch (UnsupportedVersionException e) {
            Log.e(TAG, "UnsupportedVersionException :" + e);

     * remove previous callback and setup new watchdog

    private void startIgniteWatchdog(){


Single Handler

To avoid multiple IotIgniteHandler instances, you can use the singleton pattern.

IoTIgniteAgent: Singleton Instance

To use IoTIgniteHandler in MainActiviy, you will get an instance of it and start it.


IoTIgniteAgent: Using IgniteHandler


import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    private static final String TAG ="Sample IoT-Ignite App";
    private IotIgniteHandler mIotIgniteHandler;

    protected void onCreate(Bundle savedInstanceState) {

         * Set IgniteHandler and start.

        mIotIgniteHandler = IotIgniteHandler.getInstance(getApplicationContext());


By means of this class, we have moved all IoT-Ignite related complexity here. In the following tutorials, we are going to use this class for not only registering things and nodes, but also for sending & receiving data over it as well.

Happy Codings,

Was this article helpful?

Related Articles