android_lecture_2012 (ppt 374 kB)

advertisement

Welcome to

EQ2430/EQ2440 Android Lecture

- a crash course in Android

March 21, 2012

Per Zetterberg

School of Electrical Engineering

1

The project course goes Android!

Features used in our projects:

• Display : plots and text.

• Microphone.

• Loudspeaker.

• Magnetometer measurements.

• WiFi RSSI measurements.

• Send data using WiFi.

• Accelerometers.

• Touch screen.

2

FrameWork

• FrameWork is a java project (program) in

Eclipse.

• FrameWork is a Android 2.2 project that runs (at least) on HTC Desire.

• FrameWork is a generic skeleton which can be used as the starting point for many student projects (including yours!).

• FrameWork has an object StudentCode which is where you implement your prototype .

• Application name: StudentFrameWork

(Change this)

• Creators: Martin Ohlsson and Per Zetterberg

3

FrameWork: User interface

PlotView:

Graphs and/or

Camera picture

TextView:

Textual output.

4

Downloading FrameWork

Go to

EQ2430 web => project, android and usrp support =>

Android support

Download: FrameWork_empty.zip

”Empty” because most StudentCode functions are empty

…. or almost empty.

Unzip and move the code to workspace directory.

Do “file->import->”existing project into workspace”->

5

StudentCode (i.e your code)

Parameters: useSensors, loggingOn, useMessaging, messageServer, processInterval, introText,

messageGroups, StudentMessage (class) useSensors (bitfield): “xor” the following

GPS,CAMERA,SOUND_OUT,SOUND_IN,WIFI_SCAN,

ACCELEROMETER,MAGNETIC_FIELD,PROXIMITY,TIME_SYNC

, PROXIMITY,LIGHT,SOUND_IN,CAMERA, GYROSCOPE

6

Called by StudentCode 1(2)

sound_out(short[] buffer, int length): Send data to

DAC=>loudspeaker (somewhat stochastic latency).

message_out(StudentMessage message): Send message to all the other phones.

message_out(StudentMessage message, String

groupIdString): Send message to the phones in group defined by “groupIdString.

streaming_buffer_out(short[] buffer, int length,

String groupIdString): Send buffer of int16 to the phones in group defined by “groupIdString”.

7

Called by StudentCode 1(2)

set_output_text(String text): Set the text too be displayed in TextView.

plot_data(Canvas plotCanvas, int width, int

height): Make a drawing in PlotView. plot_camera_image(Canvas plotCanvas, byte[] image, int imageWidth,int imageHeight, int width,

int height): Display a camera image in PlotView.

write_string_on_logfile(String): No ”\n” or ”;”.

load_sound_file(String fileName) +

play_sound_file(int loop): Repeatedly plays the wav-file specified by fileName. No timing gitter.

stop_sound_file(): Stops the above.

8

You fill these functions

with code!

Defined by StudentCode:

Called by FrameWork 1(2)

init(): Do all your initialization.

start(): Called when menu button ”start” is pressed.

stop (): Called when menu button ”stop” is pressed.

process(), Periodic processing

message_in(…) Received message from other phone.

gps(…) …… position estimate

magnetic_field(…) …… magnetic field measurement

light(…) : …. Light intensity (lux) event (trigger when changed)

proximity(…): Proximity state event (trigger when changed)

accelerometer(…) accelerometer measurement

9

All are inputs except plot_data!

Defined by StudentCode:

Called by FrameWork 2(2)

gyroscope(…) gyroscope measurement

sound_in(…) sound input samples

wifi_ap(…) ... signal strength of surrounding WiFi APs

screen_touched(…) position event

streaming_sound_in(…) buffer of samples (int16) received from other phone

camera_image(…) image received from camera

You fill these functions

with code!

10

Threads (internals)

“Parent thread”: Indirectly calls init().

studentProcessThread: Calls process().

ipListenerThread: Calls message_in()

guiTriggerThread: Renders what you provide with set_output_text() and plot_data().

t1: Calls sound_in(…)

wifiScanThread: calls wifi_ap(…)

ipSoundListenerThread: calls streaming_buffer_in

FrameWork implements OnRecordPositionUpdateListener:

Calls gps()

FrameWork implements SensorEventListener accelerometer(), magnetic_field(), light(), proximity()

FrameWork extends Activity: sreen_touched() streaming_sound_in(…) buffer of samples (int16) sent from other phone

11

How communication between phones works 1(2)

• A Linux computer connected to internet acts as a server (the address of which is given by messageServer).

• When FrameWork starts, it contacts the server and asks for the ip adress of one of the other phones in the project (a project is defined by the projectName string e.g. ”yellow”).

• The phone then contacts that phone and gets a list of all other phones in the project.

• When one phone calls message_out(message) the message is sent to all other phones in the project.

• The other phones receives the message through a call of message_in (”callback”).

• The message is of type StudentMessage and contains the fields that you decide.

12

How communication between phones work 2(2)

• Second form of message_out: message_out(message, groupIdString)

• Where groupIdString defines a group of users e.g.

”data_receivers” which should receive the message.

• Which groups exist ? You define the groups with the

”messageGroups” parameters.

• When FrameWork starts, it asks the user which group the phone belongs to.

• When sending buffers of int16 use the following version: streaming_buffer_out(buffer,length, groupIdString)

• This data is received through: streaming_buffer_in( buffer, int length, senderId)

13

How to use communication between phones

• Set useMessaging = true;

• If you need groups:

• String temp[] = {"MICROPHONE","SPEAKER"};

• messageGroups = temp;

• Otherwise:

• messageGroups = null;

• messageServerType = LINUX_MESSAGE_SERVER;

• messageServer = "192.168.0.68";

• Define StudentMessage (defined in

StudentMessage.java)

Which message group do this phone belong to ?: messageGroups[myGroupID]

14

Logging on file

• loggingOn = true;

• Creates a measurement file called sensorlogYYYYMMDDHHMM.csv on the SDCARD.

• Copy file to PC-harddisc (matlab command): copy_file_from_sdcard_to_working_directory.m

• Obtain the data from the log-file into matlab: get_log_data_from_FrameWork.m

• Extract data from some certain sensors: extract_acceloremeter_from_log_data.m, extract_sound_from_log_data.m, extract_magnetic_from_log_data.m

• Other sensors:

Do it yourself.

15

All, non-trivial functions should be tested like this.

Test harness

Purpose:

Test implementation of math-intensive functions/algorithms in an isolated and repeatable way.

Method:

Run your matlab- and java- (with android environment) implementations side by side with exactly the same input .

How?

• Matlab produces data, and pushes it on the sdcard

• FrameWork includes a function called ”test_harness()”.

• Inside this function the data can be read from the sdcard, the function to be tested is called, and the results are saved on the sdcard.

• Matlab then pulls the data from sdcard.

• Finally, the results from the matlab- and java-implementation are compared.

What do I need to do ?

The example test_harness_example.m shows how to automate the process. This code can be used as a starting point, however, you need to customize the code to the function you are going to test.

16

State-machine and functions

{ public void process() { switch(State) { case START: res1=func1(arg1,arg2);

res2=func2(signal1);

State=WAITIING; break; case WAITING:

Functions that can be tested against matlab implementation using harness

} sync=func3(arg1,res2); if (sync) {State=RUNNING; transmit(signal2)}

else {State=WAITING; transmit(signal1)}; break; case RUNNING:

.....

} write_string_on_logfile("State="+State);

Example of debugging method.

public void message_in(StudentMessage message)

{ switch(State) { case WAITING:

....

}

17

Give your App a unique identity

• Right click on project folder (Top level folder named

FrameWork) within the Package Explorer window

• Select Android Tools/Rename Application Package

• Add a unique name after FrameWork like: se.kth.android.FrameWork.GroupRed5

• Press Finish in the Refactoring window

• Confirm update of Launch Configurations

• Voila, you now have a separately installable version of your app!

18

Assignments

1.

Write letter ”A” and ”B” in plot_data using “draw” functions.

2.

Switch between the letters of your name every two seconds.

3.

Send a dot between two phones i.e. one user pushes the screen on one phone, and a dot appears on the other users screen on the place where the user put her/his finger.

4.

Record sound and play it again – but delayed 10 seconds.

5.

Record sound on one phone and play it on the other.

6.

Make a test-harness for a non-trivial algorithm which is relevant for your project.

7.

Print the orientation of the phone in the textView.

8.

Record sound an play it with two echos.

Deadline : Mid-term evaluation.

19

Tips

• Start command line prompt in windows 7: press windows_button+”r”, then run “cmd.exe”.

• The tool “adb” is located at C:\android-sdkwindows\tools\

• Android software developers kit: http://developer.android.com/sdk/index.html

• Killing tasks. Download and install “Advanced Task

Killer Free (ATKF)”. To kill e.g. “StudentFrameWork” open ATFK press “StudentFrameWork” until a new menu appears where you can select “Kill”

• Error message: “ ActivityManager: Warning: Activity not started, its current task has been brought to the front ”

=> Change a single line in the source files code and try again.

20

Now

Lets start!

21

Download