Tamer Hatoum Tamer Hatoum - 4 months ago 43
Java Question

Cordova callbackContext inside a loop

I am trying to write Cordova Plugin that communicates with the PDA RFID Reader , That will run a while loop and returns the Callbackcontext to javascript , the problem that is hanging .

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;
}

Answer

the Solution is Using Plugin Result that will returns that callbackcontext without hanging

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