chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 559 forks source link

First Scan fails #144

Closed chrizy closed 8 years ago

chrizy commented 9 years ago

We have an issue with our app where the first and only the first scan fails after the app is installed or waked up.

CORDOVA 3.5.0, tested on android 4.1 & 4.2.2

Code snip "Success" is fired OK when the app loads, scan event only fires on second scan.

        function fobService($http, $rootScope) {
            var _this = this;
            this.$http = $http;
            this.$rootScope = $rootScope;

            nfc.addNdefListener(function (nfcEvent) {
                _this.id = nfc.bytesToHexString(nfcEvent.tag.id); // nfc.bytesToString(nfcEvent.tag.ndefMessage[0].payload);

                var data = serviceBase + _this.id;

                _this.$http.get(serviceBase + _this.id).success(function (response) {
                    _this.state = response.success;

                    _this.unused = true && (response.status == 99 /* NoKeyAllocated */);
                    _this.state = response.success;
                    var r = { id: _this.id, status: response.status, state: response.success };

                    $rootScope.$broadcast('fobScanned', r);
                }).error(function (err, status) {
                    alert("Error in fob lookup, http error " + status);
                });
            }, function () {
                alert("Success.");
            }, function () {
                alert("Error no NFC on device, or NFC not enabled");
            });
chrizy commented 9 years ago

source added, Many Thanks

JohnMcLear commented 9 years ago

Try to replicate the problem without all of your additional code.. IE check to see if it's your code of the plugin that's bad.

Looking at things I'd guess it's your code *ducks...

don commented 9 years ago

@chrizy I'd expect that something isn't getting initialized correctly

I created a simple Angular + NFC app for debugging this. Can you try on your hardware? https://github.com/don/phonegap-nfc-issue144

dclarkenz commented 9 years ago

I have the same issue as listed. It's not the code from @chrizy. I'm using Cordova 2.9.0 and the Phonegap build (online) with the latest NFC code library. Tablet is Nexus 7 with Android 4.4.4.

I managed to limit the library version to 0.4.8 which is an option supported by Adobe Build and the problem goes away. So appears to be with these later versions of NFC library.

don commented 9 years ago

@dclarkenz Does it still happen in your app if you use Cordova 3.5 or 3.6? (Cordova 2.9.0 is too old to support for open source stuff.)

Does https://github.com/don/phonegap-nfc-issue144 work on your hardware?

dclarkenz commented 9 years ago

No. Tested with Cordova 2.9.0 and 3.5.0 (both thru Adobe Build) and the Android OS captures the tag not the app. I'm using the latest NFC library on the Adobe Build servers which I think is 0.5.1.

don commented 9 years ago

@dclarkenz My example fails on your Nexus 7? Is it the original Nexus 7 or the new one? I have an old Nexus 7 with 4.4.4. I'll have to try that tomorrow. Is your PhoneGap Build project public? The code I sent worked every time with my Nexus 5 and Galaxy Nexus built with Cordova 3.6. I'll try some more combinations to duplicate so we can solve this.

dclarkenz commented 9 years ago

New Nexus 7 (2013 edition). I'll try on my 2012 version tonight for you. Try https://build.phonegap.com/apps/1123296/install

rogervinas commented 9 years ago

Found the same issue in my app.

If I only use addNdefListener:

I've tested 2 workarounds and they work fine (hope they won't have side effects):

  1. Modify java NfcPlugin.parseMessage method to execute ndef callback instead of ndef-mime. From javascript only register the ndef callback addNdefListener.
  2. Use only addMimeTypeListener, with the correct mimeType (in my case text/plain). In this case all scans' actions are NfcAdapter.ACTION_NDEF_DISCOVERED so ndef-mime callback is executed, first scan included.
don commented 9 years ago

@rogervinas does your first NFC scan open the app with an intent defined in the android manifest file? If so, the 2nd work around is the "official" way to do this.

rogervinas commented 9 years ago

@don I don't have any intent defined in the android manifest file related to NFC, should I have one if I am using addNdefListener? I am applying the 2nd workaround and only using addMimeTypeListener, it is working fine so far. thanks!

don commented 9 years ago

The intent filter is only required to launch the app when scanning a NFC tag.

What phone are you seeing this on? What version of Android is it running? Do you want until after device ready is called before processing NFC tags?

rogervinas commented 9 years ago

Ok, I don't need to launch the app when scanning a NFC tag. I only need to scan NFC tags within the app. I am using a Samsung Galaxy Young GT-S6310N. Android 4.1.2 (Jelly Bean).

don commented 9 years ago

@rogervinas I'm not seeing this on my phones with Android 4.4+. I think this is either a Samsung or Android 4.1 issue. For now stick with your workaround.

clintgh commented 8 years ago

I experienced this issue too. I have an app that creates a listener on a tap of a button and it is not working on first scan, but it does on second (exactly how they described it), my workaround* is I created an ndefListener that does nothing upon scan on DEVICEREADY.

*I assumed that the nfc is only being initialized on first scan.

Obiwarn commented 8 years ago

We have the same issue here. First scan after app start/wake fails.

Samsung Galaxy Young 2

don commented 8 years ago

@clintgh @obiwarn make sure you're waiting until device ready to do any Cordova api calls. I haven't been able to duplicate this, so I think it's an Android or Device specific issue. If you can create the simplest cordova app that duplicates the problem, I can try and duplicate with my hardware.

Obiwarn commented 8 years ago

The device makes a scanning "success" sound (Not the sound it makes when it fails to read the tag). But the event seems to be not caught by any of the plugin events. (sorry for my english)

Oh and I use the plugin with Meteor.

don commented 8 years ago

@obiwarn can you get me a simple project that demonstrates this?

don commented 8 years ago

@obiwarn Look at https://github.com/chariotsolutions/phonegap-nfc/issues/144#issuecomment-99841539. @rogervinas has the same phone as you and was seeing a ndef-mime event fired on the first tag scan. (This seems like a device specific issue.)

If that is the case, add a 2nd listener as a work around.

// your existing listener
nfc.addNdefListener(app.onNfc, success, failure);

// add a 2nd listener for the mime type
nfc.addMimeTypeListener('text/plain', app.onNfc, success, failure);
Obiwarn commented 8 years ago

@don BTW: I had to guess the parameters for the callback, success and error functions. I am new to JS. Is there a better way to retrieve them or did you just miss it in the docs? (e.g. onNfc(result, error) , onError(error) )

addMimeTypeListener has the same lag on startup (i.e. only works 2nd time). It also causes my phone to open a system dialog ("Empty tag detected"). So it is not usable for me.

addTagDiscoveredListener works the first time. I just found out that it also retrieves the ndef id. So this seems like a solution.

I will try...

Obiwarn commented 8 years ago

Yes. addTagDiscoveredListener works great.

don commented 8 years ago

@obiwarn This sounds like a device specific problem. Since you have a work around, I'm closing this issue. If you continue to have the problem, re-open the issue with sample code that demonstrates the issue.

The documentation could be better about the callback parameters. For now look at the sample projects for more details. (There's also a book.)

clintgh commented 8 years ago

Hi @don I'm still experiencing the same issue. My first scan does not work, but the second time works.

Please see attached .txt file for the logs. log-nowork.txt <- first scan log-work.txt <- second scan

There is a major difference between the logs when it didn't work and the logs when it worked. One difference is in the log that didn't work (first scan), it is showing TECH_DISCOVERED but in the one that worked (second scan) it is showing NDEF_DISCOVERED

clintgh commented 8 years ago

Found my solution buried inside the Pull Requests:

addNdefListener not calling callback on first attempt #218

don commented 6 years ago

See #217 Changes published in v1.0.1