Cordova callbackContext里面有一个循环

时间:2016-06-10 11:26:01

标签: java cordova

我正在尝试编写与PDA RFID阅读器通信的Cordova插件,这将运行一个while循环并将Callbackcontext返回到javascript,这个问题就是悬而未决。

public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    if (action.equals("READALL")) {
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {

                cmdCode = Constants.CMD_ISO18000_6C_INVENTORY;
                while (runFlag) {
                    epcBytesList = cmdManager.inventoryRealTime();
                    for (int i = 0; i < epcBytesList.size(); i++) {
                        byte[] epcByte = epcBytesList.get(i);
                        recvEPC = Tools.Bytes2HexString(epcByte, epcByte.length);
                        // echo(recvEPC, callbackContext);
                        PluginResult result = new PluginResult(PluginResult.Status.OK, recvEPC);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        /// callbackContext.error("Failed to parse
                        /// parameters");
                    }
                }
            }
        });

        return true;
    }
    return false;
}   

1 个答案:

答案 0 :(得分:0)

解决方案是使用插件结果,它将返回该callbackcontext而不挂起

  PluginResult result = new PluginResult(PluginResult.Status.OK, recvEPC);
                    result.setKeepCallback(true);
                    callbackContext.sendPluginResult(result);