第一步:首先在linux下添加驱动
1.查看原理图,找出未使用的引脚,这里是:GPJ0_0 GPJ0_1
2.添加char字符设备驱动,找到LINUX源代码下的char设备驱动路径: FriendlyArm /Linux3.0.8/ Drivers/char/目录,在目录下新建里一个文件lzm_fjicc.c 用来写驱动用。
需要注册设备、设备的打开、关闭、取消设备等操作。
源代码如综合实验一:
第二步:建立Android测试代码,第一步要实现.so文件:
1.打开eclipseàFileàNewàAndroid Application Project com.example.TEST
2.新建jni文件夹,在文件夹内新建两个文件:test-jni.c和Android.mk
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)
jni0922.c内容如下:
/*
* 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.
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#include
#define VIB_ON 0x11
#define VIB_OFF 0x22
#define DEV_NAME "/dev/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:
*
* /project/app/TEST/src/com.example.TEST/MainActivity.java
* /project/app/TEST/src/com.example.TEST/TESTCLASS.java
*/
jstring
Java_com_example_TEST_TESTCLASS_stringFromJNI( JNIEnv* env, jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
jint
Java_com_example_TEST_TESTCLASS_IOCTL( JNIEnv* env, jobject thiz, jint fd ,jint controlcode,jint ledid)
{
/* LED */
int CTLCODE = controlcode;
int value =-1;
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 fd;
}
jint
Java_com_example_TEST_TESTCLASS_OPEN(JNIEnv* env,jobject thiz)
{
jint fd;
fd=open(DEV_NAME,O_RDWR);
return fd;
}
jint
Java_com_example_TEST_TESTCLASS_CLOSE( JNIEnv* env, jobject thiz, jint fd)
{
jint ret;
ret = close(fd);
return ret;
}
注意如果需要头文件Alog.h需要自己写,然后放在jni文件夹下的
#pragma once
#include
#define LOG_TAG "debug log"
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)
3.建立完毕,打开Cygwin工具,并进入到工程目录下的jni目录下:
$ cd d:/Program/Android/workspace/TEST /jni
$$NDK/ndk-build
这样就OK了,生成了libtest-jni.so文件了,自动生成到了工程目录下的libs/armeabi/ libtest-jni.so,发现test-jni是我们刚才在.mk文件里面的命名。
第三步:写应用程序:
1.在应用程序类com.example.TEST目录下建立一个类:TESTCLASS.java,输入如下代码,这是用来引用libtest-jni.so文件的。
package com.example.TEST;
import android.util.Log;
publicclass TESTCLASS {
public native String stringFromJNI();
public nativeint OPEN();
public nativeint IOCTL(int fd,int controlcode,int ledID);
public nativeint CLOSE(int fd);
static {
try {
System.loadLibrary("test-jni");
} catch (UnsatisfiedLinkError e) {
Log.d("HardwareControler", "HardwareControler ibrary not found!");
}
}
}
2.编写应用程序,调用TESTCLASS类中的函数OPEN()/CLOSE()/IOCTL()就可以实现底层的控制了。
添加按钮,用来打开和关闭LED灯,以及关闭驱动
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.76"
android:text="ON" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.26"
android:text="OFF" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.76"
android:text="close" />
2.编写MainActivity,添加响应函数:
package com.example.TEST;
import java.io.DataOutputStream;
import java.io.IOException;
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,btn_close;
publicstaticfinalintVIB_ON = 0x11;
publicstaticfinalintVIB_OFF = 0x22;
intfd;
intvalue = -1;
TESTCLASS mTESTCLASS1;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTESTCLASS1 = new TESTCLASS();
String s = mTESTCLASS1.stringFromJNI().toString();