少废话直接贴代码:
软件环境:android studio 2.2 ; NDK最好单独下载12r版本就可以; allwinner开发板
1、编写jni头文件及源文件
在main/java同级目录下创建jni文件夹用于存放.h,.c文件
.h文件如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_beep_GpioCtr */
#ifndef _Included_com_example_beep_GpioCtr
#define _Included_com_example_beep_GpioCtr
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_beep_GpioCtr
* Method: exportGpio
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_example_beep_GpioCtr_exportGpio
(JNIEnv *, jclass, jint);
/*
* Class: com_example_beep_GpioCtr
* Method: setGpioDirection
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_beep_GpioCtr_setGpioDirection
(JNIEnv *, jclass, jint, jint);
/*
* Class: com_example_beep_GpioCtr
* Method: readGpioStatus
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_example_beep_GpioCtr_readGpioStatus
(JNIEnv *, jclass, jint);
/*
* Class: com_example_beep_GpioCtr
* Method: writeGpioStatus
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_beep_GpioCtr_writeGpioStatus
(JNIEnv *, jclass, jint, jint);
/*
* Class: com_example_beep_GpioCtr
* Method: unexportGpio
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_example_beep_GpioCtr_unexportGpio
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
.c文件如下:
//
// Created by Administrator on 2018/12/1.
//
#include <jni.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <android/log.h>
#include "com_example_beep_GpioCtr.h"
#define TAG "jni_gpio"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__)
#define LOGW(...) __android_l