Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to open the I2C device for my android using Android studio code Java and C but I keep getting
E/fileHandle: 45
E/axoni2c: I2C: Can't set slave address

Usually fileHandle will return -1 if it can't open due to permission or something and if not it will return 0.

So I am wondering what 45 means and how to properly open the I2C.

What I have tried:

I have tried using a library called Mini6410 I2C

I set the slave address according to the sensor datasheet:
Quote:
The I2C Interface and 7-bit slave address is 0x24.

Here is the code that is returning 45:
Java
int ReadADC(int slaveAddr, int channel)
   {
       int fileHandle = 0;
       int Bits = 0;
       int[] buf = new int[4];
       try
       {
           fileHandle = i2c.open("/dev/i2c-0");
           Log.e("fileHandle", String.valueOf(fileHandle));
       }
       catch(Exception e)
       {
           Log.w(TAG, "Could not open I2C interface");
       }
       i2c.read(fileHandle, slaveAddr, buf, 3);
       Bits = buf[0]; //test buf[1], [2], [3]

I enabled root permission using ADB.

Here is some of the C code:
C++
// Open the I2C device

JNIEXPORT jint JNICALL Java_axon_test_testJNI_axonI2C_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);
    int test = open(fileName, O_RDWR);
    LOGI("test number", test)
    (*env)->ReleaseStringUTFChars(env, file, str);

    return open(fileName, O_RDWR)
}

//***************************************************************************
// Read data from the I2C device
//***************************************************************************
  
JNIEXPORT jint JNICALL Java_axon_test_testJNI_axonI2C_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_FORCE, slaveAddr);
    if (res != 0) {
        LOGE("I2C: Can't set slave address");
        goto err2;
    }


Thanks so much!
Posted
Comments
David Crow 21-Mar-21 22:36pm    
What is the value of slaveAddr at the time ioctl() is called?
ynjay 23-Mar-21 12:04pm    
Thanks for your reply! Tried 0x24,0x30 and 0x31, the sensor data sheet says: "The I2C Interface and 7-bit slave address is 0x24". 0x30 is the x-axis for the PPG waverform, and 0x31 is the y-axix.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900