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:
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];
I enabled root permission using ADB.
Here is some of the C code:
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;
}
int test = open(fileName, O_RDWR);
LOGI("test number", test)
(*env)->ReleaseStringUTFChars(env, file, str);
return open(fileName, O_RDWR)
}
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!