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() { @Override public void run() { if(!igniteConnected){ rebuildIgnite(); igniteWatchdog.postDelayed(this,IGNITE_RECONNECT_INTERVAL); 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(){ startIgniteWatchdog(); } @Override public void onConnected() { Log.i(TAG,"Ignite is connected."); igniteConnected = true; // cancel watchdog // igniteWatchdog.removeCallbacks(igniteWatchdogRunnable); } @Override public void onDisconnected() { Log.i(TAG,"Ignite is disconnected."); igniteConnected = false; // start watchdog again here. startIgniteWatchdog(); } /** * Connect to iot ignite */ private void rebuildIgnite(){ try { mIotIgniteManager = new IotIgniteManager.Builder() .setConnectionListener(this) .setContext(appContext) .build(); } catch (UnsupportedVersionException e) { Log.e(TAG, "UnsupportedVersionException :" + e); } } /** * remove previous callback and setup new watchdog */ private void startIgniteWatchdog(){ igniteWatchdog.removeCallbacks(igniteWatchdogRunnable); igniteWatchdog.postDelayed(igniteWatchdogRunnable,IGNITE_RECONNECT_INTERVAL); } }
Single Handler
To avoid multiple IotIgniteHandler instances, you can use the singleton pattern.
To use IoTIgniteHandler in MainActiviy, you will get an instance of it and start it.
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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * Set IgniteHandler and start. */ mIotIgniteHandler = IotIgniteHandler.getInstance(getApplicationContext()); mIotIgniteHandler.start(); } }
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,