您的位置:首页 > 移动开发 > Android开发

Android中APK直接通过JNI访问驱动

2017-02-03 19:07 375 查看
1.新建文件LED-JNI.c,内容如下:
#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <android/log.h>  /* liblog */

//__android_log_print(ANDROID_LOG_DEBUG, "JNIDemo", "native add ...");
 
#if 0
typedef struct {
    char *name;     /* Java里调用的函数名 */
    char *signature;   /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
    void *fnPtr;     /* C语言实现的本地函数 */
} JNINativeMethod;
#endif

static jint fd;

jint ledOpen(JNIEnv *env, jobject cls)
{
    fd = open("/dev/msmgpio", O_RDWR);
    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen : %d", fd);
    if (fd >= 0)
        return 0;
    else
        return -1;
}

void ledClose(JNIEnv *env, jobject cls)
{
    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledClose ...");
    close(fd);
}

jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status)
{
    int ret = ioctl(fd, which, status);
    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledCtrl : %d, %d, %d", which, status, ret);
    return ret;
}

static const JNINativeMethod methods[] = {
    {"ledOpen", "()I", (void *)ledOpen},
    {"ledClose", "()V", (void *)ledClose},
    {"ledCtrl", "(II)I", (void *)ledCtrl},
};

/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
    JNIEnv *env;
    jclass cls;

    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
        return JNI_ERR; /* JNI version not supported */
    }
    cls = (*env)->FindClass(env, "mobiletek/hardlibrary/HardControl");
    if (cls == NULL) {
        return JNI_ERR;
    }

    /* 2. map java hello <-->c c_hello */
    if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])) < 0)
        return JNI_ERR;

    return JNI_VERSION_1_4;
}

2.编译生成libhardcontrol.so,使用交叉编译器prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc:

arm-linux-androideabi-gcc -fPIC -shared LED-JNI.c -o libhardcontrol.so -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/  -nostdlib prebuilts/ndk/9/platforms/android-19/arch-arm/usr/lib/libc.so -I prebuilts/ndk/9/platforms/android-19/arch-arm/usr/include prebuilts/ndk/9/platforms/android-19/arch-arm/usr/lib/liblog.so

3.在Android Studio工程的app/libs目录下新建armeabi目录,将编译生成的libhardcontrol.so文件放入该目录下。

4.
在工程目录app/src/main/java/mobiletek下新建hardlibrary目录,在hardlibrary目录下新建HardControl.java文件,文件内容如下:
package mobiletek.hardlibrary;

public class HardControl {
public static native int ledCtrl(int which, int status);
public static native int ledOpen();
public static native void ledClose();

static {
try {
System.loadLibrary("hardcontrol");
} catch (Exception e) {
e.printStackTrace();
}
}

}

5.编辑app/src/main/java/mobiletek/led/MainActivity.java文件,内容如下:
package mobiletek.led;

import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import mobiletek.hardlibrary.*;

public class MainActivity extends AppCompatActivity {
private boolean ledon = false;
private Button button = null;

private CheckBox checkBoxLed1 = null;
private CheckBox checkBoxLed2 = null;
private CheckBox checkBoxLed3 = null;
private CheckBox checkBoxLed4 = null;

int []ledctrl = {0x12, 0x34, 0x56, 0x78};

class MyButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
ledon = !ledon;
if (ledon) {
button.setText("ALL OFF");
checkBoxLed1.setChecked(true);
checkBoxLed2.setChecked(true);
checkBoxLed3.setChecked(true);
checkBoxLed4.setChecked(true);

for (int i = 0; i < 4; i++)
HardControl.ledCtrl(ledctrl[i], 1);
}
else {
button.setText("ALL ON");
checkBoxLed1.setChecked(false);
checkBoxLed2.setChecked(false);
checkBoxLed3.setChecked(false);
checkBoxLed4.setChecked(false);

for (int i = 0; i < 4; i++)
HardControl.ledCtrl(ledctrl[i], 0);

}
}
}

public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked
switch(view.getId()) {
case R.id.LED1:
if (checked) {
Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[0], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[0], 0);
}
break;
case R.id.LED2:
if (checked) {
Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[1], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[1], 0);
}
break;

case R.id.LED3:
if (checked) {
Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[2], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[2], 0);
}
break;

case R.id.LED4:
if (checked) {
Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[3], 1);
}
else {
Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();
HardControl.ledCtrl(ledctrl[3], 0);
}
break;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button)findViewById(R.id.BUTTON);
HardControl.ledOpen();

checkBoxLed1 = (CheckBox) findViewById(R.id.LED1);
checkBoxLed2 = (CheckBox) findViewById(R.id.LED2);
checkBoxLed3 = (CheckBox) findViewById(R.id.LED3);
checkBoxLed4 = (CheckBox) findViewById(R.id.LED4);

button.setOnClickListener(new MyButtonListener());

/*
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
ledon = !ledon;
if (ledon)
button.setText("ALL OFF");
else
button.setText("ALL ON");
}
});
*/
}
}

6.
修改app/build.gradle文件:

apply plugin: 'com.android.application'

android {

    compileSdkVersion 25

    buildToolsVersion "25.0.2"

    defaultConfig {

        applicationId "mobiletek.led"

        minSdkVersion 15

        targetSdkVersion 25

        versionCode 1

        versionName "1.0"

    }
+    sourceSets {

+       main {

+           jniLibs.srcDirs = ['libs']

+      }

+   }

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }

    }

}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:25.1.0'

}

7.
编译,生成apk,安装到开发板,打开app,即可操作LED。

编译生成的libhardcontrol.so可以不随apk一起打包,单独放在/system/lib或者/vendor/lib目录均可,这样,就不需要新建armeabi目录,也不需要修改app/build.gradle文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: