Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wait for channelCallback #444

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

paolosanchi
Copy link

In android I want to launch my app by scanning the nfc tag, so I followed what's specified in the doc's section "Launching your Android Application when Scanning a Tag"
The issue is that if I scan the tag with the app turned off it crashes at launch time.
It seems that the method execute(), which sets channelCallback is called after sendEvent()

So channelCallback is null and there is nothing that catches the exception

// Send the event data through a channel so the JavaScript side can fire the event
private void sendEvent(String type, JSONObject tag) {
try {
JSONObject event = new JSONObject();
event.put("type", type); // TAG_DEFAULT, NDEF, NDEF_MIME, NDEF_FORMATABLE
event.put("tag", tag); // JSON representing the NFC tag and NDEF messages
PluginResult result = new PluginResult(PluginResult.Status.OK, event);
result.setKeepCallback(true);
channelCallback.sendPluginResult(result);
} catch (JSONException e) {
Log.e(TAG, "Error sending NFC event through the channel", e);
}
}

the stack of sendEvent() is this one:
onNewIntent() -> parseMessage() -> cordova.getThreadPool().execute(()=>) -> fireNdefEvent() -> sendEvent()

So what I done is to wait until channelCallback got set.

Warning!
What I did is to add a potential infinite loop, which could potentially block the thread, but it seems that sendEvent() is executed on a thread took from the threadPool (cordova.getThreadPool().execute(()=>)) so it should be fine.

In the end, it does the job done.

Maybe the problem comes from my environment:

  • ionic (angular)
  • capacitorJs
  • phonegap codrova plugin

@paolosanchi
Copy link
Author

I've added another commit to avoid the infinite loop, that's a waste of cpu, so I've added a list of sleeping thread that has to be awaken then the channel is ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant