Android系统APP访问硬件之JNI方式0002

来源:互联网 发布:阿里云网站备案 编辑:程序博客网 时间:2024/06/05 19:33

一:神马是JNIJNIJava Native Interface的缩写,中文名字为Java本地接口语言。也正是它的存在使得Java程序可以去访问Linux内核当中的驱动程序,也由于它的存在使得Android程序具有跨平台的特点。这里是以前编写的JNI文档,记录了下来http://url.cn/45e3wjL

二:平台介绍

1:开发板  tiny4412  

2:实现的目标,使用Android应用程序通过JNI的方式来直接控制4412开发板上面的四个LED灯。

①:四个复选框,可分别单独控制对应的四个led

②:一个button控件,点击第一次,四个LED全亮,点击第二次灯全灭,如此循环。

③:没操作一次界面就会提示 xxx等亮/


3:需要的资源    LED驱动程序即C库,Native层即 JNIJava类以及APP应用程序。


三:LED驱动程序的实现。

这里提供openclose以及ioctl操作,其中等的控制是使用ioctl接口来操作。

#include <linux/kernel.h>#include <linux/module.h>#include <linux/miscdevice.h>#include <linux/device.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/moduleparam.h>#include <linux/slab.h>#include <linux/ioctl.h>#include <linux/cdev.h>#include <linux/delay.h> #include <linux/gpio.h>#include <mach/gpio.h>#include <plat/gpio-cfg.h>//定义四个led 管脚static int led_gpios[] = {EXYNOS4212_GPM4(0),EXYNOS4212_GPM4(1),EXYNOS4212_GPM4(2),EXYNOS4212_GPM4(3),};static int led_open(struct inode *inode, struct file *file){/* 配置GPIO为输出引脚 */int i;for (i = 0; i < 4; i++)s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);return 0;}/* app : ioctl(fd, cmd, arg) */static long led_ioctl(struct file *filp, unsigned int cmd,unsigned long arg){/* 根据传入的参数设置GPIO *//* cmd : 0-off, 1-on *//* arg : 0-3, which led */if ((cmd != 0) && (cmd != 1))return -EINVAL;if (arg > 4)return -EINVAL;gpio_set_value(led_gpios[arg], !cmd);return 0;}static struct file_operations leds_ops = {    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */    .open   =   led_open,     .unlocked_ioctl= led_ioctl,};static int major;static struct class *cls;int leds_init(void){major = register_chrdev(0, "leds", &leds_ops);/* 为了让系统udev,mdev给我们创建设备节点 *//* 创建类, 在类下创建设备 : /sys */cls = class_create(THIS_MODULE, "leds");device_create(cls, NULL, MKDEV(major, 0), NULL, "leds"); /* /dev/leds */return 0;}void leds_exit(void){device_destroy(cls, MKDEV(major, 0));class_destroy(cls);unregister_chrdev(major, "leds");}module_init(leds_init);module_exit(leds_exit);MODULE_LICENSE("GPL");


四:native层 即JNI的实现

#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#include <android/log.h>  /* liblog */ #if 0typedef struct {    char *name;          /* Java里调用的函数名 */    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */    void *fnPtr;          /* C语言实现的本地函数 */} JNINativeMethod;#endifstatic jint fd;jint ledOpen(JNIEnv *env, jobject cls){fd = open("/dev/leds", O_RDWR);__android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen : %d", fd);if (fd >= 0)return 0;elsereturn -1;}void ledClose(JNIEnv *env, jobject cls){__android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledClose ...");close(fd);}jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status){int ret = ioctl(fd, status, which);__android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledCtrl : %d, %d, %d", which, status, ret);return ret;}static const JNINativeMethod methods[] = {{"ledOpen", "()I", (void *)ledOpen},{"ledClose", "()V", (void *)ledClose},{"ledCtrl", "(II)I", (void *)ledCtrl},};/* System.loadLibrary */JNIEXPORT jint JNICALLJNI_OnLoad(JavaVM *jvm, void *reserved){JNIEnv *env;jclass cls;//获得运行时环境if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {return JNI_ERR; /* JNI version not supported */}//找到Java层对应的类cls = (*env)->FindClass(env, "com/thisway/hardlibrary/HardControl");if (cls == NULL) {return JNI_ERR;}/* 2. 进行本地与Java层的映射*/if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])) < 0)return JNI_ERR;return JNI_VERSION_1_4;}五:Java层 Java类里面定义几个本地方法,并在静态代码块里面加载C库 System.loadLibrary("hardcontrol");其中System.loadLibrary 的参数要对应于JNI层所生成SO文件的名称即库名package com.thisway.hardlibrary;public class HardControl {    public static native int ledCtrl(int which, int status);    public static native int ledOpen();    public static native void ledClose();    static {        try {            System.loadLibrary("hardcontrol");        } catch (Exception e) {            e.printStackTrace();        }    }}



六:APP应用层


package com.thisway.app_0001_leddemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.view.View;import android.widget.CheckBox;import android.widget.Toast;import com.thisway.hardlibrary.*;public class MainActivity extends AppCompatActivity {    private boolean ledon = false;    private Button button = null;    private CheckBox checkBoxLed1 = null;    private CheckBox checkBoxLed2 = null;    private CheckBox checkBoxLed3 = null;    private CheckBox checkBoxLed4 = null;    class MyButtonListener implements View.OnClickListener {        @Override        public void onClick(View v) {            ledon = !ledon;            if (ledon) {                button.setText("ALL OFF");                checkBoxLed1.setChecked(true);                checkBoxLed2.setChecked(true);                checkBoxLed3.setChecked(true);                checkBoxLed4.setChecked(true);                for (int i = 0; i < 4; i++)                    HardControl.ledCtrl(i, 1);            }            else {                button.setText("ALL ON");                checkBoxLed1.setChecked(false);                checkBoxLed2.setChecked(false);                checkBoxLed3.setChecked(false);                checkBoxLed4.setChecked(false);                for (int i = 0; i < 4; i++)                    HardControl.ledCtrl(i, 0);            }        }    }    public void onCheckboxClicked(View view) {        // Is the view now checked?        boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked        switch(view.getId()) {            case R.id.LED1:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(0, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(0, 0);                }                break;            case R.id.LED2:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(1, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(1, 0);                }                break;            case R.id.LED3:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(2, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(2, 0);                }                break;            case R.id.LED4:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(3, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(3, 0);                }                break;            // TODO: Veggie sandwich        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.BUTTON);        HardControl.ledOpen();        checkBoxLed1 = (CheckBox) findViewById(R.id.LED1);        checkBoxLed2 = (CheckBox) findViewById(R.id.LED2);        checkBoxLed3 = (CheckBox) findViewById(R.id.LED3);        checkBoxLed4 = (CheckBox) findViewById(R.id.LED4);        button.setOnClickListener(new MyButtonListener());    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}






 

0 0
原创粉丝点击