chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 556 forks source link

Methods of injected nfc object doesn't work #309

Closed JonasReichhardt closed 6 years ago

JonasReichhardt commented 6 years ago

Hello everyone

I've got the problem that as soon as I'm using a method of the injected nfc object in my javascript the statement isn't executed along the statements behind.

I'm using Phonegap Build 0.4.5 and a Samsung Galaxy 5 mini with Android 6.0.1

My index.js

var app = {

    initialize: function () {
        this.bindEvents();
    },

    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    addNFC: function () {
        var nfcStatus = document.getElementById('nfcStatus');
        var nfcHtml = document.getElementById('nfc');

        nfc.enabled(
            function(){
                nfcStatus.innerHTML = "Enabled";
            },
            function(error){
                nfcStatus.innerHTML = "Error";
            }
        );

        nfcStatus.innerHTML = "test";

        if (device.platform == "Android") {
            nfcHTML.innerHTML = typeof(nfc);
            nfc.addTagDiscoveredListener(
                function (nfcEvent) {
                    nfcHtml.innerHTML = JSON.stringify(nfcEvent.tag);
                },
                function () {
                    nfcHtml.innerHTML = "Listening for non-NDEF tags";
                },
                function () {
                    nfcHtml.innerHTML = "An error occured adding the listener";
                }
            );
        }
    },

    onDeviceReady: function () {
        document.getElementById('platform').innerHTML = "Platform: " + device.platform;
        app.addNFC();
    },
//...

in addNFC() nothing after nfc.enabled is executed and the HTML isn't showing "Enabled" or "Error".

Anyone got the same problem or can help?

Edit1: When I'm starting my app in the browser I get a TypeError: nfc.enabled is not a function

Edit2: the plugin is listed in cordova_plugins.js (so it should be correctly installed)

don commented 6 years ago

"nfc.enabled is not a function" usually means the plugin is not installed correctly, but sometimes it's caused by a JavaScript error or build error.

Make sure the plugin is defined in config.xml. https://github.com/chariotsolutions/phonegap-nfc#phonegap-build

<gap:plugin name="phonegap-nfc" source="npm" />

I don't have a good way to debug PhoneGap build. I'd recommend installing the Android SDK and doing local development with Cordova. This way you can useadb logcatto see Java errors and chrome://inspect to debug the Javascript on your phone from your computer.

JonasReichhardt commented 6 years ago

I've already fixed the problem.

Unfortunately by changing to native Android development. We had a really short time window to finish the project therefore I needed to change technology.

But still thanks for your answer and please keep up the work on this plugin

Jonas Reichhardt