TQ210搭载Android4.0.3系统构建之BEEP从驱动到HAL到JNI到应用程序(JNI篇)

 

   对于BEEP的JNI层,并没有采用LED所用的JNI_OnLoad的方法,而是直接使用的是JNI的绑定机制,在JNI中与上层应用相对应函数采用的方式如下

 


  详细信息见JNI官网  : http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html

  

beep_under_jni.c

#include <jni.h>
#include <hardware/hardware.h>
#include <hardware/beep_under_hal.h>
#include <android/log.h>
#include <stdio.h>

#define true 1
#define false 0


static struct beep_under_device_t *beep_under_device=NULL;



//蜂鸣器JNI打开函数 通过module的methods中的open方法 带回控制设备的指针给beep_under_device
static inline int beep_open(struct hw_module_t *module,struct beep_under_device_t **device)
{
	int ret=module->methods->open(module,BEEP_UNDER_MODULE_ID,(struct hw_device_t **)device);
	if(ret!=0) __android_log_print(ANDROID_LOG_DEBUG,"msg","can not open in jni,ret=%d",ret);
	return ret;	
}

//控制蜂鸣器发声
jboolean Java_com_under_beep_BeepActivity_beepOn(JNIEnv * env, jclass clazz,jint cmd)
{
	if(beep_under_device==NULL) return false;
	return beep_under_device->beep_on(beep_under_device,1);
}

//控制蜂鸣器不发声
jboolean Java_com_under_beep_BeepActivity_beepOff(JNIEnv * env, jclass clazz,jint cmd)
{
	if(beep_under_device==NULL) return false;
	return beep_under_device->beep_off(beep_under_device,0);
}

jboolean Java_com_under_beep_BeepActivity_beepClose(JNIEnv * env, jclass clazz)
{
	if(beep_under_device==NULL) return false;
	return beep_under_device->common.close(&beep_under_device->common);
}


//蜂鸣器初始化函数 通过hw_get_module带回HAL层定义的模块给module
jboolean Java_com_under_beep_BeepActivity_beepInit(JNIEnv *env,jclass clazz)
{
	struct beep_under_module_t *module;
	int ret=hw_get_module(BEEP_UNDER_MODULE_ID, (const struct hw_module_t **)&module);
	if(ret==0)
		{
			if(beep_open(&module->common,&beep_under_device)==0) return true;
			
	        }
	__android_log_print(ANDROID_LOG_DEBUG,"msg","beepInit failed.
");
	return false;
}





 

编译文件Andrioid.mk文件

LOCAL_PATH	:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_PRELINK_MODULE	:=false
LOCAL_SRC_FILES	:=beep_under_jni.c
LOCAL_SHARED_LIBRARIES	:=libutils 
	libhardware
LOCAL_MODULE	:=libbeepunder
LOCAL_MODULE_TAGS	:=optional
LOCAL_MODULE_PATH	:=$(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)



 

原文地址:https://www.cnblogs.com/liangxinzhi/p/4275626.html