@CmdCBAnnotation(dev_type = "test_plugin", method_name = "test1")public void setId(int ret, String retJson) {}@CmdCBAnnotation(dev_type = "test_plugin", method_name = "test5")public void setId2(int ret, String retJson) {}
IOTCmdAttachment att = IOTCmdAttachment.getSingleton();TestPluginCallBack cbObj = new TestPluginCallBack();att. registerCmdHandlers(cbObj);
public int registerCmdHandlers(Object obj) { int status = 0; for (Method m : obj.getClass().getMethods()) { CmdCBAnnotation a = m.getAnnotation(CmdCBAnnotation.class); if (a != null) { Log.d(LOG_TAG, "" + a.dev_type() + ":" + a.method_name()); status = jni_reg_cmdcb(a.dev_type(), a.method_name(), obj, m); if (0 != status) return status; } } return status; }
static int reg_handler(jstring dev_type, jstring method_name, jobject jobj, jobject jmeth) { __android_log_print(ANDROID_LOG_INFO, TAGSTR, "reg_handler"); JNIEnv *env = get_jni_env(); std::string str_devtype = jstring2str(env, dev_type); std::string str_methname = jstring2str(env, method_name); //生成DeviceCmdHandler对象,存储设备类型名,设备方法名(即上面注解@CmdCBAnnotation dev_type, method_name值), //TestPluginCallBack对象,和被注解的java方法 DeviceCmdHandler * pt_dchdl = new DeviceCmdHandler(str_devtype, str_methname, jobj, jmeth); //DeviceCmdHandler::methodCallBack4c会使用pt_dchdl作为参数,并且它作为回调函数将被iot_sip_cli调用 int ret = cmd_cb_reg4cpp(str_devtype.c_str(), str_methname.c_str(), &DeviceCmdHandler::methodCallBack4c, pt_dchdl); if (0 != ret) { __android_log_print(ANDROID_LOG_ERROR, TAGSTR, "reg_handler fail ret %d:%s %s", ret, str_devtype.c_str(), str_methname.c_str()); } else { __android_log_print(ANDROID_LOG_INFO, TAGSTR, "reg_handler succ:%s %s", str_devtype.c_str(), str_methname.c_str()); } return ret; }