android通过JNI控制GPIO

本文介绍了如何在Android Studio 2.2环境下,使用JNI来控制GPIO。通过编写JNI头文件和源文件,初始化本地方法,并在Gradle配置中添加NDK支持,最终实现GPIO的控制功能。
摘要由CSDN通过智能技术生成

少废话直接贴代码:

软件环境: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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值