android访问i2c&&Android下Java通过JNI访问硬件

http://blog.csdn.net/yanjiashang/article/details/6791830

  1. class I2c {  
  2. public native int open(String path);  
  3. public native int read(int fileHander, int slaveAddr, int buf[], int bufLen);  
  4. public native int write(int fileHander, int slaveAddr, int buf[], int bufLen);  
  5. public native void close(int fileHander);  
  6. }  
  7.   
  8. class I2cTest {  
  9. int file;  
  10. int buf[64];  
  11. /*注意i2c的地址是七位,实际用的时候,看你的i2c的驱动,对这块是怎么处理的*/  
  12. int slaveAddr = 0xnn  
  13. int mode = 0x00;  
  14.   
  15. I2c i2c = new I2c();  
  16. file = i2c.open("/dev/i2c-1");  
  17. /*根据你想得到的值填len*/  
  18. i2c.read(file, slaveAddr, buf, 4);  
  19. /*根据你要控制的设备datasheet填mode*/  
  20. buf[0] = mode;  
  21. buf[1] = 0x01;  
  22. i2c.write(file, slave, buf, 2);  
  23. i2c.close();  
  24. }    
  25.   
  26. 用javah生成I2c nativ本地的头文件,以下头文件是我乱写的,不要抄。  
  27.   
  28. JNI_com_androi_I2c_open(evn ,obj, path)  
  29. {  
  30.  char pathName[64];  
  31.     
  32.  /*用jni函数将java中string path变为c中的char,在这里用那个utf-8那个函数*/  
  33.  (*env)->getstring.....(pathName, buf);/*自己查把记不清了*/  
  34.     
  35.  open(pathName);  
  36.  /*别忘了错误处理*/  
  37. }  
  38.   
  39.   
  40. JNI_com_android_I2c_write(evn, obj, fileHander, slaveAddr, buf, bufLen)  
  41. {  
  42.   char *bufByte;  
  43.   int *bufInt;  
  44.      
  45.   bufByte = malloc(bufLen);  
  46.   bufInt = malloc(bufLen);  
  47.      
  48.   (*env)->getIntArrRegion(bufInt, buf);  
  49.      
  50.      
  51.   /*只所以将int转为Byte,是因为驱动用的是byte*/  
  52.   for (i = 0; i < bufLen; i++) {  
  53.   bufByte[i] = bufInt[i];  
  54.   }  
  55.      
  56.   ioctl(fileHander, SLAVE_ADDR, slaveAddr);  
  57.      
  58.   write(fileHander, buf, bufLen);  
  59. }  
  60.   
  61. JNI_com_androi_I2c_read(evn, obj, fileHander, slaveAddr, buf, bufLen)  
  62. {  
  63. /*和read一样*/  
  64. read(fileHander, buf, bufLen);  
  65.   
  66.   for (i = 0; i < bufLen; i++) {  
  67.   bufInt[i] = bufByte[i];  
  68.   }  
  69.      
  70.   /*意思是将读取的值,返回到VM中*/  
  71.   (*env)->setIntArrRegion(bufInt);  
  72.   
  73. }  
  74.   
  75. JNI_com_android_I2c_close(fileHander)  
  76. {  
  77. close(fileHander);  

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Android下Java通过JNI访问硬件

  1. Java code  
  2.   
  3. package xxxxxxx.xx;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8.   
  9. public class I2cRadioTest extends Activity {  
  10.     private static final String TAG = "I2cRadioTest";  
  11.   
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         int[] buf = new int[4];  
  17.         int slaveAddr = 0xXX;  
  18.         int fileHander;  
  19.         int mode = 0xXX | 0xXX;  
  20.         int i;  
  21.   
  22.         I2c i2c = new I2c();  
  23.         fileHander = i2c.open("/dev/i2c-1");  
  24.   
  25.         i2c.read(fileHander, slaveAddr, buf, 4);  
  26.   
  27.         Log.w(TAG,  
  28.                 "buf0= " + Integer.toHexString(buf[0]) + " buf1= "  
  29.                         + Integer.toHexString(buf[1]) + " buf2= "  
  30.                         + Integer.toHexString(buf[2]) + " buf=3 "  
  31.                         + Integer.toHexString(buf[3]));  
  32.   
  33.         buf[0] = 0x01;  
  34.         i = 0;// i2c.write(fileHander, slaveAddr, mode, buf, 1);  
  35.         Log.w(TAG, "write length " + i);  
  36.   
  37.         for (int j = 0; j < 4; j++) {  
  38.             buf[i] = 0;  
  39.         }  
  40.   
  41.         i2c.read(fileHander, slaveAddr, buf, 4);  
  42.         Log.w(TAG,  
  43.                 "buf0= " + Integer.toHexString(buf[0]) + " buf1= "  
  44.                         + Integer.toHexString(buf[1]) + " buf2= "  
  45.                         + Integer.toHexString(buf[2]) + " buf=3 "  
  46.                         + Integer.toHexString(buf[3]));  
  47.   
  48.         i2c.close(fileHander);  
  49.   
  50.         if ((buf[0] & 0x10) == 0x01) {  
  51.             Log.w(TAG, "------success-----");  
  52.         } else {  
  53.             Log.w(TAG, "----fail-------");  
  54.         }  
  55.   
  56.         setContentView(R.layout.main);  
  57.     }  
  58. }  
  59.   
  60.   
  61. Java code  
  62.   
  63. package xxxxxx.xxx;  
  64.   
  65. /** 
  66.  * This is a I2C operation class 
  67.  */  
  68. public class I2c {  
  69.     /** 
  70.      * @param nodeName 
  71.      *            node path name 
  72.      * @return return file hander else return <0 on fail 
  73.      */  
  74.     public native int open(String nodeName);  
  75.   
  76.     /** 
  77.      * @param fileHander 
  78.      * @param i2c_adr 
  79.      *            slave addr 
  80.      * @param buf 
  81.      * @param Lenth 
  82.      *            of buf 
  83.      * @return read length 
  84.      */  
  85.     public native int read(int fileHander, int i2c_adr, int buf[], int Length);  
  86.   
  87.     /** 
  88.      * @param fileHander 
  89.      * @param i2c_adr 
  90.      *            slave addr 
  91.      * @param sub_adr 
  92.      *            sub addr 
  93.      * @param buf 
  94.      * @param Lenth 
  95.      *            of buf 
  96.      * @return write length 
  97.      */  
  98.     public native int write(int fileHander, int i2c_adr, int sub_adr,  
  99.             int buf[], int Length);  
  100.   
  101.     public native void close(int fileHander);  
  102.   
  103.     static {  
  104.         System.loadLibrary("test-i2c");  
  105.     }  
  106. }  
  107.   
  108.   
  109. C/C++ code  
  110.   
  111. /* DO NOT EDIT THIS FILE - it is machine generated */  
  112. #include <jni.h>  
  113. /* Header for class xxxxxx_xxx_I2c */  
  114. #include <stdio.h>  
  115. #include <android/log.h>  
  116. #include <fcntl.h>  
  117. #include <linux/i2c.h>  
  118. #include <memory.h>  
  119. #include <malloc.h>  
  120.   
  121. #ifndef _Included_xxxxxx_xxx_I2c  
  122. #define _Included_xxxxxx_xxx_I2c  
  123. #ifdef __cplusplus  
  124. extern "C" {  
  125. #endif  
  126. /* 
  127.  * Class:     xxxxxx_xxx_I2c 
  128.  * Method:    open 
  129.  * Signature: (Ljava/lang/String;)I 
  130.  */  
  131. JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_open  
  132.   (JNIEnv *, jobject, jstring);  
  133.   
  134. /* 
  135.  * Class:     xxxxxx_xxx_I2c 
  136.  * Method:    read 
  137.  * Signature: (II[II)I 
  138.  */  
  139. JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_read  
  140.   (JNIEnv *, jobject, jint, jint, jintArray, jint);  
  141.   
  142. /* 
  143.  * Class:     xxxxxx_xxx_I2c 
  144.  * Method:    write 
  145.  * Signature: (III[II)I 
  146.  */  
  147. JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_write  
  148.   (JNIEnv *, jobject, jint, jint, jint, jintArray, jint);  
  149.   
  150. /* 
  151.  * Class:     xxxxxx_xxx_I2c 
  152.  * Method:    close 
  153.  * Signature: (I)V 
  154.  */  
  155. JNIEXPORT void JNICALL Java_xxxxxx_xxx_I2c_close  
  156.   (JNIEnv *, jobject, jint);  
  157.   
  158. #ifdef __cplusplus  
  159. }  
  160. #endif  
  161. #endif  
  162.   
  163.   
  164.   
  165. C/C++ code  
  166.   
  167. #include "test-i2c.h"  
  168.   
  169. #define  LOG_TAG    "i2c"  
  170. #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)  
  171. #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)  
  172.   
  173. JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_open  
  174.   (JNIEnv *env, jobject obj, jstring file)  
  175.   {  
  176.       char fileName[64];  
  177.       const jbyte *str;  
  178.         
  179.       str = (*env)->GetStringUTFChars(env, file, NULL);  
  180.       if (str == NULL) {  
  181.           LOGI("Can't get file name!");  
  182.           return -1;  
  183.       }  
  184.       sprintf(fileName, "%s", str);  
  185.     LOGI("will open i2c device node %s", fileName);  
  186.       (*env)->ReleaseStringUTFChars(env, file, str);  
  187.       return open(fileName, O_RDWR);  
  188.   }  
  189.     
  190. JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_read  
  191.   (JNIEnv * env, jobject obj, jint fileHander, jint slaveAddr, jintArray bufArr, jint len)  
  192.   {  
  193.       jint *bufInt;  
  194.       char *bufByte;  
  195.       int res = 0, i = 0, j = 0;  
  196.         
  197.       if (len <= 0) {  
  198.           LOGE("I2C: buf len <=0");  
  199.           goto err0;  
  200.       }  
  201.         
  202.       bufInt = (jint *) malloc(len * sizeof(int));  
  203.       if (bufInt == 0) {  
  204.           LOGE("I2C: nomem");  
  205.           goto err0;  
  206.       }  
  207.       bufByte = (char*) malloc(len);  
  208.       if (bufByte == 0) {  
  209.           LOGE("I2C: nomem");  
  210.           goto err1;  
  211.       }  
  212.         
  213.       (*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);  
  214.         
  215.       res = ioctl(fileHander, I2C_SLAVE, slaveAddr);  
  216.       if (res != 0) {  
  217.           LOGE("I2C: Can't set slave address");  
  218.           goto err2;  
  219.       }  
  220.         
  221.       memset(bufByte, '\0', len);  
  222.       if ((j = read(fileHander, bufByte, len)) != len) {  
  223.         LOGE("read fail in i2c read jni i = %d buf 4", i);  
  224.         goto err2;          
  225.     } else {  
  226.         for (i = 0; i < j ; i++)  
  227.             bufInt[i] = bufByte[i];  
  228.         LOGI("return %d %d %d %d in i2c read jni", bufByte[0], bufByte[1], bufByte[2], bufByte[3]);  
  229.         (*env)->SetIntArrayRegion(env, bufArr, 0, len, bufInt);  
  230.     }  
  231.       
  232.     free(bufByte);  
  233.     free(bufInt);  
  234.       
  235.     return j;  
  236.   
  237. err2:  
  238.     free(bufByte);  
  239. err1:  
  240.     free(bufInt);  
  241. err0:  
  242.     return -1;                                            
  243.   }  
  244.     
  245. JNIEXPORT jint JNICALL Java_xxxxxx_xxx_I2c_write  
  246.   (JNIEnv *env, jobject obj, jint fileHander, jint slaveAddr, jint mode,  
  247.    jintArray bufArr, jint len)  
  248.   {  
  249.       jint *bufInt;  
  250.       char *bufByte;  
  251.       int res = 0, i = 0, j = 0;  
  252.         
  253.       if (len <= 0) {  
  254.           LOGE("I2C: buf len <=0");  
  255.           goto err0;  
  256.       }  
  257.         
  258.       bufInt = (jint *) malloc(len * sizeof(int));  
  259.       if (bufInt == 0) {  
  260.           LOGE("I2C: nomem");  
  261.           goto err0;  
  262.       }  
  263.       bufByte = (char*) malloc(len + 1);  
  264.       if (bufByte == 0) {  
  265.           LOGE("I2C: nomem");  
  266.           goto err1;  
  267.       }  
  268.         
  269.       (*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);  
  270.       bufByte[0] = mode;  
  271.       for (i = 0; i < len; i++)  
  272.           bufByte[i + 1] = bufInt[i];        
  273.         
  274.       res = ioctl(fileHander, I2C_SLAVE, slaveAddr);  
  275.       if (res != 0) {  
  276.           LOGE("I2C: Can't set slave address");  
  277.           goto err2;  
  278.       }  
  279.         
  280.       if ((j = write(fileHander, bufByte, len + 1)) != len + 1) {  
  281.         LOGE("write fail in i2c");  
  282.         goto err2;          
  283.     }  
  284.       
  285.     LOGI("I2C: write %d byte", j);  
  286.     free(bufByte);  
  287.     free(bufInt);  
  288.       
  289.     return j - 1;  
  290.   
  291. err2:  
  292.     free(bufByte);  
  293. err1:  
  294.     free(bufInt);  
  295. err0:  
  296.     return -1;       
  297.   }  
  298.     
  299. JNIEXPORT void JNICALL Java_xxxxxx_xxx_I2c_close  
  300.   (JNIEnv *env, jobject obj, jint fileHander)  
  301.   {  
  302.       close(fileHander);  
  303.   } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值