A good memory is better than a bad pen------button Light JNI, packaged into so files and used

Source: Internet
Author: User

MTK Platform If you add a button light on the hardware, the third-party app and the framework layer want to call, all need to pass the JNI interface, the specific usage is not the same, here is a brief introduction of my use of the process.

First, you need to add KEYLED.C and header files KeyLed.h two interface functions:

(\HARDWARE\LIBHARDWARE_LEGACY\KEY_LEDS\KEY_LEDS.C)

(\hardware\libhardware_legacy\include\hardware_legacy\keyled.h)

1, Here is a brief introduction to the method of packaging the so file (tool eclipse,ndk):

Put the KEY_LEDS.C and key_leds.h files in the JNI directory in the application, Add the C + + interface Jni_key_leds.cpp,jni_key_leds.h,android.mk file, the directory structure is as follows:

File Description:

ANDROID.MK: (define so name, introduce header file, etc.) as follows

1Local_path:= $ (Call my-dir)2 include $ (clear_vars)3 4Local_module: =jni_key_leds5Local_module_tags: =Optional6 7Local_c_includes + =    8 $ (jni_h_include)9$ (Local_path)/Key_leds.hTen$ (Local_path)/Key_leds.h One$ (Local_path)/pressure.h A      -Local_src_files: =  - Jni_key_leds.cpp the KEY_LEDS.C - pressure.c -  -Local_shared_libraries: =libutils +  -Local_ldlibs: =-Llog +  ALocal_static_libraries: = at  -Local_cflags + = -  -Local_prelink_module: =false -  -Include $ (build_shared_library)
View Code

APPLICATION.MK:

1 app_stl: = gnustl_static2 app_gppflags: =-frtti-fexceptions3 App_abi: = All

App_abi: By default, the NDK's compilation system generates machine code based on the "Armeabi" ABI. You can use App_abi to select a different ABI,

For example, to support the hardware FPU command on ARMV7 devices. You can use App_abi: = armeabi-v7a

Or to support the IA-32 instruction set, you can use App_abi: = x86

Or to support these three, you can use App_abi: = Armeabi armeabi-v7a x86

App_abi: = all compiled

Jni_key_leds.cpp: Introduce interface file and header file, write method interface

1#include <jni.h>2#include <unistd.h>3#include <fcntl.h>4#include <sys/types.h>5#include <sys/stat.h>6 7#include <stdio.h>8#include <stdlib.h>9#include <math.h>Ten#include <string.h> One  A#include"Jni_key_leds.h" -#include"Key_leds.h" -#include"pressure.h" the  -#include <android/log.h> -  - #defineLog_tag "Mainactivity" + #defineLOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__) -  + extern "C" { A      at jniexport jint jnicall java_com_elderposition_util_devicemanager_red1ledsexists ( -JNIEnv *env, Jobject obj) { -LOGD ("red1_leds_exists!!!"); -     returnred1_leds_exists (); - } - jniexport jint jnicall java_com_elderposition_util_devicemanager_red2ledsexists ( inJNIEnv *env, Jobject obj) { -LOGD ("red2_leds_exists!!!"); to     returnred2_leds_exists (); + } - ....... the}
View Code

Jni_key_leds.h:

1 #ifndef __jni_key_leds2 #define__jni_key_leds 13 4 #ifdef __cplusplus5 extern "C" {6 #endif7 8 jniexport jint jnicall java_com_elderposition_util_devicemanager_red1ledsexists (9JNIEnv *env, Jobject obj);Ten jniexport jint jnicall java_com_elderposition_util_devicemanager_red2ledsexists ( OneJNIEnv *env, Jobject obj); A}
View Code

KEY_LEDS.C: Interface file, method (Note the method name, the full path of the program with "_" connection)

1#include <stdio.h>2#include <unistd.h>3#include <fcntl.h>4#include <errno.h>5 6 #defineRed1_device "/sys/devices/platform/key_leds/red1_leds_onoff"7 #defineRed2_device "/sys/devices/platform/key_leds/red2_leds_onoff"8 #defineRed3_device "/sys/devices/platform/key_leds/red3_leds_onoff"9 #defineBlue_device "/sys/devices/platform/key_leds/blue_leds_onoff"Ten #defineGreen_device "/sys/devices/platform/key_leds/green_leds_onoff" One   A intred1_leds_exists () - { -     intFD; the  - #ifdef Qemu_hardware -     if(Qemu_check ()) { -         return 1; +     } - #endif +  AFD =Open (Red1_device, O_RDWR); at     if(FD <0) -         return 0; - Close (FD); -     return 1; -}
View Code

Key_leds.h: Receiving oral documents

1 #ifndef _hardware_ir_leds_h2 #define_hardware_ir_leds_h3 4 #if__cplusplus5 extern "C" {6 #endif7 8 intred1_leds_exists ();9 Ten intred2_leds_exists (); One}
View Code

Once the above steps have been completed, the so file is compiled in Eclipse and can be used in third-party apps.

When a third-party app wants to use JNI, it can use files packaged as so in the Lib folder, using the same usage as other APIs:

public class DeviceManager {

static {
System.loadlibrary ("jni_key_leds");//Key point 1, introduction of so file
}

public native int red1ledsexists (); Key point 2, corresponding to each method write interface

public native int red2ledsexists ();

public native int red3ledsexists ();

public native int blueledsexists ();

public native int greenledsexists ();

}

2, the upper layer uses JNI (from Java layer to framework to JNI to HAL to kernel usage process)

A good memory is better than a bad pen------button Light JNI, packaged into so files and used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.