Hello ugobyte,
sorry for late response. Maybe you have found the solution already?
Anyway here is a small receipt:
First read (if you not have yet), how to add jni code to your AndroidStudio project:
https://developer.android.com/studio/pro...ative-code
You will have or add a cpp folder in
src/main. There you create a Cmake makefile CMakeLists.txt, the wiring.h from TB wiringPi source code and a cpp library as e.g. wpi_android.cpp. The cpp library acts as bridge between the wiringPi C code and the Java code.
A CMakeLists.txt (to compile the wpi_android.cpp):
Code:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
find_library( log-lib log )
add_library( wiringPi SHARED IMPORTED )
set_target_properties( wiringPi PROPERTIES IMPORTED_LOCATION
/path/to/wiringPi/libs/${ANDROID_ABI}/libwiringPi.so )
add_library( # Sets the name of the library.
wpi_android
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
wpi_android.cpp )
target_include_directories(wpi_android PUBLIC
/path/to/wiringPi/jni/wiringPi
)
target_link_libraries( # Specifies the target library.
wpi_android
# Links the target library to the log library
# included in the NDK.
${log-lib}
wiringPi
)
You would need to correct the paths to your needs.
A wpi_android.cpp:
Code:
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <string.h>
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOG_TAG "wpi_android"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/system_properties.h>
#include <softPwm.h>
#include <dlfcn.h>
#include "wiringPi.h"
#ifdef __cplusplus
extern "C" {
#endif
jint Java_com_jw_wiringpi_wpiAndroid_wiringPiSetupSys(JNIEnv* env, jobject obj) {
LOGI("Entering wiringPiSetupSys");
return wiringPiSetupSys();
}
jint Java_com_jw_wiringpi_wpiAndroid_wiringPiSetup(JNIEnv* env, jobject obj) {
LOGI("Entering wiringPiSetup");
return wiringPiSetup();
}
//void pullUpDnControl (int pin, int pud)
void Java_com_jw_wiringpi_wpiAndroid_pullUpDnControl(JNIEnv* env, jobject obj, jint port, jint pud) {
return pullUpDnControl(port, pud);
}
//void pinMode (int pin, int mode)
void Java_com_jw_wiringpi_wpiAndroid_pinMode(JNIEnv* env, jobject obj, jint port, jint mode) {
return pinMode(port, mode);
}
//void pinModeAlt (int pin, int mode)
void Java_com_jw_wiringpi_wpiAndroid_pinModeAlt(JNIEnv* env, jobject obj, jint port, jint mode) {
pinModeAlt(port, mode);
}
jint Java_com_jw_wiringpi_wpiAndroid_digitalRead(JNIEnv* env, jobject obj, jint port) {
return digitalRead(port);
}
//int getAlt (int pin)
jint Java_com_jw_wiringpi_wpiAndroid_getAlt(JNIEnv* env, jobject obj, jint port) {
return getAlt(port);
}
void Java_com_jw_wiringpi_wpiAndroid_digitalWrite(JNIEnv* env, jobject obj, jint port, jint onoff) {
digitalWrite(port, onoff);
}
jint Java_com_jw_wiringpi_wpiAndroid_analogRead(JNIEnv* env, jobject obj, jint port) {
return analogRead(port);
}
jint Java_com_jw_wiringpi_wpiAndroid_softPwmCreate(JNIEnv* env, jobject obj, jint port, jint value, jint range) {
return softPwmCreate(port, value, range);
}
void Java_com_jw_wiringpi_wpiAndroid_softPwmWrite(JNIEnv* env, jobject obj, jint port, jint value) {
softPwmWrite(port, value);
}
void Java_com_jw_wiringpi_wpiAndroid_softPwmStop(JNIEnv* env, jobject obj, jint port) {
softPwmStop(port);
}
void Interrupt(void) {
LOGI("Interrupt");
}
jint Java_com_jw_wiringpi_wpiAndroid_wiringPiISR(JNIEnv* env, jobject obj, jint port, jint edge) {
return wiringPiISR (port, edge, &Interrupt);
}
#ifdef __cplusplus
}
#endif
Note the syntax of the functions: Java_com_jw_wiringpi_wpiAndroid_wiringPiSetup. This points to a Java class com.jw.wiringpi.wpiAndroid. But you are free to rename this to your own needs. I for myself stay with this and add a package to my AndroidStudio project
com.jw.wiringpi and a class
wpiAndroid.
In app/build.gradle there must be added
externalNativeBuild.cmake.path = "src/main/cpp/CMakeLists.txt" to make AndroidStudio find the CmakeLists.txt.
With this, the wpi_android.cpp should be compiled during build process.
The Java class wpiAndroid:
Code:
package com.jw.wiringpi;
/**
* Created by joerg on 16.09.17.
*/
public final class wpiAndroid {
static {
System.loadLibrary("wpi_android");
}
static public native int wiringPiSetup();
static public native int wiringPiSetupSys();
static public native void digitalWrite(int port, int onoff);
static public native int digitalRead(int port);
static public native void pullUpDnControl(int port, int pud);
static public native void pinMode(int port, int mode);
static public native int softPwmCreate(int port, int value, int range);
static public native void softPwmWrite (int port, int value) ;
static public native void softPwmStop (int port) ;
}
In you app code to initialize wiringPi, e.g. in onCreate():
Code:
if (wiringPiSetup() != 0) {
Log.e(TAG, "Error setting up wiringPi!");
}
If AndroidStudio doesn't find the function by itself, you can add a import statement:
Code:
import static com.jw.wiringpi.wpiAndroid.wiringPiSetup;
Somewhere in your code to read a gpio:
Code:
int in = digitalRead(24)
Or to write a gpio:
If there is no gpiomem driver with permission 666 to /dev/gpiomem, the access to /dev/mem is restricted and you would need root access with your app!
Hope it helps an happy coding.