(一)搭建一个可以使用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中对于一些底层硬件的控制。