wiki:2012/Projects/ASL/Tutorial1

Version 8 (modified by xianyi, 5 years ago) (diff)

--

Tutorial 1: A Simple Use of the Library

Prerequisites

Required Downloads

Before beginning and using this tutorial, you will need the following fundamental tools:

Follow all necessary instructions on those respective websites as to how to install each tool individually.

And of course, you will need:

Required Code

Any use of the library will require that the following permissions are set in the AndroidManifest.xml file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

And the following services need to be placed under the <application> header in AndroidManifest.xml:

        <service
            android:name="winlab.file.RunningService"
            android:enabled="true" />
        <service
            android:name="winlab.sql.Sensors_SQLite_Service"
            android:enabled="true" />
        <service
            android:name="winlab.sensoradventure.gps.GPSloggerservice"
            android:enabled="true" />


Using the Library

Start off by opening Eclipse and creating a new Android project.

Give the project a name. We will be using "Tutorial1" as both the project & application name. Place this under the winlab.tutorial package.

Press next after this.

At the time of this tutorial writing, the Eclipse used here did not contain templates for BlankActivity?. If you are using Eclipse and it autocreates the Activity and the manifest you can skip the next few steps but if not, it's a simple enough fix.

So given the current problems, uncheck "Create Activity" and just hit "Finish".

Again, if Eclipse can autocreate your activity file without errors then you can skip this next step. Expand "Tutorial1" in the Package Explorer on the left hand side. Then, right click the "src" folder and navigate to New -> Class and click on it.

We will give it the name "Tutorial1Activity".

Don't forget to place its package under winlab.tutorial.tutorial1 like we did when we created this project.

Click "Finish" and move on.

Afterwards, we will be introduced to our class. Since we want to write a simple executable application to test how the library works, we need to first modify this class and make it "Android ready".

Quickly rewrite the code like below to finish our manual creation of the Activity for the tutorial:

package winlab.tutorial.tutorial1;

import android.app.Activity;
import android.os.Bundle;

public class Tutorial1Activity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.main);

	}

}

Don't worry about any errors. Like I mentioned before, this is still due to the fact that Eclipse can't (or won't?) autogenerate an activity file.

Right now, the class should appear as below:

The main.xml file doesn't exist and neither has the activity been set as the main under the AndroidManifest?.xml. Take this time now to insert the permissions and services from above as well as set Tutorial1Activity as the activity to execute when the program runs. Double click "AndroidManifest?.xml" to open it up:

Click on "AndroidManifest" in the bottom right corner. This will bring up the XML editor.

The current code needs to be changed to the one seen below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="winlab.tutorial.tutorial1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Tutorial1Activity"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="winlab.file.RunningService"
            android:enabled="true" />
        <service
            android:name="winlab.sql.Sensors_SQLite_Service"
            android:enabled="true" />
        <service
            android:name="winlab.sensoradventure.gps.GPSloggerservice"
            android:enabled="true" />
    </application>

</manifest>

Where we have added Tutorial1Activity as an activity under application, the required services under application, and the permissions used by the library.

Save this and exit out.

Now we need to import the library. Assuming the library has been saved onto the computer, right click "Tutorial 1", go to Build Path->Add External Libraries:

ASL.jar should appear under "Referenced Libraries" as seen below.

With the library added to the project, we can move onto designing a simple two button GUI to test to see if we can use it to log data from the Android device.

Underneath res->layout, we will right click layout and go to New->Android XML File:

Select a LinearLayout?, name the file "main", and click Finish:

Drag and drop two Buttons onto the Layout:

Go to the main.xml at the bottom of the editor and change the xml as below:

Close main.xml and return to the main activity. We will now implement the main activity:

package winlab.tutorial.tutorial1;

import winlab.ASL.AndroidSensors;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Tutorial1Activity extends Activity {

	private Button start, stop;
	private AndroidSensors androidSensors;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.main);

		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);

		start.setOnClickListener(startClick);
		stop.setOnClickListener(stopClick);
		stop.setClickable(false);

		androidSensors = new AndroidSensors(this);
		androidSensors.setAccelerometer(true);
		androidSensors.writeToFile(true);
		androidSensors.setAccelerometerRate(1);
		androidSensors.prepareForLogging();

	}

	private OnClickListener startClick = new OnClickListener() {

		public void onClick(View arg0) {

			androidSensors.startLogging();
			start.setClickable(false);
			stop.setClickable(true);

		}

	};

	private OnClickListener stopClick = new OnClickListener() {

		public void onClick(View arg0) {

			androidSensors.stopLogging();
			stop.setClickable(false);
			start.setClickable(true);

		}

	};

}

The library is introduced by the class AndroidSensors?. To begin logging one sensor, we set the accelerometer to "true"; we set its update rate = 1ms, and we prepare the library for logging.

Very simple application. Once it

Attachments (16)

Download all attachments as: .zip