Interfacing Hardware

From InCircuit
Jump to: navigation, search

Java doesn't allow any direct access to a devices hardware. To fulfill this task, any hardware must be abstracted using the Java Native Interface (JNI). This usually consists of a C-Library performing the real access and an interface for java to access this library. Using these libraries and interfaces, android and custom apps can make use of the hardware.

To generate such a library, the Android NDK must be installed in addition to the Android SDK. This can be found [[1]]

Apps using the Android NDK should be built using a Linux-based Host PC.

To provide an example, using the NDK, the GPIO demo App should be used here.

In addition to the usual folders of an Android App, a further folder named jni should exist. Within this folder a file named should exist as well as a couple of C code and header files. The C-files contain the code and function, which will be called.

The file has a syntax similar to a generic Makefile, describing which files will be compiled and linked. To built this library, a terminal should be opened within the projects folder and the following command should be executed

 user@host:~/workspace/gpioDemo$ /path/to/android-ndk/ndk-build

Afterwards the library was created within the folder libs, but the functions to use has to be declared to the Java environment. In the GPIO demo App, this task is fulfilled by the object ADBJni within the package de.incircuit.hw.adbjni. Now the native functions can be used.

The GPIO demo uses native code to read and write to a couple of special files. This way GPIOs, Buzzer, counting inputs and LEDs can be controled.

More examples can be found within the samples directory of the Android NDK.

Further information can be found at the Android developer sites: [[2]]

Personal tools