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——AutoCompleteTextView的使用
  2. android 亮屏及屏幕解锁代码
  3. Android(安卓)中如何自己通过代码绘图
  4. Android中Broadcast的Intent大全
  5. android 使用代码实现 RelativeLayout布局
  6. android机顶盒获取有线mac
  7. android 自定义Android菜单背景的代码
  8. 如何下载Android(安卓)kernel内核源代码,编译烧写验证
  9. Android6.0 Audio系统代码流程

随机推荐

  1. SQL中函数 replace 的参数1的数据类型nte
  2. SQLServer 参数化查询经验分享
  3. sqlserver 2000数据库同步 同步两个SQLSe
  4. sqlserver中查找所有包含了某个文本的存
  5. BCP 大容量数据导入导出工具使用步骤
  6. SQLServer XML查询18句话入门教程
  7. SQL CONVERT转化函数使用方法小结
  8. MMC提示不能打开文件SQLServerEnterprise
  9. sqlserver 错误602,未能在sysindexes中找
  10. SQL 超时解决方案 有时并不是设置问题