Documente Academic
Documente Profesional
Documente Cultură
In this section we will discuss how to setup SWAN integrated with SENSE in eclipse and use
‘SWAN Monitor’ app to check if SWAN is working properly. We will also discuss how to write a
simple accelerometer application using SWAN.
Prerequisites
- Java (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
- Ant(http://ant.apache.org/)
- Eclipse with ADT and Java development plugin(http://developer.android.com/sdk/index.html).
Please not that we don’t have support for android studio yet.
Download Path
- SWAN-SENSE[1](https://github.com/swandroid/swan-sense). This contains two projects:
- interdroid-swan-master (https://github.com/swandroid/swan-sense/tree/master/interdroid-
swan-master)
- sense-android-library (https://github.com/swandroid/swan-sense/tree/master/sense-
android-library)
- Dependencies
- google-play-services (check https://developer.android.com/google/play-services/setup.html to
see how to add Google Play services to your project)
- SWAN Monitor[2](https://github.com/interdroid/swan-monitor)
• Select branch
• Location to clone the git repository
Testing SWAN
In order to test SWAN, we need to install SWAN in the smartphone and an application that uses
SWAN.
• Installing SWAN
• Install SWAN Monitor app[2] - Since SWAN runs as a service, it would be useful to install
SWAN Monitor app to check whether SWAN runs correct. SWAN Monitor app will interact with
SWAN to get the result from sensors.
• SWAN Monitor app in Phone - Setting preference for movement sensor and the getting result
from movement sensor
The above result shows that SWAN is running in the background. Next we will discuss how to write
a simple accelerometer application using SWAN.
In this section we will explain how to build a simple sensor based application using SWAN.
While setting up a android project in eclipse, please make sure to add the files in swan-sense/
interdroid-swan-master/swan-in-your-app to current projects libs directory as shown below.
In order to get SWAN running, the app must register a SWAN-Song expression. In the
accelerometer app the SWAN-Song expression by default will be as follows:
self@movement:x?accuracy=‘0’{ANY,0}
where the expression follows the rules similar to Sensor Expression as shown in the below
table.
The above expression in our app is created on starting a configuration activity provided by SWAN.
Every sensor has its own configuration setting. Alternatively we can create our own expression
following the rules specified in the table. Once the expression is ready, it needs to be registered in
SWAN.
The ExpressionManager class offers a public API for registering and unregistering expressions, as
well as querying for existing sensors. Once an expression is registered, SWAN will notify the app
by sending a broadcast message every time the result of the expression evaluation changes.
The accelerometer app we built will register accelerometer based expression in SWAN and shows
the sensor value as the result. It uses the second approach where the ExpressionManager will
handle receiving the broadcasted message with the new values and forwarding it to the app by
calling the associated listener.
ExpressionManager.registerValueExpression(this,
String.valueOf(REQUEST_CODE),
(ValueExpression) ExpressionFactory.parse(myExpression),
new ValueExpressionListener() {
/* Registering a listener to process new values from the registered
sensor*/
@Override
public void onNewValues(String id,TimestampedValue[] arg1) {
if (arg1 != null && arg1.length > 0) {
String value = arg1[0].getValue().toString();
tv.setText("Value = "+value);
} else {
tv.setText("Value = null");
}
}
});
Contact