Skip to content

Commit

Permalink
4.0.0 - Foreground dispatch can only be enabled when your activity is…
Browse files Browse the repository at this point in the history
… resumed #39
  • Loading branch information
EddyVerbruggen committed Jul 2, 2019
1 parent 67709c5 commit a8d18da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/nfc.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export class Nfc implements NfcApi {
private intentFilters: any;
private techLists: any;
private static firstInstance = true;
private started = false;

constructor() {
this.intentFilters = [];
Expand Down Expand Up @@ -290,6 +291,7 @@ export class Nfc implements NfcApi {
application.android.on(application.AndroidApplication.activityResumedEvent, (args: application.AndroidActivityEventData) => {
let resumingNfcAdapter = android.nfc.NfcAdapter.getDefaultAdapter(args.activity);
if (resumingNfcAdapter !== null && !args.activity.isFinishing()) {
this.started = true;
resumingNfcAdapter.enableForegroundDispatch(args.activity, this.pendingIntent, this.intentFilters, this.techLists);
// handle any pending intent
nfcIntentHandler.parseMessage();
Expand All @@ -302,13 +304,17 @@ export class Nfc implements NfcApi {
nfcIntentHandler.parseMessage();
});

// on startup, we want to make sure the adapter is listening
let startupNfcAdapter = android.nfc.NfcAdapter.getDefaultAdapter(activity);
if (startupNfcAdapter !== null) {
startupNfcAdapter.enableForegroundDispatch(activity, this.pendingIntent, this.intentFilters, this.techLists);
// handle any pending intent
nfcIntentHandler.parseMessage();
}
// on startup, we want to make sure the adapter is started, but the application must be active first, otherwise it will crash (so simply wrapping it in a timeout)
setTimeout(() => {
if (!this.started) {
let startupNfcAdapter = android.nfc.NfcAdapter.getDefaultAdapter(activity);
if (startupNfcAdapter !== null) {
startupNfcAdapter.enableForegroundDispatch(activity, this.pendingIntent, this.intentFilters, this.techLists);
// handle any pending intent
nfcIntentHandler.parseMessage();
}
}
}, 3000);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-nfc",
"version": "4.0.0",
"version": "4.0.1",
"description": "NFC plugin for your NativeScript app",
"main": "nfc",
"typings": "index.d.ts",
Expand Down

0 comments on commit a8d18da

Please sign in to comment.