Implementing DataKit in an Android Application

Contents

Setting Up the Development Environment

Android Studio is the recommended IDE for working with MD2K code. Download Android Studio here.

A Demo application is available on GitHub. You will need to install DataKitAPI on your development device as well.

After installing Android Studio, start a new project. Make note of the project location. Advance through the project setup steps, then open a terminal and navigate to the directory where the project is located and run the following command:

Gradle will prompt you to sync the project. If you are prompted to install any missing platforms, please do so. Once gradle is building without any errors or prompts, you are ready to start developing.

Implementing DataKit

DataKit has five main functions: connect, register, subscribe, insert, and query. These functions are somewhat interdependent. If DataKit has not been connected, for example, all other calls to it will fail.

DataKit can be disconnected using the same code shown above. Note: It is good practice to make sure that all DataSources are unsubscribed and unregistered before disconnecting DataKit. Not doing so can cause errors that can be rather difficult to track down. The demo application handles unsubscribing and unregistering in their own methods.

Register

The next function is registering a data source. Data sources are constructed from sensors. The demo application uses an accelerometer, but any sensor data can be used, including hardware-defined sensors and software-defined sensors.

To use a sensor for a data source, Android's Sensor, SensorManager, SensorEvent, and SensorEventListener classes must be imported. The class initializing the sensor must also implement SensorEventListener. The demo application initializes a SensorManager and Sensor instance variable upon creation. These variables can be initialized later as well.

After the data source is built, it can be passed to DataKit for registration as shown below. Note: Data source registration is not the same as registering a SensorEventListener for the corresponding sensor. Data source registration registers a data source with DataKit. SensorEventListener registers a listener for a sensor with Android.

When unregistering a data source, it must also be unsubscribed and its sensor's listener must be unregistered. If unregistering multiple data sources, the data sources should be stored in an arraylist and iterated through, calling datakitapi.unregister() on each one individually. For example:

Subscribe (optional)

Subscribing a data source registers a callback interface with DataKit that returns data as it is received by the database.

The code block below contains the logic for the subscribe button. An arraylist of DataSourceClient objects is created because DataKitAPI.find() returns an arraylist of matching DataSourceClient objects. After finding the desired data sources, the arraylist is iterated through and an OnReceiveListener is registered for each one individually.

The codeblock below is called whenever the accelerometer has new data. Gravity is divided out and then the data is inserted according to the UI switch. Implementing SensorEventListener requires two overridden methods, onSensorChanged() and onAccuracyChanged(). Only onSensorChanged() is used in the demo application, onAccuracyChanged() is left as a blank method.

The query functions are fairly straight forward. find() is used as before to find available data sources. The difference here is that the data source is built with an application attribute. This attribute represents the application making the method call.