应用程序不适用于具有NFC插件的旧Android版本

时间:2015-11-09 09:49:36

标签: android cordova nfc

我一直在使用Cordova编写Android应用程序来读取非NDEF NFC卡。 然后将标签ID发送到服务器并注册到数据库中,但这不是重点。

我制作了应用并在我的Android 5.1.1上的Nexus 4上进行了测试,我想试用一个旧的Android版本。

所以我带了一个4.0.2的同事手机,该应用程序没有使用它。

我尝试将其添加到我的config.xml

<preference name="android-minSdkVersion" value="14" />
<preference name="android-targetSdkVersion" value="21" />

但它没有任何区别,应用程序只是卡住了。因此,通过一些故障排除,我认为该应用甚至无法进入deviceready电话。

我不知道我是否是唯一一个遇到这个问题的人,但我会喜欢一些帮助。 这是代码:

var app = {
    initialize: function () {
        this.bind();
    },
    bind: function () {
        document.addEventListener('deviceready', this.deviceready, false);
        document.addEventListener('online', uploadScans, false);
    },
    deviceready: function () {
        navigator.geolocation.getCurrentPosition(app.getPosition);
        tagContents = document.getElementById('tagContents');

        if (!window.localStorage.getItem("id")) {
            window.localStorage.setItem("id", Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1));
        }
        document.getElementById('id').innerHTML = "Code unique: " + window.localStorage.getItem("id");

        nfc.addNdefListener(
            app.onNdef
        );

        if (device.platform == "Android") {

            // Android reads non-NDEF tag. BlackBerry and Windows don't.
            nfc.addTagDiscoveredListener(
                app.onNfc
            );

            // Android launches the app when tags with mime type text/pg are scanned
            // because of an intent in AndroidManifest.xml.
            // phonegap-nfc fires an ndef-mime event (as opposed to an ndef event)
            // the code reuses the same onNfc handler
            nfc.addMimeTypeListener(
                'text/pg',
                app.onNdef
            );
        }

        if (app.isOnline()) {
            app.uploadScans();
        }
    },
    onNfc: function (nfcEvent) {

        var tag = nfcEvent.tag;

        app.clearScreen();

        app.saveScan(tag);
        navigator.notification.vibrate(100);
    },
    onNdef: function (nfcEvent) {

        app.clearScreen();

        var tag = nfcEvent.tag;

        // BB7 has different names, copy to Android names
        if (tag.serialNumber) {
            tag.id = tag.serialNumber;
            tag.isWritable = !tag.isLocked;
            tag.canMakeReadOnly = tag.isLockable;
        }


        app.saveScan(tag);

        navigator.notification.vibrate(100);
    },
    clearScreen: function () {

        tagContents.innerHTML = "";

    },
    saveScan: function (tag) {
        var d = new Date();

        var scan = {
            "deviceSN": window.localStorage.getItem("id"),
            "reference": nfc.bytesToHexString(tag.id),
            "scanTime": d.toJSON(),
            "latitude": app.pos.latitude,
            "longitude": app.pos.longitude
        };

        var scans = window.localStorage.getItem("scans");
        if (scans != null) {
            scans = JSON.parse(scans);
            scans.scans.push({scan});
            scans.scans.totalcount++;
        } else {
            var scans = {
                "scans": [
                    {
                        scan
                    }
                ],
                "totalcount": 1
            };
        }
        tagContents.innerHTML = "Scan added at " + d.toString();
        window.localStorage.setItem("scans", JSON.stringify(scans));
        if (app.isOnline()) {
            app.uploadScans();
        }
    },
    getPosition: function (position) {
        app.pos = {"latitude": position.coords.latitude, "longitude": position.coords.longitude};
    },
    uploadScans: function () {
        tagContents.innerHTML += "\n Uploading...";

        var http = new XMLHttpRequest();

        var post_data = window.localStorage.getItem("scans");

        var url = 'http://example.com/scan.php';

        http.open("POST", url, true);

        http.setRequestHeader("Content-type", "application/json");
        http.setRequestHeader("Content-lenght", post_data.length);

        // post the data
        http.send(post_data);

        deleteScans();
    },
    isOnline: function () {
        var state = navigator.connection.type;

        switch (state) {
            case "unknown":
                return false;
            case "ethernet":
                return true;
            case "wifi":
                return true;
            case "2d":
                return true;
            case "3g":
                return true;
            case "4g":
                return true;
            case "cell":
                return true;
            case "none":
                return false;
        }
    }
};

function deleteScans() {
    window.localStorage.removeItem("scans");
}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

所以我不知道为什么但问题是我正在创建错误的JSON对象。这是新功能: saveScan:function(tag){    var d = new Date();

var scans = window.localStorage.getItem("scans");
if (scans != null) {
    scans = JSON.parse(scans);
    scans.scans.push({"scan": {
        "deviceSN": window.localStorage.getItem("id"),
        "reference": nfc.bytesToHexString(tag.id),
        "scanTime": d.toJSON(),
        "latitude": app.latitude,
        "longitude": app.longitude
     }});
    scans.scans.totalcount++;
} else {
   scans = {
        "scans": [
            {
                "scan": {
                    "deviceSN": window.localStorage.getItem("id"),
                    "reference": nfc.bytesToHexString(tag.id),
                    "scanTime": d.toJSON(),
                    "latitude": app.latitude,
                    "longitude": app.longitude
                }
            }
        ],
        "totalcount": 1
    };
}
app.tagContents.innerHTML = "Scan added at " + d.toString();
window.localStorage.setItem("scans", JSON.stringify(scans));
if (app.isOnline()) {
    app.uploadScans();
}

现在可行。