`

JNI开发第三步:20130801_NDK_JNI的.so文件开发-安卓访问LINUX驱动

 
阅读更多

JNI开发第三步:

JNI文件的编写 Android.mk和myjni.c(或myjni.cpp)

 

点亮一个LED

1.首先linux下要已经弄好了驱动,并已经知道了设备名LED@LZM@FJICC

2.eclipse下新建立一个android app工程TEST。然后在TEST下新建一个文件夹jni。在jni文件夹下新建两个文件Android.mktest-jni.c

Android.mk

# Copyright (C) 2009 The Android Open Source Project

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#

LOCAL_PATH := $(call my-dir)

include$(CLEAR_VARS)

LOCAL_MODULE:= test-jni

LOCAL_SRC_FILES := test-jni.c

include$(BUILD_SHARED_LIBRARY)

 

test-jni.c

#include<string.h>

#include<jni.h>

#include<ioctl.h>

#include<fcntl.h>

 

#define VIB_ON 0x11

#define VIB_OFF 0x22

#defineDEV_NAME "/dev/LED@LZM@FJICC"

/* This is a trivial JNI example where we use a native method

* to return a new VM String. See the corresponding Java source

* file located at:

*

*apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java

*/

jstring

Java_com_example_TEST__stringFromJNI( JNIEnv* env, jobject thiz )

{

return (*env)->NewStringUTF(env, "Hello from JNI !");

}

 

jint

Java_com_example_TEST_TESTCLASS_Init(JNIEnv* env)

{

 

int fd=open(DEV_NAME,O_RDWR);

return fd;

}

jint

Java_com_example_TEST_TESTCLASS_IOCTLLED( JNIEnv* env, jobject thiz, jint controlcode,jint ledid ,jint fd)

{

/* LED */

int CTLCODE = controlcode;

switch(CTLCODE)

{case VIB_ON:

{

ioctl(fd,1,ledid);//setLedState( 0, 1 );//调用驱动程序中的ioctrl接口,把命令传下去,实现硬件操作

break;

}

case VIB_OFF:

{

ioctl(fd,0,ledid);////setLedState( 0, 0 );//调用驱动程序中的ioctrl接口,把命令传下去,实现硬件操作

break;

}

default:break;

}

return 1;

}

 

com_example_TEST:这个需要和我们建立的TEST工程相对应,我们建立的TEST类位于src文件夹下的:com.example.TEST中的MainActivity.java类。

TESTCLASS:是我们后面调用JNI生成的.so文件时需要建立的类:com.example.TEST下的TESTCLASS.java类。

IOCTLLED是在JNI中实现的方法,到时候在java应用程序中调用用的。

注意:test-jni.c文件中的#include<ioctl.h>#include<fcntl.h>这两个文件时从linux/linux/include/asm-generic/…)下拷贝出来的,放在jni目录下。

3.然后打开NDK软件,在cmd命令窗口中,转到我们建立的工程目录下的jni文件下面,输入$NDK/ndk-build 就可以生成了:libtest-jni.so文件。

4.编写应用程序:在com.example.TEST下建立一个类:TESTCLASS.java

package com.example.TEST;

import android.util.Log;

publicclass TESTCLASS {

publicstaticnative String stringFromJNI();

publicstaticnativeint Init();

publicstaticnativeint IOCTLLED(int controlcode,int ledID,int fd);

static {

try {

System.loadLibrary("test-jni");

} catch (UnsatisfiedLinkError e) {

Log.d("HardwareControler", "HardwareControler ibrary not found!");

}

}

}

5.编写应用程序实现调用库中函数MainActivity.java

package com.example.TEST;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

publicclass MainActivity extends Activity {

private Button btn_on;

private Button btn_off;

publicstaticfinalintVIB_ON = 0x11;

publicstaticfinalintVIB_OFF = 0x22;

/**如果去掉public class TESTCLASS 中的那些static这里就可以定义一个类,实例化TESTCLASS TESTCLASS mTESTCLASS001;后面的函数调用就可以使用这个对象了**/

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

/**如果去掉public class TESTCLASS 中的那些static实例化TESTCLASS TESTCLASS mTESTCLASS001;那么这里就要开辟一个类了哦:

mTESTCLASS001 = new TESTCLASS();**/

btn_on = (Button)findViewById(R.id.button1);

btn_off = (Button)findViewById(R.id.button2);

/**比如这里就可以用了:fd = mTESTCLASS001.Init();*****/

finalint fd = TESTCLASS.Init();

if(fd == -1)

Toast.makeText(MainActivity.this, "没有打开设备", Toast.LENGTH_SHORT).show();

 

btn_on.setOnClickListener(new Button.OnClickListener(){

 

@Override

publicvoid onClick(View arg0) {

// TODO Auto-generated method stub

TESTCLASS.IOCTLLED(VIB_ON,0, fd);

}

});

btn_off.setOnClickListener(new Button.OnClickListener(){

 

@Override

publicvoid onClick(View v) {

// TODO Auto-generated method stub

TESTCLASS.IOCTLLED(VIB_OFF,0, fd);

}});

}

}

 

说明:黄色和绿色部分:

TESTCLASS:这个指的就是我们建立的类TESTCLASS.java

IOCTLLED:指的就是在类中的方法。

 

6.编译好后,把生成的bin/TEST.apk拷贝到对应的开发板上,就可以运行了。这里要注意,由于驱动是自己加的,要运行需要权限提升,在终端输入#chmod 777 /dev/LED@LZM@FJICC,然后在运行TEST程序就可以了。

NOTE:

对于Android工程Eclipse里编译好的.so文件放到 libs\armeabi下以后,这样.so文件就可以打包到apk文件里,在apk装到手机上以后libs\armeabi下的.so文件应该就会解压到/data/data/project的包名/lib下。

a) loadLibrary调用的时候需要去掉lib前缀 System.loadLibrary("JNITest");

b) load调用的时候需要写全路径名且不能去掉lib前缀因为这里是当成一个普通文件读取的 System.load("/data/data/com.test.test/libJNITest.so");

 

2.How to promote the right of Linux's Driver in the Android?

 

void changePerm()
{
Process chperm;
try{
chperm
=Runtime.getRuntime().exec("su");


DataOutputStream os =
newDataOutputStream(chperm.getOutputStream());
os
.writeBytes("chmod 777 /dev/radio\n");
os
.flush();

os
.writeBytes("exit\n");
os
.flush();

chperm
.waitFor();

}catch(IOException e){
// TODO Auto-generated catch block
e
.printStackTrace();
}catch(InterruptedException e){
// TODO Auto-generated catch block
e
.printStackTrace();
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics