http://blog.csdn.net/yanjiashang/article/details/6791830
- class I2c {
- public native int open(String path);
- public native int read(int fileHander, int slaveAddr, int buf[], int bufLen);
- public native int write(int fileHander, int slaveAddr, int buf[], int bufLen);
- public native void close(int fileHander);
- }
- class I2cTest {
- int file;
- int buf[64];
- /*注意i2c的地址是七位,实际用的时候,看你的i2c的驱动,对这块是怎么处理的*/
- int slaveAddr = 0xnn
- int mode = 0x00;
- I2c i2c = new I2c();
- file = i2c.open("/dev/i2c-1");
- /*根据你想得到的值填len*/
- i2c.read(file, slaveAddr, buf, 4);
- /*根据你要控制的设备datasheet填mode*/
- buf[0] = mode;
- buf[1] = 0x01;
- i2c.write(file, slave, buf, 2);
- i2c.close();
- }
- 用javah生成I2c nativ本地的头文件,以下头文件是我乱写的,不要抄。
- JNI_com_androi_I2c_open(evn ,obj, path)
- {
- char pathName[64];
- /*用jni函数将java中string path变为c中的char,在这里用那个utf-8那个函数*/
- (*env)->getstring.....(pathName, buf);/*自己查把记不清了*/
- open(pathName);
- /*别忘了错误处理*/
- }
- JNI_com_android_I2c_write(evn, obj, fileHander, slaveAddr, buf, bufLen)
- {
- char *bufByte;
- int *bufInt;
- bufByte = malloc(bufLen);
- bufInt = malloc(bufLen);
- (*env)->getIntArrRegion(bufInt, buf);
- /*只所以将int转为Byte,是因为驱动用的是byte*/
- for (i = 0; i < bufLen; i++) {
- bufByte[i] = bufInt[i];
- }
- ioctl(fileHander, SLAVE_ADDR, slaveAddr);
- write(fileHander, buf, bufLen);
- }
- JNI_com_androi_I2c_read(evn, obj, fileHander, slaveAddr, buf, bufLen)
- {
- /*和read一样*/
- read(fileHander, buf, bufLen);
- for (i = 0; i < bufLen; i++) {
- bufInt[i] = bufByte[i];
- }
- /*意思是将读取的值,返回到VM中*/
- (*env)->setIntArrRegion(bufInt);
- }
- JNI_com_android_I2c_close(fileHander)
- {
- close(fileHander);
- }
- Java code
- 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);
- }
- }
- Java code
- 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");
- }
- }
- C/C++ code
- /* 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
- C/C++ code
- #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);
- }