I recently tried to use JNI interface since my project target both iOS and Androïd platform.
I looked to the SimpleAudioEngine example as it’s interfaced with JNI but I couldn’t find a way to make what I need.
I would like to get device informations like the model and the os version of the platform so I create a DeviceDetection class in java like this :
`package com.seventhside;
import android.util.DisplayMetrics;
public class DeviceDetection {
public DeviceDetection()
{
DisplayMetrics metrics = new DisplayMetrics();
_deviceSystemName = android.os.Build.VERSION.CODENAME;
_deviceSystemVersion = android.os.Build.VERSION.RELEASE;
_deviceModel = android.os.Build.MODEL;
_deviceResolutionWidth = metrics.widthPixels;
_deviceResolutionHeight = metrics.heightPixels;
}
private String _deviceSystemName;
private String _deviceSystemVersion;
private String _deviceModel;
private int _deviceResolutionWidth;
private int _deviceResolutionHeight;
//Getters-------------------------------------------------
public String deviceSystemName()
{
return _deviceSystemName;
}
public String deviceSystemVersion()
{
return _deviceSystemVersion;
}
public String deviceModel()
{
return _deviceModel;
}
public int deviceResolutionWidth()
{
return _deviceResolutionWidth;
}
public int deviceResolutionHeight()
{
return _deviceResolutionHeight;
}
}`
Then a create the .h and .cpp file to wrap with JNI env. For the .h with juste a function to get the SystemName :
`#include <jni.h>
extern “C”
{
extern void deviceSystemNameJNI();
}`
and for the .cpp inspired from what I’ve found in SimpleAudioEngine :
`#include “DeviceDetectionJNI.h”
#include “platform/android/jni/JniHelper.h”
#include <jni.h>
#include <android/log.h>
#define LOG_TAG “deviceDetection”
#define LOGD(…) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,VA_ARGS)
static JNIEnv* getJNIEnv(void)
{
JavaVM* jvm = cocos2d::JniHelper::getJavaVM();
if (NULL == jvm) {
LOGD("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL");
return NULL;
}
JNIEnv *env = NULL;
// get jni environment
jint ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_4);
}
}
static jclass getClassID(JNIEnv *pEnv)
{
jclass ret = pEnv->FindClass(CLASS_NAME);
if (! ret)
{
LOGD(“Failed to find class of %s”, CLASS_NAME);
}
return ret;
}
static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *methodName, const char *paramCode)
{
jmethodID methodID = 0;
JNIEnv *pEnv = 0;
bool bRet = false;
do
{
pEnv = getJNIEnv();
if (! pEnv)
{
break;
}
jclass classID = getClassID(pEnv);
methodID = pEnv->GetStaticMethodID(classID, methodName, paramCode);
if (! methodID)
{
LOGD("Failed to find static method id of %s", methodName);
break;
}
methodinfo.classID = classID;
methodinfo.env = pEnv;
methodinfo.methodID = methodID;
bRet = true;
} while (0);
return bRet;
}
//DeviceDetection methods
void deviceSystemNameJNI()
{
JniMethodInfo methodInfo;
if (! getStaticMethodInfo(methodInfo, "deviceSystemName", "()V"))
{
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
Then I add the DeviceDetection.h and .cpp to allow C++ calls to this :
#include “Export.h”
class EXPORT_DLL DeviceDetection
{
public:
DeviceDetection();
~DeviceDetection();
static DeviceDetection* sharedDeviceDetection();
static void end();
void deviceSystemName();
};`
and
`#include “DeviceDetection.h”
#include “DeviceDetectionJNI.h”
static DeviceDetection* pDeviceDetection = nullptr;
DeviceDetection* DeviceDetection::sharedDeviceDetection()
{
if (! pDeviceDetection)
{
pDeviceDetection = new DeviceDetection();
}
return pDeviceDetection;
}
void DeviceDetection::deviceSystemName()
{
deviceSystemNameJNI();
}
void DeviceDetection::end()
{
endJNI();
}`
But I don’t exactly find what to do in my main.cpp to get my values so I tried with
DeviceDetection::sharedDeviceDetection()~~>deviceSystemName;
but Eclipse says :
Multiple markers at this line
~~ expected constructor, destructor, or type conversion before
‘deviceSystemName’
- expected type-specifier before ‘deviceSystemName’
And lastly I tried :
DeviceDetection* myInfo = DeviceDetection::sharedDeviceDetection();
myInfo->deviceSystemName();
But this leads to a “myInfo” does not name a type.
I currently have some difficulties to get that working. Sorry for the presentation but Inline coding does weird things :o
Could someone help on what’s wrong ?