Purpose: In the android system, use JAVA to write interface programs, call the interfaces provided by the jni intermediate library, operate a driver node, and perform read, writer ioctl, and other operations! This is an important debugging channel for underlying driver developers. It is also a method for providing some special functional interfaces in the android system!
The premise of this article: we assume that we have already written a driver that controls the lighting of the LED and creates a node:/dev/vib, that is, through the open vib node, you can use the read/write/ioctl driver to enable or disable the LED light. For more information, see my other blog post "android driver example (LED light control)".
Development Environment 1. NDK compiling environment under ubuntu; 2. Esclips Development Environment
I. Compile the JNI Module
After the NDK compiling environment is installed, the sample directory will be found under its Directory, which contains some examples. You can refer to these examples to write our own modules.
1. In the source folder, create the "LEDSJNI" folder.
2, Source/LEDSJNI/jni/directory, new "vib-jni.c"
Vib-jni.c files
# Include <string. h>
# Include <jni. h>
# Include <fcntl. h>/* include file operations, such as open () read () close () write */
// ---- For output the debug log message
# Include <android/log. h>
# Define LOG_TAG "vib-jni"
# Define LOGI (...) _ android_log_print (ANDROID_LOG_INFO, LOG_TAG, __va_args __)
# Define LOGE (...) _ android_log_print (ANDROID_LOG_ERROR, LOG_TAG ,__ VA_ARGS __)
# Define DEVICE_NAME "/dev/vib" // device point
# Define VIB_ON 0x11
# Define VIB_OFF 0x22
Int fd;
Jstring
Java_com_auly_control_vibClass_stringFromJNI (JNIEnv * env,
Jobject thiz)
{
Return (* env)-> NewStringUTF (env, "Hello from JNI -- Peter for vib! "); // Print the string
}
Jint
Java_com_auly_control_vibClass_Init (JNIEnv * env)
{
LOGE ("vibClass_Init ()/n ");
Fd = open (DEVICE_NAME, O_RDWR); // open the device
LOGE ("vibClass_Init ()-> fd = % d/n", fd );
If (fd =-1)
{
LOGE ("open device % s error/n", DEVICE_NAME); // print debugging information
Return 0;
}
Else
{
Return 1;
}
}
Jint
Java_com_auly_control_vibClass_IOCTLVIB (JNIEnv * env, jobject thiz, jint controlcode)
{
Int CTLCODE = controlcode;
LOGE ("IOCTLVIB () = % x -- vibClass_IOCTLVIB/n", CTLCODE );
Switch (CTLCODE)
{
Case VIB_ON:
{
Ioctl (fd, VIB_ON); // call the ioctrl interface in the driver and run VIB_ON to implement hardware operations.
Break;
}
Case VIB_OFF:
{
Ioctl (fd, VIB_OFF); // call the ioctrl interface in the driver and run the VIB_OFF command to implement hardware operations
Break;
}
Default: break;
}
Return 1;
}
3. Create Android. mk in the same directory as follows:
Android. mk File
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = vib-jni
LOCAL_SRC_FILES: = vib-jni.c
LOCAL_CFLAGS: =-Werror
LOCAL_LDLIBS: =-llog-lGLESv2 // _ android_log_print Function
Include $ (BUILD_SHARED_LIBRARY)
As you can see, it is mainly to modify LOCAL_SRC_FILES to point to the name of the source file!
It is also important to use the debug LOG printing function, that is, the _ android_log_print function. Add-llog to LOCAL_LDLIBS, as shown in Android. mk above.
4. Compile the JNI Module
# Cd/home/workspace/android-ndk-r4b/sources/LEDSJNI
Go to the JNI directory you just wrote.
# Ndk-build
After JNI is compiled, the libs and obj folders are generated in the LEDSJNI folder.
Get the target file libvib-jni.so under LEDSJNI/libs/armeabi
(Currently, the LEDSJNI folder has only three directories: jni, libs, and obj)
Ii. JAVA program
1. Create a project in Eclipse
Copy the LEDSJNI directory to Windows, for example, drive C. Create an Eclipse project in it.
After the new key project,
If the following error occurs:
ERROR: Unable to open class file C:/LEDSJNI/gen/com/auly/control/R. java: No such file or directory
The solution is as follows:
Right-click the project, click bulid path, configure build path, java build path, order and Export.
Check android 2.1 and Build the project.
Then Run as> Android application, the android simulator will appear, and a helloworld will come out.
2. Add button and text output
The code generated by the above process is automatically generated by the ADT. It seems that it has nothing to do with us. Let's change the code, because we call the JNI interface to access the driver to operate hardware, such as writing, reading, opening, and disabling LEDs.
The first step is to add two buttons and describe them in main. xml: Open the Res> layout> main. xml file.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Androidrientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button android: id = "@ + id/led_on"/* indicates that a unique UID is required as the Button ID, and its reference name is led_on. */
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/LEDon">/* indicates that the text of this button is from another resource description file strings. xml: Word resource LEDon */
<RequestFocus/>
</Button>
<Button android: id = "@ + id/led_off"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/LEDoff">
<RequestFocus/>
</Button>
</LinearLayout>
In the actual code, remove the comments; otherwise, the compilation will fail.
3. Add the output string resource
Project> values> strings. xml file
Modify as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Led Control Program </string>
<String name = "app_name"> LEDAPP </string>
<String name = "LEDon"> turn on the LED </string>
<String name = "LEDoff"> turn off the LED </string>
</Resources>
The above "Open LED" and other resources are strings displayed on the button.
After the above modification, the program interface now has the following effects:
Right-click the project name and choose Run as> Android application to Run the program.
4. action corresponding to the Add button
Turn on the LED button: Call the IOCTLVIB (VIB_ON) of JNI );
"Turn off LED" button: Call IOCTLVIB (VIB_OFF) of JNI );
Operation:
Go to the LEDAPP> src> com. auly. control> vibrator. java file.
Package com. auly. control;
/** Define the header file */
Import android. widget. TextView;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. util. Log;
Import android. app. Activity;
Import android. OS. Bundle;
Public class vibrator extends Activity {
/** Define variable */
Public static final int VIB_ON = 0x11;
Public static final int VIB_OFF = 0x22;
VibClass mvibClass;/** definition class */
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
/** ---------------- Initialize -------------*/
MvibClass = new vibClass ();/* Declaration class */
MvibClass. Init (); // call the initialization function in the JNI Library
/** ---------------- Button: Open LED -------------*/
Button btn1 = (Button) findViewById (R. id. led_on);/* The ID used here is the led_on */defined in main. xml */
Btn1.setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v)/** when the button is pressed */
{
MvibClass. IOCTLVIB (VIB_ON );
}
});
/** ---------------- Button: Close LED -------------*/
Button btn2 = (Button) findViewById (R. id. led_off);/* declaration Button, which is defined in id main. xml */
Btn2.setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v)/** when the button is pressed */
{
MvibClass. IOCTLVIB (VIB_OFF );
}
});
}
}
If save problems is not saved, copy the above Code and disable vibrator. java. If you are prompted to save the file, choose not to save it. In the left-side resource window, double-click it again to open the file and paste the copied content within a few days. Generally, the file can be saved normally, I don't know if it is caused by the instability of Eclipse.
5. Add a vibClass class
Right-click com. auly. control> new> Class
Parameters:
After finishing, you can get the following class file in/src/com/auly/control /.
VibClass. java
Modify as follows:
Package com. auly. control;
/* Class for Vibrator -- peter */
Public class vibClass {
Static {
System. loadLibrary ("vib-jni");/* load JNI library */
}
/* Declare a function */
Public static native String stringFromJNI ();/* output String
Corresponding to
Jstring Java_com_auly_control_vibClass_stringFromJNI (JNIEnv * env, jobject thiz)
*/
Public static native int Init ();/* initialization function, which corresponds to
Jint Java_com_auly_control_vibClass_Init (JNIEnv * env)
*/
Public static native int IOCTLVIB (int controlcode );
/* Io ctrl Interface
* Corresponds to
Jint Java_com_auly_control_vibClass_IOCTLVIB (JNIEnv * env, jobject thiz, jint controlcode)
*/
}
Iii. Compile and run
Right-click the project name, and select Run as> Android Application to view the compilation process. After compilation is complete, the android simulator is automatically called and the following results are displayed:
Install On the Development Board:
In the C:/ledsjni directory, you will see the binfolder, And the ledapp.apk in it is the installation file of this program. You can run this program on the Development Board where it is installed to view the LED lights on the Control Development Board.
Steps:
1. The kenel running on the Development Board has compiled the LED driver,
See android driver example (LED light control)
2. After the Development Board android runs, the PC opens the serial port tool such as DNW, opens the COM port connected to the development board, and then press enter to see "#" And a cursor in the terminal, the command line terminal of the Development Board is displayed,
Enter the following command:
# Chmod 777/dev/vib
This is to enable the JNI operation on the vib node to be written by us. Otherwise, the open fails because the JNI module installed in APK has insufficient permissions, this node is the control node generated by our LED driver.
You can also use the init. rc file to perform this operation during yaffs compilation in the android file system. In this case, you can write the following command line in a line in the file, which will be automatically executed at startup! In this way, you do not need to manually change the attributes of the node.
3. Install beibeiledapp.apk on the Development Board and install it on the Development Board through the Installation tool. If it is not installed, GOOGLE it,
4. Run the program and press the near button on the program to control the LED on the Development Board!
Author: zhanglongit