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

NFC events not firing on first scan Fixes #217 (and others) #324

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private String getNfcStatus() {

private void registerDefaultTag(CallbackContext callbackContext) {
addTagFilter();
restartNfc();
callbackContext.success();
}

Expand All @@ -205,11 +206,13 @@ private void removeDefaultTag(CallbackContext callbackContext) {

private void registerNdefFormatable(CallbackContext callbackContext) {
addTechList(new String[]{NdefFormatable.class.getName()});
restartNfc();
callbackContext.success();
}

private void registerNdef(CallbackContext callbackContext) {
addTechList(new String[]{Ndef.class.getName()});
restartNfc();
callbackContext.success();
}

Expand Down Expand Up @@ -248,6 +251,7 @@ private void registerMimeType(JSONArray data, CallbackContext callbackContext) t
try {
mimeType = data.getString(0);
intentFilters.add(createIntentFilter(mimeType));
restartNfc();
callbackContext.success();
} catch (MalformedMimeTypeException e) {
callbackContext.error("Invalid MIME Type " + mimeType);
Expand Down Expand Up @@ -467,7 +471,13 @@ private void startNfc() {

if (nfcAdapter != null && !getActivity().isFinishing()) {
try {
nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), getIntentFilters(), getTechLists());
IntentFilter[] intentFilters = getIntentFilters();
String[][] techLists = getTechLists();
// don't start NFC unless some intent filters or tech lists have been added,
// because empty lists act as wildcards and receives ALL scan events
if (intentFilters.length > 0 || techLists.length > 0) {
nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), intentFilters, techLists);
}

if (p2pMessage != null) {
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
Expand Down
2 changes: 1 addition & 1 deletion www/phonegap-nfc.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ require('cordova/channel').onCordovaReady.subscribe(function() {
if (!message.type) {
console.log(message);
} else {
console.log('Received NFC data, firing event');
console.log("Received NFC data, firing '" + message.type + "' event");
var e = document.createEvent('Events');
e.initEvent(message.type)
e.tag = message.tag;
Expand Down