Java部分的代码:
package xxxxxxx.xx;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class I2cRadioTest extends Activity {
private static final String TAG = "I2cRadioTest";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int[] buf = new int[4];
int slaveAddr = 0xXX;
int fileHander;
int mode = 0xXX | 0xXX;
int i;


I2c i2c = new I2c();
fileHander = i2c.open("/dev/i2c-1");


i2c.read(fileHander, slaveAddr, buf, 4);


Log.w(TAG,
"buf0= " + Integer.toHexString(buf[0]) + " buf1= "
+ Integer.toHexString(buf[1]) + " buf2= "
+ Integer.toHexString(buf[2]) + " buf=3 "
+ Integer.toHexString(buf[3]));


buf[0] = 0x01;
i = 0;// i2c.write(fileHander, slaveAddr, mode, buf, 1);
Log.w(TAG, "write length " + i);


for (int j = 0; j < 4; j++) {
buf[i] = 0;
}


i2c.read(fileHander, slaveAddr, buf, 4);
Log.w(TAG,
"buf0= " + Integer.toHexString(buf[0]) + " buf1= "
+ Integer.toHexString(buf[1]) + " buf2= "
+ Integer.toHexString(buf[2]) + " buf=3 "
+ Integer.toHexString(buf[3]));


i2c.close(fileHander);


if ((buf[0] & 0x10) == 0x01) {
Log.w(TAG, "------success-----");
} else {
Log.w(TAG, "----fail-------");
}


setContentView(R.layout.main);
}
}




package xxxxxx.xxx;


/**
* This is a I2C operation class
*/
public class I2c {
/**
* @param nodeName
* node path name
* @return return file hander else return <0 on fail
*/
public native int open(String nodeName);


/**
* @param fileHander
* @param i2c_adr
* slave addr
* @param buf
* @param Lenth
* of buf
* @return read length
*/
public native int read(int fileHander, int i2c_adr, int buf[], int Length);


/**
* @param fileHander
* @param i2c_adr
* slave addr
* @param sub_adr
* sub addr
* @param buf
* @param Lenth
* of buf
* @return write length
*/
public native int write(int fileHander, int i2c_adr, int sub_adr,
int buf[], int Length);


public native void close(int fileHander);


static {
System.loadLibrary("test-i2c");
}
}




JNI部分的代码:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class xxxxxx_xxx_I2c */
#include <stdio.h>
#include <android/log.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <memory.h>
#include <malloc.h>


#ifndef _Included_xxxxxx_xxx_I2c
#define _Included_xxxxxx_xxx_I2c
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: xxxxxx_xxx_I2c
* Method: open
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_open
(JNIEnv *, jobject, jstring);


/*
* Class: xxxxxx_xxx_I2c
* Method: read
* Signature: (II[II)I
*/
JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_read
(JNIEnv *, jobject, jint, jint, jintArray, jint);


/*
* Class: xxxxxx_xxx_I2c
* Method: write
* Signature: (III[II)I
*/
JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_write
(JNIEnv *, jobject, jint, jint, jint, jintArray, jint);


/*
* Class: xxxxxx_xxx_I2c
* Method: close
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_xxxxxx_xxx_I2c_close
(JNIEnv *, jobject, jint);


#ifdef __cplusplus
}
#endif
#endif






#include "test-i2c.h"


#define LOG_TAG "i2c"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)


JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_open
(JNIEnv *env, jobject obj, jstring file)
{
char fileName[64];
const jbyte *str;

str = (*env)->GetStringUTFChars(env, file, NULL);
if (str == NULL) {
LOGI("Can't get file name!");
return -1;
}
sprintf(fileName, "%s", str);
LOGI("will open i2c device node %s", fileName);
(*env)->ReleaseStringUTFChars(env, file, str);
return open(fileName, O_RDWR);
}

JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_read
(JNIEnv * env, jobject obj, jint fileHander, jint slaveAddr, jintArray bufArr, jint len)
{
jint *bufInt;
char *bufByte;
int res = 0, i = 0, j = 0;

if (len <= 0) {
LOGE("I2C: buf len <=0");
goto err0;
}

bufInt = (jint *) malloc(len * sizeof(int));
if (bufInt == 0) {
LOGE("I2C: nomem");
goto err0;
}
bufByte = (char*) malloc(len);
if (bufByte == 0) {
LOGE("I2C: nomem");
goto err1;
}

(*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);

res = ioctl(fileHander, I2C_SLAVE, slaveAddr);
if (res != 0) {
LOGE("I2C: Can't set slave address");
goto err2;
}

memset(bufByte, '\0', len);
if ((j = read(fileHander, bufByte, len)) != len) {
LOGE("read fail in i2c read jni i = %d buf 4", i);
goto err2;
} else {
for (i = 0; i < j ; i++)
bufInt[i] = bufByte[i];
LOGI("return %d %d %d %d in i2c read jni", bufByte[0], bufByte[1], bufByte[2], bufByte[3]);
(*env)->SetIntArrayRegion(env, bufArr, 0, len, bufInt);
}

free(bufByte);
free(bufInt);

return j;


err2:
free(bufByte);
err1:
free(bufInt);
err0:
return -1;
}

JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_write
(JNIEnv *env, jobject obj, jint fileHander, jint slaveAddr, jint mode,
jintArray bufArr, jint len)
{
jint *bufInt;
char *bufByte;
int res = 0, i = 0, j = 0;

if (len <= 0) {
LOGE("I2C: buf len <=0");
goto err0;
}

bufInt = (jint *) malloc(len * sizeof(int));
if (bufInt == 0) {
LOGE("I2C: nomem");
goto err0;
}
bufByte = (char*) malloc(len + 1);
if (bufByte == 0) {
LOGE("I2C: nomem");
goto err1;
}

(*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);
bufByte[0] = mode;
for (i = 0; i < len; i++)
bufByte[i + 1] = bufInt[i];

res = ioctl(fileHander, I2C_SLAVE, slaveAddr);
if (res != 0) {
LOGE("I2C: Can't set slave address");
goto err2;
}

if ((j = write(fileHander, bufByte, len + 1)) != len + 1) {
LOGE("write fail in i2c");
goto err2;
}

LOGI("I2C: write %d byte", j);
free(bufByte);
free(bufInt);

return j - 1;


err2:
free(bufByte);
err1:
free(bufInt);
err0:
return -1;
}

JNIEXPORT void JNICALL Java_xxxxxx_xxx_I2c_close
(JNIEnv *env, jobject obj, jint fileHander)
{
close(fileHander);
}



更多相关文章

  1. Android 中如何自己通过代码绘图
  2. android 使用代码实现 RelativeLayout布局
  3. android 亮屏及屏幕解锁代码
  4. Android屏蔽home键的代码,咋摁也不响应的方法
  5. Android6.0 Audio系统代码流程
  6. 如何下载Android kernel内核源代码,编译烧写验证
  7. android 自定义Android菜单背景的代码
  8. android中隐藏以及显示软键盘代码
  9. Android 开发常用代码片段

随机推荐

  1. Android的UI结构试图工具hierarchyviewer
  2. Android(安卓)安全加密:非对称加密详解
  3. XML布局
  4. Android内存管理基本介绍
  5. Android异步处理系列文章四篇之二 使用As
  6. 如何在Android上安装apk软件
  7. Android异步处理二:使用AsyncTask异步更新
  8. Android(安卓)多线程之synchronized锁住
  9. 二十四、Android文件的读写
  10. android中的ellipsize设置(省略号的问题)