Sunteți pe pagina 1din 13

SWAN Tutorial

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)

Setting up SWAN-SENSE in Eclipse


• Import Project from Git
• Select git repository SWAN-SENSE[1]

• Select branch
• Location to clone the git repository

• Use New Project Wizard and click finish


• Select Android project from existing code

• Locate the imported git project


• This will show google play service error

• Import google-play-service project


• Add both sense-android-library and google-play-service as a reference to SWAN

• SWANExpressionParser error might show up


• Run both buildgrammar.ant and buildjar.ant as Ant build - To fix SWANExpressionParser error

• Now, do a clean build. Eclipse setup is ready.

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 in Phone - Create an account in SENSE(https://accounts.sense-os.nl/).You need to login


to SENSE to use SWAN.
• SWAN Monitor app in Phone - Adding Movement/Accelerometer sensor

• 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.

Accelerometer app using SWAN


Link: https://github.com/swandroid/AccelerometerApp/

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.

Sensor Expression [location]@[entity]:[path]?


[configuration]{[mode],[window]}
Math Expression [Value Expression] [location]@[Math
Operator] [Value Expression]
Comparison Expression [Value Expression]
[location]@[Comparator][Value
Expression]
Logic Expression TriState Expression]
[location]@[Logic Operator]
[TriState Expression]

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.

There are two possibilities of retrieving the results:


• Registering a broadcast receiver with action intent “EXTRA_NEW_VALUE”
• Implementing a listener and passing it as the 4th parameter in the register method, similar for
both types of expressions (value or tristate): 

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

In case of any queries, please contact:

Roshan Bharath Das : roshanbharathdas@gmail.com

Vladimir Bozdog : vladimir.bozdog@gmail.com

S-ar putea să vă placă și