Skip to content

Commit

Permalink
write can reuse session from read see #419
Browse files Browse the repository at this point in the history
  • Loading branch information
don committed Jul 9, 2020
1 parent 232f631 commit 752cf1d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
56 changes: 44 additions & 12 deletions src/ios/NfcPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
@interface NfcPlugin() {
NSString* sessionCallbackId;
NSString* channelCallbackId;
id<NFCNDEFTag> connectedTag API_AVAILABLE(ios(13.0));
NFCNDEFStatus connectedTagStatus API_AVAILABLE(ios(13.0));
}
@property (nonatomic, assign) BOOL writeMode;
@property (nonatomic, assign) BOOL shouldUseTagReaderSession;
@property (nonatomic, assign) BOOL sendCallbackOnSessionStart;
@property (nonatomic, assign) BOOL returnTagInCallback;
@property (nonatomic, assign) BOOL returnTagInEvent;
@property (nonatomic, assign) BOOL keepSessionOpen;
@property (strong, nonatomic) NFCReaderSession *nfcSession API_AVAILABLE(ios(11.0));
@property (strong, nonatomic) NFCNDEFMessage *messageToWrite API_AVAILABLE(ios(11.0));
@end
Expand Down Expand Up @@ -52,7 +55,8 @@ - (void)beginSession:(CDVInvokedUrlCommand*)command {
self.sendCallbackOnSessionStart = YES; // Not sure why we were doing this
self.returnTagInCallback = NO;
self.returnTagInEvent = YES;

self.keepSessionOpen = NO;

[self startScanSession:command];
}

Expand All @@ -64,6 +68,9 @@ - (void)scanNdef:(CDVInvokedUrlCommand*)command {
self.returnTagInCallback = YES;
self.returnTagInEvent = NO;

NSArray<NSDictionary *> *options = [command argumentAtIndex:0];
self.keepSessionOpen = [options valueForKey:@"keepSessionOpen"];

[self startScanSession:command];
}

Expand All @@ -75,6 +82,9 @@ - (void)scanTag:(CDVInvokedUrlCommand*)command {
self.returnTagInCallback = YES;
self.returnTagInEvent = NO;

NSArray<NSDictionary *> *options = [command argumentAtIndex:0];
self.keepSessionOpen = [options valueForKey:@"keepSessionOpen"];

[self startScanSession:command];
}

Expand All @@ -83,6 +93,7 @@ - (void)writeTag:(CDVInvokedUrlCommand*)command API_AVAILABLE(ios(13.0)){

self.writeMode = YES;
self.shouldUseTagReaderSession = NO;
BOOL reusingSession = NO;

NSArray<NSDictionary *> *ndefData = [command argumentAtIndex:0];

Expand All @@ -107,23 +118,33 @@ - (void)writeTag:(CDVInvokedUrlCommand*)command API_AVAILABLE(ios(13.0)){
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

// Start the NFC Session
if (self.shouldUseTagReaderSession) {
NSLog(@"Using NFCTagReaderSession");

self.nfcSession = [[NFCTagReaderSession new]
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
delegate:self queue:dispatch_get_main_queue()];
if (self.nfcSession && self.nfcSession.isReady) { // reuse existing session
reusingSession = YES;
} else { // create a new session
if (self.shouldUseTagReaderSession) {
NSLog(@"Using NFCTagReaderSession");

} else {
NSLog(@"Using NFCTagReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
self.nfcSession = [[NFCTagReaderSession new]
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
delegate:self queue:dispatch_get_main_queue()];

} else {
NSLog(@"Using NFCTagReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
}
}

self.nfcSession.alertMessage = @"Hold near writable NFC tag to update.";
sessionCallbackId = [command.callbackId copy];
[self.nfcSession beginSession];

if (reusingSession) { // reusing a read session to write
self.keepSessionOpen = NO; // close session after writing
[self writeNDEFTag:self.nfcSession status:connectedTagStatus tag:connectedTag];
// TODO release tag and status when closing session
} else {
[self.nfcSession beginSession];
}
}

- (void)cancelScan:(CDVInvokedUrlCommand*)command API_AVAILABLE(ios(11.0)){
Expand Down Expand Up @@ -326,6 +347,11 @@ - (void)processNDEFTag: (NFCReaderSession *)session tag:(__kindof id<NFCNDEFTag>
if (self.writeMode) {
[self writeNDEFTag:session status:status tag:tag];
} else {
// save tag & status so we can re-use in write
if (self.keepSessionOpen) {
self->connectedTagStatus = status;
self->connectedTag = tag;
}
[self readNDEFTag:session status:status tag:tag metaData:metaData];
}

Expand Down Expand Up @@ -456,6 +482,12 @@ - (void) sessionDidBecomeActive:(NFCReaderSession *) session API_AVAILABLE(ios(
}

- (void) closeSession:(NFCReaderSession *) session API_AVAILABLE(ios(11.0)){

// this is a hack to keep a read session open to allow writing
if (self.keepSessionOpen) {
return;
}

// kill the callback so the Cordova doesn't get "Session invalidated by user"
sessionCallbackId = NULL;
[session invalidateSession];
Expand Down
6 changes: 3 additions & 3 deletions www/phonegap-nfc.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,16 @@ var nfc = {
},

// iOS only - scan for NFC NDEF tag using NFCNDEFReaderSession
scanNdef: function () {
scanNdef: function (options) {
return new Promise(function(resolve, reject) {
cordova.exec(resolve, reject, "NfcPlugin", "scanNdef", []);
cordova.exec(resolve, reject, "NfcPlugin", "scanNdef", [options]);
});
},

// iOS only - scan for NFC Tag using NFCTagReaderSession
scanTag: function (options) {
return new Promise(function(resolve, reject) {
cordova.exec(resolve, reject, "NfcPlugin", "scanTag", []);
cordova.exec(resolve, reject, "NfcPlugin", "scanTag", [options]);
});
},

Expand Down

0 comments on commit 752cf1d

Please sign in to comment.