Skip to content

Commit

Permalink
Merge pull request #324 from chariotsolutions/first-scan
Browse files Browse the repository at this point in the history
NFC events not firing on first scan  Fixes #217 (and others)
  • Loading branch information
don authored Jun 1, 2018
2 parents a3a9659 + 50f58da commit f817cb5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit f817cb5

Please sign in to comment.