In this tutorial, we’ll use AWARE as a built-in library and not require AWARE to be installed beforehand. This is ideal to create applications that leverage AWARE’s sensors’ and plugins’ APIs to reduce development time. This tutorial is split in four sections:

  1. Creating a new application using Android Studio project wizard
  2. Adding AWARE as a library to your project
  3. Overriding AWARE’s resources with your application’s resources (application icon and application style/theme)
  4. Include AWARE’s ContentProviders inside your application
  5. Include AWARE’s plugins inside your application

1. Creating a new application using Android Studio project wizard

Create a new project, following Android Studio’s wizard. Fill in your application’s name, company domain and package name with anything you wish. Since this is a standalone application, we do not need to make our application visible within AWARE’s client because we assume the AWARE client is not installed at all.

Do not use “com.aware” as a domain or package name, because that will interfere with the functionality of AWARE’s API

tut-app-1

Note that AWARE is compatible with Android 2.3.3 (Android API 10) or higher, so make sure that the minimum SDK level is set to 10, as in the following screen:

tut-app-2

Choose the default activity type (i.e., how you will implement your application interface). In this example, we’ll create a Blank Activity.

tut-app-3

Give a name to your application’s activity and title (this will be visible on the application’s launcher and toolbar):

tut-app-4

Press Finish. Android Studio will create the project and open it. By default Android Studio shows you the main interface of your application in the Layout Manager. If you go to Run > Run app, the application will be installed on your phone and you’ll see this interface on your device. This concludes the first section.

2. Adding AWARE as a library to your project

We’ll now add AWARE as a dependency to your project. To accomplish this, we right-click your project folder inside Android Studio (on the left) and choose “Open Module Settings“:

tut-app-5Choose the tab “Dependencies” on the right:

tut-app-6

Press the [+] button on the bottom of this window to add a new “Library dependency“:

tut-app-7A new window appears where you can search for libraries. We’ll need to add: awareframework, Google Play Services and Cardview, as follows:

tut-dep-aware tut-dep-cardview

tut-dep-play-services

 

 

 

 

 

 

 

 

At end, you should have 3 dependencies:

tut-dep-final

At this point, Android Studio will add all the projects’ as a dependency to your application.

3. Overriding AWARE’s resources with your application’s resources (application icon and application style/theme)

 

We now need to make a minor change to the build.gradle file within your app module (double click to open it):

tut-gradle-change

Modify the dependencies section so that it looks like this:

repositories {
 maven { url 'https://oss.sonatype.org/content/repositories/releases/' }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.awareframework:aware-core:3.6.4@aar'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.android.support:cardview-v7:23.0.1'
}

Note the com.awareframework:aware-core:3.6.4@aar. Making this change will make Android Studio always check for the latest version of AWARE from JCenter (replace the 3.6.4 with the current versions) and include the source and resources of AWARE within your project as an Android Archive (@aar). Also make sure you are using the latest versions of the other libraries.

At this point, Android Studio will ask you to sync and rebuild your project. Do it. At this point, you’ll see an error that comes from merging AWARE’s manifest and your application’s manifest. This is normal and expected:

tut-app-manifest-error

In a nutshell, the compiler is complaining that the manifests have conflicting attributes. What is happening is that AWARE’s client has its own icon and theme (how we styled the client to look the way it does when you have it installed). In our case, we want to make sure Android uses your application’s icon and theme. To fix this, you need to edit your application manifest (which should be open automatically when Android Studio reports the error) and override the conflicting elements. Fortunately, Android offers a set of tools that we can use to handle this. Edit the following line on your application’s manifest to include the XML namespace tools, like so:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package=[your application package name] >

Then we’ll tell Android to override AWARE’s icon and theme, by adding the following line (tools:replace=”android:icon, android:theme”) to your application tag, like so:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    tools:replace="android:icon, android:theme">

Now when you do Build > Rebuild project, you’ll have a successful build.

tut-app-rebuildAt this point, your app should compile and you are ready to use AWARE’s API. We’ll now let our application know about AWARE’s ContentProviders.

4. Include AWARE’s ContentProviders inside your application

In this last step, we will add AWARE’s sensor’s ContentProviders (where the data is stored) to our application. By doing so, we no longer need to have AWARE Client installed in the phone when you build an application!

Always check GitHub’s client source for an up-to-date list of the providers (in case we add more) to include on your application, here.

Edit your application manifest to include AWARE’s ContentProvider nodes, like so:

<provider
 android:name="com.aware.providers.Scheduler_Provider"
 android:authorities="${applicationId}.provider.scheduler"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
<provider
 android:name="com.aware.providers.Bluetooth_Provider"
 android:authorities="${applicationId}.provider.bluetooth"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Applications_Provider"
 android:authorities="${applicationId}.provider.applications"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Locations_Provider"
 android:authorities="${applicationId}.provider.locations"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Installations_Provider"
 android:authorities="${applicationId}.provider.installations"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.ESM_Provider"
 android:authorities="${applicationId}.provider.esm"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Temperature_Provider"
 android:authorities="${applicationId}.provider.temperature"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Linear_Accelerometer_Provider"
 android:authorities="${applicationId}.provider.accelerometer.linear"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Gravity_Provider"
 android:authorities="${applicationId}.provider.gravity"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Barometer_Provider"
 android:authorities="${applicationId}.provider.barometer"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Magnetometer_Provider"
 android:authorities="${applicationId}.provider.magnetometer"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Proximity_Provider"
 android:authorities="${applicationId}.provider.proximity"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Light_Provider"
 android:authorities="${applicationId}.provider.light"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Telephony_Provider"
 android:authorities="${applicationId}.provider.telephony"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Aware_Provider"
 android:authorities="${applicationId}.provider.aware"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Rotation_Provider"
 android:authorities="${applicationId}.provider.rotation"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.WiFi_Provider"
 android:authorities="${applicationId}.provider.wifi"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Accelerometer_Provider"
 android:authorities="${applicationId}.provider.accelerometer"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Gyroscope_Provider"
 android:authorities="${applicationId}.provider.gyroscope"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Traffic_Provider"
 android:authorities="${applicationId}.provider.traffic"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Mqtt_Provider"
 android:authorities="${applicationId}.provider.mqtt"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Processor_Provider"
 android:authorities="${applicationId}.provider.processor"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.TimeZone_Provider"
 android:authorities="${applicationId}.provider.timezone"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Battery_Provider"
 android:authorities="${applicationId}.provider.battery"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Screen_Provider"
 android:authorities="${applicationId}.provider.screen"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Network_Provider"
 android:authorities="${applicationId}.provider.network"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Communication_Provider"
 android:authorities="${applicationId}.provider.communication"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>
 <provider
 android:name="com.aware.providers.Keyboard_Provider"
 android:authorities="${applicationId}.provider.keyboard"
 android:exported="true"
 android:readPermission="com.aware.READ_CONTEXT_DATA"
 android:writePermission="com.aware.WRITE_CONTEXT_DATA"
 >
 </provider>

The ${applicationId} allows the ContentProvider to be recognised and used within your application, by dynamically replacing the database authority that matches your application.

5. Include AWARE’s plugins in your application

We don’t have the plugins bundled as a library on MavenCentral – the same way as the client is packaged and ready to import using Gradle – which means a little bit of code editing, but it’s relatively simple.

  • Download the plugin’s source you are interested in from GitHub. You have a link to each plugin’s source on their respective documentation pages.
  • Import the plugin as a new module for your application.
  • Make the plugin module a library by editing the module build.gradle:
apply plugin: 'com.android.library'
defaultConfig {
//applicationId 'com.aware.plugin.xxx' //comment the application ID
...
}
  • Add the plugin module as a dependency on your application.
  • If the plugin has ContentProviders, move the <provider> tags on the plugin’s AndroidManifest.xml to your application’s AndroidManifest.xml. You don’t need to move the Provider.java files. This is necessary to avoid conflicts with the ContentProvider authority in case you have the client and the plugin already installed. Also, this only applies to the ContentProviders, because they need to be unique on the phone. By copying the ContentProvider to within your application’s manifest, the authority gets assigned to your application package name, so no conflicting authorities.

You are now able to request AWARE to activate the plugin that is bundled on your application with Aware.startPlugin(context, package_name). That function checks if there is a bundled version of the plugin on your app and starts it.

Ready!

You can now use AWARE in your application. For example, if you wish to ask AWARE to collect accelerometer data, on your Activity (or Service) you do:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 //Initialise AWARE
 Intent aware = new Intent(this, Aware.class);
 startService(aware);
 //Activate Accelerometer
 Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);
 //Set sampling frequency
 Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 200000);
 //Apply settings
 Aware.startSensor(this, Aware_Preferences.STATUS_ACCELEROMETER);
 }

See the individual sensors and plugins documentation pages to know their settings before asking AWARE to turn them on. Every time you change the settings, calling Aware.startSensor(context, package) will apply the new settings if the sensor is already active. This concludes this tutorial. Feel free to ask questions on the discussion board!

 

 

Creating a standalone application