Sense for Android apps

Contents

  1. Introduction
  2. Set up the library in Eclipse
  3. Using the library in native Android apps
  4. Using the library in web apps with PhoneGap
  5. Troubleshooting

1. Introduction

This tutorial explains the usage of the Sense Platform Library. This library can be used to include the Sense Platform into Android apps. It allows an application to configure the sensors of the phone (e.g. location, motion and phone state) and store or access the sensor data on the CommonSense web platform.

Prerequisites

  • A working Android development installation. This can be either Eclipse plus the ADT plugin, the ADT Bundle, or even the Android Studio. See the Android Developers website for an installation guide.

2. Set up the library in Eclipse

The Sense library project can be found at our GitHub repository. There are two ways to import the code: you can use the EGit plugin for Eclipse (recommended), or you can checkout the code using some other git client.

Import using EGit

Select import method

  1. In Eclipse, go to File > Import... Choose GIT > Projects from Git

Select repo location

2. Select URI

Enter repo details

3. Enter the git repository for this project: https://github.com/senseobservationsystems/sense-android-library.git

Select branch

4. Select the branches you need. The most stable code is found in the master branch.

Enter clone destination

5. Enter the location you want to clone the repository to, usually in your workspace.

Select import method

6. Pick the method to import the project. Select Use the New Project wizard.

Select Android wizard

7. Select Android Project from Existing Code.

Enter project details

8. The wizard should find three projects: the library itself, and two sample projects. Press Finish to import the projects into your workspace.

Library project in Eclipse

9. Your library project should look something like this.

You can now continue to the next section to Configuring your project to use the library.

Import using other Git clients

  1. Clone the source code from GitHub repository. The most stable code can be found in the master branch.
  2. In Eclipse, start the new project wizard using 'File -> New -> Project...'
  3. Continue by following the EGit tutorial starting at step 7.

Configuring your project to use the library

  1. Right-click on your own Android project that will supply the GUI and select Properties.
  2. Go the the Android pane, and click "Add" in the Library section and add the Sense Library Project.
  3. Press OK to apply the changes.

Demo project in Eclipse

Your main application project should look something like this.

3. Using the library in native Android apps

The library repository includes a sample project to help you get started with creating a your own Android app using Sense. You can find this sample in the samples/sense-android-demo/ folder. We recommend that you use this project as a starting point for your own app. If you follow the Eclipse Setup Tutorial this project will be automatically created in your workspace.

To get a good overview of the library start by reviewing the MainActivity.java class. The demo project covers the following topics.

  • Binding with the Sense service
  • Logging in
  • Configuring and starting the sensor modules
  • Adding your own sensor data
  • Getting sensor data from CommonSense
  • Setting up AndroidManifest.xml

4. Using the library in web apps with PhoneGap

PhoneGap is an opensource framework to write Android applications using HTML, CSS and Javascript. For more information go to the PhoneGap website.

The library repository also includes a sample project to help you get started with creating a PhoneGap-based app that uses Sense. You can find this sample in the samples/sense-android-phonegap/ folder. We recommend that you use this project as a starting point for your own app. If you follow the Eclipse Setup Tutorial this project will be automatically created in your workspace.

You can put your HTML and JavaScript in the assets/www/ folder inside the project. Make sure that you also load the sense_platform.js script that contains the JavaScript part of our PhoneGap plugin.

  1. Load the sense_platform.js file in your HTML. You can find in inside the assets/www/ folder.
  2. The Sense platform methods are now accessible in JavaScript through window.plugins.sense.*.
  3. When starting you app, make sure that you call window.plugins.sense.init() to initialize the plugin. A good place for this would be right after the deviceready event that PhoneGap features.

5. Troubleshooting

Dependency conflicts

After adding the Sense library to your own project, Eclipse might complain about dependency conflicts, e.g.:

Found 2 versions of android-support-v4.jar in the dependency list

You can get rid it this error by removing the conflicting jar file from the libs/ folder in either one of the two projects.

Compilation problems

We have had reports of people having trouble building their Android projects. The Android logcat shows messages like:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nl.sense.demo/nl.sense.demo.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "nl.sense.demo.MainActivity" on path: /data/app/nl.sense.demo-1.apk

This is not a problem with the Sense library per se, but it seems to be caused by using the Oracle JDK 7. Switching to a different JDK resolves the problem e.g. OpenJDK 6.