Android APP interface with I2C

Hi,

I am trying tha have an android app, that runs on board and communicate with the sensor/actuator via I2C/SPI.
I tried to find a suitable library to integrate in my app, which than can be ported on the Board, but unable to find.
I went through the documentation: 2.I2C code – Alpha-Star
The code given here is in C, which i will have to integrate in my Java based Andorid App.
Is it the only solution for achieving this communication?

Thanks,
burhan

This is a common approach to low-level access on Android platforms.

Put another way, why would an Android phone offer its applications a Java API to access an I2C peripheral?

1 Like

That makes sense.
I have another open point.
I am developing app on windows env (android studio) and will run it on Mediatek X20.
I am aiming to initiate the I2C communication from app, to do so, will integrate the I2C code given in the link mentioned in the above comment. BUT android studio will not find the Data types/functions used in that code e.g. ( i2c_msg, i2c_transfer etc), and the app will not be able to build.
Please suggest how should i proceed to build app with such a functionality, with which the app can initiate I2C/SPI connection?

The referred link points to a I2C kernel driver, so functions are kernel specific. You want to develop a user-space/Java I2C driver. it is also possible to access I2C devices from user-space via the /dev interface [1] (e.g. /dev/i2c-0). Usually user-space drivers are implemented in python or C using libraries (i2c-dev.h) providing helpers.

There is no official android Java API for I2C, so you had to open the device and perform your own read/write/ioctl according to the /dev i2c interface. Note that some people developed their own I2C manager/api [2]. you can probably reuse [3].

[1] https://www.kernel.org/doc/Documentation/i2c/dev-interface
[2] I2C
[3] https://github.com/digi-embedded/android-sample-i2c

The link of the Android SDK Add on you shared is for specific hardwrae/board (ConnectCore6).
To modify it, and make it for X20, is definitely a WORK.

I read that the Mediatek also developed Android SDK:

https://www.safedk.com/marketplace/sdks/mediatek-mediatek

But i could not find information about it anywhere on mediatel portal.
Would it be possible to access the peripherals of the mediatek Board with this, if its still available?

So maybe the simplest way is to implement I2C communication in C/C++, you can includes native code in your application via Android NDK [1]. And then use the dev i2c interface [2].

[1] Android NDK  |  Android Developers
[2] https://www.kernel.org/doc/Documentation/i2c/dev-interface

I’ve done some i2c from ndk before (I didn’t use jni, but nothing stopping you from doing so).

First;
#include <linux/i2c-dev.h>

Then you’re going to;
int addr = 0x40;
char *portname = “/dev/i2c-1”;
int fd = open (portname, O_RDWR);
if (fd < 0) return -1;
if (ioctl(fd, I2C_SLAVE, addr) < 0) return -1;

And then you just use your standard read/write functions.

I tried opening the portname “/dev/i2c-1” by using open(…), but its not successful. The return is always “-1”.
I inspected i2c-dev.h as explained in i2c dev-interface. I could not find the information related to adapter. The contents of the file :
i2c-dev

I also cannot find path “external/kernel-headers/original”, as mentioned in i2c-dev.h file.

This is a permission error (-1 EPERM errno), I suggest you to disable SEPOLICY for now ($ setenforce 0) and check the /dev/i2c-1 access rights (file modes). You can set file rights/owner at boot via the ueventd.rc file.

I am using Android Studio on Windows to develop app, that access I2C port. Then i run this app on board (through apk file). How can i disable SEPOLICY? I don’t have much experience on Linux.

You can disable SE POLICY by logging on the board, via ADB (adb shell) or via a console.

Hi again.
I caught up with something else and could not continue with this. now back.

I uploaded the android image to the Board.
By default it had root access.
I can see that Sepolicy is Permissive (by getenforce).
I still cannot see i2c-0 or any other i2c file in dev folder. (dev folder is accessible)

i can see that in sys/class/i2c-adapters, i2c devices are shown.
Please suggest how can i also able to see the i2c devices in dev folder as well.

Thanks

You need to check that kernel is built with CONFIG_I2C_CHARDEV, otherwise you will have to rebuild it.

Hi Loic,

I have successfully compiled the kernel code for mediatek x20 and now i2c is accessible from the user space.

Now I need to compile i2c-tools for the mediatek x20 board. I have tried to compile it but failed. Can you guide me about it?

I have already posted the question about it on stackoverflow but no one currently answered.
https://stackoverflow.com/questions/52790710/building-i2c-tools-for-android-mediatek-x20-board

Awais

I think arguably you are not asking the best possible question. I don’t think there will be anything mediatek specific at this point so you probably want to know about “building i2c tools for Android” since there will be a far bigger audience for that question waiting to help.

Maybe add “Marshmallow” to the your web search if you’ve already tried lots of different Android.mk files without success.