|
发表于 2017-6-27 09:53:14
只看该作者
沙发
这是安卓代码的工程,里面jni涉及到的主要文件我已经圈出来
Testjni.java
- package com.gpio.jni;
- public class Testjni {
- public native int led_off();
- public native int led_on();
- }
复制代码 com_jni_gpio_Testjni.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include "com_gpio_jni_Testjni.h"
- JNIEXPORT jint JNICALL Java_com_gpio_jni_Testjni_led_1off
- (JNIEnv * env, jobject obj){
- jint res = -1;
- int fd;
- int flag = 1;
- fd = open("/dev/hello",O_RDWR);
- if (fd < 0)
- {
- return res;
- }
- write(fd, &flag, sizeof(flag));
- return 0;
- }
- JNIEXPORT jint JNICALL Java_com_gpio_jni_Testjni_led_1on
- (JNIEnv *env, jobject obj){
- jint res = -1;
- int fd;
- int flag = 0;
- fd = open("/dev/hello",O_RDWR);
- if (fd < 0)
- {
- return res;
- }
- write(fd, &flag, sizeof(flag));
- return 0;
- }
复制代码 com_jni_gpio_Testjni.h
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include <jni.h>
- /* Header for class com_gpio_jni_Testjni */
- #ifndef _Included_com_gpio_jni_Testjni
- #define _Included_com_gpio_jni_Testjni
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: com_gpio_jni_Testjni
- * Method: led_off
- * Signature: ()I
- */
- JNIEXPORT jint JNICALL Java_com_gpio_jni_Testjni_led_1off
- (JNIEnv *, jobject);
- /*
- * Class: com_gpio_jni_Testjni
- * Method: led_on
- * Signature: ()I
- */
- JNIEXPORT jint JNICALL Java_com_gpio_jni_Testjni_led_1on
- (JNIEnv *, jobject);
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码 activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.gpio.gpio_ctrl.MainActivity" >
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/button1"
- android:layout_alignBottom="@+id/button1"
- android:layout_alignParentRight="true"
- android:layout_marginRight="57dp"
- android:text="LedOff" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="23dp"
- android:text="LedOn" />
- </RelativeLayout>
复制代码
|
|