36 2. ANDROID SOFTWARE DEVELOPMENT TOOLS
Figure L1.12: HelloWorld.c.
int classNum = 9001;
int secNum = 1;
__android_log_print(ANDROID_LOG_ERROR, "HelloWorld",
"DSP %d.%03d", classNum, secNum);
e __android_log_print() method (two underscores at the beginning) is similar to the
printf function in C. e first two parameters are the log level and the message tag. e logging
level is the priority of the message, the list of which can be found in the android/log.h header file.
e tag is used to help identify the source of the message; in this case the name of the app. e
next parameter is the message to be logged. For the above example, the string has the specified
integer for the class number inserted, followed by the specified integer for the section number.
e same number formatting that is possible when using the printf function may also be used
here. For instance, the section number can be formatted to three characters width with leading
zeros. Variables are last and are inserted with the formatting specified in the message string in
the order they are listed.
Save the changes made to the HelloWorld.c source file and run the app again. is time,
Android Studio should automatically open the Android DDMS window and show the LogCat
screen. e message DSP 9001.001 would appear in the listing if the previous procedures were
performed properly (see Figure L1.13).
L1. LAB 1: GETTING FAMILIAR WITH ANDROID SOFTWARE TOOLS 37
Figure L1.13: LogCat screen.
L1.1 LAB EXERCISE
Write a C function within the above Android shell to implement the following difference equa-
tion: y.n/ D a y.n 1/ C x.n/.
Let x.n/ be a unit sample at time n D 0 and a D 0:5. Find and display the output y.n/ for
n values from 0–20. Explain how the output changes as the coefficient a is varied. Outputs need
to be displayed on the main app screen as well as being sent to LogCat using the Log library.
Hintsis exercise addresses the use of JNI conventions for native methods. Using the
example above as a template, implement the difference equation as a C function which takes
integer input for the variable n and outputs the floating-point result y.n/. Use the relations
shown in Table L1.1 as a reference for matching data types between Java, JNI, and C.
Table L1.1 shows some common data types. A multi-dimensional array is represented as
an array of arrays. With an array being an object in Java, a multi-dimensional array appears as
an array of Java object primitives (which are themselves arrays of floating-point primitives).
38 2. ANDROID SOFTWARE DEVELOPMENT TOOLS
Table L1.1: Data type conversions
Java JNI C
double jdouble double
oat jfl oat oat
long jlong long
int jint int
short jshort short
boolean jboolean int
oat[] jfl oatarray oat *
oat[][] jobjectarray oat **
For the example above, the function used is:
jstring Java_com_dsp_helloworld_MainActivity_getString (
JNIEnv* env, jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello UTD!");
}
According to the JNI convention, the inputs to this method, i.e., JNIEnv* env and
jobject thiz , are always required. Additional input variables may be added and the return
type may be changed as noted below:
jfloat Java_com_dsp_helloworld_MainActivity_getArea (
JNIEnv* env, jobject thiz, jfloat radius)
{
return 3.14159f*radius*radius;
}
with the corresponding native method in Java declared as
public native float getArea(float radius);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.8.34