ADC的JNI层采用的方式与BEEP中JNI层采用的方式是一致的,采用Java+包名+类名+方法名,具体见BEEP的JNI层
直接上源码
adc_under_jni.c
#include <android/log.h> #include <jni.h> #include <hardware/hardware.h> #include <hardware/adc_under_hal.h> static struct adc_hw_device_t *adc_device=NULL; #define true 1 #define false 0 static inline int adc_open(struct hw_module_t *module,struct adc_hw_device_t **device) //adc打开函数,获取到设备给adc_device { int ret=module->methods->open(module,ADC_UNDER_ID,(struct hw_device_t **)device); if(ret!=0) __android_log_print(ANDROID_LOG_DEBUG,"msg", "can not open adc_open in jni. "); return ret; } jint Java_com_under_adc_AdcActivity_adcRead(JNIEnv * env, jclass clazz) //读取adc的值 { if(adc_device==NULL) return false; return adc_device->read(adc_device); } jboolean Java_com_under_adc_AdcActivity_adcClose(JNIEnv * env, jclass clazz) //adc 关闭函数 { if(adc_device==NULL) return false; return adc_device->common.close(&adc_device->common); } jboolean Java_com_under_adc_AdcActivity_adcInit(JNIEnv *env,jclass clazz) //adc 初始胡函数,通过此函数获取到jni层的模块 { struct adc_hw_module_t *module; int ret=hw_get_module(ADC_UNDER_ID, (const struct hw_module_t **)&module); //通过ID获取到jni层的模块 if(ret==0) { if(adc_open(&module->common,&adc_device)==0) return true; } __android_log_print(ANDROID_LOG_DEBUG, "msg","adcInit failed. "); return false; }
编译文件 Android.mk
LOCAL_PATH :=$(call my-dir) include $(CLEAR_VARS) LOCAL_PRELINK_MODULE :=false LOCAL_SHARED_LIBRARIES :=libutils libhardware LOCAL_SRC_FILES :=adc_under_jni.c LOCAL_MODULE :=libadcunders LOCAL_MODULE_PATH :=$(LOCAL_PATH) LOCAL_MODULE_TAGS :=optional include $(BUILD_SHARED_LIBRARY)