Dynamix 1 Android App Quickstart

//Dynamix 1 Android App Quickstart
Dynamix 1 Android App Quickstart 2017-09-29T23:38:59+00:00


Need rich context information in your next Android app? What about enhanced interactions with nearby smart devices and sensors? With Dynamix, you’ll have access to a rapidly growing collection of powerful context plug-ins, which can handle a variety of sensing and actuation tasks. Native Android apps can fluidly interact with the physical world using Dynamix’s AIDL connector, which provides simple interfaces for service binding, API interactions, data serialization/deserialization, error handling and event processing. The following sections describe how to integrate Dynamix into your next Android project.


Download Dynamix!

Grab the latest Dynamix Framework, Dynamix SDK and Dynamix Logger example app, which is referenced throughout this tutorial.

Native App Integration

After downloading the SDK, extract the included Dynamix API JAR file and include it in your Android project, making sure it’s listed on your build path and exported. If you’re using Eclipse, simply drop the Dynamix JAR into your project’s “libs” folder and you’re done! You should also download any needed plug-in context data type JARs and include them on your build path as well, making sure that the Dynamix API JAR and any context data type JARs are exported with your project. When using Eclipse, make sure all JARs are checked under “Order and Export”, as shown below:


If you’re new to Android development, take a look at Building your First App on the Android Developers site.

Dynamix Callback

To receive Dynamix events in your app, create a Dynamix callback using an instance of the IDynamixListener.Stub as follows:

Bind to Dynamix

To connect your app with the local Dynamix service, begin by creating a ServiceConnection object as follows:
Notice that we are adding the app as a Dynamix listener after the¬†ServiceConnection is bound to Dynamix, which is indicated when the ‘onServiceConnected’ event is raised.
Next, bind your app to the Dynamix service using the ServiceConnection as follows:
Once the Dynamix service is connected, the ServiceListener you created above will receive an “onServiceConnected” event. At that point, use the “IDynamixFacade.Stub.asInterface” method to create an instance of “IDynamixFacade” (shown above). You’ll use this instance to communicate with Dynamix.

Create a Dynamix Session

After your app is bound with the Dynamix service, open a Dynamix session as follows:
Dynamix responds to open session requests as follows:

Add Context Support

Once your app has an open Dynamix session, it’s time to install context support using Dynamix plug-ins! Start by browsing the available Dynamix plug-ins here, noting the plug-in identifier and/or the context type you’re interested in using. A “context type” is a specific kind of ambient functionality provided by a Dynamix plug-in, such as performing a particular sensing task or performing a particular remote interaction with a remote device. Note that some plug-ins support multiple context types. For additional information about Dynamix context support, check out the Dynamix overview.

You can add context support by specifying the plug-in id and context type as follows (battery level detection):
 You can also add context support by specifying only the context type as follows:
When only specifying context type, Dynamix will attempt to discover and install appropriate plug-ins to handle the request. In this case, more than one context plug-in may be installed.
You can remove previously installed context support as follows:
Or Or
Dynamix responds to add/remove context support requests by raising events as follows:

Handle Context Interactions and Events

After context support is installed, your app may need to interact with the associated Dynamix plug-in to trigger context sensing and/or remote device interactions. Each plug-in provides its own set of context events and (optionally) an API for controlling its functionality (see the plug-in’s documentation for details). Some plug-ins run autonomously in the background broadcasting events as needed, whereas others require the app to trigger sensing or actuation. For an overview of the available plug-in types, click here.

The battery level sensor plug-in used in this example supports two modes simultaneously: autonomous and reactive. Since the plug-in supports autonomous mode, it will continually sense the device’s battery level and send events to the Web app whenever the batter level changes. Since the plug-in also supports reactive mode, the plug-in can also query the batter level on-demand as shown below.

Battery level context request (reactive mode):
Context information is sent from plug-ins to apps as events, which arrive at the¬†onContextEvent handler of the IDynamixListener.Stub created above. Incoming context events are automatically populated with data fields that are specific to the plug-in (or context type) used (see the plug-in’s documentation for details). Developers can access context information using simple ‘dot notation’, rather than parsing complex string data.

Handle incoming context events as follows:

API Reference

For details on all of the methods, events and data types available to apps, see the “org.ambientdynamix.api.application” package in the JavaDocs: