【Android嵌入式开发】使用NDK实现JAVA与C的交互,并在程序中实现对硬件的控制

(一)搭建一个可以使用NDK的开发环境

(1)第一步 安装相关插件

Toos–>SDK Manager
在这里插入图片描述
选择安装NDK 与 Cmake

(2) 第二步 新建项目

在这里插入图片描述

选择NATIVE C++

(二)添加文件

在 main -->java文件中新建一个LedControl.java文件
在 main–>cpp文件中创建一个nx_led.cpp的文件
在这里插入图片描述

(三)完成C与java的接口

然后在java文件中写一个自己想要的函数 例如:

在这里插入图片描述
在这里插入图片描述

我们可以看到一个红色的灯炮
在这里插入图片描述点击以后我们看见他在nx_led.cpp中创建了一个函数,然后我们就可以在这个函数中写自己需要的东西。

此外需要注意的是这样的文件还没有加入工程

我们还需要在CMakeList文件中添加一些东西:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

add_library( # Sets the name of the library.
        gpio-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        nx_gpio.cpp
        GpioControl.cpp)

add_library( # Sets the name of the library.
        led-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        nx_led.cpp
        )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

target_link_libraries( # Specifies the target library.
        gpio-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

target_link_libraries( # Specifies the target library.
        led-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

只有将文件手动的添加入CMakeLIst中这个文件才可以被编译与引用

下面是与在APP中点灯有关的代码

nx_led.cpp

//
// Created by root on 9/8/20.
//

#include "nx_led.h"
#include <jni.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>		// open
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <error.h>

#define ON 1
#define OFF 0
#define DEVICE_NAME		"/dev/real_led"

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_example_gpio_LedColtrol_ledOn(JNIEnv *env, jobject thiz, jint fd) {
    // TODO: implement ledOn()
    ioctl(fd, ON, 1);
    return true;
}


extern "C"
JNIEXPORT jboolean JNICALL
Java_com_example_gpio_LedColtrol_ledOff(JNIEnv *env, jobject thiz, jint fd) {
    // TODO: implement ledOff()

    ioctl(fd, OFF, 1);

    return true;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_gpio_LedColtrol_ledInit(JNIEnv *env, jobject thiz) {
    // TODO: implement ledInit()

    int fd = open(DEVICE_NAME, 0);
    if(fd==-1) return -1;
    else return fd;

}

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_gpio_LedColtrol_ledchange(JNIEnv *env, jobject thiz, jint fd) {
    // TODO: implement ledchange()
}

LedControl.java

package com.example.gpio;

public class LedColtrol {

    static  {
        System.loadLibrary("led-lib");
    }

    public int LedInit()
    {
        int fd =ledInit();
        return fd;
    }

    public  boolean LedOn(int fd)
    {
        ledOn(fd);
        return true;
    }

    public boolean LedOff(int fd)
    {
        ledOff(fd);
        return true;
    }

    public  native  int     ledInit();
    public  native  boolean ledOn(int fd);
    public  native  boolean ledOff(int fd);
    public  native  int     ledchange(int fd);
}

MainActivity.java

package com.example.gpio;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("gpio-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        LedColtrol l= new LedColtrol();
        int fd= l.LedInit();
        l.ledOn(fd);

    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
    public native boolean GpioInit(int index,int port);
}

运行后可以发现LED灯被点亮了

用同样的方法就可一实现在Android中对于一些底层硬件的控制。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

与光同程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值