chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
703 stars 547 forks source link

How to do "batch reads" on iOS? #461

Open mpdude opened 2 years ago

mpdude commented 2 years ago

Hello everyone,

I hope this question is not off-topic here and I must admit that I am not used to async/await thinking in JS (not my primary language).

I'd like to to a "batch read" of NFC tags on iOS. That is, after a tag has been successfully read, immediately start the next nfc.scanTag() session, until there is an error (including user aborts).

What I have been trying looks like this:

    function scanTags() {
        nfc.scanTag().then(
            function (tag) {
                if (tag.id) {
                    // do something with tag data
                    scanTags();
                }
            },
            function (err) {
                console.log(err);
            }
        );
    }

This does, however, not work. Here's what happens:

...
2022-01-03 09:51:23.584403+0100 MyApp[41717:9815571] tagReaderSessionDidBecomeActive
2022-01-03 09:51:24.962208+0100 MyApp[41717:9815571] tagReaderSession didDetectTags
2022-01-03 09:51:24.962460+0100 MyApp[41717:9815571] getTagInfo: NFCTagTypeMiFare with uid {length = 7, bytes = 0x04447582636b80}
2022-01-03 09:51:25.016302+0100 MyApp[41717:9815571] (null)
2022-01-03 09:51:25.016602+0100 MyApp[41717:9815571] [CoreNFC] 00000002 812c02d0 -[NFCTagReaderSession setAlertMessage:]:90  (null)
2022-01-03 09:51:25.016675+0100 MyApp[41717:9815571] fireNdefEvent
2022-01-03 09:51:25.016733+0100 MyApp[41717:9815571] Sending NFC data via sessionCallbackId
... my processing ...
... my code calling scanTags() again...
2022-01-03 09:51:25.025580+0100 MyApp[41717:9815571] scanTag
2022-01-03 09:51:25.025628+0100 MyApp[41717:9815571] shouldUseTagReaderSession 1
2022-01-03 09:51:25.025671+0100 MyApp[41717:9815571] callbackOnSessionStart 0
2022-01-03 09:51:25.025708+0100 MyApp[41717:9815571] returnTagInCallback 1
2022-01-03 09:51:25.025746+0100 MyApp[41717:9815571] returnTagInEvent 0
2022-01-03 09:51:25.025786+0100 MyApp[41717:9815571] Using NFCTagReaderSession
2022-01-03 09:51:27.874606+0100 MyApp[41717:9815571] tagReaderSession ended
2022-01-03 09:51:27.874779+0100 MyApp[41717:9815571] sendError: Session invalidated by user
2022-01-03 09:51:27.878658+0100 MyApp[41717:9815571] Session invalidated by user
2022-01-03 09:51:27.878865+0100 MyApp[41717:9815571] tagReaderSession ended
2022-01-03 09:51:27.878933+0100 MyApp[41717:9815571] sendError: System resource unavailable

To me it looks as if the tagReaderSession is still active handling the first tag (invoking my callback function), and I cannot enter another session from there.

This seems very understandable to me – the question, however, is: What is the right pattern/paradigm to solve this?

I could probably do something with timeouts and invoke scanTags() again one or two seconds after my callback has been invoked, but that seems rather clumsy.

I'd appreciate any tips or starting pointers. Thanks!

justin-augustine-atlascopco commented 1 year ago

Hi, I am facing a similar problem. Were you able to resolve this issue?

mpdude commented 1 year ago

In fact, the time-out based solution described above as a workaround