调用java静态方法
jclass led = env->GetObjectClass(jclassled);
// 获取id
jmethodID getLedId = env->GetStaticMethodID(led, "getLedId", "()I");
LOGE("#######getLedId \n");
if (getLedId == NULL)
{
LOGE("#######error getLedId\n");
return -1; /* method not found */
}
jint id = env->CallIntMethod(led, getLedId);
LOGE("#######CallIntMethod \n");
// 获取color
jmethodID getColor = env->GetStaticMethodID(led, "getColor", "()[I");
if (getColor == NULL)
{
LOGE("#######error getColor\n");
return -1; /* method not found */
}
jint color[3] = {0};
jobjectArray resultArray = (jobjectArray) env->CallObjectMethod(led, getColor);
for (int i = 0; i < env->GetArrayLength(resultArray); i++)
{
color[i] = (jint) env->GetObjectArrayElement(resultArray, i);
}
LOGE("#######id = %d, r=%d, g=%d, b=%d\n", id, color[0], color[1], color[2]);
// 设置led
set_led(id, color[0], color[1], color[2]);
调用java实例方法
jclass led = env->FindClass("com/deptech/common/base/utils/commonUtils/Led");
if (led == NULL)
{
LOGE("#######not found class.\n");
return -1;
}
// 构造方法
jmethodID mid_construct = env->GetMethodID(led, "","()V");
if (mid_construct == NULL)
{
LOGE("#######not found construct func.\n");
return -1;
}
// 获取getLedId方法
jmethodID getLedId = env->GetMethodID(led, "getLedId", "()I");
if (getLedId == NULL)
{
LOGE("#######not found getLedId func.\n");
return -1;
}
jmethodID getColorR = env->GetMethodID(led, "getColorR", "()I");
if (getColorR == NULL)
{
LOGE("#######not found getColorR func.\n");
return -1;
}
jmethodID getColorG = env->GetMethodID(led, "getColorG", "()I");
if (getColorG == NULL)
{
LOGE("#######not found getColorG func.\n");
return -1;
}
jmethodID getColorB = env->GetMethodID(led, "getColorB", "()I");
if (getColorB == NULL)
{
LOGE("#######not found getColorB func.\n");
return -1;
}
// 使用构造方法实例化
jobject ledObj = env->NewObject(led, mid_construct);
if (ledObj == NULL) {
LOGE("#######error NewObject.\n");
return -1;
}
// 调用实例方法
// 获取id
jint id = env->CallIntMethod(ledObj, getLedId);
LOGE("#######CallIntMethod %d.\n", id);
// 获取color
jint r, g, b;
r = env->CallIntMethod(ledObj, getColorR);
g = env->CallIntMethod(ledObj, getColorG);
b = env->CallIntMethod(ledObj, getColorB);
jobjectArray resultArray = (jobjectArray) env->CallObjectMethod(ledObj, getColor);
if (resultArray == NULL)
{
LOGE("#######error resultArray is null.\n");
return -1;
}
for (int i = 0; i < env->GetArrayLength(resultArray); i++)
{
color[i] = (long)env->GetObjectArrayElement(resultArray, i);
}
LOGE("#######CallObjectMethod.\n");
// 设置led
LOGE("#######id = %d, r=%d, g=%d, b=%d\n", id, r, g, b);
set_led(id, r, g, b);
// 释放
env->DeleteLocalRef(led);
env->DeleteLocalRef(ledObj);
//env->DeleteLocalRef(resultArray);